-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add helper properties on TemplateLiteralTypeNode.
Closes #1266
- Loading branch information
Showing
6 changed files
with
147 additions
and
6 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
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
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
36 changes: 35 additions & 1 deletion
36
packages/ts-morph/src/compiler/ast/type/TemplateLiteralTypeNode.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,6 +1,40 @@ | ||
import { ts } from "@ts-morph/common"; | ||
import { replaceNodeText } from "../../../manipulation"; | ||
import { TemplateHead } from "../literal"; | ||
import { TypeNode } from "./TypeNode"; | ||
|
||
export class TemplateLiteralTypeNode extends TypeNode<ts.TemplateLiteralTypeNode> { | ||
// todo: helper methods | ||
/** | ||
* Gets the template head. | ||
*/ | ||
getHead(): TemplateHead { | ||
return this._getNodeFromCompilerNode(this.compilerNode.head); | ||
} | ||
|
||
/** | ||
* Gets the template spans. | ||
*/ | ||
getTemplateSpans() { | ||
return this.compilerNode.templateSpans.map(s => this._getNodeFromCompilerNode(s)); | ||
} | ||
|
||
/** | ||
* Sets the literal value. | ||
* | ||
* Note: This could possibly replace the node if you remove all the tagged templates. | ||
* @param value - Value to set. | ||
* @returns The new node if the kind changed; the current node otherwise. | ||
*/ | ||
setLiteralValue(value: string) { | ||
const childIndex = this.getChildIndex(); | ||
const parent = this.getParentSyntaxList() ?? this.getParentOrThrow(); | ||
replaceNodeText({ | ||
sourceFile: this._sourceFile, | ||
start: this.getStart() + 1, | ||
replacingLength: this.getWidth() - 2, | ||
newText: value, | ||
}); | ||
|
||
return parent.getChildAtIndex(childIndex); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/ts-morph/src/tests/compiler/ast/type/templateLiteralTypeNodeTests.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,49 @@ | ||
import { nameof, SyntaxKind } from "@ts-morph/common"; | ||
import { expect } from "chai"; | ||
import { TemplateExpression, TemplateLiteralTypeNode } from "../../../../compiler"; | ||
import { getInfoFromTextWithDescendant } from "../../testHelpers"; | ||
|
||
function getTypeNode(text: string) { | ||
return getInfoFromTextWithDescendant<TemplateLiteralTypeNode>(text, SyntaxKind.TemplateLiteralType).descendant; | ||
} | ||
|
||
describe("TemplateLiteralTypeNode", () => { | ||
describe(nameof<TemplateLiteralTypeNode>("getHead"), () => { | ||
function doTest(text: string, expectedText: string) { | ||
const expression = getTypeNode(text); | ||
expect(expression.getHead().getText()).to.equal(expectedText); | ||
} | ||
|
||
it("should get the correct template head", () => { | ||
doTest("type Test = `foo${test}`", "`foo${"); | ||
}); | ||
}); | ||
|
||
describe(nameof<TemplateLiteralTypeNode>("getTemplateSpans"), () => { | ||
function doTest(text: string, expectedText: string) { | ||
const expression = getTypeNode(text); | ||
expect(expression.getTemplateSpans()[0].getText()).to.equal(expectedText); | ||
} | ||
|
||
it("should get the correct template spans", () => { | ||
doTest("type Test = `foo${test}`", "test}`"); | ||
}); | ||
}); | ||
|
||
describe(nameof<TemplateLiteralTypeNode>("setLiteralValue"), () => { | ||
function doTest(text: string, newText: string, expectedText: string) { | ||
const expression = getTypeNode(text); | ||
const sourceFile = expression._sourceFile; | ||
expect(expression.setLiteralValue(newText).wasForgotten()).to.be.false; | ||
expect(sourceFile.getText()).to.equal(expectedText); | ||
} | ||
|
||
it("should set the value", () => { | ||
doTest("type Test = `foo${test}`", "testing${this}out", "type Test = `testing${this}out`"); | ||
}); | ||
|
||
it("should set the value to not have any tagged templates", () => { | ||
doTest("type Test = `foo${test}`", "testing", "type Test = `testing`"); | ||
}); | ||
}); | ||
}); |