![](/img/trans.png)
[英]How important is to enforce the “linebreak-style” rule in ESlint? Can I just turn it off?
[英]How can I write a ESLint rule for "linebreak-style", changing depending on Windows or Unix?
眾所周知,Windows中使用的換行符(新行)通常是回車符(CR)后跟換行符(LF)即(CRLF),而Linux和Unix使用簡單的換行符(LF)
現在,就我而言,我的構建服務器使用支持 Linux 和 Unix 格式,因此,以下規則在構建服務器上完美運行:
linebreak-style: ["error", "unix"]
但我正在 Windows 上進行開發,我需要更新每個git 拉/git 推的規則,如下所示,
linebreak-style: ["error", "windows"]
那么,有什么方法可以編寫通用的換行符樣式規則來支持 Linux/Unix 和 Windows 這兩種環境?
注意:我正在使用 ECMAScript6[js]、WebStorm[ide] 進行開發
任何解決方案/建議將不勝感激。 謝謝!
我花了一些時間試圖找到如何關閉鏈接中斷樣式並由於還原我的一些代碼而丟失了它,我認為其他人也喜歡這樣做。
在.eslintrc
文件中,您還可以將linebreak-style
設置為0
以關閉換行功能:
module.exports = {
extends: 'google',
quotes: [2, 'single'],
globals: {
SwaggerEditor: false
},
env: {
browser: true
},
rules:{
"linebreak-style": 0 // <----------
}
};
eslint 配置文件可以是導出配置對象的常規.js
文件(即不是 JSON,而是帶有邏輯的完整 JS)。
這意味着您可以根據當前環境(或您能想到的任何其他 JS 邏輯)更改換linebreak-style
規則的配置。
例如,當您的節點環境為“prod”時,要使用不同的linebreak-style
配置:
module.exports = {
"root": true,
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 6
},
"rules": {
// windows linebreaks when not in production environment
"linebreak-style": ["error", process.env.NODE_ENV === 'prod' ? "unix" : "windows"]
}
};
示例用法:
$ NODE_ENV=prod node_modules/.bin/eslint src/test.js
src/test.js
1:25 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
2:30 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
3:36 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
4:26 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
5:17 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
6:50 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
7:62 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
8:21 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
✖ 8 problems (8 errors, 0 warnings)
$ NODE_ENV=dev node_modules/.bin/eslint src/test.js
$ # no errors
在您的.eslintrc.js
:
"rules": {
"linebreak-style": ["error", (process.platform === "win32" ? "windows" : "unix")], // https://stackoverflow.com/q/39114446/2771889
}
另請參閱: 如何使用 Node.js 確定當前操作系統
.eslintc 用於 Windows visualstudio 代碼
{ "env": { "node": true }, "rules":{ "linebreak-style": 0 } }
end_of_line
在.editorconfig文件中添加end_of_line規則:
[*]
end_of_line= lf
EditorConfig是當今大多數代碼編輯器的擴展,它可以更改您剛剛保存的文件的內容。 此規則強制每次開發人員保存文件時所有行結尾始終是unix 一致的( \n
)(注意: MacOs 不再使用\r
=> cr
)。 對於強制Windows行尾( \r\n
)使用crlf
。
此選項更可取,因為它避免了將所有行結束更改記錄在項目的存儲庫中(版本控制)。
正如EditorConfig 注釋所述:
如果您想在不同操作系統之間使用本機行尾,最好不要設置此選項並將該任務留給 VCS! 將來我們可能會為此場景添加一個像 native 這樣的值(參見#226 )。
這將替代接受的 answer的解決方案:
"linebreak-style": process.env.NODE_ENV === 'prod' ? "unix" : "windows"
這仍然比完全禁用規則更好( "linebreak-style": 0
):開發人員/貢獻者不一致地可能在同一個文件中使用他們喜歡的行結尾......
如果您在 Windows 上使用 Vs Code,請轉到“.eslintrc.json”文件(或“.js”,具體取決於您在設置 ESLint 時選擇的選項); 該文件通常位於項目的根文件夾中; 並在規則下添加換行選項以使用 Windows CRLF,如下所示:
"rules": {
"linebreak-style": ["error", "windows"]
}
保存文件,當您返回 JavaScript 文件時,所有那些討厭的紅線都會消失。
有一些與.eslintrc
的答案,其中一個甚至建議完全關閉該規則。
但是,我更喜歡將 VCS 設置更改為結帳LF
而不是CRLF
。 假設即使在 windows 環境中,現代 IDE 也應該可以很好地處理LF
。 缺點是像記事本這樣的一些 Windows 軟件不會喜歡它。 但是這樣,如果您的換行樣式不正確,您將收到警告。 即使他們的 VCS 配置錯誤,人們也不太可能提交不正確的換行樣式。 只需運行git config --global core.autocrlf false
或按照如何強制 git 在 Windows 下使用 LF 而不是 CR+LF 的說明進行操作?
無論如何,通常不鼓勵關閉規則,因為這會影響其他團隊成員,除非您得到團隊的所有同意。
就我而言, VS 代碼的默認設置為 line break CRLF ,因為它是新安裝的。 我不得不去改變它: Ctrl + Shift + P
打開命令面板並搜索:
更改行尾
在那里你可以將它切換到LF 。
更改換行樣式的 ESLint 規則所需的配置文件的位置可能會根據您是要更改本地、項目還是全局設置而改變,它首先搜索本地,這會覆蓋樹上更遠的那些,所以在頂部進行更改為全局向下傳播的樹
我使用了 airbnb 樣式,我的全局設置位於此處:node_modules/eslint-config-airbnb-base/rules/style.js:
如果您不確定文件的位置,您可以隨時搜索包含與設置相關的文本的文件列表,在 Linux 上查找所有具有換行設置的文件,導航到安裝 ESLint 的文件夾並使用:
grep -r linebreak
.eslintrc.js
和.prettierrc
文件中的"endOfLine": "auto"
設置對我有用。
文件.eslintrc.js
應該有:
rules: {
/* ...the other code is omitted for the brevity */
'arrow-body-style': [1, 'as-needed'],
'import/extensions': 'off',
'prettier/prettier': [
'error',
{
semi: false,
singleQuote: true,
useTabs: true,
endOfLine: 'auto', /* this setting should be included */
},
],
文件.prettierrc
應該有:
{
"semi": false,
"trailingComma": "all",
"singleQuote": true,
"useTabs": true,
"tabWidth": 2,
"endOfLine": "auto" /* this setting should be included */
}
如果你想為所有linux/unix、mac和windows設置它,只需將eslintrc.json中的linebreak-style
替換為"linebreak-style": ["error","unix | windows"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.