-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: nested comments and strings, new regexp utils (#7650)
- Loading branch information
Showing
6 changed files
with
151 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { emptyString } from '../../node/cleanString' | ||
|
||
test('comments', () => { | ||
expect( | ||
emptyString(` | ||
// comment1 // comment | ||
// comment1 | ||
/* coment2 */ | ||
/* | ||
// coment3 | ||
*/ | ||
/* // coment3 */ | ||
/* // coment3 */ // comment | ||
// comment 4 /* comment 5 */ | ||
`).trim() | ||
).toBe('') | ||
}) | ||
|
||
test('strings', () => { | ||
const clean = emptyString(` | ||
// comment1 | ||
const a = 'aaaa' | ||
/* coment2 */ | ||
const b = "bbbb" | ||
/* | ||
// coment3 | ||
*/ | ||
/* // coment3 */ | ||
// comment 4 /* comment 5 */ | ||
`) | ||
expect(clean).toMatch("const a = '\0\0\0\0'") | ||
expect(clean).toMatch('const b = "\0\0\0\0"') | ||
}) | ||
|
||
test('strings comment nested', () => { | ||
expect( | ||
emptyString(` | ||
// comment 1 /* " */ | ||
const a = "a //" | ||
// comment 2 /* " */ | ||
`) | ||
).toMatch('const a = "\0\0\0\0"') | ||
|
||
expect( | ||
emptyString(` | ||
// comment 1 /* ' */ | ||
const a = "a //" | ||
// comment 2 /* ' */ | ||
`) | ||
).toMatch('const a = "\0\0\0\0"') | ||
|
||
expect( | ||
emptyString(` | ||
// comment 1 /* \` */ | ||
const a = "a //" | ||
// comment 2 /* \` */ | ||
`) | ||
).toMatch('const a = "\0\0\0\0"') | ||
|
||
expect( | ||
emptyString(` | ||
const a = "a //" | ||
console.log("console") | ||
`) | ||
).toMatch('const a = "\0\0\0\0"') | ||
|
||
expect( | ||
emptyString(` | ||
const a = "a /*" | ||
console.log("console") | ||
const b = "b */" | ||
`) | ||
).toMatch('const a = "\0\0\0\0"') | ||
|
||
expect( | ||
emptyString(` | ||
const a = "a ' " | ||
console.log("console") | ||
const b = "b ' " | ||
`) | ||
).toMatch('const a = "\0\0\0\0"') | ||
|
||
expect( | ||
emptyString(` | ||
const a = "a \` " | ||
console.log("console") | ||
const b = "b \` " | ||
`) | ||
).toMatch('const a = "\0\0\0\0"') | ||
}) | ||
|
||
test('find empty string flag in raw index', () => { | ||
const str = ` | ||
const a = "aaaaa" | ||
const b = "bbbbb" | ||
` | ||
const clean = emptyString(str) | ||
expect(clean).toMatch('const a = "\0\0\0\0\0"') | ||
expect(clean).toMatch('const b = "\0\0\0\0\0"') | ||
|
||
const aIndex = str.indexOf('const a = "aaaaa"') | ||
const aStart = clean.indexOf('\0\0\0\0\0', aIndex) | ||
expect(str.slice(aStart, aStart + 5)).toMatch('aaaaa') | ||
|
||
const bIndex = str.indexOf('const b = "bbbbb"') | ||
const bStart = clean.indexOf('\0\0\0\0\0', bIndex) | ||
expect(str.slice(bStart, bStart + 5)).toMatch('bbbbb') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// 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 blankReplacer = (s: string) => ' '.repeat(s.length) | ||
const stringBlankReplacer = (s: string) => | ||
`${s[0]}${'\0'.repeat(s.length - 2)}${s[0]}` | ||
|
||
export function emptyString(raw: string): string { | ||
return raw.replace(cleanerRE, (s: string) => | ||
s[0] === '/' ? blankReplacer(s) : stringBlankReplacer(s) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters