lines-around-comment (Rules) – Eslint 中文開發手冊
lines-around-comment (Rules) - Eslint 中文開發手冊
在--fix命令列上的選項可以自動修復一些被這條規則反映的問題。
許多風格指南在評論之前或之後需要空行。這些規則的主要目標是使評論更易於閱讀並提高程式碼的可讀性。
規則細節
此規則在評論之前和/或之後需要空行。它可以單獨為block(/*)和line(//)註釋啟用。此規則不適用於與程式碼顯示在同一行上的註釋,並且不需要在檔案的開頭或結尾處出現空行。
選項
該規則有一個物件選項:
"beforeBlockComment": true (預設)在塊註釋之前需要一個空行"afterBlockComment": true 塊註釋後需要一個空行"beforeLineComment": true 行註釋前需要一個空行"afterLineComment": true 行註釋後需要一個空行"allowBlockStart": true 允許評論出現在塊語句的開始處"allowBlockEnd": true 允許註釋出現在塊語句的末尾"allowObjectStart": true 允許評論出現在物件文字的開頭"allowObjectEnd": true 允許註釋出現在物件文字的末尾"allowArrayStart": true 允許註釋出現在陣列文字的開頭"allowArrayEnd": true 允許註釋出現在陣列文字的末尾"allowClassStart": true 允許評論出現在課程開始時"allowClassEnd": true 允許評論出現在課程結束時"applyDefaultIgnorePatterns" 啟用或禁用規則忽略的預設評論模式"ignorePattern" 自定義模式被規則忽略
beforeBlockComment
此規則的預設程式碼錯誤程式碼示例{ "beforeBlockComment": true }:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/ var night = "long"; /* what a great and wonderful day */ var day = "great"
具有預設選項的此規則的正確程式碼示例{ "beforeBlockComment": true }:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/ var night = "long"; /* what a great and wonderful day */ var day = "great"
afterBlockComment
此規則的錯誤程式碼示例包含以下{ "afterBlockComment": true }選項:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/ var night = "long"; /* what a great and wonderful day */ var day = "great"
此規則的正確程式碼示例包含以下{ "afterBlockComment": true }選項:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/ var night = "long"; /* what a great and wonderful day */ var day = "great"
beforeLineComment
此規則的錯誤程式碼示例包含以下{ "beforeLineComment": true }選項:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/ var night = "long"; // what a great and wonderful day var day = "great"
此規則的正確程式碼示例包含以下{ "beforeLineComment": true }選項:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/ var night = "long"; // what a great and wonderful day var day = "great"
afterLineComment
此規則的錯誤程式碼示例包含以下{ "afterLineComment": true }選項:
/*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/ var night = "long"; // what a great and wonderful day var day = "great"
此規則的正確程式碼示例包含以下{ "afterLineComment": true }選項:
/*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/ var night = "long"; // what a great and wonderful day var day = "great"
allowBlockStart
此規則的正確程式碼示例包含以下{ "beforeLineComment": true, "allowBlockStart": true }選項:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowBlockStart": true }]*/ function foo(){ // what a great and wonderful day var day = "great" return day; }
此規則的正確程式碼示例包含以下{ "beforeBlockComment": true, "allowBlockStart": true }選項:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowBlockStart": true }]*/ function foo(){ /* what a great and wonderful day */ var day = "great" return day; }
allowBlockEnd
此規則的正確程式碼示例包含以下{ "afterLineComment": true, "allowBlockEnd": true }選項:
/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowBlockEnd": true }]*/ function foo(){ var day = "great" return day; // what a great and wonderful day }
此規則的正確程式碼示例包含以下{ "afterBlockComment": true, "allowBlockEnd": true }選項:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowBlockEnd": true }]*/ function foo(){ var day = "great" return day; /* what a great and wonderful day */ }
allowClassStart
此規則的錯誤程式碼示例包含以下{ "beforeLineComment": true, "allowClassStart": false }選項:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/ class foo { // what a great and wonderful day day() {} };
此規則的正確程式碼示例包含以下{ "beforeLineComment": true, "allowClassStart": false }選項:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/ class foo { // what a great and wonderful day day() {} };
此規則的正確程式碼示例包含以下{ "beforeLineComment": true, "allowClassStart": true }選項:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": true }]*/ class foo { // what a great and wonderful day day() {} };
此規則的錯誤程式碼示例包含以下{ "beforeBlockComment": true, "allowClassStart": false }選項:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": false }]*/ class foo { /* what a great and wonderful day */ day() {} };
此規則的正確程式碼示例包含以下{ "beforeBlockComment": true, "allowClassStart": false }選項:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": false }]*/ class foo { /* what a great and wonderful day */ day() {} };
此規則的正確程式碼示例包含以下{ "beforeBlockComment": true, "allowClassStart": true }選項:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": true }]*/ class foo { /* what a great and wonderful day */ day() {} };
allowClassEnd
此規則的正確程式碼示例包含以下{ "afterLineComment": true, "allowClassEnd": true }選項:
/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowClassEnd": true }]*/ class foo { day() {} // what a great and wonderful day };
此規則的正確程式碼示例包含以下{ "afterBlockComment": true, "allowClassEnd": true }選項:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowClassEnd": true }]*/ class foo { day() {} /* what a great and wonderful day */ };
allowObjectStart
此規則的正確程式碼示例包含以下{ "beforeLineComment": true, "allowObjectStart": true }選項:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowObjectStart": true }]*/ var foo = { // what a great and wonderful day day: "great" }; const { // what a great and wonderful day foo: someDay } = {foo: "great"}; const { // what a great and wonderful day day } = {day: "great"};
此規則的正確程式碼示例包含以下{ "beforeBlockComment": true, "allowObjectStart": true }選項:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowObjectStart": true }]*/ var foo = { /* what a great and wonderful day */ day: "great" }; const { /* what a great and wonderful day */ foo: someDay } = {foo: "great"}; const { /* what a great and wonderful day */ day } = {day: "great"};
allowObjectEnd
此規則的正確程式碼示例包含以下{ "afterLineComment": true, "allowObjectEnd": true }選項:
/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowObjectEnd": true }]*/ var foo = { day: "great" // what a great and wonderful day }; const { foo: someDay // what a great and wonderful day } = {foo: "great"}; const { day // what a great and wonderful day } = {day: "great"};
此規則的正確程式碼示例包含以下{ "afterBlockComment": true, "allowObjectEnd": true }選項:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowObjectEnd": true }]*/ var foo = { day: "great" /* what a great and wonderful day */ }; const { foo: someDay /* what a great and wonderful day */ } = {foo: "great"}; const { day /* what a great and wonderful day */ } = {day: "great"};
allowArrayStart
此規則的正確程式碼示例包含以下{ "beforeLineComment": true, "allowArrayStart": true }選項:
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowArrayStart": true }]*/ var day = [ // what a great and wonderful day "great", "wonderful" ]; const [ // what a great and wonderful day someDay ] = ["great", "not great"];
此規則的正確程式碼示例包含以下{ "beforeBlockComment": true, "allowArrayStart": true }選項:
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowArrayStart": true }]*/ var day = [ /* what a great and wonderful day */ "great", "wonderful" ]; const [ /* what a great and wonderful day */ someDay ] = ["great", "not great"];
allowArrayEnd
此規則的正確程式碼示例包含以下{ "afterLineComment": true, "allowArrayEnd": true }選項:
/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowArrayEnd": true }]*/ var day = [ "great", "wonderful" // what a great and wonderful day ]; const [ someDay // what a great and wonderful day ] = ["great", "not great"];
此規則的正確程式碼示例包含以下{ "afterBlockComment": true, "allowArrayEnd": true }選項:
/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowArrayEnd": true }]*/ var day = [ "great", "wonderful" /* what a great and wonderful day */ ]; const [ someDay /* what a great and wonderful day */ ] = ["great", "not great"];
ignorePattern
預設情況下,這個規則忽略先從下面的話評論:eslint,jshint,jslint,istanbul,global,exported,jscs。可以提供一個替代的正則表示式。
選項的正確程式碼示例ignorePattern:
/*eslint lines-around-comment: ["error"]*/ foo(); /* eslint mentioned in this comment */, bar(); /*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */ foo(); /* a valid comment using pragma in it */
選項的錯誤程式碼示例ignorePattern:
/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */ 1 + 1; /* something else */
applyDefaultIgnorePatterns
即使ignorePattern提供預設忽略模式也會應用。如果您想省略預設模式,請將此選項設定為false。
選項的正確程式碼示例{ "applyDefaultIgnorePatterns": false }:
/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma", applyDefaultIgnorePatterns: false }] */ foo(); /* a valid comment using pragma in it */
選項的錯誤程式碼示例{ "applyDefaultIgnorePatterns": false }:
/*eslint lines-around-comment: ["error", { "applyDefaultIgnorePatterns": false }] */ foo(); /* eslint mentioned in comment */
何時不使用它
許多人都喜歡一種比較特別的程式碼風格,不介意程式碼碰撞的評論。如果你屬於這個類別,這條規則不適合你。
相關規則
space-before-blocksspaced-comment
版本
這條規則是在 ESLint 0.22.0 中引入的。
資源
Rule sourceDocumentation source
Eslint 中文開發手冊 ]