Skip to content

Commit

Permalink
Fix: Change SourceCode usage deprecated node properties (fixes eslint…
Browse files Browse the repository at this point in the history
  • Loading branch information
o.drapeza committed May 13, 2020
1 parent 3d03df0 commit d85769f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/source-code/source-code.js
Expand Up @@ -349,7 +349,9 @@ class SourceCode extends TokenStore {
let currentToken = this.getTokenBefore(node, { includeComments: true });

while (currentToken && isCommentToken(currentToken)) {
if (node.parent && (currentToken.start < node.parent.start)) {

// See issue #13293, all tokens always inside Program range
if (node.parent && node.parent.type !== "Program" && (currentToken.range[0] < node.parent.range[0])) {
break;
}
comments.leading.push(currentToken);
Expand All @@ -361,7 +363,9 @@ class SourceCode extends TokenStore {
currentToken = this.getTokenAfter(node, { includeComments: true });

while (currentToken && isCommentToken(currentToken)) {
if (node.parent && (currentToken.end > node.parent.end)) {

// See issue #13293, all tokens always inside Program range
if (node.parent && node.parent.type !== "Program" && (currentToken.range[1] > node.parent.range[1])) {
break;
}
comments.trailing.push(currentToken);
Expand Down
23 changes: 22 additions & 1 deletion tests/lib/source-code/source-code.js
Expand Up @@ -950,7 +950,8 @@ describe("SourceCode", () => {
);
});

it("should return comments around nodes", () => {
// See issue #13293
it("should return comments around nodes inside a program", () => {
const code = [
"// Leading comment for VariableDeclaration",
"var a = 42;",
Expand All @@ -968,6 +969,26 @@ describe("SourceCode", () => {
assert.isEmpty(linter.verify(code, config));
});

it("should return comments around nodes inside a block", () => {
const code = [
"{",
" // Leading comment for VariableDeclaration",
" var a = 42;",
" /* Trailing comment for VariableDeclaration */",
"}"
].join("\n");

linter.defineRule("checker", () => ({
Program: assertCommentCount(0, 0),
VariableDeclaration: assertCommentCount(1, 1),
VariableDeclarator: assertCommentCount(0, 0),
Identifier: assertCommentCount(0, 0),
Literal: assertCommentCount(0, 0)
}));

assert.isEmpty(linter.verify(code, config));
});

it("should return trailing comments inside a block", () => {
const code = [
"{",
Expand Down

0 comments on commit d85769f

Please sign in to comment.