Skip to content

Commit

Permalink
Breaking: Add class options for lines-around-comment (fixes #8564)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mardak committed May 17, 2017
1 parent c70b0ed commit 950113e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
49 changes: 49 additions & 0 deletions docs/rules/lines-around-comment.md
Expand Up @@ -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:
Expand Down
34 changes: 30 additions & 4 deletions lib/rules/lines-around-comment.js
Expand Up @@ -82,6 +82,12 @@ module.exports = {
allowBlockEnd: {
type: "boolean"
},
allowClassStart: {
type: "boolean"
},
allowClassEnd: {
type: "boolean"
},
allowObjectStart: {
type: "boolean"
},
Expand Down Expand Up @@ -212,7 +218,7 @@ module.exports = {
* @returns {boolean} True if the comment is at block start.
*/
function isCommentAtBlockStart(token) {
return isCommentAtParentStart(token, "ClassBody") || isCommentAtParentStart(token, "BlockStatement") || isCommentAtParentStart(token, "SwitchCase");
return isCommentAtParentStart(token, "BlockStatement") || isCommentAtParentStart(token, "SwitchCase");
}

/**
Expand All @@ -221,7 +227,25 @@ module.exports = {
* @returns {boolean} True if the comment is at block end.
*/
function isCommentAtBlockEnd(token) {
return isCommentAtParentEnd(token, "ClassBody") || isCommentAtParentEnd(token, "BlockStatement") || isCommentAtParentEnd(token, "SwitchCase") || isCommentAtParentEnd(token, "SwitchStatement");
return 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");
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/rules/lines-around-comment.js
Expand Up @@ -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 }
},
{
Expand Down Expand Up @@ -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 }
},
Expand Down

0 comments on commit 950113e

Please sign in to comment.