Babel and AST - reg exp variableDeclarator
See original GitHub issueIn 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:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Dude, I love that! Sounds like the project couldn’t pass it’s own rules though 😉
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!