Skip to content

add fix-its for a bare regex literal with leading and/or trailing spaces #2744

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

Conversation

AppAppWorks
Copy link
Contributor

fix #1479

@AppAppWorks AppAppWorks marked this pull request as draft July 19, 2024 06:20
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

You should be able to remove the TODO in RegexLiteralLexer.swift:259 as well.

@AppAppWorks
Copy link
Contributor Author

You should be able to remove the TODO in RegexLiteralLexer.swift:259 as well.

I've found two other TODOs in RegexLiteralLexer.swift:210-212 about .spaceAtEndOfRegexLiteral

@AppAppWorks AppAppWorks force-pushed the fixit-for-spaces-at-ends-of-bare-regex-literal branch from aeca20b to 148533e Compare July 20, 2024 23:52
@ahoppen
Copy link
Member

ahoppen commented Jul 22, 2024

I've found two other TODOs in RegexLiteralLexer.swift:210-212 about .spaceAtEndOfRegexLiteral

Good catch. You can remove them as well.

@ahoppen
Copy link
Member

ahoppen commented Jul 22, 2024

@AppAppWorks AppAppWorks force-pushed the fixit-for-spaces-at-ends-of-bare-regex-literal branch from 148533e to 9dbaab0 Compare July 23, 2024 19:12
@AppAppWorks
Copy link
Contributor Author

I've found two other TODOs in RegexLiteralLexer.swift:210-212 about .spaceAtEndOfRegexLiteral

Good catch. You can remove them as well.

// TODO: We ought to have a fix-it that suggests #/.../#. We could
// suggest escaping, but that would be wrong if the user has written (?x).
// TODO: Should we suggest #/.../# for space-as-first character too?

I don't quite understand what "that would be wrong if the user has written (?x)." means, I am hesitating to remove these two TODOs.

@AppAppWorks AppAppWorks force-pushed the fixit-for-spaces-at-ends-of-bare-regex-literal branch 2 times, most recently from 07ea8b4 to 47b2113 Compare July 23, 2024 21:10
@AppAppWorks AppAppWorks force-pushed the fixit-for-spaces-at-ends-of-bare-regex-literal branch from 47b2113 to f73f4d0 Compare July 23, 2024 23:20
@hamishknight
Copy link
Contributor

hamishknight commented Jul 24, 2024

I don't quite understand what "that would be wrong if the user has written (?x)." means, I am hesitating to remove these two TODOs

We support an "extended syntax mode" in regex literals, which can be enabled with (?x) and makes whitespace non-semantic, e.g /(?x) a b c/ is equivalent to /abc/. This is relevant for the trailing space case because if you have e.g
/(?x) /, providing a fix-it of /(?x)\ / would change the meaning of the regex (previously it was empty, now it matches a literal space). This likely isn't a common case, but I think the easiest way of handling it is to only suggest adding # to the regex delimiters for the trailing space case, which won't change the semantic meaning. Checking for the use of (?x) isn't entirely trivial since it can appear anywhere in the pattern, can appear with other matching options, and can be disabled later on in the pattern.

@AppAppWorks
Copy link
Contributor Author

I don't quite understand what "that would be wrong if the user has written (?x)." means, I am hesitating to remove these two TODOs

We support an "extended syntax mode" in regex literals, which can be enabled with (?x) and makes whitespace non-semantic, e.g /(?x) a b c/ is equivalent to /abc/. This is relevant for the trailing space case because if you have e.g /(?x) /, providing a fix-it of /(?x)\ / would change the meaning of the regex (previously it was empty, now it matches a literal space). This likely isn't a common case, but I think the easiest way of handling it is to only suggest adding # to the regex delimiters for the trailing space case, which won't change the semantic meaning. Checking for the use of (?x) isn't entirely trivial since it can appear anywhere in the pattern, can appear with other matching options, and can be disabled later on in the pattern.

Thanks for your detailed explanation!

Considering that the parser of now will never emit diagnostics for the trailing space case alone, I guess we can safely discard the two TODOs ?

@hamishknight
Copy link
Contributor

hamishknight commented Jul 24, 2024

Considering that the parser of now will never emit diagnostics for the trailing space case alone, I guess we can safely discard the two TODOs ?

IIRC it can be emitted for things like this:

let x = /, /

(Oh huh I never added a test for that, my bad)

@AppAppWorks
Copy link
Contributor Author

Considering that the parser of now will never emit diagnostics for the trailing space case alone, I guess we can safely discard the two TODOs ?

IIRC it can be emitted for things like this:

let x = /, /

(Oh huh I never added a test for that, my bad)

I got it, I will revert back to my earlier design that the presence of a trailing space will mandate only "convert to extended regex literal with '#'" as the only Fix-it.

@AppAppWorks AppAppWorks force-pushed the fixit-for-spaces-at-ends-of-bare-regex-literal branch 2 times, most recently from 8e94551 to 8ee1211 Compare July 24, 2024 22:04
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

@AppAppWorks AppAppWorks force-pushed the fixit-for-spaces-at-ends-of-bare-regex-literal branch from 8ee1211 to f7119d9 Compare July 25, 2024 00:22
@AppAppWorks
Copy link
Contributor Author

Done, please check.

@AppAppWorks AppAppWorks marked this pull request as ready for review July 25, 2024 00:33
@ahoppen
Copy link
Member

ahoppen commented Jul 25, 2024

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge July 25, 2024 16:10
@ahoppen
Copy link
Member

ahoppen commented Jul 25, 2024

@swift-ci Please test Windows

added "convert to extended regex literal" and/or "insert '\'" to `SwiftSyntax.TokenDiagnostic.fixIts`
introduced a more general `assertParse` that can test for different combinations of fix-its against fixed sources
modified related test cases
auto-merge was automatically disabled July 25, 2024 20:32

Head branch was pushed to by a user without write access

@AppAppWorks AppAppWorks force-pushed the fixit-for-spaces-at-ends-of-bare-regex-literal branch from f7119d9 to bba7b15 Compare July 25, 2024 20:32
@ahoppen
Copy link
Member

ahoppen commented Jul 25, 2024

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge July 25, 2024 22:02
@AppAppWorks
Copy link
Contributor Author

The check statuses have stayed unchanged for 5 days, could there be any problem with the CI?

@bnbarham
Copy link
Contributor

@swift-ci please test

@ahoppen ahoppen merged commit 20ce3cb into swiftlang:main Jul 31, 2024
3 checks passed
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.

Add fix-it for a regex that starts or ends with a space
4 participants