Skip to content

Commit

Permalink
refactor: Remove VariableDeclarationListStructure and `VariableDecl…
Browse files Browse the repository at this point in the history
…arationList#set` and `#getStructure`

BREAKING CHANGE: Removed `VariableDeclarationListStructure` and `VariableDeclarationList#set` and `#getStructure`. This was done to simplify the structures.
  • Loading branch information
dsherret committed Apr 19, 2019
1 parent e3bd2e9 commit e6953ed
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 84 deletions.
11 changes: 7 additions & 4 deletions src/compiler/ast/statement/VariableStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ export class VariableStatement extends VariableStatementBase<ts.VariableStatemen
set(structure: Partial<VariableStatementStructure>) {
callBaseSet(VariableStatementBase.prototype, this, structure);

this.getDeclarationList().set({
declarationKind: structure.declarationKind,
declarations: structure.declarations
});
if (structure.declarationKind != null)
this.setDeclarationKind(structure.declarationKind);
if (structure.declarations != null) {
const existingDeclarations = this.getDeclarations();
this.addDeclarations(structure.declarations);
existingDeclarations.forEach(d => d.remove());
}

return this;
}
Expand Down
32 changes: 1 addition & 31 deletions src/compiler/ast/variable/VariableDeclarationList.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import * as errors from "../../../errors";
import { getNodesToReturn, insertIntoCommaSeparatedNodes, insertIntoParentTextRange } from "../../../manipulation";
import { CommaSeparatedStructuresPrinter } from "../../../structurePrinters";
import { VariableDeclarationListStructure, VariableDeclarationStructure, VariableDeclarationListSpecificStructure, OptionalKind } from "../../../structures";
import { VariableDeclarationStructure, OptionalKind } from "../../../structures";
import { SyntaxKind, ts } from "../../../typescript";
import { ModifierableNode } from "../base";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { VariableDeclaration } from "./VariableDeclaration";
import { VariableDeclarationKind } from "./VariableDeclarationKind";
import { callBaseGetStructure } from "../callBaseGetStructure";

export const VariableDeclarationListBase = ModifierableNode(Node);
export class VariableDeclarationList extends VariableDeclarationListBase<ts.VariableDeclarationList> {
Expand Down Expand Up @@ -116,32 +114,4 @@ export class VariableDeclarationList extends VariableDeclarationListBase<ts.Vari

return getNodesToReturn(this.getDeclarations(), index, structures.length);
}

/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<VariableDeclarationListStructure>) {
callBaseSet(VariableDeclarationListBase.prototype, this, structure);

if (structure.declarationKind != null)
this.setDeclarationKind(structure.declarationKind);
if (structure.declarations != null) {
const existingDeclarations = this.getDeclarations();
this.addDeclarations(structure.declarations);
existingDeclarations.forEach(d => d.remove());
}

return this;
}

/**
* Gets the structure equivalent to this node.
*/
getStructure(): VariableDeclarationListStructure {
return callBaseGetStructure<VariableDeclarationListSpecificStructure>(VariableDeclarationListBase.prototype, this, {
declarationKind: this.getDeclarationKind(),
declarations: this.getDeclarations().map(declaration => declaration.getStructure())
}) as any as VariableDeclarationListStructure;
}
}
12 changes: 0 additions & 12 deletions src/structures/statement/VariableDeclarationListStructure.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/structures/statement/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./StatementedNodeStructure";
export * from "./VariableDeclarationListStructure";
export * from "./VariableDeclarationStructure";
export * from "./VariableStatementStructure";
37 changes: 1 addition & 36 deletions src/tests/compiler/ast/statement/variableDeclarationListTests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import { VariableDeclaration, VariableDeclarationKind, VariableDeclarationList } from "../../../../compiler";
import { VariableDeclarationListStructure, VariableDeclarationStructure, OptionalKind } from "../../../../structures";
import { VariableDeclarationStructure, OptionalKind } from "../../../../structures";
import { getInfoFromText } from "../../testHelpers";

describe(nameof(VariableDeclarationList), () => {
Expand Down Expand Up @@ -128,39 +128,4 @@ describe(nameof(VariableDeclarationList), () => {
doTest("var v1;", { name: "v2" }, "var v1, v2;");
});
});

describe(nameof<VariableDeclarationList>(d => d.set), () => {
function doTest(text: string, fillStructure: Partial<VariableDeclarationListStructure>, expectedText: string) {
const { sourceFile } = getInfoFromText(text);
sourceFile.getVariableStatements()[0].getDeclarationList().set(fillStructure);
expect(sourceFile.getFullText()).to.equal(expectedText);
}

it("should set the variable declaration kind", () => {
doTest("const t = '';", { declarationKind: VariableDeclarationKind.Let }, "let t = '';");
});

it("should set declarations", () => {
doTest("const t = '';", { declarations: [{ name: "v2" }, { name: "v3" }] }, "const v2, v3;");
});

it("should remove the statement when specifying an empty declarations array", () => {
doTest("const t = '';", { declarations: [] }, "");
});
});

describe(nameof<VariableDeclarationList>(d => d.getStructure), () => {
function doTest(text: string, expected: VariableDeclarationListStructure) {
const structure = getInfoFromText(text).sourceFile.getVariableStatements()[0].getDeclarationList().getStructure();
structure.declarations = structure.declarations.map(d => ({ name: d.name }));
expect(structure).to.deep.equal(expected);
}

it("should get structure of the variable declaration list", () => {
doTest("let t, u;", {
declarationKind: VariableDeclarationKind.Let,
declarations: [{ name: "t" }, { name: "u" }]
});
});
});
});

0 comments on commit e6953ed

Please sign in to comment.