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

Tokens arrays of tagged template literals not linked to the tagged template #562

Open
overlookmotel opened this issue Dec 14, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@overlookmotel
Copy link
Owner

From MDN:

For any particular tagged template literal expression, the tag function will always be called with the exact same literal array, no matter how many times the literal is evaluated.

Input:

function tag(tokens) {
  return tokens;
}

function runTag() {
  return tag`a${123}b${456}c`;
}

const tokens = runTag();

export default function compare() {
  return runTag() === tokens;
}

In the source code, compare() returns true. But in output it returns false because Livepack does not understand the link between tokens and the template literal in runTag().

Output:

const Object$0 = Object,
  ObjectFreeze = Object$0.freeze;
export default (
  (tokens, tag, runTag$0) => (
    runTag$0 = function runTag() {
      return tag`a${123}b${456}c`;
    },
    function compare() {
      return runTag$0() === tokens;
    }
  )
)(
  ObjectFreeze(
    Object$0.defineProperties(
      ["a", "b", "c"],
      { raw: { value: ObjectFreeze(["a", "b", "c"]) } }
    )
  ),
  function tag(tokens) {
    return tokens;
  }
);

This would be annoying to solve.

Instrumentation would need to replace the tag function with a wrapper which records:

  • the tokens array
  • the raw property of the tokens array
  • the function which the tagged template is in

If the tokens array/raw tokens array and the function are both included in output, it'd need to rewrite the runTag function as:

const tokens = (t => t)`a${0}b${0}c`;
function runTag() {
  // Was: tag`a${123}b${456}c`
  return tag(tokens, 123, 456);
}
@overlookmotel overlookmotel added the bug Something isn't working label Dec 14, 2023
@overlookmotel
Copy link
Owner Author

overlookmotel commented Dec 14, 2023

NB: The tokens array is lexically bound to the tagged template.

const tag = tokens => tokens;
const makeRunTag = () => () => tag`a${1}b${2}c`;
const tokens1 = makeRunTag()();
const tokens2 = makeRunTag()();

console.log(tokens1 === tokens2); // true

i.e. A single tagged template in source code corresponds to a single tokens array, regardless of how many function instances it's in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant