Skip to content

Commit

Permalink
fix: #366 - Should be able to add a question token to a node without …
Browse files Browse the repository at this point in the history
…a type.
  • Loading branch information
dsherret committed Jul 17, 2018
1 parent 3daae87 commit c43c1e7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/compiler/base/QuestionTokenableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ export function QuestionTokenableNode<T extends Constructor<QuestionTokenableNod
if (TypeGuards.isExclamationTokenableNode(this))
this.setHasExclamationToken(false);

const colonNode = this.getFirstChildByKindOrThrow(SyntaxKind.ColonToken);
insertIntoParentTextRange({
insertPos: colonNode.getStart(),
insertPos: getInsertPos.call(this),
parent: this,
newText: "?"
});
Expand All @@ -65,6 +64,18 @@ export function QuestionTokenableNode<T extends Constructor<QuestionTokenableNod
removeChildren({ children: [questionTokenNode!] });

return this;

function getInsertPos(this: QuestionTokenableNode & Node) {
const colonNode = this.getFirstChildByKind(SyntaxKind.ColonToken);
if (colonNode != null)
return colonNode.getStart();

const semicolonToken = this.getLastChildByKind(SyntaxKind.SemicolonToken);
if (semicolonToken != null)
return semicolonToken.getStart();

return this.getEnd();
}
}

fill(structure: Partial<QuestionTokenableNodeStructure>) {
Expand Down
14 changes: 12 additions & 2 deletions src/tests/compiler/base/questionTokenableNodeTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { expect } from "chai";
import { ClassDeclaration, PropertyDeclaration, QuestionTokenableNode } from "../../../compiler";
import { ParameterDeclaration, ClassDeclaration, PropertyDeclaration, QuestionTokenableNode } from "../../../compiler";
import * as errors from "../../../errors";
import { SyntaxKind } from "../../../typescript";
import { QuestionTokenableNodeStructure } from "../../../structures";
import { getInfoFromText } from "../testHelpers";
import { getInfoFromText, getInfoFromTextWithDescendant } from "../testHelpers";

describe(nameof(QuestionTokenableNode), () => {
function getInfoWithFirstPropertyFromText(text: string) {
Expand Down Expand Up @@ -70,6 +72,14 @@ describe(nameof(QuestionTokenableNode), () => {
it("should do nothing when setting to same value", () => {
doTest("class MyClass { prop: string; }", false, "class MyClass { prop: string; }");
});

it("should be set as optional when has no type and has a semi-colon", () => {
doTest("class MyClass { prop; }", true, "class MyClass { prop?; }");
});

it("should be set as optional when has no type nor semi-colon", () => {
doTest("class MyClass { prop }", true, "class MyClass { prop? }");
});
});

describe(nameof<PropertyDeclaration>(p => p.fill), () => {
Expand Down
1 change: 1 addition & 0 deletions src/tests/compiler/testHelpers/getInfoFromText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function getInfoFromText<TFirstChild extends Node>(text: string, opts?: G
};
}

// todo: use the mapping between syntax kind and nodes for the descendant
export function getInfoFromTextWithDescendant<TDescendant extends Node>(text: string, descendantKind: SyntaxKind, opts?: GetInfoFromTextOptions) {
const info = getInfoFromTextInternal(text, opts);
return {
Expand Down

0 comments on commit c43c1e7

Please sign in to comment.