Skip to content
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

Tagged template has been broken since v20200315 #3589

Closed
teppeis opened this issue May 4, 2020 · 2 comments
Closed

Tagged template has been broken since v20200315 #3589

teppeis opened this issue May 4, 2020 · 2 comments
Assignees

Comments

@teppeis
Copy link
Contributor

teppeis commented May 4, 2020

Input:

function tag(args) {
  console.log(args);
}
tag`aaa`;

Output (v20200426, SIMPLE):

var $jscomp$templatelit$2052066224$0 = $jscomp.createTemplateTagFirstArg(["aaa"]), $jscomp = $jscomp || {};
$jscomp.scope = {};
$jscomp.createTemplateTagFirstArg = function(a) {
  return a.raw = a;
};
$jscomp.createTemplateTagFirstArgWithRaw = function(a, b) {
  a.raw = b;
  return a;
};
function tag(a) {
  console.log(a);
}
tag($jscomp$templatelit$2052066224$0);

Runtime error (in Node v12.16.3):

var $jscomp$templatelit$2052066224$0 = $jscomp.createTemplateTagFirstArg(["aaa"]), $jscomp = $jscomp || {};
                                               ^

TypeError: Cannot read property 'createTemplateTagFirstArg' of undefined
    at Object.<anonymous> (dist/bundle.js:1:48)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47

Correct output (v20200224):

var $jscomp$templatelit$0 = ["aaa"];
$jscomp$templatelit$0.raw = $jscomp$templatelit$0.slice();
function tag(a) {
  console.log(a);
}
tag($jscomp$templatelit$0);
@brad4d
Copy link
Contributor

brad4d commented May 4, 2020

I think I know what's going on here.

  1. The definitions of $jscomp and its methods are automatically injected at the start of the first input script / module.
  2. When we transpile the template tag, we must declare the template tag arguments at the script / module level, so we put the declaration at the top of the current script / module.
  3. Unfortunately, the tag is in the first script / module, so we end up putting the declaration before the definitions it depends on.

I believe the best fix is instead to traverse up the AST from the tagged template to find the module - level statement containing it and insert the declaration at that point instead.

Rishi, would you mind making that change?

@rishipal
Copy link
Contributor

rishipal commented May 5, 2020

Fixed with cedbad5

@rishipal rishipal closed this as completed May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants