8000 Add a function to check if a name can be used as an identifier in a given context by ahoppen · Pull Request #2434 · swiftlang/swift-syntax · GitHub
[go: up one dir, main page]

Skip to content

Add a function to check if a name can be used as an identifier in a given context #2434

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

Merged
merged 1 commit into from
Jan 26, 2024

Conversation

ahoppen
Copy link
Member
@ahoppen ahoppen commented Jan 22, 2024

rdar://120721971

@ahoppen ahoppen requested a review from bnbarham as a code owner January 22, 2024 03:49
@ahoppen
Copy link
Member Author
ahoppen commented Jan 22, 2024

@swift-ci Please test

@bnbarham bnbarham requested a review from rintaro January 22, 2024 22:38
@ahoppen ahoppen force-pushed the ahoppen/identifier-check branch from f24b22a to b81c8bc Compare January 22, 2024 23:04
@ahoppen
Copy link
Member Author
ahoppen commented Jan 22, 2024

@swift-ci Please test

/// - `test` is a valid identifier for member access because `myStruct.test` is valid
/// - `class` is a valid identifier for member access becuase `myStruct.class` is valid, even though `class`
/// needs to be wrapped in backticks when used to declare a variable.
/// - `self` is not a valid identifiers for member access because `myStruct.self` does not access a member named
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// - `self` is not a valid identifiers for member access because `myStruct.self` does not access a member named
/// - `self` is not a valid identifier for member access because `myStruct.self` does not access a member named

XCTAssertTrue(isValidIdentifier("`class`", for: .memberAccess))
XCTAssertFalse(isValidIdentifier("self", for: .memberAccess))
XCTAssertTrue(isValidIdentifier("`self`", for: .memberAccess))
XCTAssertTrue(isValidIdentifier("`let`", for: .memberAccess))
Copy link
Contributor

Choose a reason for hiding this comment

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

let as well? Same above

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed the tests to add an assertValidIdentifier that makes it easier to check if a name is a valid identifier in the different contexts.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, nice. If you're going that far you could have the init just take a map rather than each case, but it's just a test so I what you have is also fine 👍

@ahoppen ahoppen force-pushed the ahoppen/identifier-check branch from b81c8bc to 23637ba Compare January 22, 2024 23:38
@ahoppen
Copy link
Member Author
ahoppen commented Jan 22, 2024

@swift-ci Please test

@ahoppen
Copy link
Member Author
ahoppen commented Jan 22, 2024

@swift-ci Please test Windows

private func isValidVariableName(_ name: String) -> Bool {
var parser = Parser("var \(name)")
let decl = DeclSyntax.parse(from: &parser)
guard parser.at(.endOfFile) else {
Copy link
Member

Choose a reason for hiding this comment

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

This should also check the current lexeme's leadingTriviaByteLength == 0.

// We parsed the name as a keyword, eg. `self`, so not a valid identifier.
return false
}
guard identifier.leadingTrivia.isEmpty && identifier.trailingTrivia.isEmpty else {
Copy link
Member

Choose a reason for hiding this comment

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

variable.bindingSpecifier.trailingTriviaLength == 1 too. Consider name = " foo" case.

Copy link
Member
@rintaro rintaro Jan 24, 2024

Choose a reason for hiding this comment

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

Or maybe identifier.rawText.count == name.utf8.count so we can remove all the trivia check including .endOfFile check above.

edit: wait, we're already checking identifier.tokenKind == .identifier(name). Is this trivia check actually needed?

Copy link
Member Author
@ahoppen ahoppen Jan 24, 2024

Choose a reason for hiding this comment

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

Yes, you’re right. The trivia checks aren’t actually needed. Nice 👍🏽

Which also means that we don’t need the check that there are only 2/3 tokens in the parsed tree.

guard let identifier = variable.bindings.first?.pattern.as(IdentifierPatternSyntax.self)?.identifier else {
return false
}
guard identifier.tokenKind == .identifier(name) else {
8000
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
guard identifier.tokenKind == .identifier(name) else {
guard identifier.rawTokenKind == .identfiier && identifier.rawTokenText.count == name.utf8.count else {

should give us a tiny performance increase.

@ahoppen ahoppen force-pushed the ahoppen/identifier-check branch from 23637ba to 8e22883 Compare January 24, 2024 18:04
@ahoppen
Copy link
Member Author
ahoppen commented Jan 24, 2024

@swift-ci Please test

@ahoppen
Copy link
Member Author
ahoppen commented Jan 24, 2024

@swift-ci Please test Windows

@ahoppen ahoppen force-pushed the ahoppen/identifier-check branch from 8e22883 to 8df8315 Compare January 24, 2024 22:18
@ahoppen
Copy link
Member Author
ahoppen commented Jan 24, 2024

@swift-ci Please test

@ahoppen
Copy link
Member Author
ahoppen commented Jan 24, 2024

@swift-ci Please test Windows

@ahoppen ahoppen force-pushed the ahoppen/identifier-check branch from fd86785 to 8df8315 Compare January 25, 2024 00:04
@ahoppen ahoppen force-pushed the ahoppen/identifier-check branch from 8df8315 to 38b7b38 Compare January 25, 2024 04:45
@ahoppen
Copy link
Member Author
ahoppen commented Jan 25, 2024

@swift-ci Please test

@ahoppen
Copy link
Member Author
ahoppen commented Jan 25, 2024

@swift-ci Please test Windows

@ahoppen ahoppen merged commit 0fb0018 into swiftlang:main Jan 26, 2024
@ahoppen ahoppen deleted the ahoppen/identifier-check branch January 26, 2024 23:31
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.

4 participants
0