Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensions ignored in type length analysis #1

Closed
regexident opened this issue Feb 7, 2020 · 2 comments · Fixed by #8
Closed

Extensions ignored in type length analysis #1

regexident opened this issue Feb 7, 2020 · 2 comments · Fixed by #8
Labels
help wanted Extra attention is needed

Comments

@regexident
Copy link

How to reproduce:

  1. Create empty directory named "Foobar"

  2. Initialize swift package via $ swift package init

  3. Change …

    struct Foobar {
        var text = "Hello, World!"
    }

    … to …

struct Foobar {
    var text = "Hello, World!"
// }

// extension Foobar {
    func foo() -> Int {
        var i: Int = 0
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        i += 1
        return i
    }
}
  1. run $ sitrep
Overview
   Files scanned: 5
   Structs: 1
   Classes: 1
   Enums: 0
   Protocols: 0
   Extensions: 0

Sizes
   Total lines of code: 105
   Source lines of code: 93
   Longest file: Foobar.swift (45 source lines)
   Longest type: Foobar (45 source lines)

Structure
   Imports: XCTest (3), FoobarTests (1), Foobar (1), PackageDescription (1)
   UIKit View Controllers: 0
   UIKit Views: 0
   SwiftUI Views: 0
  1. Uncomment these lines …
// }

// extension Foobar {
  1. run $ sitrep again
Overview
   Files scanned: 5
   Structs: 1
   Classes: 1
   Enums: 0
   Protocols: 0
---   Extensions: 0
+++   Extensions: 1

Sizes
   Total lines of code: 105
   Source lines of code: 93
   Longest file: Foobar.swift (45 source lines)
---   Longest type: Foobar (45 source lines)
+++   Longest type: FoobarTests (11 source lines)

Structure
   Imports: XCTest (3), FoobarTests (1), Foobar (1), PackageDescription (1)
   UIKit View Controllers: 0
   UIKit Views: 0
   SwiftUI Views: 0
  1. Notice how Foobar is now suddenly not considered the longest type any more.

This is wrong.
The contents of a type's extensions should be considered content of the type.

@twostraws
Copy link
Owner

Yeah, this is something I'd really love to see – the system should collapse extensions into their parent type before running analysis. I think this would be a great thing for someone else to implement, because not only can they collapse length of the type, but they can also collapse the inheritance arrays too.

I'm going to mark this with a help wanted tag to encourage someone to pick up this and have a go at implementing.

@twostraws twostraws added the help wanted Extra attention is needed label Feb 7, 2020
@alexito4
Copy link
Contributor

alexito4 commented Feb 11, 2020

@twostraws I was gonna take a look at this but I can't make the Package run from Xcode. It always fails because it doesn't find the SwiftSyntax dlyb.

Library not loaded: @rpath/lib_InternalSwiftSyntaxParser.dylib
  Referenced from: /Users/user/Library/Developer/Xcode/DerivedData/Sitrep-ebzxljrugggysccfsqrpaitiyxak/Build/Products/Debug/SitrepCoreTests.xctest/Contents/MacOS/SitrepCoreTests
  Reason: image not found)

Running the tests has the additional error of

xctest[25377:498974] The bundle “SitrepCoreTests” couldn’t be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle.

This seems to be some known issue https://forums.swift.org/t/swiftsyntax-with-swift-5-1/29051/15 but I haven't been able to find a workaround that works. Building, running and testing from the CI works tho.

Do you have any special setup to make it work from Xcode?

EDIT: I've did a fix anyway, running the test from the CLI. That's when you realise how awesome Xcode is 😂

alexito4 added a commit to alexito4/Sitrep that referenced this issue Feb 11, 2020
Gather all types based on name so extensions count towards their extending type.
alexito4 added a commit to alexito4/Sitrep that referenced this issue Feb 11, 2020
Gather all types based on name so extensions count towards their extending type.
twostraws added a commit that referenced this issue Feb 20, 2020
Fixes #1 - improved type length analysis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants