Skip to content

Commit

Permalink
fix: clean string regexp (#7871)
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Apr 24, 2022
1 parent fc89057 commit ecc78bc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 11 additions & 0 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 @@ -51,6 +52,16 @@ test('escape character', () => {
expect(clean).not.toMatch('1')
})

test('regexp affect', () => {
const clean = emptyString(`
/'/
'1'
/"/
"1"
`)
expect(clean).not.toMatch('1')
})

test('strings comment nested', () => {
expect(
emptyString(`
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/cleanString.ts
Expand Up @@ -2,8 +2,9 @@ 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 = /"([^"\r\n]|(?<=\\)")*"|'([^'\r\n]|(?<=\\)')*'/.source
const commentsRE = /\/\*(.|[\r\n])*?\*\/|\/\/.*/.source
const cleanerRE = new RegExp(`${stringsRE}|${commentsRE}`, 'g')

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

0 comments on commit ecc78bc

Please sign in to comment.