terryyin / lizard

A simple code complexity analyser without caring about the C/C++ header files or Java imports, supports most of the popular languages.
Other
1.85k stars 250 forks source link

adding an _expect_declaration_name state for Swift #241

Closed Coeur closed 5 years ago

Coeur commented 5 years ago

This is an untested attempt to fix #240 by adding a declarative state to ignore variable names like get or set.

Coeur commented 5 years ago

We read case and , for a valid situation like:

    enum Foo {
        case get, set
        func bar() {}
    }

We read var and let for similar reasons like:

    let get = true
    var set: Bool?
    func bar() {}
Coeur commented 5 years ago

OK, I found a nice solution by interpreting , as a declarative token. I've also added support for:

// https://stackoverflow.com/a/30593673/1033581
extension Collection {
    /// Returns the element at the specified index iff it is within bounds, otherwise nil.
    subscript (safe index: Index) -> Iterator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

and:

    var foo: Int = -1 {
        willSet {
            print(newValue)
        }
        didSet {
            print(foo)
        }
    }

I've been through the list of tokens in https://github.com/google/code-prettify/blob/master/src/lang-swift.js (last commit is from me by the way), so I think we're not missing much now.

Coeur commented 5 years ago

The Swift tests all pass. The CI jobs (AppVeyor and Travis) are failing for other reasons.

terryyin commented 5 years ago

HI @Coeur thanks very much! Is it possible to add several new tests? For the new identifiers and new state.

Coeur commented 5 years ago

HI @Coeur thanks very much! Is it possible to add several new tests? For the new identifiers and new state.

Sure, I was planning to, but was busy on an "emergency release" in office, ah ah. Will do it today.

Coeur commented 5 years ago

@terryyin done. The most interesting test is test_keyword_declarations, as it's filled with traps.

terryyin commented 5 years ago

Thanks!