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

Quote inside of RegExp breaks cleanString #7855

Closed
7 tasks done
kherock opened this issue Apr 22, 2022 · 3 comments · Fixed by #7871
Closed
7 tasks done

Quote inside of RegExp breaks cleanString #7855

kherock opened this issue Apr 22, 2022 · 3 comments · Fixed by #7871
Assignees
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) pending triage

Comments

@kherock
Copy link
Contributor

kherock commented Apr 22, 2022

Describe the bug

Plugins that use the emptyString() function have a serious issue when they're activated while the raw code has a RegExp expression with a single or double quote inside.

test('strings', () => {
  const clean = emptyString(`
    // comment1
    const a = 'aaaa'
    /* coment2 */
    const b = "bbbb"
    const regexp = /'/
    const c = "cccc"
    // '
    /*
      // coment3
    */
    /* // coment3 */
    // comment 4 /* comment 5 */
  `)
  expect(clean).toMatch("const a = '\0\0\0\0'")
  expect(clean).toMatch('const b = "\0\0\0\0"')
  expect(clean).toMatch('const c = "\0\0\0\0"')
})

In this test, the entire const c assignment is clobbered over with null characters due to the single quotes surrounding it in the RegExp and in the comment. In cases where the "matching" single quote occurs in the middle of a statement, major parsing errors occur and the whole build can fail.

Reproduction

I'm having trouble isolating this in vite.new, but it's fairly straightforward to reproduce in the unit test file. I can work on providing a repo if it's absolutely necessary.

System Info

System:
    OS: macOS 11.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 55.61 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 3.2.0 - /usr/local/bin/yarn
    npm: 8.5.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 100.0.4896.127
    Edge: 100.0.1185.44
    Firefox: 99.0.1
    Safari: 15.0

Used Package Manager

pnpm

Logs

● strings

    expect(received).toMatch(expected)

    Expected substring: "const c = \"\""
    Received string:    "················
        const a = ''··················
        const b = \"\"
        const regexp = /''·····················································································
      "

      35 |   expect(clean).toMatch("const a = '\0\0\0\0'")
      36 |   expect(clean).toMatch('const b = "\0\0\0\0"')
    > 37 |   expect(clean).toMatch('const c = "\0\0\0\0"')
         |                 ^
      38 | })
      39 |
      40 | test('strings comment nested', () => {

      at Object.<anonymous> (packages/vite/src/node/__tests__/cleanString.spec.ts:37:17)

Validations

@sodatea sodatea assigned sodatea and poyoho and unassigned sodatea Apr 22, 2022
@poyoho
Copy link
Member

poyoho commented Apr 22, 2022

appear in this ?

const regexp = /'/ 
const c = "cccc" 

@poyoho poyoho added the p3-minor-bug An edge case that only affects very specific usage (priority) label Apr 22, 2022
@kherock
Copy link
Contributor Author

kherock commented Apr 22, 2022

@poyoho the single quote on the next line is important:

const regexp = /'/ 
const c = "cccc" 
// '

You can yield syntax errors if the quote is actually used in code later

const regexp = /'/ 
const c = "cccc" 
const str = "It's broken!"

emptyString transforms the above into

const regexp = /''s broken!\"

@poyoho poyoho mentioned this issue Apr 23, 2022
9 tasks
@poyoho
Copy link
Member

poyoho commented Apr 23, 2022

very thank for your report. would you had a time help me to test #7871 ?

@github-actions github-actions bot locked and limited conversation to collaborators May 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) pending triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants