繁体   English   中英

JS创建babel插件:如何获取匿名函数的参数

[英]JS creating babel plugin: how to get arguments of anonymous function

我正在尝试创建一个插件,该插件在转译并返回其参数时检测功能的执行

例如:

码:

testFN("hello");
testFN("world");

将在babel转译时返回:

hello
world

我创建了一个babel插件,可以检测该函数并输出其参数,但是无法在其中找到参数

所以目前看起来像这样

module.exports = function ({ types: t }) {
  return {
    visitor: {
      Identifier(path) {
        if (path.node.name === 'testFN') {
          console.log(path.node);
        }
      },
    },
  };
};

它输出:

Node {
  type: 'Identifier',
  start: 753,
  end: 759,
  loc:
   SourceLocation {
     start: Position { line: 19, column: 8 },
     end: Position { line: 19, column: 14 },
     identifierName: 'testFN' },
  name: 'testFN' }
Node {
  type: 'Identifier',
  start: 830,
  end: 836,
  loc:
   SourceLocation {
     start: Position { line: 23, column: 2 },
     end: Position { line: 23, column: 8 },
     identifierName: 'testFN' },
  name: 'testFN' }

我尝试使用AST浏览器,但是它提供了我在babel插件代码中无法到达的不同对象路径https://astexplorer.net/#/gist/763d13950ad0334ac8ea3187464fcdbf/295a8fd8640210cee586444aa58445401e8baa690

如何通过babel访问我的函数的参数?

谢谢

愚蠢的我-试图通过标识符获取参数。 我需要通过CallExpression来获得它。 干杯

码:

// eslint-disable-next-line func-names
module.exports = function ({ types: t }) {
  return {
    visitor: {
      CallExpression(path, { file }) {
        if (path.node.callee.name === "testFN") {
          console.log(path.node.arguments[)
        }
      },
    },
  };
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM