-
-
Notifications
You must be signed in to change notification settings - Fork 180
Tagged template literals support #638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Perryvw
merged 12 commits into
TypeScriptToLua:master
from
hazzard993:tagged-template-literals
Jul 2, 2019
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9b9b4a2
Implement Tagged Template Literals
hazzard993 c949e25
Merge remote-tracking branch 'upstream/master' into tagged-template-l…
hazzard993 742aa9d
Add unicode escape test
hazzard993 51ed012
Keep track of raw strings
hazzard993 bf24fbc
Improve raw string preservation and use type narrowing
hazzard993 cd147fb
Simplfy method using TypeScript's example
hazzard993 517d998
Merge remote-tracking branch 'upstream/master' into tagged-template-l…
hazzard993 7357109
Fix ContextType reference
hazzard993 33dbac4
Remove unnecessary check
hazzard993 016a039
Reduce checks and use filterUndefinedAndCast
hazzard993 5158042
Add transformContextualCallExpression to transform call expression le…
hazzard993 0bd8b65
Merge remote-tracking branch 'upstream/master' into tagged-template-l…
hazzard993 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,123 @@ | ||
| import * as util from "../util"; | ||
|
|
||
| const testCases = [ | ||
| { | ||
| callExpression: "func``", | ||
| joinAllResult: "", | ||
| joinRawResult: "", | ||
| }, | ||
| { | ||
| callExpression: "func`hello`", | ||
| joinAllResult: "hello", | ||
| joinRawResult: "hello", | ||
| }, | ||
| { | ||
| callExpression: "func`hello ${1} ${2} ${3}`", | ||
| joinAllResult: "hello 1 2 3", | ||
| joinRawResult: "hello ", | ||
| }, | ||
| { | ||
| callExpression: "func`hello ${(() => 'iife')()}`", | ||
| joinAllResult: "hello iife", | ||
| joinRawResult: "hello ", | ||
| }, | ||
| { | ||
| callExpression: "func`hello ${1 + 2 + 3} arithmetic`", | ||
| joinAllResult: "hello 6 arithmetic", | ||
| joinRawResult: "hello arithmetic", | ||
| }, | ||
| { | ||
| callExpression: "func`begin ${'middle'} end`", | ||
| joinAllResult: "begin middle end", | ||
| joinRawResult: "begin end", | ||
| }, | ||
| { | ||
| callExpression: "func`hello ${func`hello`}`", | ||
| joinAllResult: "hello hello", | ||
| joinRawResult: "hello ", | ||
| }, | ||
| { | ||
| callExpression: "func`hello \\u00A9`", | ||
| joinAllResult: "hello ©", | ||
| joinRawResult: "hello \\u00A9", | ||
| }, | ||
| { | ||
| callExpression: "func`hello $ { }`", | ||
| joinAllResult: "hello $ { }", | ||
| joinRawResult: "hello $ { }", | ||
| }, | ||
| { | ||
| callExpression: "func`hello { ${'brackets'} }`", | ||
| joinAllResult: "hello { brackets }", | ||
| joinRawResult: "hello { }", | ||
| }, | ||
| { | ||
| callExpression: "func`hello \\``", | ||
| joinAllResult: "hello `", | ||
| joinRawResult: "hello \\`", | ||
| }, | ||
| { | ||
| callExpression: "obj.func`hello ${'propertyAccessExpression'}`", | ||
| joinAllResult: "hello propertyAccessExpression", | ||
| joinRawResult: "hello ", | ||
| }, | ||
| { | ||
| callExpression: "obj['func']`hello ${'elementAccessExpression'}`", | ||
| joinAllResult: "hello elementAccessExpression", | ||
| joinRawResult: "hello ", | ||
| }, | ||
| ]; | ||
|
|
||
| test.each(testCases)("TaggedTemplateLiteral call (%p)", ({ callExpression, joinAllResult }) => { | ||
| const result = util.transpileAndExecute(` | ||
| function func(strings: TemplateStringsArray, ...expressions: any[]) { | ||
| const toJoin = []; | ||
| for (let i = 0; i < strings.length; ++i) { | ||
| if (strings[i]) { | ||
| toJoin.push(strings[i]); | ||
| } | ||
| if (expressions[i]) { | ||
| toJoin.push(expressions[i]); | ||
| } | ||
| } | ||
| return toJoin.join(""); | ||
| } | ||
| const obj = { | ||
| func | ||
| }; | ||
| return ${callExpression}; | ||
| `); | ||
|
|
||
| expect(result).toBe(joinAllResult); | ||
| }); | ||
|
|
||
| test.each(testCases)("TaggedTemplateLiteral raw preservation (%p)", ({ callExpression, joinRawResult }) => { | ||
| const result = util.transpileAndExecute(` | ||
| function func(strings: TemplateStringsArray, ...expressions: any[]) { | ||
| return strings.raw.join(""); | ||
| } | ||
| const obj = { | ||
| func | ||
| }; | ||
| return ${callExpression}; | ||
| `); | ||
|
|
||
| expect(result).toBe(joinRawResult); | ||
| }); | ||
|
|
||
| test.each(["func`noSelfParameter`", "obj.func`noSelfParameter`", "obj[`func`]`noSelfParameter`"])( | ||
| "TaggedTemplateLiteral no self parameter", | ||
| callExpression => { | ||
| const result = util.transpileAndExecute(` | ||
| function func(this: void, strings: TemplateStringsArray, ...expressions: any[]) { | ||
| return strings.join(""); | ||
| } | ||
| const obj = { | ||
| func | ||
| }; | ||
| return ${callExpression}; | ||
| `); | ||
|
|
||
| expect(result).toBe("noSelfParameter"); | ||
| } | ||
| ); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it always calls functions with global context:
Also there is a case where
objisn't pure:In call expressions it's handled like that:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good find! Took a bit to understand what to do here, I've added
transformContextualCallExpressionin an attempt to preserve this left hand side behaviour to call-like expressions which includes these template literals.