Commit Graph

23 Commits

Author SHA1 Message Date
JP Simard 1ee5154687
Update SwiftSyntax to `fa7ff05` (#4455)
There's a new SwiftParserDiagnostics module and the rewriter visit
function signatures changed.
2022-10-23 11:56:22 -04:00
JP Simard fa6bf50a22
Rethink body line count calculation (#4369)
A long-standing limitation with SourceKit's "editor open" request is
that we weren't able to get certain tokens, such as braces, brackets and
parentheses.

This meant that this code block would be counted as two lines:

```swift
print(
  "hi"
)
```

because the trailing `)` would be treated as a whitespace line.

This meant that our "body length" family of rules that measure the
effective line count of declarations like functions, types or closures
would often significantly under-count the number of content lines in a
body.

Now with SwiftSyntax, we can get all tokens, including the ones
SourceKit was previously ignoring, so we can get much more accurate line
counts when ignoring whitespace and comments.

In addition, we weren't very thorough in how we measured body length.

As an exercise, how many lines long would you say the body of this
function is?

```swift
func hello() {
  print("hello")
}
```

Does the body span one line or three lines?

I propose that we consistently ignore the left and right brace lines
when calculating the body line count of these scopes so that we measure
body line counts like this:

```swift
// 1 line
{ print("foo") }
// 1 line
{
}
// 1 line
{
  print("foo")
}
// 2 lines
{
  let sum = 1 + 2
  print(sum)
}
```

Now with those changes in place, in order to keep the default
configuration thresholds to similar levels as before, we need to adjust
them slightly. Here's what I'm suggesting:

|Rule|Before|After|
|-|-|-|
|closure_body_length|20/100|30/100|
|function_body_length|40/100|50/100|
|type_body_length|200/350|250/350|

This is a pretty significant breaking change and I suspect we'll hear
from users who are surprised that some of their declarations now exceed
the rule limits, but I believe this new approach to calculating body
lines is more correct and intuitive compared to what we've had until
now.

OSSCheck is also going to report a bazillion changes with this, which is
expected given the scope of this change.
2022-10-14 03:16:26 -04:00
JP Simard 33f6ee1f36
Update SwiftSyntax (#4363)
Noteworthy:

* https://github.com/apple/swift-syntax/pull/912
* https://github.com/apple/swift-syntax/pull/932
2022-10-13 10:28:00 -04:00
JP Simard 0282bf1923
Remove optionality in getting syntax tree and source location converter (#4353)
Parsing does not throw errors.
See https://github.com/apple/swift-syntax/pull/912.
2022-10-12 12:50:17 -04:00
JP Simard 602070164b
Reduce memory usage (#4349)
When linting SwiftLint, this brings memory usage down by around 20%,
going from 372MB to 297MB.

This is achieved by removing the unused `RebuildQueue` and by clearing
cached data associated with files after processing them.

Depends on https://github.com/jpsim/SourceKitten/pull/749.
2022-10-12 09:39:36 -04:00
JP Simard 87e9757af2
Update SwiftSyntax (#4346)
I think there might have been changes impacting the `static_operator`
rule, so I want to look more into those here.

Remove workaround for https://github.com/apple/swift-syntax/issues/888
2022-10-11 21:12:29 -04:00
JP Simard fe6a930bd4
Use os_unfair_lock instead of NSLock on Darwin (#4330) 2022-10-07 20:42:22 +00:00
JP Simard 1a76a882ca
Ignore incorrect keypath parser diagnostics (#4325)
Works around https://github.com/apple/swift-syntax/issues/888
2022-10-07 10:33:13 -04:00
JP Simard 44382ea397
Avoid making SourceKit requests to get parser diagnostics (#4286)
By using SwiftSyntax instead, we avoid the overhead of a SourceKit
request, which has a significant impact if none of the rules being
corrected use SourceKit.
2022-10-05 15:38:11 +00:00
JP Simard 2388e49190
Use SwiftSyntax's new SwiftParser (#4216) 2022-10-01 15:05:36 -04:00
JP Simard 3037946bbc
Migrate ClosureSpacingRule to SwiftSyntax (#4092)
Fixes #4090. Continued from #4091.
2022-08-15 18:27:43 +00:00
JP Simard 6c5f53ef66
Add micro-optimization to commands cache (#4050)
Speeds up overall lint time by ~2% for Lyft's iOS monorepo.
2022-07-28 15:44:34 +00:00
Marcelo Fabri ebc77391bc
Add workaround to avoid stack overflow in debug (#3963) 2022-05-01 14:57:28 -07:00
JP Simard a773c3ec21
Apply minor changes to the syntactic sugar rewrite (#3915)
* Improve docstrings for `StringView+SwiftSyntax.swift`
* Move changelog entry to correct section & reword
* Change parameter type from `Int` to `ByteCount`
* Move AbsolutePosition / ByteCount conversion to internal API
* Only warn once if syntax tree cannot be parsed
* Move Syntactic Sugar examples to a dedicated file
* Change SyntacticSugarRuleVisitor from SyntaxAnyVisitor to SyntaxVisitor
* Add `SugaredType` enum to help with the implement `SyntacticSugarRule`
2022-03-24 10:27:05 -04:00
JP Simard de3569cb3b
Update SwiftSyntax to 0.50600.0 (#3901)
Uses SwiftSyntax 5.5 on Linux when building with Swift 5.5. We use the 5.6 version of
SwiftSyntax when building with Swift 5.5 and 5.6 on macOS because we statically link
`lib_InternalSwiftSyntaxParser` thanks to
https://github.com/keith/StaticInternalSwiftSyntaxParser/releases/tag/5.6.

This keeps SwiftLint binaries portable across machines on macOS, regardless of
_where_ or even _if_ `lib_InternalSwiftSyntaxParser` is installed.

* Run TSan CI job with `--configuration release` to avoid stack overflows
* Add Swift 5.6 CI job
* Fix linker settings
2022-03-17 13:19:48 -04:00
JP Simard 28dd673c48
Use SwiftSyntax visitor to parse commands (#3872)
* Cache SwiftSyntax syntax trees
* Use SwiftSyntax visitor to parse commands
* Update changelog entry
* Cache commands
2022-03-08 12:33:06 -05:00
JP Simard 6d2e8cfc06
Skip correcting files with parser diagnostics (#3349)
* Skip correcting files with parser diagnostics

Also fix many rule examples with parser diagnostics.

* Sourcery

* Link to issue in changelog entry
2020-09-17 18:14:00 -04:00
JP Simard 9b93b3efb8
[SwiftLintFile] Remove lock in favor of UUID (#3347)
We were using this lock to guarantee a new ID for every file, but we can
get that benefit by using values that are guaranteed to be unique
without the need for locks, such as a UUID.
2020-09-17 07:35:05 -07:00
JP Simard b744cf08f1
Temporarily remove all SwiftSyntax rules and support (#3107)
The new rules introduced in 0.39.0 that depend on SwiftSyntax have been temporarily removed as we work out release packaging issues.

* `prohibited_nan_comparison`
* `return_value_from_void_function`
* `tuple_pattern`
* `void_function_in_ternary`

See https://github.com/realm/SwiftLint/issues/3105 for details.
2020-02-11 13:40:04 -08:00
Marcelo Fabri 8d9c501cb8
Add optional return_value_from_void_function rule using SwiftSyntax (#3054)
* Add optional return_value_from_void_function rule using SwiftSyntax

* Use Script/bootstrap in CI

* Make SwiftSyntax optional

* Make SwiftSyntax optional in SPM

* Fix Package.swift

* Try again

* Add minSwiftVersion

* Fix thread sanitizer issue

* Take 2

* Fix false positive on nested computed variables

* Remove support for Xcode 10.x

* Fix rule description

* Enable opt-in rule in configuration file

* Extract code into `SyntaxRule` protocol

* nit: make property private

* Missing docs

* Fix MasterRuleList.swift

* Update CHANGELOG

* Remove unused imports

* Use Example type

* Change rule kind to .idiomatic

* Update CHANGELOG

* Bump deployment target to macOS 10.12

* Simplify SyntaxRule.validate(file:visitor)

* Make TSan happy

* Use script/bootstrap in the README
2020-02-08 02:43:40 -08:00
JP Simard 37167a8a35
Add documentation comments to all public declarations (#3027) 2020-01-08 09:47:10 -08:00
Paul Taykalo 73802c285d Add own wrappers over syntax tokens and syntax map 2019-11-10 22:55:54 +02:00
Paul Taykalo ac40778cb3 Use SwiftLintFile wrapper over the File 2019-11-07 15:19:17 +02:00