From d85769f09db7e7161ae0ec3ecd74b66d33e1dded Mon Sep 17 00:00:00 2001 From: "o.drapeza" Date: Wed, 13 May 2020 17:57:07 +0300 Subject: [PATCH] Fix: Change SourceCode usage deprecated node properties (fixes #13293) --- lib/source-code/source-code.js | 8 ++++++-- tests/lib/source-code/source-code.js | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/source-code/source-code.js b/lib/source-code/source-code.js index 591d5a7e454e..21f9df9c4757 100644 --- a/lib/source-code/source-code.js +++ b/lib/source-code/source-code.js @@ -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); @@ -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); diff --git a/tests/lib/source-code/source-code.js b/tests/lib/source-code/source-code.js index e89f9a3283d7..6242a951e8f4 100644 --- a/tests/lib/source-code/source-code.js +++ b/tests/lib/source-code/source-code.js @@ -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;", @@ -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 = [ "{",