Commit Graph

284 Commits

Author SHA1 Message Date
JP Simard 4bd7da32ea Reduce visibility of rules to be internal (#4533)
There's no reason to expose these publicly and this will make it nicer
to move to a new module outside of the core SwiftLint functionality.
2022-11-09 11:01:26 -05:00
Danny Mösch 1edef5ebdc Trigger prefer_self_in_static_references rule on constructor calls (#4519) 2022-11-06 14:55:28 +01:00
Danny Mösch 7e8a3ae2ac Introduce onlyElement property on Collection (#4518) 2022-11-06 07:37:44 -05:00
Marcelo Fabri 45ac3dcdff Rewrite required_enum_case with SwiftSyntax (#4521) 2022-11-06 01:26:11 -08:00
Marcelo Fabri 22522254df Validate shorthand option binding in self_binding rule (#4514)
* Validate shorthand option binding in `self_binding` rule

* Fix CHANGELOG
2022-11-05 21:32:09 -07:00
Danny Mösch 9a706ba371 Trigger prefer_self_in_static_references rule in computed property blocks (#4517) 2022-11-06 00:19:48 +01:00
Danny Mösch d4ea19e434 Trigger prefer_self_in_static_references rule on key paths (#4516) 2022-11-05 13:39:27 +01:00
Danny Mösch eb3d502ad7 Rephrase rule description letting it no longer refer to classes only (#4515) 2022-11-05 07:13:24 -04:00
Danny Mösch 65874dc40f Rewrite prefer_self_in_static_references with SwiftSyntax (#4504) 2022-11-05 11:46:33 +01:00
Craig Siemens ec5124d939 Rewrite multiline_arguments_brackets with SwiftSyntax (#4512)
Fix false-positives when a function call has a single line closure.
2022-11-03 21:57:27 -04:00
JP Simard 79e3c8bd4a Add special handling for https://github.com/apple/swift/issues/61817 (#4498) 2022-10-31 09:58:36 -04:00
JP Simard a18436d4d6 Rewrite optional_enum_case_matching with SwiftSyntax (#4488) 2022-10-28 19:30:33 +00:00
JP Simard d551cb8c16 Update SwiftSyntax (#4480)
Moves syntax classifications to a new IDEUtils module.
2022-10-28 09:23:06 -04:00
Marcelo Fabri d37465ad37 Rewrite vertical_parameter_alignment with SwiftSyntax (#4466)
* Rewrite `vertical_parameter_alignment` rule with SwiftSyntax

* Validate inits
2022-10-24 12:56:48 -07:00
Marcelo Fabri fa6db3cca7 Rewrite collection_alignment rule with SwiftSyntax (#4472) 2022-10-24 10:51:31 -07:00
Marcelo Fabri 95838aa230 Add example for #2008 (#4470)
Fixes #2008
2022-10-24 05:32:56 -04:00
Marcelo Fabri f8d505a225 Add example for #3945 (#4469)
Fixes #3945
2022-10-24 09:23:37 +00:00
Marcelo Fabri 351c8be2e0 Rewrite return_arrow_whitespace with SwiftSyntax (#4413) 2022-10-23 15:06:14 -07:00
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 401d0f7929 Update SwiftSyntax to 60c7037 (#4454)
This has changes to how comments are associated to nodes.

See https://github.com/apple/swift-syntax/pull/985
2022-10-23 11:15:27 -04:00
Marcelo Fabri 25a04e8eeb Migrate inclusive_language rule to SwiftSyntax (#4390) 2022-10-23 14:00:40 +00:00
Marcelo Fabri 0bd9e4e65f Rewrite unused_optional_binding with SwiftSyntax (#4449) 2022-10-23 06:13:54 -07:00
Marcelo Fabri 60054da0f7 Rewrite prefer_self_type_over_type_of_self with SwiftSyntax (#4451) 2022-10-23 06:06:16 -07:00
Marcelo Fabri 9c8708bc01 Extract common SwiftSyntax extensions (#4445) 2022-10-23 00:54:18 -07:00
Marcelo Fabri 7b1099a33d Rewrite prefixed_toplevel_constant with SwiftSyntax (#4425) 2022-10-21 13:11:40 -07:00
Marcelo Fabri 1fb04902c0 Rewrite multiline_parameters rule with SwiftSyntax (#4438) 2022-10-21 09:08:27 +00:00
JP Simard e84430f222 Update SwiftSyntax (#4431) 2022-10-20 13:16:00 -04:00
Marcelo Fabri 2929e35183 Rewrite single_test_class rule with SwiftSyntax (#4424) 2022-10-20 09:13:23 -07:00
Danny Mösch a8bc9e9b42 Let more rules rely on SwiftSyntaxRule's defaults (#4401) 2022-10-18 02:23:39 -04:00
JP Simard 1b86aa7eeb Simplify SwitchCaseAlignmentRule (#4399)
By using `ReasonedRuleViolation`, `SwiftSyntaxRule` and `makeVisitor()`.
2022-10-17 10:59:07 -04:00
Marcelo Fabri 844b7c02dd Rewrite trailing_comma rule with SwiftSyntax (#4385) 2022-10-16 23:36:11 -07:00
Danny Mösch 0fbd03cf2b Require visitors to exist (#4383) 2022-10-16 18:40:12 +02:00
Danny Mösch f8e5339c69 Introduce ReasonedRuleViolation type to associate a reason with a violation position (#4379) 2022-10-16 12:53:38 +02:00
Marcelo Fabri 9959f971f9 Add way to preprocess syntaxTree in SwiftSyntaxRule (#4381)
* Add way to preprocess syntaxTree in SwiftSyntaxRule

* Update Source/SwiftLintFramework/Protocols/SwiftSyntaxRule.swift

Co-authored-by: Danny Mösch <danny.moesch@icloud.com>

* Update Source/SwiftLintFramework/Protocols/SwiftSyntaxRule.swift

Co-authored-by: Danny Mösch <danny.moesch@icloud.com>

Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2022-10-16 01:40:02 -07:00
Danny Mösch bd8c9e5bcb Provide syntax visitor base class allowing for convenient skipping of declaration nodes (#4310) 2022-10-15 19:03:49 +02:00
Marcelo Fabri c9ef157c29 Rewrite conditional_returns_on_newline with SwiftSyntax (#4340)
* Rewrite `conditional_returns_on_newline` with SwiftSyntax

* Keep original behavior

* PR feedback
2022-10-15 02:25:56 -07:00
JP Simard f04e28cbf8 Rewrite switch_case_alignment with SwiftSyntax (#4373) 2022-10-14 10:06:54 +00: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 4e3c750086 Rewrite attributes with SwiftSyntax (#4359)
Not 100% the same as it previously was, but I think all the changes
reported by OSSCheck are improvements or neutral.
2022-10-13 00:40:24 -04:00
JP Simard 9aaeff67d0 Add SyntaxProtocol.isContainedIn(regions:locationConverter:) helper (#4356) 2022-10-12 19:50:32 +00:00
JP Simard 1a7f03bbc5 Rewrite redundant_discardable_let with SwiftSyntax (#4355) 2022-10-12 15:27:03 -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
Marcelo Fabri 6998d8af23 Migrate shorthand_operator rule to SwiftSyntax (#4336)
* Migrate `shorthand_operator` rule to SwiftSyntax

* Remove unneeded imports
2022-10-10 21:28:55 -07:00
Marcelo Fabri 4da2ff1118 Rewrite operator_whitespace rule with SwiftSyntax (#4335) 2022-10-10 14:12:03 -07:00
Marcelo Fabri b938a4e07d Convert number_separator rule to SwiftSyntax (#4333) 2022-10-09 21:03:57 -07:00
Marcelo Fabri 04bd091810 Migrate legacy_multiple rule to SwiftSyntax (#4317) 2022-10-07 23:49:14 -07:00
JP Simard 3834e52ac4 Rewrite empty_parameters with SwiftSyntax (#4295) 2022-10-05 18:26:57 -04:00
Marcelo Fabri 8926596da0 Migrate closure_parameter_position to SwiftSyntax (#4264) 2022-10-03 10:18:43 -07:00
Marcelo Fabri 647819eedc Migrate switch_case_on_newline to SwiftSyntax (#4269) 2022-10-03 09:28:29 -07:00
Marcelo Fabri 268415893d Rewrite protocol_property_accessors_order with SwiftSyntax (#4237)
* Rewrite protocol_property_accessors_order with SwiftSyntax

* PR feedback
2022-10-02 17:29:04 -07:00