Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 */`;

Expand Down
35 changes: 23 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof commonMethods> {}
interface RefactorSessionChainable extends ReturnType<typeof commonMethods> { }
}

const debug = DEBUG('shift-refactor:plugin:common');
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
},
Expand All @@ -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));
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down