版本

自訂處理器

您也可以建立自訂處理器,告訴 ESLint 如何處理標準 JavaScript 以外的檔案。例如,您可以編寫自訂處理器,從 Markdown 檔案中提取和處理 JavaScript(@eslint/markdown 包含用於此目的的自訂處理器)。

自訂處理器規格

為了建立自訂處理器,從您的模組匯出的物件必須符合以下介面

const plugin = {

    meta: {
        name: "eslint-plugin-example",
        version: "1.2.3"
    },
    processors: {
        "processor-name": {
            meta: {
                name: "eslint-processor-name",
                version: "1.2.3"
            },
            // takes text of the file and filename
            preprocess(text, filename) {
                // here, you can strip out any non-JS content
                // and split into multiple strings to lint

                return [ // return an array of code blocks to lint
                    { text: code1, filename: "0.js" },
                    { text: code2, filename: "1.js" },
                ];
            },

            // takes a Message[][] and filename
            postprocess(messages, filename) {
                // `messages` argument contains two-dimensional array of Message objects
                // where each top-level array item contains array of lint messages related
                // to the text that was returned in array from preprocess() method

                // you need to return a one-dimensional array of the messages you want to keep
                return [].concat(...messages);
            },

            supportsAutofix: true // (optional, defaults to false)
        }
    }
};

// for ESM
export default plugin;

// OR for CommonJS
module.exports = plugin;

preprocess 方法接受檔案內容和檔案名稱作為引數,並傳回要進行 lint 的程式碼區塊陣列。程式碼區塊將會分開進行 lint,但仍會註冊到檔案名稱。

程式碼區塊有兩個屬性 textfilenametext 屬性是區塊的內容,而 filename 屬性是區塊的名稱。區塊的名稱可以是任何名稱,但應包含檔案副檔名,這會告訴 ESLint 如何處理目前的區塊。ESLint 檢查專案設定中符合的 files 條目,以判斷是否應對程式碼區塊進行 lint。

是否只需要傳回非 JavaScript 檔案的一部分或多個部分,取決於外掛程式。例如,在處理 .html 檔案的情況下,您可能會想要只傳回陣列中的一個項目,方法是合併所有指令碼。但是,對於 .md 檔案,您可以傳回多個項目,因為每個 JavaScript 區塊可能是獨立的。

postprocess 方法接受一個二維的 lint 訊息陣列和檔案名稱。輸入陣列中的每個項目都對應於從 preprocess 方法傳回的部分。postprocess 方法必須調整所有錯誤的位置,使其對應於原始、未處理程式碼中的位置,並將它們聚合到單一扁平化陣列中並傳回。

回報的問題在每個 lint 訊息中都包含以下位置資訊

type LintMessage = {

  /// The 1-based line number where the message occurs.
  line?: number;

   /// The 1-based column number where the message occurs.
  column?: number;

  /// The 1-based line number of the end location.
  endLine?: number;

  /// The 1-based column number of the end location.
  endColumn?: number;

  /// If `true`, this is a fatal error.
  fatal?: boolean;

  /// Information for an autofix.
  fix: Fix;

  /// The error message.
  message: string;

  /// The ID of the rule which generated the message, or `null` if not applicable.
  ruleId: string | null;

  /// The severity of the message.
  severity: 0 | 1 | 2;

  /// Information for suggestions.
  suggestions?: Suggestion[];
};

type Fix = {
    range: [number, number];
    text: string;
}

type Suggestion = {
    desc?: string;
    messageId?: string;
    fix: Fix;
}

預設情況下,即使在命令列上啟用 --fix 旗標,當使用自訂處理器時,ESLint 也不會執行自動修復。若要允許 ESLint 在使用您的處理器時自動修復程式碼,您應該採取以下額外步驟

  1. 更新 postprocess 方法以額外轉換回報問題的 fix 屬性。所有可自動修復的問題都有 fix 屬性,這是一個具有以下結構描述的物件

    {
        range: [number, number],
        text: string
    }
    

    range 屬性包含程式碼中的兩個索引,分別指向要替換的連續文字區段的開始和結束位置。text 屬性指向將替換給定範圍的文字。

    在問題的初始清單中,fix 屬性將指向已處理 JavaScript 中的修復。postprocess 方法應該轉換物件,使其指向原始、未處理檔案中的修復。

  2. supportsAutofix: true 屬性新增至處理器。

您可以在單一外掛程式中同時擁有規則和自訂處理器。您也可以在一個外掛程式中擁有多個處理器。為了支援多個副檔名,請將每個副檔名新增至 processors 元素,並將它們指向相同的物件。

如何使用 meta 物件

meta 物件可協助 ESLint 快取使用處理器的設定,並提供更友善的偵錯訊息。

外掛程式 meta 物件

外掛程式 meta 物件提供關於外掛程式本身的資訊。當使用字串格式 plugin-name/processor-name 指定處理器時,ESLint 會自動使用外掛程式 meta 來產生處理器的名稱。這是處理器最常見的情況。

範例

// eslint.config.js
import example from "eslint-plugin-example";

export default [
    {
        plugins: {
            example
        },
        processor: "example/processor-name"
    },
    // ... other configs
];

在此範例中,處理器名稱為 "example/processor-name",這將是用於序列化設定的值。

處理器 meta 物件

每個處理器也可以指定自己的 meta 物件。當處理器物件直接傳遞到設定中的 processor 時,會使用此資訊。在這種情況下,ESLint 不知道處理器屬於哪個外掛程式。meta.name 屬性應符合處理器名稱,而 meta.version 屬性應符合處理器的 npm 套件版本。完成此操作最簡單的方法是從您的 package.json 讀取此資訊。

範例

// eslint.config.js
import example from "eslint-plugin-example";

export default [
    {
        processor: example.processors["processor-name"]
    },
    // ... other configs
];

在此範例中,直接指定 example.processors["processor-name"] 會使用處理器自己的 meta 物件,必須定義該物件,以確保在未透過外掛程式名稱參考處理器時的正確處理。

為何需要兩個 Meta 物件

建議外掛程式和每個處理器都提供各自的 meta 物件。這可確保依賴 meta 物件的功能(例如 --print-config--cache)在設定中指定處理器的方式為何時都能正常運作。

在設定檔中指定處理器

為了在設定檔中使用外掛程式中的處理器,請匯入外掛程式並將其包含在 plugins 金鑰中,指定命名空間。然後,使用該命名空間在 processor 設定中參考處理器,如下所示

// eslint.config.js
import example from "eslint-plugin-example";

export default [
    {
        plugins: {
            example
        },
        processor: "example/processor-name"
    }
];

如需更多詳細資訊,請參閱外掛程式設定文件中的指定處理器

變更語言