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.

Babel and AST - reg exp variableDeclarator

See original GitHub issue

In the video that introduces Babel and AST it is suggested to use t.variableDeclarator(newIdentifier, path.node), my initial idea was to do t.variableDeclarator(newIdentifier, t.regExpLiteral(path.node.pattern, path.node.flags)) instead. This would fail with following error:

TypeError: unknown: undefined is not an object (evaluating 'n.extra.raw')
genericPrintNoParens — 8-b12b22d8d7b33937e14f-12.js:1587:8752
genericPrint — 8-b12b22d8d7b33937e14f-12.js:1587:1349
r — 8-b12b22d8d7b33937e14f-12.js:1587:582
n — 8-b12b22d8d7b33937e14f-12.js:1587:522
a — 8-b12b22d8d7b33937e14f-12.js:1587:449
printComments — 8-b12b22d8d7b33937e14f-12.js:2133:3525
t — 8-b12b22d8d7b33937e14f-12.js:1587:220
call — 8-b12b22d8d7b33937e14f-12.js:4811:1780

Do you have idea why this would not work? According to docs t.regExpLiteral(pattern, flags) should be ok

My babel plugin code:

export default function (babel) {
  const { types: t } = babel;
  
  return {
    name: "regexHoister", // not required
    visitor: {
      RegExpLiteral(path) {
        const oldName = path.parent.id.name;
        const newIdentifier = path.scope.generateUidIdentifier(oldName);
        //const hoistedVar = t.variableDeclarator(newIdentifier, path.node);
        const hoistedVar = t.variableDeclarator(newIdentifier, t.regExpLiteral(path.node.pattern, path.node.flags));
        const varDeclaration = t.variableDeclaration('const', [hoistedVar]);
        const program = path.findParent(t.isProgram);

        
        path.scope.rename(oldName, newIdentifier.name)
        program.node.body.unshift(varDeclaration)
        path.parentPath.remove()
      }
    }
  };
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, Sep 18, 2017

Dude, I love that! Sounds like the project couldn’t pass it’s own rules though 😉

1reaction
kentcdoddscommented, Sep 16, 2017

Hi there!

I’m not sure… Lots of this stuff is still a bit of a mystery to me. There’s a lot to know! My guess is it could be due to the fact that you’re removing the parent node so those nodes are somehow disconnected or something. But yeah, it’s hard to tell. Sorry I couldn’t be more helpful. Good luck though!

Read more comments on GitHub >

github_iconTop Results From Across the Web

babel/types
AST Node BindExpression shape: object : Expression (required); callee : Expression (required). Aliases: Expression ...
Read more >
Understanding ASTs by Building Your Own Babel Plugin
The babel-traverse module allows you to explore, analyse and potentially modify the AST. Generate. Finally, the babel-generator module is used ...
Read more >
How to use the babel-core.types.memberExpression function ...
To help you get started, we've selected a few babel-core.types. ... concat(strings) const ast = parse(source) const replacement = transformer.run(ast, ...
Read more >
Detecting unused styles in JavaScript with `babel-traverse`
Last week, my coworker Charlie asked what it would take to ... If asked 2 years ago, I probably would have gone with...
Read more >
Learn Introducing Babel and AST - Frontend Masters
Code Transformation and Linting with ASTs Introducing Babel and AST ... So yeah, what we want to do is find any RegExp that...
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