遷移至 v1.0.0
ESLint v1.0.0 是第一個主要版本發布。因此,ESLint 在 0.x 版本期間的運作方式與未來運作方式之間存在一些重大變更。這些變更是直接回應 ESLint 使用者社群的回饋,並且在未適當考量升級路徑的情況下制定。我們相信這些變更使 ESLint 變得更好,雖然升級需要一些工作,但我們希望這次升級的痛苦很小,您將看到升級的好處。
所有規則預設關閉
v1.0.0 中最重要的差異是所有規則預設為關閉。我們在收到許多要求允許從設定檔中關閉預設規則後,做出了此變更。雖然在技術上不可行,但預設關閉所有規則,然後使用 extends
在設定檔中重新啟用規則是可行的。因此,我們已將 --reset
行為設為預設值,並移除了此命令列選項。
當使用 --init
時,您的設定檔將自動包含以下行
{
"extends": "eslint:recommended"
}
此設定模擬了 0.x 版本中的某些預設行為,但並非全部。如果您不想使用任何建議的規則,您可以刪除此行。
要處理:如果您目前正在使用 --reset
,則應停止在命令列上傳遞 --reset
;無需其他變更。如果您未使用 --reset
,則應檢閱您的設定以確定哪些規則應預設為開啟。您可以透過將以下內容新增至您的設定檔來部分還原某些預設行為
"eslint:recommended"
設定包含許多與 0.x 版本相同的預設規則設定,但並非全部。這些規則不再預設為開啟,因此您應檢閱您的設定以確保它們仍然符合您的預期
- no-alert
- no-array-constructor
- no-caller
- no-catch-shadow
- no-empty-label
- no-eval
- no-extend-native
- no-extra-bind
- no-extra-strict
- no-implied-eval
- no-iterator
- no-label-var
- no-labels
- no-lone-blocks
- no-loop-func
- no-multi-spaces
- no-multi-str
- no-native-reassign
- no-new
- no-new-func
- no-new-object
- no-new-wrappers
- no-octal-escape
- no-process-exit
- no-proto
- no-return-assign
- no-script-url
- no-sequences
- no-shadow
- no-shadow-restricted-names
- no-spaced-func
- no-trailing-spaces
- no-undef-init
- no-underscore-dangle
- no-unused-expressions
- no-use-before-define
- no-with
- no-wrap-func
- camelcase
- comma-spacing
- consistent-return
- curly
- dot-notation
- eol-last
- eqeqeq
- key-spacing
- new-cap
- new-parens
- quotes
- semi
- semi-spacing
- space-infix-ops
- space-return-throw-case
- space-unary-ops
- strict
- yoda
另請參閱:完整差異,其中預設值已變更。
以下是一個設定檔,其中包含最接近舊預設值的等效設定
{
"extends": "eslint:recommended",
"rules": {
"no-alert": 2,
"no-array-constructor": 2,
"no-caller": 2,
"no-catch-shadow": 2,
"no-empty-label": 2,
"no-eval": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-implied-eval": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-loop-func": 2,
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-native-reassign": 2,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-new-wrappers": 2,
"no-octal-escape": 2,
"no-process-exit": 2,
"no-proto": 2,
"no-return-assign": 2,
"no-script-url": 2,
"no-sequences": 2,
"no-shadow": 2,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-trailing-spaces": 2,
"no-undef-init": 2,
"no-underscore-dangle": 2,
"no-unused-expressions": 2,
"no-use-before-define": 2,
"no-with": 2,
"camelcase": 2,
"comma-spacing": 2,
"consistent-return": 2,
"curly": [2, "all"],
"dot-notation": [2, { "allowKeywords": true }],
"eol-last": 2,
"no-extra-parens": [2, "functions"],
"eqeqeq": 2,
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"new-cap": 2,
"new-parens": 2,
"quotes": [2, "double"],
"semi": 2,
"semi-spacing": [2, {"before": false, "after": true}],
"space-infix-ops": 2,
"space-return-throw-case": 2,
"space-unary-ops": [2, { "words": true, "nonwords": false }],
"strict": [2, "function"],
"yoda": [2, "never"]
}
}
已移除的規則
在過去的幾個版本中,我們一直在棄用規則並引入新規則來取代它們。以下是已移除規則及其替代規則的清單
- generator-star 已由 generator-star-spacing 取代
- global-strict 已由 strict 取代
- no-comma-dangle 已由 comma-dangle 取代
- no-empty-class 已由 no-empty-character-class 取代
- no-extra-strict 已由 strict 取代
- no-reserved-keys 已由 quote-props 取代
- no-space-before-semi 已由 semi-spacing 取代
- no-wrap-func 已由 no-extra-parens 取代
- space-after-function-name 已由 space-before-function-paren 取代
- space-before-function-parentheses 已由 space-before-function-paren 取代
- space-in-brackets 已由object-curly-spacing 和 array-bracket-spacing 取代
- space-unary-word-ops 已由 space-unary-ops 取代
- spaced-line-comment 已由 spaced-comment 取代
要處理:您需要更新您的規則設定以使用新規則。當您使用已移除的規則時,ESLint v1.0.0 也會警告您,並建議替代規則。希望這能在升級過程中減少意外。
欄號從 1 開始
從一開始,ESLint 就使用從 0 開始的欄號回報錯誤,因為這是 Esprima,以及後來的 Espree 回報的。然而,大多數工具和編輯器都使用從 1 開始的欄號,這使得與 ESLint 的整合有些棘手。在 v1.0.0 中,我們已切換為使用從 1 開始的欄號回報錯誤,以符合開發人員每天使用的工具。
要處理:如果您已建立編輯器整合,或必須更正欄號的工具,則需要更新為僅傳遞來自 ESLint 的欄號。否則,無需變更。
不再匯出 cli
在 0.x 版本中,匯出了 cli
物件供外部工具使用。後來它被棄用,改用 CLIEngine
。在 v1.0.0 中,我們不再匯出 cli
,因為外部工具不應使用它。這將會破壞現有使用它的工具。
要處理:如果您正在使用匯出的 cli
物件,請改為切換使用 CLIEngine
。
棄用 eslint-tester
長期以來一直是 ESLint 規則主要測試器的 eslint-tester
模組,現在已移至 eslint
模組中。這是這兩個模組之間關係困難的結果,這種關係造成了循環相依性,並在規則測試中造成了很多問題。將測試器移至 eslint
模組中解決了許多這些問題。
eslint-tester
的替代品稱為 RuleTester
。它是 ESLintTester
的簡化版本,旨在與任何測試框架搭配使用。此物件由套件公開。
要處理:轉換您的所有規則測試以使用 RuleTester
。如果您有使用 ESLintTester
的測試
var eslint = require("../../../lib/eslint"),
ESLintTester = require("eslint-tester");
var eslintTester = new ESLintTester(eslint);
eslintTester.addRuleTest("lib/rules/your-rule", {
valid: [],
invalid: []
});
那麼您可以變更為
var rule = require("../../../lib/rules/your-rule"),
RuleTester = require("eslint").RuleTester;
var ruleTester = new RuleTester();
ruleTester.run("your-rule", rule, {
valid: [],
invalid: []
});