Skip to content

Commit

Permalink
feat!: use js-tokens instead of acorn (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
sheremet-va and antfu committed Dec 29, 2023
1 parent 0e97234 commit bad0e6f
Show file tree
Hide file tree
Showing 20 changed files with 158 additions and 425 deletions.
35 changes: 1 addition & 34 deletions README.md
Expand Up @@ -2,7 +2,7 @@

[![NPM version](https://img.shields.io/npm/v/strip-literal?color=a1b858&label=)](https://www.npmjs.com/package/strip-literal)

Strip comments and string literals from JavaScript code. Powered by [acorn](https://github.com/acornjs/acorn)'s tokenizer.
Strip comments and string literals from JavaScript code. Powered by [`js-tokens`](https://github.com/lydell/js-tokens).

## Usage

Expand All @@ -16,39 +16,6 @@ stripLiteral('const foo = `//foo ${bar}`') // 'const foo = ` ${bar}`'

Comments, string literals will be replaced by spaces with the same length to keep the source map untouched.

## Functions

### `stripLiteralAcorn`

Strip literal using [Acorn](https://github.com/acornjs/acorn)'s tokenizer.

Will throw error if the input is not valid JavaScript.

[Source](./src/acorn.ts)

### `stripLiteralRegex`

Strip literal using RegExp.

This will be faster and can work on non-JavaScript input. But will have some caveats on distinguish strings and comments.

[Source](./src/regex.ts)

### `stripLiteral`

Strip literal from code.

Try to use `stripLiteralAcorn` first, and fallback to `stripLiteralRegex` if Acorn fails.

[Source](./src/index.ts)

### `createIsLiteralPositionAcorn`
Returns a function that returns whether the position is in a literal using [Acorn](https://github.com/acornjs/acorn)'s tokenizer.

Will throw error if the input is not valid JavaScript.

[Source](./src/acorn.ts)

## Sponsors

<p align="center">
Expand Down
12 changes: 3 additions & 9 deletions bench/index.bench.ts
@@ -1,7 +1,7 @@
/* eslint-disable test/consistent-test-it */
import { readFile } from 'node:fs/promises'
import { bench, describe } from 'vitest'
import { createIsLiteralPositionAcorn, stripLiteralAcorn, stripLiteralRegex } from '../src'
import { stripLiteralJsTokens } from '../src'

const modules = {
'vue-global': './node_modules/vue/dist/vue.runtime.global.js',
Expand All @@ -11,14 +11,8 @@ const modules = {
Object.entries(modules).forEach(([name, path]) => {
describe(`bench ${name}`, async () => {
const code = await readFile(path, 'utf-8')
bench('stripLiteral (regex)', () => {
stripLiteralRegex(code)
})
bench('stripLiteral (acorn)', () => {
stripLiteralAcorn(code)
})
bench('createIsLiteralPositionAcorn (acorn)', () => {
createIsLiteralPositionAcorn(code)
bench('stripLiteral (js-tokens)', () => {
stripLiteralJsTokens(code)
})
})
})
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -41,7 +41,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"acorn": "^8.11.2"
"js-tokens": "^8.0.2"
},
"devDependencies": {
"@antfu/eslint-config": "^2.6.1",
Expand Down
11 changes: 8 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

148 changes: 0 additions & 148 deletions src/acorn.ts

This file was deleted.

33 changes: 4 additions & 29 deletions src/index.ts
@@ -1,44 +1,19 @@
import { _stripLiteralAcorn } from './acorn'
import { stripLiteralRegex } from './regex'
import { stripLiteralJsTokens } from './js-tokens'
import type { StripLiteralOptions } from './types'

export { stripLiteralAcorn, createIsLiteralPositionAcorn } from './acorn'
export { stripLiteralRegex } from './regex'
export { stripLiteralJsTokens } from './js-tokens'
export * from './types'

/**
* Strip literal from code.
*
* Using Acorn's tokenizer first, and fallback to Regex if Acorn fails.
*/
export function stripLiteral(code: string, options?: StripLiteralOptions) {
return stripLiteralDetailed(code, options).result
}

/**
* Strip literal from code, return more detailed information.
*
* Using Acorn's tokenizer first, and fallback to Regex if Acorn fails.
*/
export function stripLiteralDetailed(code: string, options?: StripLiteralOptions): {
mode: 'acorn' | 'regex'
result: string
acorn: {
tokens: any[]
error?: any
}
} {
const acorn = _stripLiteralAcorn(code, options)
if (!acorn.error) {
return {
mode: 'acorn',
result: acorn.result,
acorn,
}
}
return {
mode: 'regex',
result: stripLiteralRegex(acorn.result + code.slice(acorn.result.length), options),
acorn,
}
export function stripLiteralDetailed(code: string, options?: StripLiteralOptions) {
return stripLiteralJsTokens(code, options)
}

0 comments on commit bad0e6f

Please sign in to comment.