Commit Graph

1003 Commits

Author SHA1 Message Date
Marcelo Fabri 98e9c2f1f4
Fix SwiftVersionTests when using Xcode 14.2 (#4492) 2022-10-30 19:50:49 -04:00
Henry 63c043664d
Add new `no_magic_numbers` rule (#4265) 2022-10-30 15:28:20 +01:00
Marcelo Fabri 59ad84f6e9
Add ns_number_init_as_function_reference rule (#4414) 2022-10-20 09:15:17 -07:00
Marcelo Fabri 844b7c02dd
Rewrite `trailing_comma` rule with SwiftSyntax (#4385) 2022-10-16 23:36:11 -07:00
Marcelo Fabri 4522005ab4
Migrate `implicitly_unwrapped_optional` to SwiftSyntax (#4278)
* Migrate `implicitly_unwrapped_optional` to SwiftSyntax

* Update Source/SwiftLintFramework/Rules/RuleConfigurations/ImplicitlyUnwrappedOptionalConfiguration.swift

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

* Fix after rebase

Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2022-10-16 22:55:32 -07:00
Marcelo Fabri 7ff3094821
Support .focused() in keys for corrections (#4384) 2022-10-16 07:50:10 -04:00
Danny Mösch a7ff8a8d76
Rename `if_let_shadowing` rule to `shorthand_optional_binding` (#4334) 2022-10-14 21:47:37 +00:00
JP Simard 39bb05f7d1
Rewrite "body length" rules with SwiftSyntax (#4370)
- `closure_body_length`
- `function_body_length`
- `type_body_length`
2022-10-14 03:50:39 -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
Danny Mösch 9db88947b9
Fix bugs when auto-correcting content given via stdin (#4212)
Fixes #4211.
Fixes #4234.
Fixes #4347.
2022-10-13 00:34:48 -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 5ae1d98596
Create new GeneratedTests and IntegrationTests test targets (#4304)
* Move generated rule tests to a dedicated GeneratedTests target
* Move integration tests to a dedicated IntegrationTests target

This will improve bazel cacheability by having fewer tests depend on all
lint inputs, which are only needed by the integration tests.
2022-10-06 00:10:04 -04:00
JP Simard 503f8c506b
Move ExtraRulesTests to its own target & directory (#4300)
Also create a SwiftLintTestHelpers test target.
2022-10-05 23:36:46 -04:00
JP Simard cfabd8ec99
Organize `Tests/BUILD` with comments (#4301) 2022-10-05 21:40:12 -04:00
JP Simard 91d4a42687
Loosen correction location assertions (#4297)
With SwiftSyntax rewriters, the visitors get called with the new nodes
after previous mutations have been applied, so it's not straightforward
to translate those back into the original source positions.

So only check the first locations.
2022-10-05 20:51:46 -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 41b8834a13
Rewrite `private_outlet` with SwiftSyntax (#4228) 2022-10-05 11:29:01 -04:00
Vasiliy Kattouf 129a55fb74
Add ability to filter rules for `generate-docs` subcommand (#4195)
Added rules filtering options, like in the `rules` subcommand.

<img width="880" alt="generate-docs--help" src="https://user-images.githubusercontent.com/7829589/189372666-2f5aba62-57a1-49dc-9155-c60f9e085984.png">

To achieve reuse with `rules` subcommand:
- extracted filtering logic into `RulesFilter` (and write tests)
- extracted filtering command line parameters into `RulesFilterOptions: ParsableArguments`
2022-10-04 18:45:52 +00:00
Danny Mösch 7be6f866fe
Add new `if_let_shadowing` rule (#4206) 2022-09-22 19:27:45 +02:00
dahlborn f3d367f667
Add `LibraryContentProvider` to `file_types_order` rule (#4209) 2022-09-19 22:15:41 +02:00
Danny Mösch 49ace1729f
Add Swift 5.6.3 to version test (#4207) 2022-09-14 23:20:51 +02:00
Chris Brakebill 9db303e3a3
Respect 'validates_start_with_lowercase' for function names in `identifier_name` rule (#4188) 2022-09-09 23:36:55 +02:00
Ryan Cole c34955ca64
Add `accessibility_trait_for_button` rule (#3989) 2022-09-06 21:44:09 +00:00
Marcelo Fabri c7c4e0a1f3
Rewrite `implicit_getter` rule with SwiftSyntax (#4160)
* Rewrite `implicit_getter` rule with SwiftSyntax

* Handle different reason messages

* Add changelog + implicit return
2022-09-05 01:28:25 -04:00
Marcelo Fabri c1650e6918
Add `excludes_trivial_init` for `missing_docs (#4152)
Fixes #4107
2022-09-03 22:11:57 -07:00
JP Simard 9979c4fd27
Add new `self_binding` opt-in rule (#4146)
To enforce that `self` identifiers are consistently re-bound to a common
identifier name.

Configure `bind_identifier` to the name you want to use.

Defaults to `self`.

Addresses https://github.com/realm/SwiftLint/issues/2495
2022-08-30 16:37:19 -04:00
JP Simard 83bf37e642
Add `SwiftLintFile.isTestFile` (#4122) 2022-08-23 12:04:30 -04:00
JP Simard e8f738b39c
Automate producing release tarballs for Bazel (#4113) 2022-08-19 17:26:21 +00:00
JP Simard 408ba29951
Integrate with rules_xcodeproj (#4095) 2022-08-15 16:22:47 -04:00
sarastro-nl d1a1485af6
enable glob for includePaths (#4093) 2022-08-15 13:54:49 +00:00
Danny Mösch c1b74cd752
Rename test classes (#4083) 2022-08-11 07:33:11 +02:00
Danny Mösch d730e0b3fa
Let all example verification tests be generated by Sourcery (#4076) 2022-08-10 22:49:28 +02:00
Danny Mösch 828f36958f
Introduce option to ignore indentation of compiler directives in `indentation_width` rule (#4066) 2022-08-10 21:52:32 +02:00
Sara Tavares f8e1b178d6
chore(typo): fix typo on test (#4078) 2022-08-10 13:36:03 -04:00
Danny Mösch 449190d324
Verify examples in rules by default and enforce explicit exclusion (#4065)
A rule must conform to ManuallyTestedExamplesRule to skip generation of a test for its examples.
2022-08-09 22:32:09 +02:00
JP Simard 8b8b3d0738
Run all tests with Bazel (#4067)
Other than the one test case that uses paths with spaces in them which
Bazel doesn't support.
2022-08-07 16:56:45 -04:00
JP Simard 0ded5859a1
Rewrite ColonRule with SwiftSyntax (#4063)
Making it about 7x faster, finding some previously missed cases.
2022-08-04 17:23:52 -04:00
Danny Mösch cdb87e3869
Verify all examples provided in rule descriptions (#4061) 2022-08-04 23:06:37 +02:00
Danny Mösch 54a8435479
Use specific failure messages in extended tests (#4058) 2022-07-31 14:45:47 +02:00
JP Simard 1dab8181b8
Fix bazel extra rules tests on Linux (#4056) 2022-07-29 19:34:54 +00:00
JP Simard 8b90623b38
[Bazel] Set up tests (#4055)
Run with `bazel test --spawn_strategy=standalone //Tests/...`

Consumers who define rules via `swiftlint_extra_rules` can run tests for
their custom rules too:

```
./bazelw test \
  --spawn_strategy=standalone \
  @SwiftLint//Tests:ExtraRulesTests
```
2022-07-28 19:14:03 +00:00
JP Simard 05ac3c9d75
Require macOS 12 & Swift 5.6 (#4037)
This will unblock using Swift Concurrency features and updating to the
latest versions of Swift Argument Parser.

This won't drop support for linting projects with an older toolchain /
Xcode selected, as long as SwiftLint was _built_ with 5.6+ and is
_running_ on macOS 12+. So the main breaking change for end users here
is requiring macOS 12 to run.

However, the upside to using Swift Concurrency features is worth the
breaking change in my opinion. Also being able to stay on recent Swift
Argument Parser releases.
2022-07-26 03:55:36 -04:00
Danny Mösch 1c7b8b7dcb
Run Sourcery enabling automatic test for ModifierOrderRule (#4034) 2022-07-24 14:11:09 -04:00
Danny Mösch a51be7bcce
Refactor deployment target configuration to avoid duplications (#4017) 2022-07-06 17:07:15 -04:00
Taha Bebek e34f49695b
Support extension targets in `deployment_target` rule (#4011) 2022-07-05 22:41:56 +02:00
Marcelo Fabri 45a03dec20
Add back `void_function_in_ternary` rule (#3956) 2022-06-27 09:41:52 -07:00
Marcelo Fabri a19ffddf66
Support arrays for `included` & `excluded` in custom rules (#4006)
* Support arrays for `included` & `excluded` in custom rules

* Extract shouldValidate(filePath:) and add tests
2022-06-27 00:29:26 -07:00
Danny Mösch a20a75d422
Fix various shortcomings in relative path computation (#4005) 2022-06-24 02:25:37 -04:00
Marcelo Fabri 4382ef49b9
Use URL(fileURLWithPath:isDirectory) to avoid file system call (#3976) 2022-05-26 21:54:23 +02:00
Marcelo Fabri f880b66cbf
Rewrite `operator_usage_whitespace` with SwiftSyntax (#3962) 2022-05-06 16:48:57 -07:00