diff --git a/docs/rules/lines-around-comment.md b/docs/rules/lines-around-comment.md index f0b5c22576a9..d772428af1ea 100644 --- a/docs/rules/lines-around-comment.md +++ b/docs/rules/lines-around-comment.md @@ -172,6 +172,55 @@ function foo(){ } ``` +### allowClassStart + +Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowClassStart": true }` option: + +```js +/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": true }]*/ + +class foo { + // what a great and wonderful day + day() {"great"} +}; +``` + +Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowClassStart": true }` option: + +```js +/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": true }]*/ + +class foo { + /* what a great and wonderful day */ + day() {"great"} +}; +``` + +### allowClassEnd + +Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowClassEnd": true }` option: + +```js +/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowClassEnd": true }]*/ + +class foo { + day() {"great"} + // what a great and wonderful day +}; +``` + +Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowClassEnd": true }` option: + +```js +/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowClassEnd": true }]*/ + +class foo { + day() {"great"} + + /* what a great and wonderful day */ +}; +``` + ### allowObjectStart Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowObjectStart": true }` option: diff --git a/lib/rules/lines-around-comment.js b/lib/rules/lines-around-comment.js index 5b4cd8ebe92c..e7f403405a27 100644 --- a/lib/rules/lines-around-comment.js +++ b/lib/rules/lines-around-comment.js @@ -82,6 +82,12 @@ module.exports = { allowBlockEnd: { type: "boolean" }, + allowClassStart: { + type: "boolean" + }, + allowClassEnd: { + type: "boolean" + }, allowObjectStart: { type: "boolean" }, @@ -224,6 +230,24 @@ module.exports = { return isCommentAtParentEnd(token, "ClassBody") || isCommentAtParentEnd(token, "BlockStatement") || isCommentAtParentEnd(token, "SwitchCase") || isCommentAtParentEnd(token, "SwitchStatement"); } + /** + * Returns whether or not comments are at the class start or not. + * @param {token} token The Comment token. + * @returns {boolean} True if the comment is at class start. + */ + function isCommentAtClassStart(token) { + return isCommentAtParentStart(token, "ClassBody"); + } + + /** + * Returns whether or not comments are at the class end or not. + * @param {token} token The Comment token. + * @returns {boolean} True if the comment is at class end. + */ + function isCommentAtClassEnd(token) { + return isCommentAtParentEnd(token, "ClassBody"); + } + /** * Returns whether or not comments are at the object start or not. * @param {token} token The Comment token. @@ -286,13 +310,15 @@ module.exports = { const blockStartAllowed = options.allowBlockStart && isCommentAtBlockStart(token), blockEndAllowed = options.allowBlockEnd && isCommentAtBlockEnd(token), + classStartAllowed = options.allowClassStart && isCommentAtClassStart(token), + classEndAllowed = options.allowClassEnd && isCommentAtClassEnd(token), objectStartAllowed = options.allowObjectStart && isCommentAtObjectStart(token), objectEndAllowed = options.allowObjectEnd && isCommentAtObjectEnd(token), arrayStartAllowed = options.allowArrayStart && isCommentAtArrayStart(token), arrayEndAllowed = options.allowArrayEnd && isCommentAtArrayEnd(token); - const exceptionStartAllowed = blockStartAllowed || objectStartAllowed || arrayStartAllowed; - const exceptionEndAllowed = blockEndAllowed || objectEndAllowed || arrayEndAllowed; + const exceptionStartAllowed = blockStartAllowed || classStartAllowed || objectStartAllowed || arrayStartAllowed; + const exceptionEndAllowed = blockEndAllowed || classEndAllowed || objectEndAllowed || arrayEndAllowed; // ignore top of the file and bottom of the file if (prevLineNum < 1) { diff --git a/tests/lib/rules/lines-around-comment.js b/tests/lib/rules/lines-around-comment.js index 9b2aac00e915..1a15d2fd1377 100644 --- a/tests/lib/rules/lines-around-comment.js +++ b/tests/lib/rules/lines-around-comment.js @@ -221,7 +221,7 @@ ruleTester.run("lines-around-comment", rule, { }, { code: "class A {\n/**\n* hi\n */\nconstructor() {}\n}", - options: [{ allowBlockStart: true }], + options: [{ allowClassStart: true }], parserOptions: { ecmaVersion: 6 } }, { @@ -414,7 +414,7 @@ ruleTester.run("lines-around-comment", rule, { code: "class B {\nconstructor() {}\n\n/**\n* hi\n */\n}", options: [{ afterBlockComment: true, - allowBlockEnd: true + allowClassEnd: true }], parserOptions: { ecmaVersion: 6 } },