Skip to content

Commit

Permalink
Fix: getNameLocationInGlobalDirectiveComment end location (refs #12334)…
Browse files Browse the repository at this point in the history
… (#13086)
  • Loading branch information
mdjermanovic committed Mar 28, 2020
1 parent ae14a02 commit 920465b
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 22 deletions.
8 changes: 7 additions & 1 deletion lib/rules/utils/ast-utils.js
Expand Up @@ -1468,11 +1468,17 @@ module.exports = {
const match = namePattern.exec(comment.value);

// Convert the index to loc.
return sourceCode.getLocFromIndex(
const start = sourceCode.getLocFromIndex(
comment.range[0] +
"/*".length +
(match ? match.index + 1 : 0)
);
const end = {
line: start.line,
column: start.column + (match ? name.length : 1)
};

return { start, end };
},

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -3816,7 +3816,9 @@ describe("Linter", () => {
messages,
[{
column: 30,
endColumn: 33,
line: 2,
endLine: 2,
message: "'aaa' is already defined by a variable declaration.",
messageId: "redeclaredBySyntax",
nodeType: "Block",
Expand All @@ -3838,7 +3840,9 @@ describe("Linter", () => {
messages,
[{
column: 31,
endColumn: 34,
line: 2,
endLine: 2,
message: "'aaa' is already defined by a variable declaration.",
messageId: "redeclaredBySyntax",
nodeType: "Block",
Expand Down
225 changes: 204 additions & 21 deletions tests/lib/rules/no-redeclare.js
Expand Up @@ -145,16 +145,26 @@ ruleTester.run("no-redeclare", rule, {
{
code: "/*global b:false*/ var b = 1;",
options: [{ builtinGlobals: true }],
errors: [
{ message: "'b' is already defined by a variable declaration.", type: "Block" }
]
errors: [{
message: "'b' is already defined by a variable declaration.",
type: "Block",
line: 1,
column: 10,
endLine: 1,
endColumn: 11
}]
},
{
code: "/*global b:true*/ var b = 1;",
options: [{ builtinGlobals: true }],
errors: [
{ message: "'b' is already defined by a variable declaration.", type: "Block" }
]
errors: [{
message: "'b' is already defined by a variable declaration.",
type: "Block",
line: 1,
column: 10,
endLine: 1,
endColumn: 11
}]
},
{
code: "function f() { var a; var a; }",
Expand Down Expand Up @@ -305,40 +315,213 @@ ruleTester.run("no-redeclare", rule, {
{
code: "/*globals Array */",
options: [{ builtinGlobals: true }],
errors: [
{ message: "'Array' is already defined as a built-in global variable.", type: "Block" }
]
errors: [{
message: "'Array' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 11,
endLine: 1,
endColumn: 16
}]
},
{
code: "/*globals parseInt */",
options: [{ builtinGlobals: true }],
errors: [{
message: "'parseInt' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 11,
endLine: 1,
endColumn: 19
}]
},
{
code: "/*globals foo, Array */",
options: [{ builtinGlobals: true }],
errors: [{
message: "'Array' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 16,
endLine: 1,
endColumn: 21
}]
},
{
code: "/* globals foo, Array, baz */",
options: [{ builtinGlobals: true }],
errors: [{
message: "'Array' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 17,
endLine: 1,
endColumn: 22
}]
},
{
code: "/*global foo, Array, baz*/",
options: [{ builtinGlobals: true }],
errors: [{
message: "'Array' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 15,
endLine: 1,
endColumn: 20
}]
},
{
code: "/*global array, Array*/",
options: [{ builtinGlobals: true }],
errors: [{
message: "'Array' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 17,
endLine: 1,
endColumn: 22
}]
},
{
code: "/*globals a,Array*/",
options: [{ builtinGlobals: true }],
errors: [{
message: "'Array' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 13,
endLine: 1,
endColumn: 18
}]
},
{
code: "/*globals a:readonly, Array:writable */",
options: [{ builtinGlobals: true }],
errors: [{
message: "'Array' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 23,
endLine: 1,
endColumn: 28
}]
},
{
code: "\n/*globals Array */",
options: [{ builtinGlobals: true }],
errors: [{
message: "'Array' is already defined as a built-in global variable.",
type: "Block",
line: 2,
column: 11,
endLine: 2,
endColumn: 16
}]
},
{
code: "/*globals\nArray */",
options: [{ builtinGlobals: true }],
errors: [{
message: "'Array' is already defined as a built-in global variable.",
type: "Block",
line: 2,
column: 1,
endLine: 2,
endColumn: 6
}]
},
{
code: "\n/*globals\n\nArray*/",
options: [{ builtinGlobals: true }],
errors: [{
message: "'Array' is already defined as a built-in global variable.",
type: "Block",
line: 4,
column: 1,
endLine: 4,
endColumn: 6
}]
},
{
code: "/*globals foo,\n Array */",
options: [{ builtinGlobals: true }],
errors: [{
message: "'Array' is already defined as a built-in global variable.",
type: "Block",
line: 2,
column: 5,
endLine: 2,
endColumn: 10
}]
},
{
code: "/*globals a */",
options: [{ builtinGlobals: true }],
globals: { a: "readonly" },
errors: [
{ message: "'a' is already defined as a built-in global variable.", type: "Block" }
]
errors: [{
message: "'a' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 11,
endLine: 1,
endColumn: 12
}]
},
{
code: "/*globals a */",
options: [{ builtinGlobals: true }],
globals: { a: "writable" },
errors: [
{ message: "'a' is already defined as a built-in global variable.", type: "Block" }
]
errors: [{
message: "'a' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 11,
endLine: 1,
endColumn: 12
}]
},
{
code: "/*globals a */ /*globals a */",
errors: [
{ message: "'a' is already defined.", type: "Block", column: 26 }
]
errors: [{
message: "'a' is already defined.",
type: "Block",
line: 1,
column: 26,
endLine: 1,
endColumn: 27
}]
},
{
code: "/*globals a */ /*globals a */ var a = 0",
options: [{ builtinGlobals: true }],
globals: { a: "writable" },
errors: [
{ message: "'a' is already defined as a built-in global variable.", type: "Block", column: 11 },
{ message: "'a' is already defined as a built-in global variable.", type: "Block", column: 26 },
{ message: "'a' is already defined as a built-in global variable.", type: "Identifier", column: 35 }
{
message: "'a' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 11,
endLine: 1,
endColumn: 12
},
{
message: "'a' is already defined as a built-in global variable.",
type: "Block",
line: 1,
column: 26,
endLine: 1,
endColumn: 27
},
{
message: "'a' is already defined as a built-in global variable.",
type: "Identifier",
line: 1,
column: 35,
endLine: 1,
endColumn: 36
}
]
}
]
Expand Down

0 comments on commit 920465b

Please sign in to comment.