簡體   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