Skip to content

Commit

Permalink
refactor: Rename toggleDeclareKeyword to setHasDeclareKeyword for con…
Browse files Browse the repository at this point in the history
…sistency.

BREAKING CHANGE: toggleDeclareKeyword is now setHasDeclareKeyword.
  • Loading branch information
dsherret committed Feb 12, 2018
1 parent 121ee59 commit 0cecefe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
14 changes: 9 additions & 5 deletions src/compiler/base/AmbientableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export interface AmbientableNode {
*/
isAmbient(): boolean;
/**
* Toggles or sets if this node has a declare keyword.
* @param value - If to add the declare keyword or not.
* Sets if this node has a declare keyword.
* @param value - To add the declare keyword or not.
*/
toggleDeclareKeyword(value?: boolean): this;
setHasDeclareKeyword(value?: boolean): this;
}

export function AmbientableNode<T extends Constructor<AmbientableNodeExtensionType>>(Base: T): Constructor<AmbientableNode> & T {
Expand Down Expand Up @@ -64,7 +64,11 @@ export function AmbientableNode<T extends Constructor<AmbientableNodeExtensionTy
return TypeGuards.isSourceFile(topParent) && topParent.isDeclarationFile();
}

toggleDeclareKeyword(value?: boolean) {
setHasDeclareKeyword(value: boolean) {
// do nothing for these kind of nodes
if (TypeGuards.isInterfaceDeclaration(this) || TypeGuards.isTypeAliasDeclaration(this))
return this;

this.toggleModifier("declare", value);
return this;
}
Expand All @@ -73,7 +77,7 @@ export function AmbientableNode<T extends Constructor<AmbientableNodeExtensionTy
callBaseFill(Base.prototype, this, structure);

if (structure.hasDeclareKeyword != null)
this.toggleDeclareKeyword(structure.hasDeclareKeyword);
this.setHasDeclareKeyword(structure.hasDeclareKeyword);

return this;
}
Expand Down
27 changes: 12 additions & 15 deletions src/tests/compiler/base/ambientableNodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,27 @@ describe(nameof(AmbientableNode), () => {
});
});

describe(nameof<AmbientableNode>(n => n.toggleDeclareKeyword), () => {
function doTest(text: string, value: boolean | undefined, expected: string) {
const {firstChild, sourceFile} = getInfoFromText<ClassDeclaration>(text);
if (value !== undefined)
firstChild.toggleDeclareKeyword(value);
else
firstChild.toggleDeclareKeyword();
describe(nameof<AmbientableNode>(n => n.setHasDeclareKeyword), () => {
function doTest(text: string, value: boolean, expected: string) {
const {firstChild, sourceFile} = getInfoFromText<AmbientableNode & Node>(text);
firstChild.setHasDeclareKeyword(value);
expect(sourceFile.getText()).to.equal(expected);
}

it("should add declare keyword when doesn't have one", () => {
doTest("class MyClass { }", undefined, "declare class MyClass { }");
it("should add declare keyword when setting it to true", () => {
doTest("class MyClass { }", true, "declare class MyClass { }");
});

it("should remove declare keyword when it has one", () => {
doTest("declare class MyClass { }", undefined, "class MyClass { }");
it("should remove declare keyword when setting it to false", () => {
doTest("declare class MyClass { }", false, "class MyClass { }");
});

it("should add declare keyword when explicitly toggling it", () => {
doTest("class MyClass { }", true, "declare class MyClass { }");
it("should do nothing for an interface when setting to true", () => {
doTest("interface MyInterface { }", true, "interface MyInterface { }");
});

it("should remove declare keyword when explicitly toggling it", () => {
doTest("declare class MyClass { }", false, "class MyClass { }");
it("should do nothing for a type alias when setting to true", () => {
doTest("type MyType = string;", true, "type MyType = string;");
});
});

Expand Down

0 comments on commit 0cecefe

Please sign in to comment.