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

Fix: getNameLocationInGlobalDirectiveComment end location (refs #12334) #13086

Merged
merged 1 commit into from Mar 28, 2020
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
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