initial commit
This commit is contained in:
21
node_modules/strip-literal/LICENSE
generated
vendored
Normal file
21
node_modules/strip-literal/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Anthony Fu <https://github.com/antfu>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
62
node_modules/strip-literal/README.md
generated
vendored
Normal file
62
node_modules/strip-literal/README.md
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
# strip-literal
|
||||
|
||||
[](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.
|
||||
|
||||
## Usage
|
||||
|
||||
<!-- eslint-disable no-template-curly-in-string -->
|
||||
|
||||
```ts
|
||||
import { stripLiteral } from 'strip-literal'
|
||||
|
||||
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">
|
||||
<a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
|
||||
<img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
|
||||
</a>
|
||||
</p>
|
||||
|
||||
## License
|
||||
|
||||
[MIT](./LICENSE) License © 2022 [Anthony Fu](https://github.com/antfu)
|
||||
115
node_modules/strip-literal/dist/index.cjs
generated
vendored
Normal file
115
node_modules/strip-literal/dist/index.cjs
generated
vendored
Normal file
@@ -0,0 +1,115 @@
|
||||
'use strict';
|
||||
|
||||
const acorn = require('acorn');
|
||||
|
||||
function stripLiteralAcorn(code) {
|
||||
const FILL = " ";
|
||||
let result = "";
|
||||
function fulfill(index) {
|
||||
if (index > result.length)
|
||||
result += code.slice(result.length, index).replace(/[^\n]/g, FILL);
|
||||
}
|
||||
const tokens = acorn.tokenizer(code, {
|
||||
ecmaVersion: "latest",
|
||||
sourceType: "module",
|
||||
allowHashBang: true,
|
||||
allowAwaitOutsideFunction: true,
|
||||
allowImportExportEverywhere: true
|
||||
});
|
||||
const inter = tokens[Symbol.iterator]();
|
||||
while (true) {
|
||||
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);
|
||||
}
|
||||
fulfill(code.length);
|
||||
return result;
|
||||
}
|
||||
function createIsLiteralPositionAcorn(code) {
|
||||
const positionList = [];
|
||||
const tokens = acorn.tokenizer(code, {
|
||||
ecmaVersion: "latest",
|
||||
sourceType: "module",
|
||||
allowHashBang: true,
|
||||
allowAwaitOutsideFunction: true,
|
||||
allowImportExportEverywhere: true,
|
||||
onComment(_isBlock, _text, start, end) {
|
||||
positionList.push(start);
|
||||
positionList.push(end);
|
||||
}
|
||||
});
|
||||
const inter = tokens[Symbol.iterator]();
|
||||
while (true) {
|
||||
const { done, value: token } = inter.next();
|
||||
if (done)
|
||||
break;
|
||||
if (token.type.label === "string") {
|
||||
positionList.push(token.start + 1);
|
||||
positionList.push(token.end - 1);
|
||||
} else if (token.type.label === "template") {
|
||||
positionList.push(token.start);
|
||||
positionList.push(token.end);
|
||||
}
|
||||
}
|
||||
return (position) => {
|
||||
const i = binarySearch(positionList, (v) => position < v);
|
||||
return (i - 1) % 2 === 0;
|
||||
};
|
||||
}
|
||||
function binarySearch(array, pred) {
|
||||
let low = -1;
|
||||
let high = array.length;
|
||||
while (1 + low < high) {
|
||||
const mid = low + (high - low >> 1);
|
||||
if (pred(array[mid]))
|
||||
high = mid;
|
||||
else
|
||||
low = mid;
|
||||
}
|
||||
return high;
|
||||
}
|
||||
|
||||
const multilineCommentsRE = /\/\*.*?\*\//gms;
|
||||
const singlelineCommentsRE = /(?:^|\n|\r)\s*\/\/.*(?:\r|\n|$)/gm;
|
||||
const templateLiteralRE = /\$\{(\s*(?:(?!\$\{).|\n|\r)*?\s*)\}/g;
|
||||
const quotesRE = [
|
||||
/(["'`])((?:\\\1|(?!\1)|.|\r)*?)\1/gm,
|
||||
/([`])((?:\\\1|(?!\1)|.|\n|\r)*?)\1/gm
|
||||
];
|
||||
function stripLiteralRegex(code) {
|
||||
code = code.replace(multilineCommentsRE, (s) => " ".repeat(s.length)).replace(singlelineCommentsRE, (s) => " ".repeat(s.length));
|
||||
let expanded = code;
|
||||
for (let i = 0; i < 16; i++) {
|
||||
const before = expanded;
|
||||
expanded = expanded.replace(templateLiteralRE, "` $1`");
|
||||
if (expanded === before)
|
||||
break;
|
||||
}
|
||||
quotesRE.forEach((re) => {
|
||||
expanded = expanded.replace(re, (s, quote, body, index) => {
|
||||
code = code.slice(0, index + 1) + " ".repeat(s.length - 2) + code.slice(index + s.length - 1);
|
||||
return quote + " ".repeat(s.length - 2) + quote;
|
||||
});
|
||||
});
|
||||
return code;
|
||||
}
|
||||
|
||||
function stripLiteral(code) {
|
||||
try {
|
||||
return stripLiteralAcorn(code);
|
||||
} catch (e) {
|
||||
return stripLiteralRegex(code);
|
||||
}
|
||||
}
|
||||
|
||||
exports.createIsLiteralPositionAcorn = createIsLiteralPositionAcorn;
|
||||
exports.stripLiteral = stripLiteral;
|
||||
exports.stripLiteralAcorn = stripLiteralAcorn;
|
||||
exports.stripLiteralRegex = stripLiteralRegex;
|
||||
30
node_modules/strip-literal/dist/index.d.ts
generated
vendored
Normal file
30
node_modules/strip-literal/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Strip literal using Acorn's tokenizer.
|
||||
*
|
||||
* Will throw error if the input is not valid JavaScript.
|
||||
*/
|
||||
declare function stripLiteralAcorn(code: string): string;
|
||||
/**
|
||||
* Returns a function that returns whether the position is
|
||||
* in a literal using Acorn's tokenizer.
|
||||
*
|
||||
* Will throw error if the input is not valid JavaScript.
|
||||
*/
|
||||
declare function createIsLiteralPositionAcorn(code: string): (position: number) => boolean;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
declare function stripLiteralRegex(code: string): string;
|
||||
|
||||
/**
|
||||
* Strip literal from code.
|
||||
*
|
||||
* Using Acorn's tokenizer first, and fallback to Regex if Acorn fails.
|
||||
*/
|
||||
declare function stripLiteral(code: string): string;
|
||||
|
||||
export { createIsLiteralPositionAcorn, stripLiteral, stripLiteralAcorn, stripLiteralRegex };
|
||||
110
node_modules/strip-literal/dist/index.mjs
generated
vendored
Normal file
110
node_modules/strip-literal/dist/index.mjs
generated
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
import { tokenizer } from 'acorn';
|
||||
|
||||
function stripLiteralAcorn(code) {
|
||||
const FILL = " ";
|
||||
let result = "";
|
||||
function fulfill(index) {
|
||||
if (index > result.length)
|
||||
result += code.slice(result.length, index).replace(/[^\n]/g, FILL);
|
||||
}
|
||||
const tokens = tokenizer(code, {
|
||||
ecmaVersion: "latest",
|
||||
sourceType: "module",
|
||||
allowHashBang: true,
|
||||
allowAwaitOutsideFunction: true,
|
||||
allowImportExportEverywhere: true
|
||||
});
|
||||
const inter = tokens[Symbol.iterator]();
|
||||
while (true) {
|
||||
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);
|
||||
}
|
||||
fulfill(code.length);
|
||||
return result;
|
||||
}
|
||||
function createIsLiteralPositionAcorn(code) {
|
||||
const positionList = [];
|
||||
const tokens = tokenizer(code, {
|
||||
ecmaVersion: "latest",
|
||||
sourceType: "module",
|
||||
allowHashBang: true,
|
||||
allowAwaitOutsideFunction: true,
|
||||
allowImportExportEverywhere: true,
|
||||
onComment(_isBlock, _text, start, end) {
|
||||
positionList.push(start);
|
||||
positionList.push(end);
|
||||
}
|
||||
});
|
||||
const inter = tokens[Symbol.iterator]();
|
||||
while (true) {
|
||||
const { done, value: token } = inter.next();
|
||||
if (done)
|
||||
break;
|
||||
if (token.type.label === "string") {
|
||||
positionList.push(token.start + 1);
|
||||
positionList.push(token.end - 1);
|
||||
} else if (token.type.label === "template") {
|
||||
positionList.push(token.start);
|
||||
positionList.push(token.end);
|
||||
}
|
||||
}
|
||||
return (position) => {
|
||||
const i = binarySearch(positionList, (v) => position < v);
|
||||
return (i - 1) % 2 === 0;
|
||||
};
|
||||
}
|
||||
function binarySearch(array, pred) {
|
||||
let low = -1;
|
||||
let high = array.length;
|
||||
while (1 + low < high) {
|
||||
const mid = low + (high - low >> 1);
|
||||
if (pred(array[mid]))
|
||||
high = mid;
|
||||
else
|
||||
low = mid;
|
||||
}
|
||||
return high;
|
||||
}
|
||||
|
||||
const multilineCommentsRE = /\/\*.*?\*\//gms;
|
||||
const singlelineCommentsRE = /(?:^|\n|\r)\s*\/\/.*(?:\r|\n|$)/gm;
|
||||
const templateLiteralRE = /\$\{(\s*(?:(?!\$\{).|\n|\r)*?\s*)\}/g;
|
||||
const quotesRE = [
|
||||
/(["'`])((?:\\\1|(?!\1)|.|\r)*?)\1/gm,
|
||||
/([`])((?:\\\1|(?!\1)|.|\n|\r)*?)\1/gm
|
||||
];
|
||||
function stripLiteralRegex(code) {
|
||||
code = code.replace(multilineCommentsRE, (s) => " ".repeat(s.length)).replace(singlelineCommentsRE, (s) => " ".repeat(s.length));
|
||||
let expanded = code;
|
||||
for (let i = 0; i < 16; i++) {
|
||||
const before = expanded;
|
||||
expanded = expanded.replace(templateLiteralRE, "` $1`");
|
||||
if (expanded === before)
|
||||
break;
|
||||
}
|
||||
quotesRE.forEach((re) => {
|
||||
expanded = expanded.replace(re, (s, quote, body, index) => {
|
||||
code = code.slice(0, index + 1) + " ".repeat(s.length - 2) + code.slice(index + s.length - 1);
|
||||
return quote + " ".repeat(s.length - 2) + quote;
|
||||
});
|
||||
});
|
||||
return code;
|
||||
}
|
||||
|
||||
function stripLiteral(code) {
|
||||
try {
|
||||
return stripLiteralAcorn(code);
|
||||
} catch (e) {
|
||||
return stripLiteralRegex(code);
|
||||
}
|
||||
}
|
||||
|
||||
export { createIsLiteralPositionAcorn, stripLiteral, stripLiteralAcorn, stripLiteralRegex };
|
||||
62
node_modules/strip-literal/package.json
generated
vendored
Normal file
62
node_modules/strip-literal/package.json
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
{
|
||||
"name": "strip-literal",
|
||||
"version": "1.0.0",
|
||||
"packageManager": "pnpm@7.0.0",
|
||||
"description": "Strip comments and string literals from JavaScript code",
|
||||
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
||||
"license": "MIT",
|
||||
"funding": "https://github.com/sponsors/antfu",
|
||||
"homepage": "https://github.com/antfu/strip-literal#readme",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/antfu/strip-literal.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/antfu/strip-literal/issues"
|
||||
},
|
||||
"keywords": [],
|
||||
"sideEffects": false,
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"require": "./dist/index.cjs",
|
||||
"import": "./dist/index.mjs"
|
||||
}
|
||||
},
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"dev": "unbuild --stub",
|
||||
"lint": "eslint .",
|
||||
"prepublishOnly": "nr build",
|
||||
"release": "bumpp --commit --push --tag && npm publish",
|
||||
"start": "esmo src/index.ts",
|
||||
"test": "vitest",
|
||||
"bench": "vitest bench",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"acorn": "^8.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@antfu/eslint-config": "^0.30.1",
|
||||
"@antfu/ni": "^0.18.5",
|
||||
"@types/node": "^18.11.9",
|
||||
"bumpp": "^8.2.1",
|
||||
"eslint": "^8.27.0",
|
||||
"esmo": "^0.16.3",
|
||||
"pnpm": "^7.16.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"three": "^0.146.0",
|
||||
"typescript": "^4.8.4",
|
||||
"unbuild": "^0.9.4",
|
||||
"vite": "^3.2.3",
|
||||
"vitest": "^0.25.2",
|
||||
"vue": "^3.2.45"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user