Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 29, 2023
1 parent 6c5377b commit 7a88d41
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 36 deletions.
19 changes: 9 additions & 10 deletions src/index.ts
@@ -1,4 +1,3 @@
import { _stripLiteralAcorn } from './acorn'
import { _stripLiteralJsTokens } from './js-tokens'
import { stripLiteralRegex } from './regex'
import type { StripLiteralOptions } from './types'
Expand All @@ -22,24 +21,24 @@ export function stripLiteral(code: string, options?: StripLiteralOptions) {
* Using Acorn's tokenizer first, and fallback to Regex if Acorn fails.
*/
export function stripLiteralDetailed(code: string, options?: StripLiteralOptions): {
mode: 'acorn' | 'regex'
mode: 'js-tokens' | 'regex'
result: string
acorn: {
resultJsTokens: {
tokens: any[]
error?: any
}
} {
const acorn = _stripLiteralJsTokens(code, options)
if (!acorn.error) {
const resultJsTokens = _stripLiteralJsTokens(code, options)
if (!resultJsTokens.error) {
return {
mode: 'acorn',
result: acorn.result,
acorn,
mode: 'js-tokens',
result: resultJsTokens.result,
resultJsTokens,
}
}
return {
mode: 'regex',
result: stripLiteralRegex(acorn.result + code.slice(acorn.result.length), options),
acorn,
result: stripLiteralRegex(resultJsTokens.result + code.slice(resultJsTokens.result.length), options),
resultJsTokens,
}
}
20 changes: 10 additions & 10 deletions test/__snapshots__/index.test.ts.snap
@@ -1,7 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`escape character 1`] = `
"// mode: acorn
"// mode: js-tokens
' '
" "
" "
Expand All @@ -18,7 +18,7 @@ exports[`escape character 1`] = `
`;

exports[`regexp affect 1`] = `
"// mode: acorn
"// mode: js-tokens
[
/ /,
' ',
Expand All @@ -28,55 +28,55 @@ exports[`regexp affect 1`] = `
`;

exports[`strings comment nested 1`] = `
"// mode: acorn
"// mode: js-tokens
const a = " "
"
`;

exports[`strings comment nested 2`] = `
"// mode: acorn
"// mode: js-tokens
const a = " "
"
`;

exports[`strings comment nested 3`] = `
"// mode: acorn
"// mode: js-tokens
const a = " "
"
`;

exports[`strings comment nested 4`] = `
"// mode: acorn
"// mode: js-tokens
const a = " "
console.log(" ")"
`;

exports[`strings comment nested 5`] = `
"// mode: acorn
"// mode: js-tokens
const a = " "
console.log(" ")
const b = " ""
`;

exports[`strings comment nested 6`] = `
"// mode: acorn
"// mode: js-tokens
const a = " "
console.log(" ")
const b = " ""
`;

exports[`strings comment nested 7`] = `
"// mode: acorn
"// mode: js-tokens
const a = " "
console.log(" ")
const b = " ""
`;

exports[`works 1`] = `
"// mode: acorn
"// mode: js-tokens
const a = ' '
Expand Down
2 changes: 1 addition & 1 deletion test/custom-fill.test.ts
Expand Up @@ -20,7 +20,7 @@ const c = \`aaaa\${foo}dddd\${bar}\`
)

expect(result).toMatchInlineSnapshot(`
"// mode: acorn
"// mode: js-tokens
const a = 'aaaa'
Expand Down
2 changes: 1 addition & 1 deletion test/filter.test.ts
Expand Up @@ -23,7 +23,7 @@ const c = \`aaaa\${foo}dddd\${bar}\`
)

expect(result).toMatchInlineSnapshot(`
"// mode: acorn
"// mode: js-tokens
const a = 'aaaa'
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/backtick-escape.output.js
@@ -1,4 +1,4 @@
// mode: acorn
// mode: js-tokens
this.error(` `)
this.error(` `)
this.error(` `)
2 changes: 1 addition & 1 deletion test/fixtures/backtick-in-regex.output.js
@@ -1,4 +1,4 @@
// mode: acorn
// mode: js-tokens

var r = / /;
foobar(`${foo({ class: " " })}`);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/comment-in-string.output.js
@@ -1,4 +1,4 @@
// mode: acorn
// mode: js-tokens
const url= ` `;
const url1= ' ';
onMounted(() => console.log(123))
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/forgiving.output.vue
@@ -1,4 +1,4 @@
// mode: acorn
// mode: js-tokens
<script type=" ">
const rawModules = import.meta.globEager(' ', {
as: ' '
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/issue4.output.js
@@ -1,4 +1,4 @@
// mode: acorn
// mode: js-tokens
import __variableDynamicImportRuntimeHelper from " ";
const getTestData = async () => {
const filename = " ";
Expand Down
18 changes: 9 additions & 9 deletions test/index.test.ts
Expand Up @@ -96,51 +96,51 @@ it('acorn syntax error', () => {
foo(\`fooo \${foo({ class: "foo" })} bar\`)
`, false))
.toMatchInlineSnapshot(`
"// mode: acorn
"// mode: js-tokens
foo(\` \${foo({ class: " " })} \`)"
`)
})

it('template string nested', () => {
let str = '`aaaa`'
expect(executeWithVerify(str)).toMatchInlineSnapshot(`
"// mode: acorn
"// mode: js-tokens
\` \`"
`)

str = '`aaaa` `aaaa`'
expect(executeWithVerify(str)).toMatchInlineSnapshot(`
"// mode: acorn
"// mode: js-tokens
\` \` \` \`"
`)

str = '`aa${a}aa`'
expect(executeWithVerify(str)).toMatchInlineSnapshot(`
"// mode: acorn
"// mode: js-tokens
\` \${a} \`"
`)

str = '`aa${a + `a` + a}aa`'
expect(executeWithVerify(str)).toMatchInlineSnapshot(`
"// mode: acorn
"// mode: js-tokens
\` \${a + \` \` + a} \`"
`)

str = '`aa${a + `a` + a}aa` `aa${a + `a` + a}aa`'
expect(executeWithVerify(str)).toMatchInlineSnapshot(`
"// mode: acorn
"// mode: js-tokens
\` \${a + \` \` + a} \` \` \${a + \` \` + a} \`"
`)

str = '`aa${a + `aaaa${c + (a = {b: 1}) + d}` + a}aa`'
expect(executeWithVerify(str)).toMatchInlineSnapshot(`
"// mode: acorn
"// mode: js-tokens
\` \${a + \` \${c + (a = {b: 1}) + d}\` + a} \`"
`)

str = '`aa${a + `aaaa${c + (a = {b: 1}) + d}` + a}aa` `aa${a + `aaaa${c + (a = {b: 1}) + d}` + a}aa`'
expect(executeWithVerify(str)).toMatchInlineSnapshot(`
"// mode: acorn
"// mode: js-tokens
\` \${a + \` \${c + (a = {b: 1}) + d}\` + a} \` \` \${a + \` \${c + (a = {b: 1}) + d}\` + a} \`"
`)
})
Expand All @@ -152,7 +152,7 @@ it('backtick escape', () => {
'this.error(`\\``)',
].join('\n')
expect(executeWithVerify(str)).toMatchInlineSnapshot(`
"// mode: acorn
"// mode: js-tokens
this.error(\` \`)
this.error(\` \`)
this.error(\` \`)"
Expand Down

0 comments on commit 7a88d41

Please sign in to comment.