Navigation Menu

Skip to content

Commit

Permalink
feat: update lines-around-comment for class static blocks (#15323)
Browse files Browse the repository at this point in the history
Updates the `lines-around-comment` rule so that options `"allowBlockStart"` and `"allowBlockEnd"` apply to class static blocks.

Refs #15016
  • Loading branch information
mdjermanovic committed Nov 19, 2021
1 parent 5c64747 commit f03cd14
Show file tree
Hide file tree
Showing 3 changed files with 627 additions and 10 deletions.
85 changes: 83 additions & 2 deletions docs/rules/lines-around-comment.md
Expand Up @@ -15,8 +15,8 @@ This rule has an object option:
* `"afterBlockComment": true` requires an empty line after block comments
* `"beforeLineComment": true` requires an empty line before line comments
* `"afterLineComment": true` requires an empty line after line comments
* `"allowBlockStart": true` allows comments to appear at the start of block statements
* `"allowBlockEnd": true` allows comments to appear at the end of block statements
* `"allowBlockStart": true` allows comments to appear at the start of block statements, function bodies, classes, and class static blocks
* `"allowBlockEnd": true` allows comments to appear at the end of block statements, function bodies, classes, and class static blocks
* `"allowObjectStart": true` allows comments to appear at the start of object literals
* `"allowObjectEnd": true` allows comments to appear at the end of object literals
* `"allowArrayStart": true` allows comments to appear at the start of array literals
Expand Down Expand Up @@ -133,6 +133,25 @@ function foo(){
var day = "great"
return day;
}

if (bar) {
// what a great and wonderful day
foo();
}

class C {
// what a great and wonderful day

method() {
// what a great and wonderful day
foo();
}

static {
// what a great and wonderful day
foo();
}
}
```

Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowBlockStart": true }` options:
Expand All @@ -145,6 +164,25 @@ function foo(){
var day = "great"
return day;
}

if (bar) {
/* what a great and wonderful day */
foo();
}

class C {
/* what a great and wonderful day */

method() {
/* what a great and wonderful day */
foo();
}

static {
/* what a great and wonderful day */
foo();
}
}
```

### allowBlockEnd
Expand All @@ -159,6 +197,26 @@ function foo(){
return day;
// what a great and wonderful day
}

if (bar) {
foo();
// what a great and wonderful day
}

class C {

method() {
foo();
// what a great and wonderful day
}

static {
foo();
// what a great and wonderful day
}

// what a great and wonderful day
}
```

Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowBlockEnd": true }` option:
Expand All @@ -172,6 +230,29 @@ function foo(){

/* what a great and wonderful day */
}

if (bar) {
foo();

/* what a great and wonderful day */
}

class C {

method() {
foo();

/* what a great and wonderful day */
}

static {
foo();

/* what a great and wonderful day */
}

/* what a great and wonderful day */
}
```

### allowClassStart
Expand Down
61 changes: 54 additions & 7 deletions lib/rules/lines-around-comment.js
Expand Up @@ -185,10 +185,39 @@ module.exports = {
/**
* Returns the parent node that contains the given token.
* @param {token} token The token to check.
* @returns {ASTNode} The parent node that contains the given token.
* @returns {ASTNode|null} The parent node that contains the given token.
*/
function getParentNodeOfToken(token) {
return sourceCode.getNodeByRangeIndex(token.range[0]);
const node = sourceCode.getNodeByRangeIndex(token.range[0]);

/*
* For the purpose of this rule, the comment token is in a `StaticBlock` node only
* if it's inside the braces of that `StaticBlock` node.
*
* Example where this function returns `null`:
*
* static
* // comment
* {
* }
*
* Example where this function returns `StaticBlock` node:
*
* static
* {
* // comment
* }
*
*/
if (node && node.type === "StaticBlock") {
const openingBrace = sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token

return token.range[0] >= openingBrace.range[0]
? node
: null;
}

return node;
}

/**
Expand All @@ -200,8 +229,15 @@ module.exports = {
function isCommentAtParentStart(token, nodeType) {
const parent = getParentNodeOfToken(token);

return parent && isParentNodeType(parent, nodeType) &&
token.loc.start.line - parent.loc.start.line === 1;
if (parent && isParentNodeType(parent, nodeType)) {
const parentStartNodeOrToken = parent.type === "StaticBlock"
? sourceCode.getFirstToken(parent, { skip: 1 }) // opening brace of the static block
: parent;

return token.loc.start.line - parentStartNodeOrToken.loc.start.line === 1;
}

return false;
}

/**
Expand All @@ -213,7 +249,7 @@ module.exports = {
function isCommentAtParentEnd(token, nodeType) {
const parent = getParentNodeOfToken(token);

return parent && isParentNodeType(parent, nodeType) &&
return !!parent && isParentNodeType(parent, nodeType) &&
parent.loc.end.line - token.loc.end.line === 1;
}

Expand All @@ -223,7 +259,12 @@ 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, "ClassBody") ||
isCommentAtParentStart(token, "BlockStatement") ||
isCommentAtParentStart(token, "StaticBlock") ||
isCommentAtParentStart(token, "SwitchCase")
);
}

/**
Expand All @@ -232,7 +273,13 @@ 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, "ClassBody") ||
isCommentAtParentEnd(token, "BlockStatement") ||
isCommentAtParentEnd(token, "StaticBlock") ||
isCommentAtParentEnd(token, "SwitchCase") ||
isCommentAtParentEnd(token, "SwitchStatement")
);
}

/**
Expand Down

0 comments on commit f03cd14

Please sign in to comment.