Skip to content

Commit

Permalink
add GlobalState manager and separated it from RefactorSession
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoverson committed Jul 7, 2020
1 parent 2263e70 commit b2c8d2c
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 264 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RefactorSessionChainable } from './refactor-session-chainable';
import pluginUnsafe from './refactor-plugin-unsafe';
import pluginCommon from './refactor-plugin-common';

export { RefactorSession } from './refactor-session';
export { RefactorSession, GlobalState as GlobalSession } from './refactor-session';
export { RefactorSessionChainable } from './refactor-session-chainable'

// export * as Shift from 'shift-ast';
Expand Down
14 changes: 7 additions & 7 deletions src/refactor-plugin-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default function pluginCommon() {
},

unshorten(this: RefactorSessionChainable) {
const lookupTable = this.session.getLookupTable();
const lookupTable = this.session.globalSession.getLookupTable();

this.nodes.forEach((node: Node) => {
if (node.type !== 'VariableDeclarator') {
Expand All @@ -143,9 +143,9 @@ export default function pluginCommon() {
const lookup = lookupTable.variableMap.get(from);
lookup[0].declarations.forEach((decl: Declaration) => (decl.node.name = to.name));
lookup[0].references.forEach((ref: Reference) => (ref.node.name = to.name));
this.session._queueDeletion(node);
this.session.globalSession._queueDeletion(node);
});
return this.session.conditionalCleanup();
return this.session.globalSession.conditionalCleanup();
},

expandBoolean(this: RefactorSessionChainable) {
Expand All @@ -157,14 +157,14 @@ export default function pluginCommon() {
`UnaryExpression[operator="!"][operand.value=1]`,
() => new LiteralBooleanExpression({ value: false }),
);
return this.session.conditionalCleanup();
return this.session.globalSession.conditionalCleanup();
},

normalizeIdentifiers(this: RefactorSessionChainable, seed = 1) {
const lookupTable = this.session.getLookupTable();
const lookupTable = this.session.globalSession.getLookupTable();
const idGenerator = new MemorableIdGenerator(seed);
renameScope(lookupTable.scope, idGenerator, this.session.parentMap);
return this.session.conditionalCleanup();
renameScope(lookupTable.scope, idGenerator, this.session.globalSession.parentMap);
return this.session.globalSession.conditionalCleanup();
}

}
Expand Down
12 changes: 6 additions & 6 deletions src/refactor-plugin-unsafe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export default function pluginUnsafe() {

massRename(this: RefactorSessionChainable, namePairs: string[][]) {
namePairs.forEach(([from, to]) => {
this.session.lookupVariableByName(from).forEach((lookup: Variable) => this.session.renameInPlace(lookup, to));
this.lookupVariableByName(from).forEach((lookup: Variable) => this.session.renameInPlace(lookup, to));
});
},

inlineLiterals(this: RefactorSessionChainable) {
for (const variable of this.session.variables.values()) {
for (const variable of this.session.globalSession.variables.values()) {
// Haven't thought about how to deal with this yet. Might be easy. PR welcome.
if (variable.declarations.length !== 1) continue;
const declaration = variable.declarations[0];
Expand All @@ -43,7 +43,7 @@ export default function pluginUnsafe() {
}
}
}
this.session.conditionalCleanup();
this.session.globalSession.conditionalCleanup();
},

removeDeadVariables(this: RefactorSessionChainable) {
Expand All @@ -55,8 +55,8 @@ export default function pluginUnsafe() {
// TODO handle this at some point.
if (nameNode.type === 'ArrayBinding' || nameNode.type === 'ObjectBinding') return;

const lookup = this.session.lookupVariable(nameNode);
const scope = this.session.lookupScope(lookup) as Scope;
const lookup = this.session.globalSession.lookupVariable(nameNode);
const scope = this.session.globalSession.lookupScope(lookup) as Scope;

if (scope.type === ScopeType.GLOBAL) return;

Expand Down Expand Up @@ -103,7 +103,7 @@ export default function pluginUnsafe() {
this.session.delete(decl);
}
});
return this.session.conditionalCleanup();
return this.session.globalSession.conditionalCleanup();
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/refactor-session-chainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Node } from 'shift-ast';
import { Declaration, Reference, Variable } from 'shift-scope';
import pluginCommon from './refactor-plugin-common';
import pluginUnsafe from './refactor-plugin-unsafe';
import { RefactorSession } from './refactor-session';
import { RefactorSession, GlobalState } from './refactor-session';
import { Replacer, SelectorOrNode, SimpleIdentifier, SimpleIdentifierOwner } from './types';

// type ApiExtension = { [key: string]: any };
Expand Down Expand Up @@ -243,12 +243,12 @@ export class RefactorSessionChainable {
return this.$(this.session.findOne(selectorOrNode));
}

references(): Reference[] {
return this.session.findReferences(this.first() as SimpleIdentifier | SimpleIdentifierOwner);
references(): Reference[][] {
return this.nodes.map(node => this.session.globalSession.findReferences(node as SimpleIdentifier | SimpleIdentifierOwner));
}

declarations(): Declaration[] {
return this.session.findDeclarations(this.first() as SimpleIdentifier | SimpleIdentifierOwner)
declarations(): Declaration[][] {
return this.nodes.map(node => this.session.globalSession.findDeclarations(node as SimpleIdentifier | SimpleIdentifierOwner));
}

closest(closestSelector: string) {
Expand All @@ -257,11 +257,11 @@ export class RefactorSessionChainable {

lookupVariable(): Variable {
const id = this.first() as SimpleIdentifierOwner | SimpleIdentifierOwner[] | SimpleIdentifier | SimpleIdentifier[];
return this.session.lookupVariable(id);
return this.session.globalSession.lookupVariable(id);
}

lookupVariableByName(name: string): Variable[] {
return this.session.lookupVariableByName(name);
return this.session.globalSession.lookupVariableByName(name);
}

generate() {
Expand All @@ -282,6 +282,7 @@ export const RefactorChainableWithPlugins = RefactorSessionChainable.with(plugin
* @alpha
*/
export function refactor(input: string | Node, { autoCleanup = true } = {}) {
const globalSession = new RefactorSession(input, { autoCleanup });
return RefactorSessionChainable.with(pluginUnsafe).with(pluginCommon).create(globalSession);
const globalSession = new GlobalState(input, { autoCleanup });
const refactorSession = new RefactorSession(globalSession.root, globalSession);
return RefactorSessionChainable.with(pluginUnsafe).with(pluginCommon).create(refactorSession);
}

0 comments on commit b2c8d2c

Please sign in to comment.