Skip to content

Commit

Permalink
Add set method to PropertyAssignment and ShorthandPropertyAssignment
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Sep 22, 2018
1 parent 9507df2 commit b8d07a5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/compiler/expression/object/PropertyAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { InitializerGetExpressionableNode, PropertyNamedNode, QuestionTokenableN
import { Node } from "../../common";
import { ShorthandPropertyAssignment } from "./ShorthandPropertyAssignment";
import { PropertyAssignmentStructure, PropertyAssignmentSpecificStructure } from "../../../structures";
import { callBaseSet } from "../../callBaseSet";
import { callBaseGetStructure } from "../../callBaseGetStructure";

// This node only has a question token in order to tell the user about bad code.
Expand Down Expand Up @@ -64,6 +65,21 @@ export class PropertyAssignment extends PropertyAssignmentBase<ts.PropertyAssign
removeCommaSeparatedChild(this);
}

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

if (structure.initializer != null)
this.setInitializer(structure.initializer);
else if (structure.hasOwnProperty(nameof(structure.initializer)))
return this.removeInitializer();

return this;
}

/**
* Gets the structure equivalent to this node.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/compiler/expression/object/ShorthandPropertyAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Expression } from "../Expression";
import { PropertyAssignment } from "./PropertyAssignment";
import { ShorthandPropertyAssignmentStructure, ShorthandPropertyAssignmentSpecificStructure, QuestionTokenableNodeStructure } from "../../../structures";
import { callBaseGetStructure } from "../../callBaseGetStructure";
import { callBaseSet } from "../../callBaseSet";

// This node only has an object assignment initializer, equals token, and question token, in order to tell the user about bad code
// (See https://github.com/Microsoft/TypeScript/pull/5121/files)
Expand Down Expand Up @@ -97,6 +98,16 @@ export class ShorthandPropertyAssignment extends ShorthandPropertyAssignmentBase
removeCommaSeparatedChild(this);
}

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

return this;
}

/**
* Gets the structure equivalent to this node.
*/
Expand Down
25 changes: 25 additions & 0 deletions src/tests/compiler/expression/object/propertyAssignmentTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ describe(nameof(PropertyAssignment), () => {
});
});

describe(nameof<PropertyAssignment>(p => p.set), () => {
function test(code: string, structure: Partial<PropertyAssignmentStructure>, expectedText: string) {
const { descendant, sourceFile } = getInfoFromTextWithDescendant<PropertyAssignment>(code, SyntaxKind.PropertyAssignment);
expect(descendant.set(structure).wasForgotten()).to.be.false;
expect(sourceFile.getFullText()).to.equal(expectedText);
}

it("should not change when nothing is specified", () => {
const code = "const t = { prop1: 1 };";
test(code, { }, code);
});

it("should set when everything is specified", () => {
const structure: MakeRequired<PropertyAssignmentStructure> = {
name: "NewName",
initializer: "5"
};
test("const t = { prop1: 1 };", structure, "const t = { NewName: 5 };");
});

it("should remove initializer when specifying undefined", () => {
test("const t = { prop: 1 };", { initializer: undefined }, "const t = { prop };");
});
});

describe(nameof<PropertyAssignment>(p => p.getStructure), () => {
function test(code: string, expectedStructure: MakeRequired<PropertyAssignmentStructure>) {
const { descendant } = getInfoFromTextWithDescendant<PropertyAssignment>(code, SyntaxKind.PropertyAssignment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ describe(nameof(ShorthandPropertyAssignment), () => {
});
});

describe(nameof<ShorthandPropertyAssignment>(p => p.set), () => {
function test(code: string, structure: Partial<ShorthandPropertyAssignmentStructure>, expectedText: string) {
const { descendant, sourceFile } = getInfoFromTextWithDescendant<ShorthandPropertyAssignment>(code, SyntaxKind.ShorthandPropertyAssignment);
expect(descendant.set(structure).wasForgotten()).to.be.false;
expect(sourceFile.getFullText()).to.equal(expectedText);
}

it("should not change when nothing is specified", () => {
const code = "const t = { prop1 };";
test(code, {}, code);
});

it("should set when everything is specified", () => {
const structure: MakeRequired<ShorthandPropertyAssignmentStructure> = {
name: "NewName"
};
test("const t = { prop1 };", structure, "const t = { NewName };");
});
});

describe(nameof<ShorthandPropertyAssignment>(p => p.getStructure), () => {
function test(code: string, expectedStructure: MakeRequired<ShorthandPropertyAssignmentStructure>) {
const { descendant } = getInfoFromTextWithDescendant<ShorthandPropertyAssignment>(code, SyntaxKind.ShorthandPropertyAssignment);
Expand Down

0 comments on commit b8d07a5

Please sign in to comment.