mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Omit needless words: never reduce a name down to "using", "for", or
"with" Treat these prepositions as vacuous names, so we never reduce any parameter name down to one of them. If that would happen, leave the entire parameter name alone. This makes our transformation a bit more conservative. Swift SVN r32460
This commit is contained in:
@@ -467,7 +467,11 @@ static StringRef omitNeedlessWordsFromPrefix(StringRef name,
|
||||
|
||||
/// Identify certain vacuous names to which we do not want to reduce any name.
|
||||
static bool isVacuousName(StringRef name) {
|
||||
return name == "set" || name == "get";
|
||||
return camel_case::sameWordIgnoreFirstCase(name, "get") ||
|
||||
camel_case::sameWordIgnoreFirstCase(name, "for") ||
|
||||
camel_case::sameWordIgnoreFirstCase(name, "set") ||
|
||||
camel_case::sameWordIgnoreFirstCase(name, "using") ||
|
||||
camel_case::sameWordIgnoreFirstCase(name, "with");
|
||||
}
|
||||
|
||||
static StringRef omitNeedlessWords(StringRef name,
|
||||
@@ -628,17 +632,7 @@ static StringRef omitNeedlessWords(StringRef name,
|
||||
return origName;
|
||||
break;
|
||||
|
||||
case NameRole::SubsequentParameter: {
|
||||
// For subsequent parameters, drop useless leading prepositions such as
|
||||
// "with" and "using".
|
||||
StringRef firstWord = camel_case::getFirstWord(name);
|
||||
if (firstWord.size() < name.size() &&
|
||||
(firstWord == "with" || firstWord == "using")) {
|
||||
name = toLowercaseWord(name.substr(firstWord.size()), scratch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case NameRole::SubsequentParameter:
|
||||
case NameRole::FirstParameter:
|
||||
case NameRole::Partial:
|
||||
break;
|
||||
@@ -716,6 +710,9 @@ static bool omitNeedlessWordsMatchingFirstArgumentLabel(
|
||||
unsigned splitPos = nameWordRevIter.base().getPosition();
|
||||
if (splitPos == 0) return false;
|
||||
|
||||
// If we would be left with a vacuous argument name, don't split.
|
||||
if (isVacuousName(name.substr(splitPos))) return false;
|
||||
|
||||
// Split into base name/first argument label.
|
||||
argName = ::toLowercaseWord(name.substr(splitPos), scratch);
|
||||
name = name.substr(0, splitPos);
|
||||
@@ -875,8 +872,14 @@ bool swift::omitNeedlessWords(StringRef &baseName,
|
||||
afterSplitPos += 3;
|
||||
|
||||
// Create a first argument name with the remainder of the base name,
|
||||
// lowercased.
|
||||
argNames[0] = toLowercaseWord(newName.substr(afterSplitPos), scratch);
|
||||
// lowercased. If we would end up with a vacuous name, go
|
||||
// back and get the original.
|
||||
StringRef newArgName = newName.substr(afterSplitPos);
|
||||
if (isVacuousName(newArgName)) {
|
||||
size_t pos = name.rfind(newArgName);
|
||||
newArgName = name.substr(pos);
|
||||
}
|
||||
argNames[0] = toLowercaseWord(newArgName, scratch);
|
||||
|
||||
// Update the base name by splitting at the preposition.
|
||||
newName = newName.substr(0, splitPos);
|
||||
|
||||
@@ -11,16 +11,16 @@ func renameFirst(vc: NSViewController) {
|
||||
|
||||
func dropDefaultedNil(array: NSArray, sel: Selector,
|
||||
body: ((AnyObject!, Int, UnsafeMutablePointer<ObjCBool>) -> Void)?) {
|
||||
array.makeObjectsPerformSelector(sel, withObject: nil) // expected-warning{{'makeObjectsPerformSelector(_:withObject:)' could be named 'makeObjectsPerform(_:with:)'}}{{9-35=makeObjectsPerform}}
|
||||
array.makeObjectsPerformSelector(sel, withObject: nil, withObject: nil) // expected-warning{{'makeObjectsPerformSelector(_:withObject:withObject:)' could be named 'makeObjectsPerform(_:with:with:)'}}{{9-35=makeObjectsPerform}}
|
||||
array.makeObjectsPerformSelector(sel, withObject: nil) // expected-warning{{'makeObjectsPerformSelector(_:withObject:)' could be named 'makeObjectsPerform(_:withObject:)'}}{{9-35=makeObjectsPerform}}
|
||||
array.makeObjectsPerformSelector(sel, withObject: nil, withObject: nil) // expected-warning{{'makeObjectsPerformSelector(_:withObject:withObject:)' could be named 'makeObjectsPerform(_:withObject:withObject:)'}}{{9-35=makeObjectsPerform}}
|
||||
array.enumerateObjectsRandomlyWithBlock(nil) // expected-warning{{'enumerateObjectsRandomlyWithBlock' could be named 'enumerateObjectsRandomly(withBlock:)'}}{{9-42=enumerateObjectsRandomly}}{{43-46=}}
|
||||
array.enumerateObjectsRandomlyWithBlock(body) // expected-warning{{'enumerateObjectsRandomlyWithBlock' could be named 'enumerateObjectsRandomly(withBlock:)'}}{{9-42=enumerateObjectsRandomly}}{{43-43=withBlock: }}
|
||||
}
|
||||
|
||||
func dropDefaultedOptionSet(array: NSArray) {
|
||||
array.enumerateObjectsWithOptions([]) { obj, idx, stop in print("foo") } // expected-warning{{'enumerateObjectsWithOptions(_:usingBlock:)' could be named 'enumerateObjects(with:block:)'}}{{9-36=enumerateObjects}}{{36-40=}}
|
||||
array.enumerateObjectsWithOptions([], usingBlock: { obj, idx, stop in print("foo") }) // expected-warning{{'enumerateObjectsWithOptions(_:usingBlock:)' could be named 'enumerateObjects(with:block:)'}}{{9-36=enumerateObjects}}{{37-41=}}
|
||||
array.enumerateObjectsWhileOrderingPizza(true, withOptions: [], usingBlock: { obj, idx, stop in print("foo") }) // expected-warning{{'enumerateObjectsWhileOrderingPizza(_:withOptions:usingBlock:)' could be named 'enumerateObjectsWhileOrdering(pizza:with:block:)'}}{{48-65=}}{{44-44=pizza: }}
|
||||
array.enumerateObjectsWithOptions([]) { obj, idx, stop in print("foo") } // expected-warning{{'enumerateObjectsWithOptions(_:usingBlock:)' could be named 'enumerateObjects(withOptions:usingBlock:)'}}{{9-36=enumerateObjects}}{{36-40=}}
|
||||
array.enumerateObjectsWithOptions([], usingBlock: { obj, idx, stop in print("foo") }) // expected-warning{{'enumerateObjectsWithOptions(_:usingBlock:)' could be named 'enumerateObjects(withOptions:usingBlock:)'}}{{9-36=enumerateObjects}}{{37-41=}}
|
||||
array.enumerateObjectsWhileOrderingPizza(true, withOptions: [], usingBlock: { obj, idx, stop in print("foo") }) // expected-warning{{'enumerateObjectsWhileOrderingPizza(_:withOptions:usingBlock:)' could be named 'enumerateObjectsWhileOrdering(pizza:withOptions:usingBlock:)'}}{{48-65=}}{{44-44=pizza: }}
|
||||
}
|
||||
|
||||
func dropDefaultedWithoutRename(domain: String, code: Int, array: NSArray) {
|
||||
|
||||
@@ -25,10 +25,10 @@
|
||||
// CHECK-FOUNDATION: func makeObjectsPerform(_: Selector)
|
||||
|
||||
// Note: "with" parameters.
|
||||
// CHECK-FOUNDATION: func makeObjectsPerform(_: Selector, with: AnyObject?)
|
||||
// CHECK-FOUNDATION: func makeObjectsPerform(_: Selector, withObject: AnyObject?)
|
||||
|
||||
// Note: "with" parameters drop the "with".
|
||||
// CHECK-FOUNDATION: func makeObjectsPerform(_: Selector, with: AnyObject?, with: AnyObject?)
|
||||
// CHECK-FOUNDATION: func makeObjectsPerform(_: Selector, withObject: AnyObject?, withObject: AnyObject?)
|
||||
|
||||
// Note: id -> "Object".
|
||||
// CHECK-FOUNDATION: func indexOf(_: AnyObject) -> Int
|
||||
@@ -37,15 +37,12 @@
|
||||
// CHECK-OBJECTIVEC: func isKindOf(aClass: AnyClass) -> Bool
|
||||
|
||||
// Note: Pointer-to-struct name matching; "with" splits the first piece.
|
||||
// CHECK-FOUNDATION: func copy(with _: NSZone = nil) -> AnyObject!
|
||||
// CHECK-FOUNDATION: func copy(withZone _: NSZone = nil) -> AnyObject!
|
||||
|
||||
// Note: Objective-C type parameter names.
|
||||
// CHECK-FOUNDATION: func objectFor(_: NSCopying) -> AnyObject?
|
||||
// CHECK-FOUNDATION: func removeObjectFor(_: NSCopying)
|
||||
|
||||
// Note: Allow argument labels that are keywords.
|
||||
// CHECK-FOUNDATION: func setObject(_: AnyObject, `for`: NSCopying)
|
||||
|
||||
// Note: Don't drop the name of the first parameter in an initializer entirely.
|
||||
// CHECK-FOUNDATION: init(array: [AnyObject])
|
||||
|
||||
@@ -68,7 +65,7 @@
|
||||
// CHECK-FOUNDATION: func subtract(_: Int32) -> NSNumber
|
||||
|
||||
// Note: multi-word enum name matching; "with" splits the first piece.
|
||||
// CHECK-FOUNDATION: func someMethod(with _: NSDeprecatedOptions = [])
|
||||
// CHECK-FOUNDATION: func someMethod(withDeprecatedOptions _: NSDeprecatedOptions = [])
|
||||
|
||||
// Note: class name matching; don't drop "With".
|
||||
// CHECK-FOUNDATION: class func withString(_: String!) -> Self!
|
||||
@@ -113,10 +110,10 @@
|
||||
|
||||
// Note: usingBlock -> body
|
||||
// CHECK-FOUNDATION: func enumerateObjectsUsing(_: ((AnyObject!, Int, UnsafeMutablePointer<ObjCBool>) -> Void)!)
|
||||
// CHECK-FOUNDATION: func enumerateObjects(with _: NSEnumerationOptions = [], using: ((AnyObject!, Int, UnsafeMutablePointer<ObjCBool>) -> Void)!)
|
||||
// CHECK-FOUNDATION: func enumerateObjects(withOptions _: NSEnumerationOptions = [], usingBlock: ((AnyObject!, Int, UnsafeMutablePointer<ObjCBool>) -> Void)!)
|
||||
|
||||
// Note: WithBlock -> body, nullable closures default to nil.
|
||||
// CHECK-FOUNDATION: func enumerateObjectsRandomly(with _: ((AnyObject!, Int, UnsafeMutablePointer<ObjCBool>) -> Void)? = nil)
|
||||
// CHECK-FOUNDATION: func enumerateObjectsRandomly(withBlock _: ((AnyObject!, Int, UnsafeMutablePointer<ObjCBool>) -> Void)? = nil)
|
||||
|
||||
// Note: id<Proto> treated as "Proto".
|
||||
// CHECK-FOUNDATION: func doSomethingWith(_: NSCopying)
|
||||
@@ -164,7 +161,7 @@
|
||||
// CHECK-APPKIT: func drawInAirAt(_: Point3D)
|
||||
|
||||
// Note: with<something> -> <something>
|
||||
// CHECK-APPKIT: func drawAt(_: Point3D, attributes: [String : AnyObject]?)
|
||||
// CHECK-APPKIT: func drawAt(_: Point3D, withAttributes: [String : AnyObject]?)
|
||||
|
||||
// Note: Don't strip names that aren't preceded by a verb or preposition.
|
||||
// CHECK-APPKIT: func setTextColor(_: NSColor?)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
class C1 {
|
||||
init(tasteString: String) { } // expected-warning{{'init(tasteString:)' could be named 'init(taste:)'}}{{8-8=taste }}
|
||||
func processWithString(string: String, forInt: Int) { } // expected-warning{{'processWithString(_:forInt:)' could be named 'processWith(_:for:)'}}{{8-25=processWith}}{{42-42=for }}
|
||||
func processWithString(string: String, toInt: Int) { } // expected-warning{{'processWithString(_:toInt:)' could be named 'processWith(_:to:)'}}{{8-25=processWith}}{{42-42=to }}
|
||||
func processWithInt(value: Int) { } // expected-warning{{'processWithInt' could be named 'processWith'}}{{8-22=processWith}}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ extension String {
|
||||
|
||||
func callSites(s: String) {
|
||||
let c1 = C1(tasteString: "blah") // expected-warning{{'init(tasteString:)' could be named 'init(taste:)'}}{{15-26=taste}}
|
||||
c1.processWithString("a", forInt: 1) // expected-warning{{'processWithString(_:forInt:)' could be named 'processWith(_:for:)'}}{{6-23=processWith}}{{29-35=for}}
|
||||
c1.processWithString("a", toInt: 1) // expected-warning{{'processWithString(_:toInt:)' could be named 'processWith(_:to:)'}}{{6-23=processWith}}{{29-34=to}}
|
||||
c1.processWithInt(5) // expected-warning{{'processWithInt' could be named 'processWith'}}{{6-20=processWith}}
|
||||
_ = String.randomString // expected-warning{{'randomString' could be named 'random'}}{{14-26=random}}
|
||||
_ = s.wonkycasedString // expected-warning{{'wonkycasedString' could be named 'wonkycased'}}{{9-25=wonkycased}}
|
||||
|
||||
Reference in New Issue
Block a user