Skip to content

Commit

Permalink
fixed types
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoverson committed May 18, 2020
1 parent 9e0ce56 commit 3f89517
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 219 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isString, findNodes, isFunction, isShiftNode, isStatement, copy, isArra
import { SelectorOrNode, Replacer, RefactorError } from './types';
import { RefactorCommonPlugin } from "./refactor-plugin-common";
import { RefactorPlugin } from './refactor-plugin';
import { RefactorUnsafePlugin } from './refactor-plugin-unsafe';

const debug = DEBUG('shift-refactor');

Expand All @@ -34,6 +35,7 @@ export class RefactorSession {

this._rebuildParentMap();
this.use(RefactorCommonPlugin);
this.use(RefactorUnsafePlugin);
}

use<T extends RefactorPlugin>(Plugin: new(session:RefactorSession) => T) {
Expand Down
67 changes: 3 additions & 64 deletions src/refactor-plugin-common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import debug from "debug";
import { ClassDeclaration, ComputedMemberAssignmentTarget, ComputedMemberExpression, ComputedPropertyName, FunctionDeclaration, IdentifierExpression, LiteralBooleanExpression, LiteralStringExpression, StaticMemberAssignmentTarget, StaticMemberExpression, StaticPropertyName, VariableDeclarator } from "shift-ast";
import { Declaration, Reference, Variable } from "shift-scope";
import { ComputedMemberAssignmentTarget, ComputedMemberExpression, ComputedPropertyName, IdentifierExpression, LiteralBooleanExpression, LiteralStringExpression, StaticMemberAssignmentTarget, StaticMemberExpression, StaticPropertyName, VariableDeclarator } from "shift-ast";
import { Declaration, Reference } from "shift-scope";
import { default as isValid } from 'shift-validator';
import { IdGenerator, MemorableIdGenerator } from "./id-generator";
import { RefactorPlugin } from "./refactor-plugin";
import { SelectorOrNode } from "./types";
import { findNodes, isLiteral, isStatement, renameScope } from "./util";
import { findNodes, renameScope } from "./util";

declare module "." {
interface RefactorSession {
Expand All @@ -14,7 +14,6 @@ declare module "." {
}

export class RefactorCommonPlugin extends RefactorPlugin {
name = "common";

register() {
this.session.common = this;
Expand Down Expand Up @@ -112,64 +111,4 @@ export class RefactorCommonPlugin extends RefactorPlugin {
return this;
}

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

removeDeadVariables() {
this.session.query('VariableDeclarator, FunctionDeclaration, ClassDeclaration').forEach(
(decl: VariableDeclarator | FunctionDeclaration | ClassDeclaration) => {
let name = decl instanceof VariableDeclarator ? decl.binding : decl.name;
const lookup = this.session.lookupVariable(name);

const reads = lookup.references.filter((ref: Reference) => {
const isRead = ref.accessibility.isRead;
const isBoth = ref.accessibility.isReadWrite;
if (isBoth) {
// if we're an UpdateExpression
const immediateParent = this.session.findParent(ref.node);
if (!immediateParent) return false;
const nextParent = this.session.findParent(immediateParent);
if (isStatement(nextParent)) return false;
else return true;
} else {
return isRead;
}
});

if (reads.length === 0) {
lookup.references.forEach((ref: Reference) => {
const node = ref.node;
const immediateParent = this.session.findParent(node);
if (!immediateParent) return;
const contextualParent = this.session.findParent(immediateParent);

if (['VariableDeclarator', 'FunctionDeclaration', 'ClassDeclaration'].indexOf(immediateParent.type) > -1) {
this.session.delete(immediateParent);
} else if (immediateParent.type === 'UpdateExpression' && isStatement(contextualParent)) {
this.session.delete(contextualParent);
} else if (node.type === 'AssignmentTargetIdentifier') {
if (immediateParent.type === 'AssignmentExpression') {
if (isLiteral(immediateParent.expression)) {
if (isStatement(contextualParent)) {
this.session.delete(contextualParent);
} else {
this.session.replace(immediateParent, immediateParent.expression);
}
} else {
this.session.replace(immediateParent, immediateParent.expression);
}
}
}
});
this.session.delete(decl);
}
},
);
if (this.session.autoCleanup) this.session.cleanup();
return this;
}

}
1 change: 0 additions & 1 deletion src/refactor-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { RefactorSession } from ".";

export abstract class RefactorPlugin {
abstract name: string;
session: RefactorSession;

abstract register(): void;
Expand Down
87 changes: 0 additions & 87 deletions test/plugin-common/plugin-common.test.ts

This file was deleted.

67 changes: 0 additions & 67 deletions test/plugin-common/removeDeadVariables.test.ts

This file was deleted.

0 comments on commit 3f89517

Please sign in to comment.