Skip to content

Commit

Permalink
feat: introduce forgiving option
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 7, 2022
1 parent 8dc9d0b commit d216f0f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { tokenizer } from 'acorn'

export function stripLiteral(code: string) {
export function stripLiteral(code: string, forgiving = false) {
const FILL = ' '
let result = ''
function fulfill(index: number) {
Expand All @@ -11,16 +11,29 @@ export function stripLiteral(code: string) {
const tokens = tokenizer(code, {
ecmaVersion: 'latest',
sourceType: 'module',
allowHashBang: true,
allowAwaitOutsideFunction: true,
allowImportExportEverywhere: true,
})
const inter = tokens[Symbol.iterator]()

for (const token of tokens) {
fulfill(token.start)
if (token.type.label === 'string')
result += code[token.start] + FILL.repeat(token.end - token.start - 2) + code[token.end - 1]
else if (token.type.label === 'template')
result += FILL.repeat(token.end - token.start)
else
result += code.slice(token.start, token.end)
while (true) {
try {
const { done, value: token } = inter.next()
if (done)
break
fulfill(token.start)
if (token.type.label === 'string')
result += code[token.start] + FILL.repeat(token.end - token.start - 2) + code[token.end - 1]
else if (token.type.label === 'template')
result += FILL.repeat(token.end - token.start)
else
result += code.slice(token.start, token.end)
}
catch (e) {
if (!forgiving)
throw e
}
}

fulfill(code.length)
Expand Down
36 changes: 36 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,39 @@ test('backtick escape', () => {
this.error(\` \`)"
`)
})

test('forgiving', () => {
expect(stripLiteral(`
<script type="module">
const rawModules = import.meta.globEager('/dir/*.json', {
as: 'raw'
})
const globraw = {}
Object.keys(rawModules).forEach((key) => {
globraw[key] = JSON.parse(rawModules[key])
})
document.querySelector('.globraw').textContent = JSON.stringify(
globraw,
null,
2
)
</script>
`, true)).toMatchInlineSnapshot(`
"
<script type=\\" \\">
const rawModules = import.meta.globEager(' ', {
as: ' '
})
const globraw = {}
Object.keys(rawModules).forEach((key) => {
globraw[key] = JSON.parse(rawModules[key])
})
document.querySelector(' ').textContent = JSON.stringify(
globraw,
null,
2
)
<
"
`)
})

0 comments on commit d216f0f

Please sign in to comment.