question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Private field support in babel-template

See original GitHub issue

Current Behavior An error is thrown when templates containing private fields are parsed.

Input Code

module.exports = function ({ template }) {
  const set_private = template`this.#IDENT = VALUE`;
};

Expected behavior/code I would expect to be able to correctly parse and use templates containing private fields.

Environment (fairly certain none of this matters except Babel version)

  • Babel version: 7.4.4
  • Node/npm version: Node 12
  • OS: Ubuntu 19.04
  • Monorepo: no
  • How you are using Babel: CLI

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, Jun 9, 2019

Currently it can be replaced with the following code:

const refs = t.privateName(t.identifier('refs'));

return template.ast`
	if (${expr}) {
		this.${refs}[${ref_number}].setAttribute('${attr}', '');
	} else {
		this.${refs}[${ref_number}].removeAttribute('${attr}');
	}
`;
0reactions
jhprattcommented, Jun 9, 2019

Just for reference, here’s something that could trivially be replaced with a template if/when private fields (and other context-dependent nodes) are allowed within templates.

return t.ifStatement(
    expr,
    t.blockStatement([
        t.expressionStatement(
            t.callExpression(
                t.memberExpression(
                    t.memberExpression(
                        t.memberExpression(
                            t.thisExpression(),
                            t.privateName(t.identifier('refs')),
                        ),
                        t.numericLiteral(ref_number),
                        true,
                    ),
                    t.identifier('setAttribute'),
                ),
                [t.stringLiteral(attr), t.stringLiteral('')],
            ),
        ),
    ]),
    t.blockStatement([
        t.expressionStatement(
            t.callExpression(
                t.memberExpression(
                    t.memberExpression(
                        t.memberExpression(
                            t.thisExpression(),
                            t.privateName(t.identifier('refs')),
                        ),
                        t.numericLiteral(ref_number),
                        true,
                    ),
                    t.identifier('removeAttribute'),
                ),
                [t.stringLiteral(attr)],
            ),
        ),
    ]),
);

That could be trivially replaced with the following.

return template.ast`
	if (${expr}) {
		this.#refs[${ref_number}].setAttribute('${attr}', '');
	} else {
		this.#refs[${ref_number}].removeAttribute('${attr}');
	}
`;

Needless to say the latter is far more readable and maintainable. It’s also trivial to write (I did it off the top of my head), where explicitly writing out the AST is tedious and error-prone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Babel 7.14 enables class fields & private methods by default ...
Babel 7.14 enables class fields & private methods by default in @babel/preset-env ... Babel is a free and open-source JavaScript transcompiler. It ...
Read more >
babel/template
You can use two different kinds of placeholders: syntactic placeholders (e.g. %%name%% ) or identifier placeholders (e.g. NAME ). @babel/template supports both ...
Read more >
@babel/template | Yarn - Package Manager
(@nicolo-ribaudo); #10483 [parser] Add support for private fields in TypeScript. (@nicolo-ribaudo). babel-generator , babel-parser , babel-types.
Read more >
@babel/template 7.7.0 on Node.js Yarn - NewReleases.io
(@nicolo-ribaudo); #10483 [parser] Add support for private fields in TypeScript. (@nicolo-ribaudo). babel-generator , babel-parser , babel-types.
Read more >
How to remove the surround IIFE from @babel/template?
Thanks for the help of loganfsmyth on Slack, the problem was finally resolved. I should replace the node of whole export default but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found