Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update lines-around-comment for class static blocks #15323

Merged
merged 1 commit into from Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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