433 Commits

Author SHA1 Message Date
Koichiro Ueki e65767baf8 Add new excluded_paths option to file_name rule (#6092)
Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2025-06-25 21:30:32 +02:00
JP Simard f7f3caa50e Migrate TrailingWhitespaceRule from SourceKit to SwiftSyntax (#6117)
## Summary

Convert TrailingWhitespaceRule to use SwiftSyntax instead of SourceKit
for improved performance and better handling of trailing whitespace
detection, especially within block comments.

## Key Technical Improvements

- **Enhanced block comment detection** distinguishing between lines fully
  covered by block comments vs lines containing block comments with code
- **Accurate whitespace detection** using CharacterSet.whitespaces for
  all Unicode whitespace characters, not just space and tab
- **Improved comment handling** with proper detection of line-ending
  comments and multi-line block comment structures
- **Better correction mechanism** using ViolationCorrection ranges
  instead of manual string reconstruction
- **Line-based analysis** maintaining efficiency while providing precise
  violation positions

## Migration Details

- Replaced `CorrectableRule` with `@SwiftSyntaxRule(correctable: true)`
- Implemented `ViolationsSyntaxVisitor` pattern for line-based validation
- Added `collectLinesFullyCoveredByBlockComments` to properly handle
  test framework comment wrapping scenarios
- Distinguished between three comment scenarios: lines fully within block
  comments, full-line comments, and lines ending with comments
- Maintained all configuration options (ignores_empty_lines, ignores_comments)
- Preserved exact violation position reporting with UTF-8 offset calculations
2025-06-24 21:12:32 -04:00
JP Simard ab7d117030 Migrate FileHeaderRule from SourceKit to SwiftSyntax (#6112)
## Summary

Convert FileHeaderRule to use SwiftSyntax instead of SourceKit for
improved performance and better handling of file header comments,
shebangs, and doc comments.

## Key Technical Improvements

- **Enhanced shebang support** properly skipping past `#!/usr/bin/env swift` lines
- **Better comment type discrimination** excluding doc comments from header analysis
- **Accurate position calculation** converting between UTF-8 and UTF-16 offsets for regex matching
- **Improved trivia traversal** for comprehensive header comment collection
- **SwiftLint command filtering** to exclude directive comments from header content

## Migration Details

- Replaced `OptInRule` with `@SwiftSyntaxRule(optIn: true)` annotation
- Implemented `ViolationsSyntaxVisitor` pattern for file-level analysis
- Added logic to start header collection after shebang.endPosition if present
- Distinguished between regular comments and doc comments (///, /** */)
- Maintained UTF-16 offset calculations for NSRegularExpression compatibility
- Added `skipDisableCommandTests: true` for SwiftSyntax disable command behavior
- Removed unnecessary SourceKittenFramework import
2025-06-24 09:48:25 -04:00
Danny Mösch 4b9208a37b Ignore various assignment operators in void_function_in_ternary (#6133) 2025-06-24 09:47:40 +02:00
JP Simard 388d45246e Migrate ExpiringTodoRule from SourceKit to SwiftSyntax (#6113) 2025-06-23 07:43:04 -04:00
Kent Kaseda c22de52b9b Respect ignore_swiftui_view_bodies option in view builders and preview macros/providers (#6075)
Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2025-06-22 01:32:48 +02:00
JP Simard 5a2cf4b1fe Remove dead code (#6125)
In particular lots of stuff that used to be needed with SourceKit that
we no longer need to keep around.

Identified using Periphery: https://github.com/peripheryapp/periphery
2025-06-21 15:19:37 -04:00
JP Simard 4ecae5b252 Migrate LineLengthRule from SourceKit to SwiftSyntax (#6111)
Convert LineLengthRule to use SwiftSyntax instead of SourceKit for
improved performance and better detection accuracy.

The SwiftSyntax implementation:
- Uses ViolationsSyntaxVisitor pattern with line-based validation
- Pre-computes ignored lines using helper visitors for efficiency
- Implements pure SwiftSyntax comment detection without SourceKit
- Correctly handles function declarations, multiline strings
- Maintains all configuration options including URL stripping
- Preserves exact line position reporting for violations
2025-06-21 14:47:19 -04:00
JP Simard 45c4766a02 Migrate AccessibilityLabelForImageRule from SourceKit to SwiftSyntax (#6101)
* Migrate AccessibilityLabelForImageRule from SourceKit to SwiftSyntax

\## Summary

Fix `.accessibilityElement(children:)` exemption logic where the
previous implementation incorrectly exempted images with `.combine`
when only `.ignore` should exempt children.

\## Improvements Based on OSSCheck Results

\### New Violations (Legitimately Detected)
- `Image(uiImage:)` and `Image(nsImage:)` calls providing contextual
  content
- Previously missed by SourceKit's pattern matching limitations
- These represent real accessibility issues requiring labels

\### Fixed Violations (False Positives Removed)
- Images inside Button/NavigationLink labels (containers handle
  accessibility)
- Images in custom components with proper accessibility context
- System icons incorrectly flagged when already accessible

\## Key Technical Improvements

- **Comprehensive detection** of all Image initializer types
- **Better context awareness** through syntax tree traversal
- **Accurate `.accessibilityElement(children:)` behavior**
  (`.ignore` exempts, `.combine`/`.contain` do not)
- **Reduced false positives** while catching real accessibility issues

\## Regression Test Examples

Added comprehensive examples demonstrating the migration benefits:

\### Non-Triggering (Reduced False Positives)
- Images in `Button`/`NavigationLink` labels with accessibility context
- Images in containers with `.accessibilityElement(children: .ignore)`
- Complex hierarchies where container accessibility handles children
- Various Image initializer types in proper accessible contexts

\### Triggering (Improved Detection)
- `Image(uiImage:)` and `Image(nsImage:)` providing contextual content
- Images in containers with `.combine`/`.contain` that don't exempt
- Status icons, favicons, and background images needing labels

* Update changelog
2025-06-21 11:34:06 -04:00
Danny Mösch 286e67ff23 Report more specific target types (#6123) 2025-06-21 17:17:55 +02:00
Danny Mösch 18403e8604 Harmonize violation positions (#6124) 2025-06-21 17:15:25 +02:00
JP Simard cc03c83eb9 Migrate ClosureEndIndentationRule from SourceKit to SwiftSyntax (#6109)
* Migrate ClosureEndIndentationRule from SourceKit to SwiftSyntax

- Converted rule to SwiftSyntax
- Implemented recursive anchor detection for method chains
- Fixed indentation calculation for trailing closures
- Fixed correction logic to properly handle existing whitespace

* Revert debug changes to OSSCheck

* Count whitespace for indentation, not first non-whitespace position

* Simplify `hasNewlineInTrivia` calculation

* Update changelog
2025-06-21 11:01:20 -04:00
JP Simard 5eac9be50d Migrate AccessibilityTraitForButtonRule from SourceKit to SwiftSyntax (#6108)
## Summary

Convert AccessibilityTraitForButtonRule to use SwiftSyntax instead of
SourceKit for improved performance and better accessibility trait
detection in SwiftUI modifier chains.

## Key Technical Improvements

- **Bidirectional modifier chain analysis** for comprehensive
  accessibility trait detection
- **Better context awareness** through SwiftSyntax tree traversal
- **Accurate container exemption logic** for Button/Link components
- **Enhanced gesture detection** supporting `.onTapGesture` and
  `.gesture` modifiers
- **Improved performance** with SwiftSyntax visitor pattern over
  SourceKit AST parsing

## Migration Details

- Replaced `ASTRule` with `@SwiftSyntaxRule(optIn: true)` annotation
- Implemented `ViolationsSyntaxVisitor` pattern for systematic tree
  traversal
- Added bidirectional accessibility trait detection that checks both
  before and after tap gestures
- Enhanced exemption logic for inherently accessible containers
  (Button, NavigationLink)
- Maintained full compatibility with existing rule behavior and test
  cases
2025-06-21 10:11:43 -04:00
Danny Mösch 5a3c8c9ba3 Inline rule-specific parts of BodyLengthVisitor (#6121)
Main goal is to bring implementations, rules and examples closer together.
Another advantage is that the rule's layouts are in line with other
`@SwiftSyntaxRule`s.

After the refactoring, violation messages can be
better adapted to the object causing the issue. Violation positions
should be harmonized and more cases for protocols, subscripts and
deinitializers can be added.
2025-06-21 14:02:28 +02:00
Danny Mösch 573ac4da7d Report warning threshold (#6122) 2025-06-21 01:40:06 +02:00
Danny Mösch 7e0ea598ce Let large_tuple rule adhere to common style (#6120) 2025-06-20 22:23:29 +00:00
JP Simard 81474e36d0 Enforce SourceKitFreeRule contract with fatal error (#6107) 2025-06-20 07:25:01 -04:00
Matt Pennig 1e25cf6be6 Migrate VerticalWhitespaceRule from SourceKit to SwiftSyntax (#6103)
* Migrate VerticalWhitespaceRule from SourceKit to SwiftSyntax

* Adds tests for new horizontal whitespace behavior
2025-06-19 17:31:51 -04:00
JP Simard d1687859fe Migrate FileLengthRule from SourceKit to SwiftSyntax (#6100)
Convert FileLengthRule to use SwiftSyntax instead of SourceKit for
improved performance and fewer false positives.

The SwiftSyntax implementation:
- Uses ViolationsSyntaxVisitor pattern with token traversal
- Correctly handles multiline tokens by counting all spanned lines
- Properly excludes comment-only and whitespace-only lines
- Accurately attributes trivia to correct line positions
- Extracts common line range logic to reduce code duplication
2025-06-18 22:17:47 +00:00
Danny Mösch f961ad1728 Prefer string blocks (#6090) 2025-06-15 16:00:39 -04:00
Sonal e2ef7ed470 Treat actors as classes in class_delegate_protocol rule (#6067)
Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2025-06-15 17:56:09 +00:00
Danny Mösch a3aec89e21 Add test case for trailing comma in multiline arguments (#6085) 2025-05-21 07:29:00 -04:00
Kent Kaseda e1ac6f8607 Remove validates_start_with_lowercase option entirely (#6077) 2025-05-15 16:48:00 -04:00
Martin Redington 3ed7579fd2 Allow magic numbers to be configured (#6051) 2025-04-13 21:08:42 +01:00
Danny Mösch ed5b3741fe Remove opaque_over_existential rule (#6049)
This partially reverts commit 82736d1925.
2025-04-08 06:38:12 -04:00
Danny Mösch 47335d7f95 Remove tracking of correction positions (#5950)
Report number of corrections per file instead.
2025-04-05 06:04:37 -04:00
Danny Mösch 2578cb3862 Use SwiftSyntax version 601.0.0 (#6003) 2025-04-03 21:27:57 +02:00
Henry ed20aa593d Add new option ignores_multiline_strings to line_length rule (#6014) 2025-04-03 14:28:41 -04:00
suojae de36c51c75 Ignore UIColor initializers in no_magic_numbers rule (#6035)
Co-authored-by: Effie <effie.thedev@gmail.com>
Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2025-03-30 16:10:17 +00:00
Danny Mösch 9710148f76 Replace Sourcery with internal implementation (#6034)
Tasks we perform with Sourcery are rather simple and can be replaced
with a basic collection of files and the generation of lists in the
new `swiftlint-dev` command. This spares contributors from installing
either Sourcery or Bazel.
2025-03-26 20:38:53 +00:00
fraioli e68797ebea Respect macro types in file_name rule (#6027)
Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2025-03-22 20:55:18 -04:00
Jared Grubb 63fea48d83 Add excluded_methods option to unneeded_override rule (#6010) 2025-03-22 21:40:05 +01:00
Danny Mösch b14272d4f2 Rewrite type_contents_order rule with SwiftSyntax (#6002) 2025-03-17 20:15:02 +01:00
Danny Mösch 22e0f42ae1 Fix crash when disable command is preceded by unicode character (#5976) 2025-03-15 23:33:11 +01:00
Danny Mösch 59b4b4d8e2 Extract common code to method (#6019) 2025-03-10 18:42:02 +00:00
Eduard Miniakhmetov d2d1aacaac Add new allowed_types option to legacy_objc_type rule (#6012) 2025-03-10 09:28:04 +00:00
Martin Redington 5517d233fd Started to add rationales (#5681) 2025-03-06 10:16:30 +00:00
Danny Mösch 0d8e183cc0 Silence no_magic_numbers rule in #if conditions (#6001) 2025-02-15 18:17:37 +01:00
Danny Mösch 82736d1925 Add new opaque_over_existential rule (#5915) 2025-02-12 16:46:28 -05:00
Danny Mösch 6c8e8993ee Exclude @Suite types and @Test functions from no_magic_numbers analysis (#5968) 2025-02-12 14:27:33 -05:00
Danny Mösch 6ba231a11a Consider composed inherited types (#5984) 2025-01-30 22:50:18 +01:00
Danny Mösch b4eb2df567 Allow severity of duplicate_imports rule to be configurable (#5979) 2025-01-22 20:47:11 +00:00
Danny Mösch 9b22cda361 Add ib_segue_action to default configuration (#5956) 2025-01-14 21:50:11 +01:00
Riley Williams 651776896e Remove lints for redundant_sendable on protocols (#5959) 2025-01-13 23:15:40 +00:00
Danny Mösch 3f2b86c54d Remove trailing comma when Sendable was last (#5955) 2025-01-13 20:18:54 +01:00
Koichiro Ueki fc70594ad2 Ignore TipKit's #Rule macro in empty_count rule (#5918)
Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
2025-01-07 09:22:10 -05:00
Danny Mösch ae8aeb3995 Add option to disable redundant_discardable_let in SwiftUI view bodies (#5929) 2025-01-03 19:30:35 +01:00
Danny Mösch bef8acfb0e Avoid NSRegularExpression in configurations (#5921) 2024-12-28 12:54:23 -05:00
Danny Mösch 1d8af83b48 Collect corrections together with violations (#5916) 2024-12-27 09:58:53 +01:00
Danny Mösch ff21ff796c Specify rule properties in attribute 2024-12-26 20:40:29 +01:00