-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #765 - Add QuestionDotTokenableNode.
- Loading branch information
Showing
11 changed files
with
262 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/ts-morph/src/compiler/ast/base/QuestionDotTokenableNode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { errors, SyntaxKind, ts } from "@ts-morph/common"; | ||
import { insertIntoParentTextRange, removeChildren } from "../../../manipulation"; | ||
import { QuestionDotTokenableNodeStructure } from "../../../structures"; | ||
import { Constructor } from "../../../types"; | ||
import { callBaseSet } from "../callBaseSet"; | ||
import { Node } from "../common"; | ||
import { callBaseGetStructure } from "../callBaseGetStructure"; | ||
|
||
export type QuestionDotTokenableNodeExtensionType = Node<ts.Node & { questionDotToken?: ts.QuestionDotToken; }>; | ||
|
||
export interface QuestionDotTokenableNode { | ||
/** | ||
* If it has a question dot token. | ||
*/ | ||
hasQuestionDotToken(): boolean; | ||
/** | ||
* Gets the question dot token node or returns undefined if it doesn't exist. | ||
*/ | ||
getQuestionDotTokenNode(): Node<ts.QuestionDotToken> | undefined; | ||
/** | ||
* Gets the question dot token node or throws. | ||
*/ | ||
getQuestionDotTokenNodeOrThrow(): Node<ts.QuestionDotToken>; | ||
/** | ||
* Sets if this node has a question dot token. | ||
* @param value - If it should have a question dot token or not. | ||
*/ | ||
setHasQuestionDotToken(value: boolean): this; | ||
} | ||
|
||
export function QuestionDotTokenableNode<T extends Constructor<QuestionDotTokenableNodeExtensionType>>(Base: T): Constructor<QuestionDotTokenableNode> & T { | ||
return class extends Base implements QuestionDotTokenableNode { | ||
hasQuestionDotToken() { | ||
return this.compilerNode.questionDotToken != null; | ||
} | ||
|
||
getQuestionDotTokenNode(): Node<ts.QuestionDotToken> | undefined { | ||
return this._getNodeFromCompilerNodeIfExists(this.compilerNode.questionDotToken); | ||
} | ||
|
||
getQuestionDotTokenNodeOrThrow(): Node<ts.QuestionDotToken> { | ||
return errors.throwIfNullOrUndefined(this.getQuestionDotTokenNode(), "Expected to find a question dot token."); | ||
} | ||
|
||
setHasQuestionDotToken(value: boolean) { | ||
const questionDotTokenNode = this.getQuestionDotTokenNode(); | ||
const hasQuestionDotToken = questionDotTokenNode != null; | ||
|
||
if (value === hasQuestionDotToken) | ||
return this; | ||
|
||
if (value) { | ||
if (Node.isPropertyAccessExpression(this)) | ||
this.getFirstChildByKindOrThrow(SyntaxKind.DotToken).replaceWithText("?."); | ||
else { | ||
insertIntoParentTextRange({ | ||
insertPos: getInsertPos.call(this), | ||
parent: this, | ||
newText: "?." | ||
}); | ||
} | ||
} | ||
else { | ||
if (Node.isPropertyAccessExpression(this)) | ||
questionDotTokenNode!.replaceWithText("."); | ||
else | ||
removeChildren({ children: [questionDotTokenNode!] }); | ||
} | ||
|
||
return this; | ||
|
||
function getInsertPos(this: QuestionDotTokenableNode & Node) { | ||
if (Node.isCallExpression(this)) | ||
return this.getFirstChildByKindOrThrow(SyntaxKind.OpenParenToken).getStart(); | ||
if (Node.isElementAccessExpression(this)) | ||
return this.getFirstChildByKindOrThrow(SyntaxKind.OpenBracketToken).getStart(); | ||
errors.throwNotImplementedForSyntaxKindError(this.compilerNode.kind); | ||
} | ||
} | ||
|
||
set(structure: Partial<QuestionDotTokenableNodeStructure>) { | ||
callBaseSet(Base.prototype, this, structure); | ||
|
||
if (structure.hasQuestionDotToken != null) | ||
this.setHasQuestionDotToken(structure.hasQuestionDotToken); | ||
|
||
return this; | ||
} | ||
|
||
getStructure() { | ||
return callBaseGetStructure<QuestionDotTokenableNodeStructure>(Base.prototype, this, { | ||
hasQuestionDotToken: this.hasQuestionDotToken() | ||
}); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/ts-morph/src/compiler/ast/expression/CallExpression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
packages/ts-morph/src/compiler/ast/expression/ElementAccessExpression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/ts-morph/src/compiler/ast/expression/PropertyAccessExpression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { ts } from "@ts-morph/common"; | ||
import { NamedNode } from "../base"; | ||
import { NamedNode, QuestionDotTokenableNode } from "../base"; | ||
import { LeftHandSideExpressionedNode } from "./expressioned"; | ||
import { MemberExpression } from "./MemberExpression"; | ||
|
||
const createBase = <T extends typeof MemberExpression>(ctor: T) => NamedNode(LeftHandSideExpressionedNode(ctor)); | ||
const createBase = <T extends typeof MemberExpression>(ctor: T) => NamedNode(QuestionDotTokenableNode(LeftHandSideExpressionedNode(ctor))); | ||
export const PropertyAccessExpressionBase = createBase(MemberExpression); | ||
export class PropertyAccessExpression<T extends ts.PropertyAccessExpression = ts.PropertyAccessExpression> extends PropertyAccessExpressionBase<T> { | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/ts-morph/src/structures/base/QuestionDotTokenableNodeStructure.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface QuestionDotTokenableNodeStructure { | ||
hasQuestionDotToken?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/ts-morph/src/tests/compiler/ast/base/questionDotTokenableNodeTests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { expect } from "chai"; | ||
import { QuestionDotTokenableNode, ExpressionStatement } from "../../../../compiler"; | ||
import { QuestionDotTokenableNodeStructure } from "../../../../structures"; | ||
import { getInfoFromText } from "../../testHelpers"; | ||
|
||
describe(nameof(QuestionDotTokenableNode), () => { | ||
function getInfoWithFirstExpr(text: string) { | ||
const result = getInfoFromText<ExpressionStatement>(text); | ||
return { ...result, expr: result.firstChild.getExpression() as any as QuestionDotTokenableNode }; | ||
} | ||
|
||
describe(nameof<QuestionDotTokenableNode>(d => d.hasQuestionDotToken), () => { | ||
function doTest(text: string, value: boolean) { | ||
const { expr: firstMember } = getInfoWithFirstExpr(text); | ||
expect(firstMember.hasQuestionDotToken()).to.equal(value); | ||
} | ||
|
||
it("should have when has with call expression", () => { | ||
doTest("callExpr?.()", true); | ||
}); | ||
|
||
it("should have when has with element access expression", () => { | ||
doTest("elementAccess?.[0]", true); | ||
}); | ||
|
||
it("should have when has with prop access expression", () => { | ||
doTest("propAccess?.test", true); | ||
}); | ||
|
||
it("should not have when not has one", () => { | ||
doTest("callExpr()", false); | ||
}); | ||
}); | ||
|
||
describe(nameof<QuestionDotTokenableNode>(d => d.getQuestionDotTokenNode), () => { | ||
it("should get it", () => { | ||
const { expr: firstMember } = getInfoWithFirstExpr("call?.()"); | ||
expect(firstMember.getQuestionDotTokenNode()!.getText()).to.equal("?."); | ||
}); | ||
|
||
it("should be undefined when not has one", () => { | ||
const { expr: firstMember } = getInfoWithFirstExpr("call()"); | ||
expect(firstMember.getQuestionDotTokenNode()).to.be.undefined; | ||
}); | ||
}); | ||
|
||
describe(nameof<QuestionDotTokenableNode>(d => d.getQuestionDotTokenNodeOrThrow), () => { | ||
it("should get it", () => { | ||
const { expr: firstMember } = getInfoWithFirstExpr("call?.()"); | ||
expect(firstMember.getQuestionDotTokenNodeOrThrow()!.getText()).to.equal("?."); | ||
}); | ||
|
||
it("should throw when not has one", () => { | ||
const { expr: firstMember } = getInfoWithFirstExpr("call()"); | ||
expect(() => firstMember.getQuestionDotTokenNodeOrThrow()).to.throw(); | ||
}); | ||
}); | ||
|
||
describe(nameof<QuestionDotTokenableNode>(d => d.setHasQuestionDotToken), () => { | ||
function doTest(startText: string, value: boolean, expected: string) { | ||
const { expr: firstMember, sourceFile } = getInfoWithFirstExpr(startText); | ||
firstMember.setHasQuestionDotToken(value); | ||
expect(sourceFile.getFullText()).to.be.equal(expected); | ||
} | ||
|
||
it("should be set when not", () => { | ||
doTest("call()", true, "call?.()"); | ||
}); | ||
|
||
it("should be set as not when is", () => { | ||
doTest("call?.()", false, "call()"); | ||
}); | ||
|
||
it("should do nothing when setting to same value", () => { | ||
doTest("call?.()", true, "call?.()"); | ||
}); | ||
|
||
it("should be set when element access", () => { | ||
doTest("element[0]", true, "element?.[0]"); | ||
}); | ||
|
||
it("should remove when element access", () => { | ||
doTest("element?.[0]", false, "element[0]"); | ||
}); | ||
|
||
it("should be set when prop access", () => { | ||
doTest("prop.test", true, "prop?.test"); | ||
}); | ||
|
||
it("should be set when prop access on multiple lines", () => { | ||
doTest("prop\n .test", true, "prop\n ?.test"); | ||
}); | ||
|
||
it("should remove when prop access", () => { | ||
doTest("prop?.test", false, "prop.test"); | ||
}); | ||
|
||
it("should remove when prop access on multiple lines", () => { | ||
doTest("prop\n ?.test", false, "prop\n .test"); | ||
}); | ||
}); | ||
|
||
// todo: getStructure and set tests (not used right now) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters