Skip to content

Commit

Permalink
Separate collection and reassignment of initializers for double decla…
Browse files Browse the repository at this point in the history
…rations
  • Loading branch information
lukastaegert committed Jun 20, 2018
1 parent 39cc149 commit 8cacd62
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/ast/nodes/Identifier.ts
Expand Up @@ -37,9 +37,9 @@ export default class Identifier extends NodeBase {
if (
this.variable !== null &&
(<LocalVariable>this.variable).isLocal &&
!(<LocalVariable>this.variable).bound
(<LocalVariable>this.variable).additionalInitializers !== null
) {
(<LocalVariable>this.variable).bind();
(<LocalVariable>this.variable).consolidateInitializers();
}
}

Expand Down
35 changes: 17 additions & 18 deletions src/ast/variables/LocalVariable.ts
Expand Up @@ -21,48 +21,49 @@ const MAX_PATH_DEPTH = 7;

export default class LocalVariable extends Variable {
declarations: (Identifier | ExportDefaultDeclaration)[];
init: ExpressionEntity;
init: ExpressionEntity | null;
isLocal: true;
bound: boolean = false;
additionalInitializers: ExpressionEntity[] | null = null;

private initialisers: ExpressionEntity[];
private reassignmentTracker: EntityPathTracker;

constructor(
name: string,
declarator: Identifier | ExportDefaultDeclaration | null,
init: ExpressionEntity,
init: ExpressionEntity | null,
reassignmentTracker: EntityPathTracker
) {
super(name);
this.declarations = declarator ? [declarator] : [];
this.init = init;
this.initialisers = [init];
this.reassignmentTracker = reassignmentTracker;
}

addDeclaration(identifier: Identifier, init: ExpressionEntity) {
addDeclaration(identifier: Identifier, init: ExpressionEntity | null) {
this.declarations.push(identifier);
this.initialisers.push(init);
if (this.additionalInitializers === null) {
this.additionalInitializers = this.init === null ? [] : [this.init];
this.init = UNKNOWN_EXPRESSION;
this.isReassigned = true;
}
if (init !== null) {
this.additionalInitializers.push(init);
}
}

bind() {
if (this.bound) return;
this.bound = true;
if (this.initialisers.length > 1) {
this.isReassigned = true;
for (const initialiser of this.initialisers) {
initialiser.reassignPath(UNKNOWN_PATH);
consolidateInitializers() {
if (this.additionalInitializers !== null) {
for (const initializer of this.additionalInitializers) {
initializer.reassignPath(UNKNOWN_PATH);
}
this.init = UNKNOWN_EXPRESSION;
this.additionalInitializers = null;
}
}

getLiteralValueAtPath(
path: ObjectPath,
recursionTracker: ImmutableEntityPathTracker
): LiteralValueOrUnknown {
if (!this.bound) this.bind();
if (
this.isReassigned ||
!this.init ||
Expand All @@ -78,7 +79,6 @@ export default class LocalVariable extends Variable {
path: ObjectPath,
recursionTracker: ImmutableEntityPathTracker
): ExpressionEntity {
if (!this.bound) this.bind();
if (
this.isReassigned ||
!this.init ||
Expand Down Expand Up @@ -158,7 +158,6 @@ export default class LocalVariable extends Variable {
}

reassignPath(path: ObjectPath) {
if (!this.bound) this.bind();
if (path.length > MAX_PATH_DEPTH) return;
if (!(this.isReassigned || this.reassignmentTracker.track(this, path))) {
if (path.length === 0) {
Expand Down

0 comments on commit 8cacd62

Please sign in to comment.