国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

如果尚未匯入輔助函數(shù),則新增匯入聲明:具有自訂 ESLint 規(guī)則修復(fù)程式的增強(qiáng)解決方案
P粉312195700
P粉312195700 2024-03-28 09:26:00
0
2
475

我有一個(gè)用例,我想替換變數(shù)函數(shù)調(diào)用,特別是 foo.value.toString() 來(lái)使用輔助函數(shù) getStringValue(foo) 。如果我找到它,我可以使用修復(fù)程式替換 CallExpression 節(jié)點(diǎn)上的文本,因此我的規(guī)則修復(fù)程式目前如下所示:

fixer => fixer.replaceText(node, `getStringValue(${identifierNode.getText()})`);

以這種方式自動(dòng)修復(fù)此錯(cuò)誤的問(wèn)題是 getStringValue 可能已匯入到檔案中,也可能尚未匯入到檔案中。我希望這個(gè)修復(fù)程式具有以下行為:

  1. 如果函數(shù)已匯入到檔案中,則無(wú)需執(zhí)行任何其他操作。
  2. 如果函數(shù)未導(dǎo)入,但其包含的檔案模組已導(dǎo)入,請(qǐng)將此函數(shù)新增至該模組的導(dǎo)入。
  3. 如果函數(shù)及其包含的檔案模組均未匯入,則將該模組與該函數(shù)一起匯入。

據(jù)我從文件中了解到,沒(méi)有簡(jiǎn)單的方法可以使用 fixercontext 物件來(lái)存取根 ESTree 節(jié)點(diǎn)。最接近的是 SourceCode.getText(),這意味著我必須解析原始文字才能解析導(dǎo)入 - 我寧願(yuàn)直接與整個(gè) AST 互動(dòng)。執(zhí)行此自動(dòng)導(dǎo)入過(guò)程的最佳方法是什麼?

P粉312195700
P粉312195700

全部回覆(2)
P粉596161915

如果您想在這裡稍微不安全,您可以假設(shè)使用者沒(méi)有在其文件中本地重新定義getStringValue 函數(shù)(如果您擁有此規(guī)則所適用的程式碼庫(kù),通常是一個(gè)安全的假設(shè))。

在這種情況下,最好的方法是使用選擇器來(lái)檢查導(dǎo)入,例如:

module.exports = {
  create(context) {
    let hasImport = false;
    let lastImport = null;
    return {
      ImportDeclaration(node) {
        lastImport = node;
        if (isImportICareAbout(node)) {
          hasImport = true;
        }
      },
      "My Selector For Other Linting Logic"(node) {
        // ...
        context.report({
          messageId: "myReport",
          fix(fixer) {
            const fixes = [
              fixer.replaceText(node, `getStringValue(${identifierNode.name})`),
            ];
            if (!hasImport) {
              const newImport = 'import { getStringValue } from "module";';
              if (lastImport) {
                // insert after the last import decl
                fixes.push(fixer.insertTextBefore(lastImport, newImport));
              } else {
                // insert at the start of the file
                fixes.push(fixer.insertTextAfterRange([0, 0], newImport));
              }
            }
            return fixes;
          },
        });
      },
    };
  },
};
P粉098979048

事實(shí)證明,有一個(gè)簡(jiǎn)單的方法可以從 context 物件中提取 AST 根節(jié)點(diǎn)。它位於 context.getSourceCode().ast。我用以下邏輯重寫了我的修復(fù):

fixer => {
  fixer.replaceText(node, `getStringValue(${identifierNode.getText()})`);
  const body = context.getSourceCode().ast;
  const importDeclarations = body.filter(statement => statement.type === AST_NODE_TYPES.ImportDeclaration);
  ... // Check if the declaration has the import and add the appropriate imports if necessary
}
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板