diff --git a/README.md b/README.md index d9dcfd4..c0a18dc 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ $ npm install refactor-plugin-common ```js const {refactor} = require('shift-refactor'); -const commonMethods = require('refactor-plugin-common'); +const {commonMethods} = require('refactor-plugin-common'); const src = `/* js source */`; diff --git a/src/index.ts b/src/index.ts index f40efe2..2bb6063 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,22 +3,27 @@ import { BindingIdentifier, DebuggerStatement, FormalParameters, + ObjectBinding, + ArrayBinding, + BindingWithDefault, + BindingPropertyIdentifier, + AssignmentTargetIdentifier, FunctionBody, + Node, IdentifierExpression, LiteralBooleanExpression, - Node, ReturnStatement, StaticMemberAssignmentTarget, StaticMemberExpression, StaticPropertyName, } from 'shift-ast'; -import {isLiteral, RefactorSessionChainable} from 'shift-refactor'; -import {Declaration, Reference, Scope} from 'shift-scope'; -import {default as isValid} from 'shift-validator'; -import {BaseIdGenerator, MemorableIdGenerator} from './id-generator/id-generator'; +import { isLiteral, RefactorSessionChainable } from 'shift-refactor'; +import { Declaration, Reference, Scope } from 'shift-scope'; +import { default as isValid } from 'shift-validator'; +import { BaseIdGenerator, MemorableIdGenerator } from './id-generator/id-generator'; declare module 'shift-refactor' { - interface RefactorSessionChainable extends ReturnType {} + interface RefactorSessionChainable extends ReturnType { } } const debug = DEBUG('shift-refactor:plugin:common'); @@ -52,7 +57,7 @@ export function commonMethods() { node.body, new FunctionBody({ directives: [], - statements: [new DebuggerStatement(), new ReturnStatement({expression: node.body})], + statements: [new DebuggerStatement(), new ReturnStatement({ expression: node.body })], }), ); } else { @@ -153,11 +158,11 @@ export function commonMethods() { expandBoolean(this: RefactorSessionChainable) { this.session.replace( `UnaryExpression[operator="!"][operand.value=0]`, - () => new LiteralBooleanExpression({value: true}), + () => new LiteralBooleanExpression({ value: true }), ); this.session.replace( `UnaryExpression[operator="!"][operand.value=1]`, - () => new LiteralBooleanExpression({value: false}), + () => new LiteralBooleanExpression({ value: false }), ); return this.session.globalSession.conditionalCleanup(); }, @@ -179,9 +184,15 @@ function renameScope(scope: Scope, idGenerator: BaseIdGenerator, parentMap: Weak const isParam = variable.declarations.find(_ => _.type.name === 'Parameter'); let newName = `$$${nextId}`; if (isParam) { - const parent = parentMap.get(isParam.node) as FormalParameters; - const position = parent.items.indexOf(isParam.node as BindingIdentifier); - newName = `$arg${position}_${nextId}`; + type ParamBindingParent = BindingWithDefault | ArrayBinding | ObjectBinding | BindingIdentifier; + for (let parent: Node, node = isParam.node as ParamBindingParent; parentMap.has(node); node = parent) { + parent = parentMap.get(node) as FormalParameters | ParamBindingParent; + if (parent.type==="FormalParameters") { + const position = parent.items.indexOf(node); + newName = `$arg${position}_${nextId}`; + break; + } + } } variable.declarations.forEach(_ => (_.node.name = newName)); variable.references.forEach(_ => (_.node.name = newName)); diff --git a/test/index.test.ts b/test/index.test.ts index ca37ad0..fa5948e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -17,6 +17,16 @@ describe('plugin-common', () => { parse(`const arst=1; var aiai; function foie($arg0_${second}){const $$${first}=2;$$${first}++};foie();`), ); }); + it('should not throw an Error on any types of parameters', () => { + let ast=parse(`(function (w,x=1,{y},[z]) {})`); + const gen=new MemorableIdGenerator(10); + const paramNames=[gen.next().value,gen.next().value,gen.next().value,gen.next().value]; + const $script = refactor(ast, commonMethods); + $script.normalizeIdentifiers(10); + expect($script.raw()).to.deep.equal( + parse(`(function ($arg0_${paramNames[0]},$arg1_${paramNames[1]}=1,{$arg2_${paramNames[2]}},[$arg3_${paramNames[3]}]) {})`), + ); + }); it('should not change global vars', () => { let ast = parse(`(function(){const zzzz=1; console.log(zzzz)})`); const gen = new MemorableIdGenerator(10);