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:
Doug Gregor
2015-10-06 07:40:34 +00:00
parent e60e5a3605
commit 2dcc3afd34
4 changed files with 31 additions and 31 deletions

View File

@@ -467,7 +467,11 @@ static StringRef omitNeedlessWordsFromPrefix(StringRef name,
/// Identify certain vacuous names to which we do not want to reduce any name. /// Identify certain vacuous names to which we do not want to reduce any name.
static bool isVacuousName(StringRef 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, static StringRef omitNeedlessWords(StringRef name,
@@ -628,17 +632,7 @@ static StringRef omitNeedlessWords(StringRef name,
return origName; return origName;
break; break;
case NameRole::SubsequentParameter: { 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::FirstParameter: case NameRole::FirstParameter:
case NameRole::Partial: case NameRole::Partial:
break; break;
@@ -716,6 +710,9 @@ static bool omitNeedlessWordsMatchingFirstArgumentLabel(
unsigned splitPos = nameWordRevIter.base().getPosition(); unsigned splitPos = nameWordRevIter.base().getPosition();
if (splitPos == 0) return false; 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. // Split into base name/first argument label.
argName = ::toLowercaseWord(name.substr(splitPos), scratch); argName = ::toLowercaseWord(name.substr(splitPos), scratch);
name = name.substr(0, splitPos); name = name.substr(0, splitPos);
@@ -875,8 +872,14 @@ bool swift::omitNeedlessWords(StringRef &baseName,
afterSplitPos += 3; afterSplitPos += 3;
// Create a first argument name with the remainder of the base name, // Create a first argument name with the remainder of the base name,
// lowercased. // lowercased. If we would end up with a vacuous name, go
argNames[0] = toLowercaseWord(newName.substr(afterSplitPos), scratch); // 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. // Update the base name by splitting at the preposition.
newName = newName.substr(0, splitPos); newName = newName.substr(0, splitPos);

View File

@@ -11,16 +11,16 @@ func renameFirst(vc: NSViewController) {
func dropDefaultedNil(array: NSArray, sel: Selector, func dropDefaultedNil(array: NSArray, sel: Selector,
body: ((AnyObject!, Int, UnsafeMutablePointer<ObjCBool>) -> Void)?) { 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) // 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(_:with:with:)'}}{{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(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: }} array.enumerateObjectsRandomlyWithBlock(body) // expected-warning{{'enumerateObjectsRandomlyWithBlock' could be named 'enumerateObjectsRandomly(withBlock:)'}}{{9-42=enumerateObjectsRandomly}}{{43-43=withBlock: }}
} }
func dropDefaultedOptionSet(array: NSArray) { 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([]) { 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(with:block:)'}}{{9-36=enumerateObjects}}{{37-41=}} 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:with:block:)'}}{{48-65=}}{{44-44=pizza: }} 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) { func dropDefaultedWithoutRename(domain: String, code: Int, array: NSArray) {

View File

@@ -25,10 +25,10 @@
// CHECK-FOUNDATION: func makeObjectsPerform(_: Selector) // CHECK-FOUNDATION: func makeObjectsPerform(_: Selector)
// Note: "with" parameters. // Note: "with" parameters.
// CHECK-FOUNDATION: func makeObjectsPerform(_: Selector, with: AnyObject?) // CHECK-FOUNDATION: func makeObjectsPerform(_: Selector, withObject: AnyObject?)
// Note: "with" parameters drop the "with". // 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". // Note: id -> "Object".
// CHECK-FOUNDATION: func indexOf(_: AnyObject) -> Int // CHECK-FOUNDATION: func indexOf(_: AnyObject) -> Int
@@ -37,15 +37,12 @@
// CHECK-OBJECTIVEC: func isKindOf(aClass: AnyClass) -> Bool // CHECK-OBJECTIVEC: func isKindOf(aClass: AnyClass) -> Bool
// Note: Pointer-to-struct name matching; "with" splits the first piece. // 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. // Note: Objective-C type parameter names.
// CHECK-FOUNDATION: func objectFor(_: NSCopying) -> AnyObject? // CHECK-FOUNDATION: func objectFor(_: NSCopying) -> AnyObject?
// CHECK-FOUNDATION: func removeObjectFor(_: NSCopying) // 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. // Note: Don't drop the name of the first parameter in an initializer entirely.
// CHECK-FOUNDATION: init(array: [AnyObject]) // CHECK-FOUNDATION: init(array: [AnyObject])
@@ -68,7 +65,7 @@
// CHECK-FOUNDATION: func subtract(_: Int32) -> NSNumber // CHECK-FOUNDATION: func subtract(_: Int32) -> NSNumber
// Note: multi-word enum name matching; "with" splits the first piece. // 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". // Note: class name matching; don't drop "With".
// CHECK-FOUNDATION: class func withString(_: String!) -> Self! // CHECK-FOUNDATION: class func withString(_: String!) -> Self!
@@ -113,10 +110,10 @@
// Note: usingBlock -> body // Note: usingBlock -> body
// CHECK-FOUNDATION: func enumerateObjectsUsing(_: ((AnyObject!, Int, UnsafeMutablePointer<ObjCBool>) -> Void)!) // 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. // 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". // Note: id<Proto> treated as "Proto".
// CHECK-FOUNDATION: func doSomethingWith(_: NSCopying) // CHECK-FOUNDATION: func doSomethingWith(_: NSCopying)
@@ -164,7 +161,7 @@
// CHECK-APPKIT: func drawInAirAt(_: Point3D) // CHECK-APPKIT: func drawInAirAt(_: Point3D)
// Note: with<something> -> <something> // 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. // Note: Don't strip names that aren't preceded by a verb or preposition.
// CHECK-APPKIT: func setTextColor(_: NSColor?) // CHECK-APPKIT: func setTextColor(_: NSColor?)

View File

@@ -2,7 +2,7 @@
class C1 { class C1 {
init(tasteString: String) { } // expected-warning{{'init(tasteString:)' could be named 'init(taste:)'}}{{8-8=taste }} 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}} func processWithInt(value: Int) { } // expected-warning{{'processWithInt' could be named 'processWith'}}{{8-22=processWith}}
} }
@@ -13,7 +13,7 @@ extension String {
func callSites(s: String) { func callSites(s: String) {
let c1 = C1(tasteString: "blah") // expected-warning{{'init(tasteString:)' could be named 'init(taste:)'}}{{15-26=taste}} 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}} 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}} _ = String.randomString // expected-warning{{'randomString' could be named 'random'}}{{14-26=random}}
_ = s.wonkycasedString // expected-warning{{'wonkycasedString' could be named 'wonkycased'}}{{9-25=wonkycased}} _ = s.wonkycasedString // expected-warning{{'wonkycasedString' could be named 'wonkycased'}}{{9-25=wonkycased}}