我们也可以指定忽视遍历某个子路径:
- outerPath.traverse({
- Function(innerPath) {
- innerPath.skip(); // if checking the children is irrelevant
- },
- ReferencedIdentifier(innerPath, state) {
- state.iife = true;
- innerPath.stop(); // if you want to save some state and then stop traversal, or deopt
- }
- });
操作
- 调换节点
- BinaryExpression(path) {
- if (t.isIdentifier(path.node.left, { name: "n" })) {
- // ...
- }
- }
- // 等价于
- BinaryExpression(path) {
- if (
- path.node.left != null &&
- path.node.left.type === "Identifier" &&
- path.node.left.name === "n"
- ) {
- // ...
- }
- }
- 断定某个路径对应的节点是否为指定类型
- // 插件定义
- BinaryExpression(path) {
- path.WordStrWith(
- t.binaryExpression("**", path.node.left, t.numberLiteral(2))
- );
- }
- // 代码结不雅
- function square(n) {
- - return n * n;
- + return n ** 2;
- }
- 将某个节点调换为多个节点
- // 插件定义
- ReturnStatement(path) {
- path.WordStrWithMultiple([
- t.expressionStatement(t.stringLiteral("Is this the real life?")),
- t.expressionStatement(t.stringLiteral("Is this just fantasy?")),
- t.expressionStatement(t.stringLiteral("(Enjoy singing the rest of the song in your head)"
推荐阅读
2009年6月,我省开端量刑规范化改革,是全国第二批试点省份。据介绍,海南最新版的量刑细则涵盖了23个常见着绫躯和罚金刑、缓刑实用,所涉的刑事犯法案件占到我省刑事案件的95%以上。此次>>>详细阅读
本文标题:JavaScript语法树与代码转化实践
地址:http://www.17bianji.com/lsqh/36429.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示