Skip to content

Commit

Permalink
fix: escaped
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Apr 23, 2022
1 parent 2ec1ff1 commit 5f4ab17
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
27 changes: 5 additions & 22 deletions packages/vite/src/node/__tests__/cleanString.spec.ts
@@ -1,3 +1,4 @@
import { assetAttrsConfig } from './../plugins/html'
import { emptyString } from '../../node/cleanString'

test('comments', () => {
Expand Down Expand Up @@ -55,32 +56,14 @@ test('regexp', () => {
const clean = emptyString(`
/'1\\'1'/
'1\\'1'
/"1\\"1"/
"1\\"1"
/"1\\"1\\"1"/
"1\\"1\\"1"
/"1\\'1'\\"1"/
"1\\'1'\\"1"
/"1'1'"/
"1'1'"
/"1'\\'1\\''\\"1\\"\\""/
"1'\\'1\\''\\"1\\"\\""
/'1"\\"1\\""\\"1\\"\\"'/
'1"\\"1\\""\\"1\\"\\"'
/'""1""'/
'""1""'
/'"""1"""'/
'"""1"""'
/'""""1""""'/
'""""1""""'
/"''1''"/
"''1''"
/"'''1'''"/
"'''1'''"
/"''''1''''"/
"''''1''''"
/'1'/ // '1'
/'1'/ /* '1' */
/'1'/ // '1'
/'1'/ /* '1' */
/'\\/1'/ // '1'
/'\\/1'/ /* '1' */
`)
expect(clean).not.toMatch('1')
})
Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/node/cleanString.ts
Expand Up @@ -2,8 +2,10 @@ import type { RollupError } from 'rollup'
// bank on the non-overlapping nature of regex matches and combine all filters into one giant regex
// /`([^`\$\{\}]|\$\{(`|\g<1>)*\})*`/g can match nested string template
// but js not support match expression(\g<0>). so clean string template(`...`) in other ways.
const cleanerRE =
/"([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*'|\/\*(.|[\r\n])*?\*\/|\/\/.*|\/([^\/]|(?<=\/)\/)*\//g
const stringsRE = /"([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*'/
const commentsRE = /\/\*(.|[\r\n])*?\*\/|\/\/.*/
const regexpRE = /\/([^\/\r\n]|(?<=\\)\/)*\//
const cleanerRE = new RegExp(`${stringsRE}|${commentsRE}|${regexpRE}`, 'g')

const blankReplacer = (s: string) => ' '.repeat(s.length)
const stringBlankReplacer = (s: string) =>
Expand Down

0 comments on commit 5f4ab17

Please sign in to comment.