版本

max-statements

強制函數區塊中允許的最大語句數量

max-statements 規則允許您指定函數中允許的最大語句數量。

function foo() {
  const bar = 1; // one statement
  const baz = 2; // two statements
  const qux = 3; // three statements
}

規則詳細資訊

此規則強制函數區塊中允許的最大語句數量。

選項

此規則有一個數字或物件選項

  • "max"(預設值 10)強制函數區塊中允許的最大語句數量

已棄用: 物件屬性 maximum 已棄用;請改用物件屬性 max

此規則有一個物件選項

  • "ignoreTopLevelFunctions": true 忽略頂層函數

max

此規則的不正確程式碼範例,使用預設的 { "max": 10 } 選項

在 Playground 中開啟
/*eslint max-statements: ["error", 10]*/

function foo() {
  const foo1 = 1;
  const foo2 = 2;
  const foo3 = 3;
  const foo4 = 4;
  const foo5 = 5;
  const foo6 = 6;
  const foo7 = 7;
  const foo8 = 8;
  const foo9 = 9;
  const foo10 = 10;

  const foo11 = 11; // Too many.
}

const bar = () => {
  const foo1 = 1;
  const foo2 = 2;
  const foo3 = 3;
  const foo4 = 4;
  const foo5 = 5;
  const foo6 = 6;
  const foo7 = 7;
  const foo8 = 8;
  const foo9 = 9;
  const foo10 = 10;

  const foo11 = 11; // Too many.
};

此規則的正確程式碼範例,使用預設的 { "max": 10 } 選項

在 Playground 中開啟
/*eslint max-statements: ["error", 10]*/

function foo() {
  const foo1 = 1;
  const foo2 = 2;
  const foo3 = 3;
  const foo4 = 4;
  const foo5 = 5;
  const foo6 = 6;
  const foo7 = 7;
  const foo8 = 8;
  const foo9 = 9;
  return function () { // 10

    // The number of statements in the inner function does not count toward the
    // statement maximum.

    let bar;
    let baz;
    return 42;
  };
}

const bar = () => {
  const foo1 = 1;
  const foo2 = 2;
  const foo3 = 3;
  const foo4 = 4;
  const foo5 = 5;
  const foo6 = 6;
  const foo7 = 7;
  const foo8 = 8;
  const foo9 = 9;
  return function () { // 10

    // The number of statements in the inner function does not count toward the
    // statement maximum.

    let bar;
    let baz;
    return 42;
  };
}

請注意,此規則不適用於類別靜態區塊,且類別靜態區塊中的語句不計為封閉函數中的語句。

此規則的正確程式碼範例,使用 { "max": 2 } 選項

在 Playground 中開啟
/*eslint max-statements: ["error", 2]*/

function foo() {
    let one;
    let two = class {
        static {
            let three;
            let four;
            let five;
            if (six) {
                let seven;
                let eight;
                let nine;
            }
        }
    };
}

ignoreTopLevelFunctions

此規則的其他正確程式碼範例,使用 { "max": 10 }, { "ignoreTopLevelFunctions": true } 選項

在 Playground 中開啟
/*eslint max-statements: ["error", 10, { "ignoreTopLevelFunctions": true }]*/

function foo() {
  const foo1 = 1;
  const foo2 = 2;
  const foo3 = 3;
  const foo4 = 4;
  const foo5 = 5;
  const foo6 = 6;
  const foo7 = 7;
  const foo8 = 8;
  const foo9 = 9;
  const foo10 = 10;
  const foo11 = 11;
}

版本

此規則在 ESLint v0.0.9 中引入。

資源

變更語言