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

Rule Request: Warn on use of some NS types in Swift code #2758

Open
2 tasks done
72A12F4E opened this issue May 14, 2019 · 1 comment
Open
2 tasks done

Rule Request: Warn on use of some NS types in Swift code #2758

72A12F4E opened this issue May 14, 2019 · 1 comment
Labels
rule-request Requests for a new rules.

Comments

@72A12F4E
Copy link

72A12F4E commented May 14, 2019

New Issue Checklist

New rule request

Please describe the rule idea, format
this issue's title as Rule Request: [Rule Name] and describe:

  1. Why should this rule be added? Share links to existing discussion about what
    the community thinks about this.

This would prevent developers from using legacy NS types where swift versions of those types already exist. Apple outlines guidance on which types get bridged here:

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/working_with_foundation_types

  1. Provide several examples of what would and wouldn't trigger violations.

Bad

All of the below types have a Swift analogue and should not be used in Swift code

let foo1: NSDictionary = bar1
let foo2 = NSArray()
let foo3: NSString = bar3
let foo4 = NSSet()

Ok

These types do not have a Swift analogue, so they should be allowed.

let vc = NSViewController()
let orderedSet = NSOrderedSet()
  1. Should the rule be configurable, if so what parameters should be configurable?

Unsure

  1. Should the rule be opt-in or enabled by default? Why?
    See README.md for guidelines on when to mark a rule as opt-in.

I think this rule should be opt-in. There is a lot of potential for flagging types that should be completely valid in normal Swift code, and in codebases where very particular Swift-ObjC is required.

@72A12F4E
Copy link
Author

72A12F4E commented May 14, 2019

Here is a regex based rule that I implemented to identify these issues. I hope it helps!

custom_rules:
  legacy_objc_type:
    included: ".*.swift"
    name: "Legacy Obj-C Type"
    regex: "NSAffineTransform|NSArray|NSCalendar|NSCharacterSet|NSData|NSDateComponents|NSDateInterval|NSDate|NSDecimalNumber|NSDictionary|NSIndexPath|NSIndexSet|NSLocale|NSMeasurement|NSNotification|NSNumber|NSPersonNameComponents|NSSet|NSString|NSTimeZone|NSURL|NSURLComponents|NSURLQueryItem|NSURLRequest|NSUUID"
    message: "Avoid using Obj-C types when Swift types will suffice."
    severity: warning
    match_kinds:
      - typeidentifier

@PaulTaykalo PaulTaykalo added the rule-request Requests for a new rules. label Nov 8, 2019
jpsim pushed a commit that referenced this issue Feb 23, 2021
jpsim added a commit that referenced this issue Feb 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests

2 participants