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

Upgrade the DontRepeatTypeInStaticProperties rule. #777

Conversation

shawnhyam
Copy link
Contributor

  • Using the visit method to examine all the variable declarations should allow specific decls to be ignored.
  • Update the PipelineGenerator to call visitPost methods
  • Modify the PipelineGenerator to produce output matching the current generated files.

@shawnhyam
Copy link
Contributor Author

Should be a partial fix for #771, since it upgrades one rule only. Interested in feedback on if there is a better way to do this before upgrading any other rules.

@shawnhyam shawnhyam force-pushed the upgrade-dont-repeat-type-in-static-properties branch 2 times, most recently from 9cdffee to a700197 Compare July 24, 2024 13:22
@ahoppen
Copy link
Member

ahoppen commented Jul 29, 2024

@shawnhyam Instead of merging main into your branch, could you rebase your branch? I find it creates a nicer git history.

@shawnhyam shawnhyam force-pushed the upgrade-dont-repeat-type-in-static-properties branch from 1280cae to 7496b30 Compare July 29, 2024 18:08
@shawnhyam
Copy link
Contributor Author

@allevato I was wondering if you had any thoughts on this, especially the changes to the pipeline generator (so that visitPost gets called).

extension Syntax {
/// Returns the name of the immediately enclosing type of this node if there is one,
/// otherwise nil.
fileprivate var typeName: String? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of this property feels a little misleading; when I saw you accessing the typeName of a VariableDeclSyntax node above, it read to me like it was accessing the type of the variable. Can we call it containingDeclName so that the nature of the traversal is clear at the call site?

@@ -16,6 +16,7 @@ import SwiftSyntax
///
/// This rule does not apply to test code, defined as code which:
/// * Contains the line `import XCTest`
/// * The function is marked with `@Test` attribute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this noise from another PR that was merged in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this noise from another PR that was merged in?

This was because I ran generate-swift-format, and there have been some manual edits to the generated files. So I made the code edits necessary to keep the generated file the same.

Sources/SwiftFormat/Core/Pipelines+Generated.swift Outdated Show resolved Hide resolved
diagnose(.removeTypeFromName(name: varName, type: bareTypeName), on: identifierPattern)
}
let varName = identifierPattern.identifier.text
if varName.contains(bareTypeName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I'm getting another look at this, I wonder if we should update this to be hasSuffix, since the diagnostic explicitly talks about suffixes.

Copy link
Contributor Author

@shawnhyam shawnhyam Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question... we wouldn't catch plurals (which we don't mention or have tests for), but there are likely false positives with the current logic too.

public actor Cookie {
  // cases that are caught with .contains but not with .hasSuffix
  static let chocolateChipCookies: Int
  static let rawCookieDough: Int
}

- Using the visit method to examine all the variable declarations should
  allow specific decls to be ignored.
- Modify the PipelineGenerator to produce output matching the current
  generated files.
@allevato
Copy link
Member

allevato commented Aug 1, 2024

Thanks for fixing this!

@allevato allevato merged commit f0d1167 into swiftlang:main Aug 1, 2024
@shawnhyam shawnhyam deleted the upgrade-dont-repeat-type-in-static-properties branch August 6, 2024 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants