Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Template Literal handling (#217)
* feat: add combination helper * feat: handling Template Literals * test: add test for #214 * feat: implementing #64 * test: adding template literal as example * fix: adding fallback for unsupported template literal cases * refacto: extract extractLiteralValue * doc: adding warning for unsupported case * refacto: import ordering * test: fix use case * fix: more logical behaviour
- Loading branch information
Showing
8 changed files
with
428 additions
and
0 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
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,37 @@ | ||
import ts, { factory } from "typescript"; | ||
import { extractLiteralValue } from "./extractLiteralValue"; | ||
|
||
describe("extractLiteralValue", () => { | ||
it("should extract string literal value", () => { | ||
const source = factory.createStringLiteral("hello"); | ||
expect(extractLiteralValue(source)).toBe("hello"); | ||
}); | ||
|
||
it("should extract numeric literal value", () => { | ||
const source = factory.createNumericLiteral("42"); | ||
expect(extractLiteralValue(source)).toBe("42"); | ||
}); | ||
|
||
it("should extract negative numeric literal value", () => { | ||
const source = factory.createPrefixUnaryExpression( | ||
ts.SyntaxKind.MinusToken, | ||
factory.createNumericLiteral("42") | ||
); | ||
expect(extractLiteralValue(source)).toBe("-42"); | ||
}); | ||
|
||
it("should extract true literal value", () => { | ||
const source = factory.createTrue(); | ||
expect(extractLiteralValue(source)).toBe("true"); | ||
}); | ||
|
||
it("should extract false literal value", () => { | ||
const source = factory.createFalse(); | ||
expect(extractLiteralValue(source)).toBe("false"); | ||
}); | ||
|
||
it("should return empty string for unknown literal value", () => { | ||
const source = factory.createNull(); | ||
expect(extractLiteralValue(source)).toBe(""); | ||
}); | ||
}); |
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,28 @@ | ||
import ts from "typescript"; | ||
|
||
/** | ||
* Extract the string representation of a literal value | ||
*/ | ||
export function extractLiteralValue(node: ts.Expression): string { | ||
if (ts.isStringLiteral(node)) { | ||
return node.text; | ||
} | ||
if (ts.isNumericLiteral(node)) { | ||
return node.text; | ||
} | ||
if (ts.isPrefixUnaryExpression(node)) { | ||
if ( | ||
node.operator === ts.SyntaxKind.MinusToken && | ||
ts.isNumericLiteral(node.operand) | ||
) { | ||
return "-" + node.operand.text; | ||
} | ||
} | ||
if (node.kind === ts.SyntaxKind.TrueKeyword) { | ||
return "true"; | ||
} | ||
if (node.kind === ts.SyntaxKind.FalseKeyword) { | ||
return "false"; | ||
} | ||
return ""; | ||
} |
Oops, something went wrong.