GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Tagged template literals support by hazzard993 · Pull Request #638 · TypeScriptToLua/TypeScriptToLua

Conversation

@hazzard993

@hazzard993 hazzard993 commented Jun 20, 2019

Copy link
Copy Markdown
Contributor

Closes #611

Raw strings are usable too.

Equivalent output code does look hard to read.

function func(strings: TemplateStringsArray, ...expressions: any[]) {}

func`\u00A9 ${4}`;
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
function func(self, strings, ...)
end
func(_G, {
    "© ",
    "",
    raw = {
        "\\u00A9 ",
        "",
    },
}, 4)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
hazzard993 added 4 commits June 20, 2019 21:58
@hazzard993
Implement Tagged Template Literals
9b9b4a2
@hazzard993
Merge remote-tracking branch 'upstream/master' into tagged-template-l…
c949e25
…iterals
@hazzard993
Add unicode escape test
742aa9d
@hazzard993
Keep track of raw strings
51ed012
tomblind
tomblind requested changes Jun 20, 2019
View reviewed changes

@tomblind tomblind left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

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.

I did a quick test and stumbled on the fact that if the tag function's first argument is not typed as TemplateStringsArray (could be any[], string[], etc...), getResolvedSignature fails and so no context parameter is passed to the function in the resulting lua. I'm not sure off the top of my head how to best handle that, but we'll need to do something or the code breaks at runtime.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/LuaTransformer.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/LuaTransformer.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@tomblind

tomblind commented Jun 20, 2019

Copy link
Copy Markdown
Collaborator

I did some more tests:

Works:

  • TemplateStringsArray
  • readonly string[]
  • ReadonlyArray<string>
  • any
  • unknown

Doesn't work:

  • string[]
  • any[]

So it seems getResolvedSignature fails if the type is a non-readonly array type.

Perhaps we should just require the type of that parameter to be TemplateStringsArray to keep things simple?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ark120202

ark120202 commented Jun 20, 2019

Copy link
Copy Markdown
Contributor

Turns out getResolvedSignature resolves the correct signature there when called from a non-diagnostic-producing type checker. It is caused by a slight difference between them: microsoft/TypeScript#28584 (comment). This difference was removed in microsoft/TypeScript#28564, but it doesn't seem like it's going to get merged any time soon. For now I don't think there's need to do anything - user would get a semantic error in this case anyway.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tomblind

tomblind commented Jun 20, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

I think we should do something since the result right now is bad lua being silently generated.
Nevermind. I am wrong. Ignore me 😄

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hazzard993
Improve raw string preservation and use type narrowing
bf24fbc
ark120202
ark120202 reviewed Jun 21, 2019
View reviewed changes
Comment thread src/LuaTransformer.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@hazzard993
Simplfy method using TypeScript's example
cd147fb
@hazzard993

hazzard993 commented Jun 23, 2019

Copy link
Copy Markdown
Contributor Author

Is there still anything weird to fix up with the getResolvedSignature issue?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hazzard993 added 2 commits June 23, 2019 22:36
@hazzard993
Merge remote-tracking branch 'upstream/master' into tagged-template-l…
517d998
…iterals
@hazzard993
Fix ContextType reference
7357109
ark120202
ark120202 reviewed Jun 23, 2019
View reviewed changes
Comment thread src/LuaTransformer.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@tomblind

tomblind commented Jun 23, 2019

Copy link
Copy Markdown
Collaborator

getResolvedSignature should be fine - turns out an error is thrown by TS if the wrong type is attempted to be used, and I just wasn't seeing it when I was testing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hazzard993 added 2 commits June 23, 2019 23:12
@hazzard993
Remove unnecessary check
33dbac4
@hazzard993
Reduce checks and use filterUndefinedAndCast
016a039
ark120202
ark120202 reviewed Jun 26, 2019
View reviewed changes
Comment thread test/unit/taggedTemplateLiterals.spec.ts
joinRawResult: "hello \\`",
},
{
callExpression: "obj.func`hello ${'propertyAccessExpression'}`",

@ark120202 ark120202 Jun 26, 2019

Copy link
Copy Markdown
Contributor

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:

const obj = {
    func(strings: TemplateStringsArray, ...expressions: any[]) {
        console.log(this === obj); // `true` in JS, `false` in Lua
    },
};

obj.func`hello`;
local obj = {func = function(self, strings, ...)
    print(self == obj)
end}
obj.func(_G, { -- should be `obj`
    "hello",
    raw = {"hello"},
})

Also there is a case where obj isn't pure:

getObj()["func"]`hello`;

In call expressions it's handled like that:

(function()
    local ____TS_self = getObj(_G)
    return ____TS_self.func(____TS_self, {
        "hello",
        raw = {"hello"},
    })
end)()

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@hazzard993 hazzard993 Jul 1, 2019

Copy link
Copy Markdown
Contributor Author

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 transformContextualCallExpression in an attempt to preserve this left hand side behaviour to call-like expressions which includes these template literals.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
hazzard993 added 2 commits July 1, 2019 16:41
@hazzard993
Add transformContextualCallExpression to transform call expression le…
5158042
…ft hand side
@hazzard993
Merge remote-tracking branch 'upstream/master' into tagged-template-l…
0bd8b65
…iterals
tomblind
tomblind approved these changes Jul 2, 2019
View reviewed changes
ark120202
ark120202 approved these changes Jul 2, 2019
View reviewed changes
@Perryvw
Perryvw merged commit eb7ae08 into TypeScriptToLua:master Jul 2, 2019
@hazzard993
hazzard993 deleted the tagged-template-literals branch July 2, 2019 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tomblind tomblind tomblind approved these changes
+1 more reviewer
@ark120202 ark120202 ark120202 approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Feature request: Tagged template literals

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@hazzard993 @tomblind @ark120202 @Perryvw
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.