-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
195 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "@unminify/ast-utils", | ||
"type": "module", | ||
"version": "0.0.1", | ||
"packageManager": "[email protected]", | ||
"description": "🔪📦 Unminify and beautify bundled code", | ||
"author": "Pionxzh", | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.js" | ||
} | ||
}, | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"package.json" | ||
], | ||
"engines": { | ||
"node": ">=16.0.0" | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.ts --format cjs,esm --dts --sourcemap --clean", | ||
"test": "vitest run --globals", | ||
"test:update": "vitest run --update --globals", | ||
"test:watch": "vitest watch --globals", | ||
"test:type": "tsc --noEmit", | ||
"lint": "eslint src", | ||
"lint:fix": "eslint src --fix" | ||
}, | ||
"dependencies": { | ||
"@babel/types": "^7.20.7", | ||
"eslint": "^8.32.0", | ||
"jscodeshift": "^0.14.0" | ||
}, | ||
"devDependencies": { | ||
"@types/jscodeshift": "^0.11.6", | ||
"@types/yargs": "^17.0.20", | ||
"tsup": "^6.5.0", | ||
"typescript": "^4.9.4", | ||
"vitest": "^0.28.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { isIIFE } from './isIIFE' | ||
export { isTopLevel } from './isTopLevel' | ||
export { renameFunctionParameters } from './renameFunctionParameters' | ||
export { splitVariableDeclarators } from './splitVariableDeclarators' | ||
export { pruneComments } from './pruneComments' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { ExpressionStatement, Statement } from 'jscodeshift' | ||
|
||
export function isIIFE(node: Statement): node is ExpressionStatement { | ||
if (node.type !== 'ExpressionStatement') return false | ||
const expression = (node as ExpressionStatement).expression | ||
if (expression.type !== 'CallExpression') return false | ||
const callee = expression.callee | ||
return callee.type === 'FunctionExpression' | ||
|| callee.type === 'ArrowFunctionExpression' | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { Collection, JSCodeshift } from 'jscodeshift' | ||
|
||
export function pruneComments(j: JSCodeshift, collection: Collection): void { | ||
// @ts-expect-error - Comment type is wrong | ||
collection.find(j.Comment).forEach(path => path.prune()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { ArrowFunctionExpression, FunctionExpression, JSCodeshift } from 'jscodeshift' | ||
|
||
export function renameFunctionParameters(j: JSCodeshift, node: FunctionExpression | ArrowFunctionExpression, parameters: string[]): void { | ||
node.params.forEach((param, index) => { | ||
if (param.type === 'Identifier') { | ||
const oldName = param.name | ||
const newName = parameters[index] | ||
|
||
// Only get the immediate function scope | ||
const functionScope = j(node).closestScope().get() | ||
|
||
// Check if the name is in the current scope and rename it | ||
if (functionScope.scope.getBindings()[oldName]) { | ||
j(functionScope) | ||
.find(j.Identifier, { name: oldName }) | ||
.forEach((path) => { | ||
// Exclude MemberExpression properties | ||
if (!(path.parent.node.type === 'MemberExpression' && path.parent.node.property === path.node) | ||
&& path.scope.node === functionScope.node) { | ||
path.node.name = newName | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { Collection, JSCodeshift } from 'jscodeshift' | ||
|
||
/** | ||
* ```js | ||
* var a = 1, b = true, c = func(d) | ||
* -> | ||
* var a = 1 | ||
* var b = true | ||
* var c = func(d) | ||
* ``` | ||
*/ | ||
export function splitVariableDeclarators(j: JSCodeshift, collection: Collection) { | ||
collection | ||
.find(j.VariableDeclaration, { | ||
declarations: [ | ||
{ | ||
type: 'VariableDeclarator', | ||
id: { type: 'Identifier' }, | ||
}, | ||
], | ||
}) | ||
.filter((path) => { | ||
if (path.parent?.node.type === 'ForStatement') return false | ||
return path.node.declarations.length > 1 | ||
}) | ||
.forEach((p) => { | ||
const { kind, declarations } = p.node | ||
j(p).replaceWith(declarations.map(d => j.variableDeclaration(kind, [d]))) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"lib": ["ESNext"], | ||
"moduleResolution": "Node", | ||
"strict": true, | ||
"outDir": "dist", | ||
"sourceMap": true, | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"esModuleInterop": true, | ||
"noEmit": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noImplicitReturns": true, | ||
"skipLibCheck": true, | ||
"useDefineForClassFields": true | ||
}, | ||
"include": [ | ||
"src" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"dist" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "unminify", | ||
"name": "@unminify/kit", | ||
"type": "module", | ||
"version": "0.0.1", | ||
"packageManager": "[email protected]", | ||
|
@@ -38,6 +38,7 @@ | |
"@babel/core": "^7.20.12", | ||
"@babel/preset-env": "^7.20.2", | ||
"@babel/types": "^7.20.7", | ||
"@unminify/ast-utils": "workspace:*", | ||
"eslint": "^8.32.0", | ||
"fs-extra": "^11.1.0", | ||
"globby": "^11.1.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters