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 3b4bca2 commit 49cbc78
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 50 deletions.
35 changes: 1 addition & 34 deletions README.md
Original file line number Diff line number Diff line change
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
17 changes: 2 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,14 @@ 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): {
result: string
resultJsTokens: {
tokens: any[]
}
} {
const resultJsTokens = stripLiteralJsTokens(code, options)
return {
result: resultJsTokens.result,
resultJsTokens,
}
export function stripLiteralDetailed(code: string, options?: StripLiteralOptions) {
return stripLiteralJsTokens(code, options)
}
3 changes: 2 additions & 1 deletion src/js-tokens.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import jsTokens, { type Token as JSToken } from 'js-tokens'
import type { Token as JSToken } from 'js-tokens'
import jsTokens from 'js-tokens'
import type { StripLiteralOptions } from './types'

export function stripLiteralJsTokens(code: string, options?: StripLiteralOptions) {
Expand Down

0 comments on commit 49cbc78

Please sign in to comment.