Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: use js-tokens instead of acorn #6

Merged
merged 9 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}