Skip to content

Commit 727451e

Browse files
authored
fix: fix regression of global mode report range in strict rule (#20462)
* fix: fix regression of global mode report range in `strict` * test: tweak test cases
1 parent e80485f commit 727451e

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/rules/strict.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ module.exports = {
268268
}
269269
}
270270

271+
/** @type {import('../types').Rule.RuleListener} */
271272
const rule = {
272273
Program(node) {
273274
const useStrictDirectives = getUseStrictDirectives(node.body);
@@ -281,7 +282,16 @@ module.exports = {
281282
node.body.length > 0 &&
282283
useStrictDirectives.length === 0
283284
) {
284-
context.report({ node, messageId: "global" });
285+
/*
286+
* Report the range as v9 does
287+
*/
288+
context.report({
289+
loc: {
290+
start: node.body[0].loc.start,
291+
end: node.body.at(-1).loc.end,
292+
},
293+
messageId: "global",
294+
});
285295
}
286296
reportAllExceptFirst(useStrictDirectives, "multiple", true);
287297
} else {

tests/lib/rules/strict.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ ruleTester.run("strict", rule, {
6868
// "global" mode
6969
{ code: "// Intentionally empty", options: ["global"] },
7070
{ code: '"use strict"; foo();', options: ["global"] },
71+
{
72+
code: "/* license */\n/* eslint-disable rule-to-test/strict */\nfoo();",
73+
options: ["global"],
74+
},
7175
{
7276
code: "foo();",
7377
options: ["global"],
@@ -397,6 +401,20 @@ ruleTester.run("strict", rule, {
397401
},
398402
],
399403
},
404+
{
405+
code: "/* license */\nfunction foo() {}\nfunction bar() {}\n/* end */",
406+
output: null,
407+
options: ["global"],
408+
errors: [
409+
{
410+
messageId: "global",
411+
line: 2,
412+
column: 1,
413+
endLine: 3,
414+
endColumn: 18,
415+
},
416+
],
417+
},
400418
{
401419
code: "function foo() { 'use strict'; return; }",
402420
output: null,

0 commit comments

Comments
 (0)