Skip to content

Commit

Permalink
fix(un-default-parameter): handle parameters with gap (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Mar 26, 2024
1 parent 95d3d00 commit 1458630
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
1 change: 0 additions & 1 deletion packages/ast-utils/src/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export function isValidIdentifier(
export function generateName(input: string, scope: Scope | null = null, existedNames: string[] = []): string {
const cleanName = input
.replace(/^@/, '')
.replace(/^_+/, '')
.replace(/_+$/, '')
.replace(/_+/, '_')
.replace(/^\.+/g, '')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ function test(x = 1, y = 2, b, z = "hello", e = world(), f = true) {
`,
)

inlineTest('default parameters - With gap',
`
function test(a) {
var b = arguments.length > 1 ? arguments[1] : undefined;
var e = arguments.length > 4 && undefined !== arguments[4]
? arguments[4]
: world();
var z = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : "hello";
var _param_3 = 1;
}
`,
`
function test(a, b, _param_2, _param_3_1, e = world(), _param_5, z = "hello") {
var _param_3 = 1;
}
`,
)

inlineTest('default parameters - ArrowFunctionExpression',
`
const test = (a, b) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const transformAST: ASTTransformation = (context, params) => {
left = j.variableDeclaration('var', [j.variableDeclarator(_result)])
}
else {
const tempVariableName = generateName('_value', scope)
const tempVariableName = generateName('value', scope)
left = j.identifier(tempVariableName)
const assignment = j.assignmentExpression('=', left, _result)
body.unshift(j.expressionStatement(assignment))
Expand Down
5 changes: 4 additions & 1 deletion packages/unminify/src/transformations/smart-rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,12 @@ function handlePropertyRename(j: JSCodeshift, objectPattern: ObjectPattern, scop
let newName = key.name

// if the newName is not a valid identifier, _{newName} is used instead
if (!isValidIdentifier(newName) || isDeclared(scope, newName)) {
if (!isValidIdentifier(newName)) {
newName = generateName(`_${newName}`, scope)
}
else if (isDeclared(scope, newName)) {
newName = generateName(newName, scope)
}

renameIdentifier(j, scope, value.name, newName)
property.shorthand = newName === value.name
Expand Down
31 changes: 31 additions & 0 deletions packages/unminify/src/transformations/un-default-parameter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { assertScopeExists } from '@wakaru/ast-utils/assert'
import { generateName } from '@wakaru/ast-utils/identifier'
import { isLogicalNot, isUndefined } from '@wakaru/ast-utils/matchers'
import { isVariableIdentifier } from '@wakaru/ast-utils/reference'
import { createJSCodeshiftTransformationRule } from '@wakaru/shared/rule'
Expand Down Expand Up @@ -93,6 +95,9 @@ function handleBody(j: JSCodeshift, path: ASTPath<FunctionDeclaration | Function
const body = (path.node.body as BlockStatement).body
if (body.length === 0) return

const scope = path.scope
assertScopeExists(scope)

const params = path.node.params

const bodyInThreshold = body.slice(0, BODY_LENGTH_THRESHOLD)
Expand Down Expand Up @@ -206,6 +211,19 @@ function handleBody(j: JSCodeshift, path: ASTPath<FunctionDeclaration | Function
return false
}

// insert placeholder parameter when there is a gap
if (normalMatch.index >= params.length) {
for (let i = params.length; i < normalMatch.index; i++) {
if (params[i]) continue

const scope = path.get('body', 'body', index).scope
assertScopeExists(scope)

const parameterName = generateName(`_param_${i}`, scope)
params.push(j.identifier(parameterName))
}
}

params.splice(normalMatch.index, 0, identifier)
return false
}
Expand All @@ -220,6 +238,19 @@ function handleBody(j: JSCodeshift, path: ASTPath<FunctionDeclaration | Function
return false
}

// insert placeholder parameter when there is a gap
if (defaultMatch.index >= params.length) {
for (let i = params.length; i < defaultMatch.index; i++) {
if (params[i]) continue

const scope = path.get('body', 'body', index).scope
assertScopeExists(scope)

const parameterName = generateName(`_param_${i}`, scope)
params.push(j.identifier(parameterName))
}
}

params.splice(defaultMatch.index, 0, j.assignmentPattern(identifier, defaultParam))
return false
}
Expand Down

0 comments on commit 1458630

Please sign in to comment.