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

fix: clean string regexp #7871

Merged
merged 9 commits into from Apr 24, 2022
Merged

Conversation

poyoho
Copy link
Member

@poyoho poyoho commented Apr 23, 2022

Description

fix: #7855

Additional context

make cleanString don't affect by regexp
https://regex101.com/r/G2SYip/1


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@kherock
Copy link
Contributor

kherock commented Apr 23, 2022

This still needs to handle comments that are on the same line as a regexp:

const foo = /'/ // this doesn't work

result:

const foo = this doesn't work

@poyoho
Copy link
Member Author

poyoho commented Apr 23, 2022

thank again, fixed! https://regex101.com/r/9myfsm/1

@kherock
Copy link
Contributor

kherock commented Apr 23, 2022

It doesn't seem to work right when there's an escaped \/ within the regex:

https://regex101.com/r/I3tD8q/1

The following seems to work better for me:

"([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*'|\/\*(.|[\r\n])*?\*\/|\/\/.*|\/.*?(?<!\\)\/

@poyoho
Copy link
Member Author

poyoho commented Apr 23, 2022

I found, hhh. And fixing now.

@kherock
Copy link
Contributor

kherock commented Apr 23, 2022

Actually yours was just one character off, you had \\ instead of \/ at the end, this works

"([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*'|\/\*(.|[\r\n])*?\*\/|\/\/.*|\/([^\/]|(?<=\\)\/)*\/

kherock
kherock previously approved these changes Apr 23, 2022
Copy link
Contributor

@kherock kherock left a comment

Choose a reason for hiding this comment

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

I was able to verify that my site is now able to build with these changes, thank you 👍

@poyoho
Copy link
Member Author

poyoho commented Apr 23, 2022

https://regex101.com/r/Kk2cJ0/1

I think the best way is make the affect in one line. beacause we can't to process

1 / 1 + new URL('./hello.js', import.meta.url).href + 1 / 1

And I think an error match in one line (such as the below error) can accept.

const a = /111'111/ // new URL('./hello.js', import.meta.url).href 

@kherock
Copy link
Contributor

kherock commented Apr 23, 2022

I was able to catch more cases by ignoring syntactically invalid quote characters. I think this is as good as we can make it without doing a separate initial pass that strips away comments.

https://regex101.com/r/acTUGD/1

The opening quote character is ignored when the most recent non-whitespace character is a word character: (?<=(?:^|[^\w\s])\s*).

Additionally, the closing quote character is ignored when the next non-whitespace character is a word character: (?=\s*(?:$|[^\w\s])). This check prevents single " or ' in comments from being interpreted as a closing quote.

@patak-dev patak-dev added the p3-minor-bug An edge case that only affects very specific usage (priority) label Apr 24, 2022
Co-authored-by: patak <matias.capeletto@gmail.com>
@poyoho
Copy link
Member Author

poyoho commented Apr 24, 2022

#7871 (comment)

Although it can narrow the scope of the problem, it still feels that it can not completely solve the problem. I want to solve this problem thoroughly through other methods. 🙈

@poyoho
Copy link
Member Author

poyoho commented Apr 24, 2022

@kherock if you interesting in this let talk in discord 😊

@patak-dev patak-dev merged commit ecc78bc into vitejs:main Apr 24, 2022
@poyoho poyoho deleted the fix/cleanString-regexp branch April 24, 2022 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Quote inside of RegExp breaks cleanString
3 participants