Skip to content

Commit 1e4b030

Browse files
authored
Replace .loc with sourceCode.getLoc() (#2558)
1 parent b95dc12 commit 1e4b030

27 files changed

+161
-62
lines changed

eslint.dogfooding.config.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,16 @@ import eslintPluginUnicorn from './index.js';
66

77
const config = [
88
eslintPluginUnicorn.configs.all,
9+
disableExternalRules([
10+
// If external rules needs to be disabled, add the rule name here.
11+
'n/no-unsupported-features/es-syntax',
12+
'eslint-plugin/require-meta-default-options',
13+
'internal/no-restricted-property-access',
14+
]),
915
{
1016
linterOptions: {
1117
reportUnusedDisableDirectives: false,
1218
},
13-
// Fake rule to allow inline config to disable
14-
plugins: {
15-
n: {
16-
rules: {
17-
'no-unsupported-features/es-syntax': {},
18-
},
19-
},
20-
'eslint-plugin': {
21-
rules: {
22-
'require-meta-default-options': {},
23-
},
24-
},
25-
},
2619
},
2720
{
2821
ignores: [
@@ -56,4 +49,17 @@ const config = [
5649
},
5750
];
5851

52+
// Create rule to allow inline config to disable
53+
function disableExternalRules(rules) {
54+
const plugins = {};
55+
for (const rule of rules) {
56+
const [pluginName, ...rest] = rule.split('/');
57+
const ruleName = rest.join('/');
58+
plugins[pluginName] ??= {rules: {}};
59+
plugins[pluginName].rules[ruleName] ??= {};
60+
}
61+
62+
return {plugins};
63+
}
64+
5965
export default config;

rules/consistent-date-clone.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ const create = context => ({
2424
return;
2525
}
2626

27+
const {sourceCode} = context;
2728
return {
2829
node: callExpression,
29-
loc: {start: callExpression.callee.property.loc.start, end: callExpression.loc.end},
30+
loc: {
31+
start: sourceCode.getLoc(callExpression.callee.property).start,
32+
end: sourceCode.getLoc(callExpression).end,
33+
},
3034
messageId: MESSAGE_ID_ERROR,
31-
fix: fixer => removeMethodCall(fixer, callExpression, context.sourceCode),
35+
fix: fixer => removeMethodCall(fixer, callExpression, sourceCode),
3236
};
3337
},
3438
});

rules/empty-brace-spaces.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const getProblem = (node, context) => {
2222

2323
return {
2424
loc: {
25-
start: openingBrace.loc.end,
26-
end: closingBrace.loc.start,
25+
start: sourceCode.getLoc(openingBrace).end,
26+
end: sourceCode.getLoc(closingBrace).start,
2727
},
2828
messageId: MESSAGE_ID,
2929
fix: fixer => fixer.removeRange([start, end]),

rules/expiring-todo-comments.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ const create = context => {
354354
if (dates.length > 1) {
355355
uses++;
356356
context.report({
357-
loc: comment.loc,
357+
loc: sourceCode.getLoc(comment),
358358
messageId: MESSAGE_ID_AVOID_MULTIPLE_DATES,
359359
data: {
360360
expirationDates: dates.join(', '),
@@ -368,7 +368,7 @@ const create = context => {
368368
const shouldIgnore = options.ignoreDatesOnPullRequests && ci.isPR;
369369
if (!shouldIgnore && reachedDate(expirationDate, options.date)) {
370370
context.report({
371-
loc: comment.loc,
371+
loc: sourceCode.getLoc(comment),
372372
messageId: MESSAGE_ID_EXPIRED_TODO,
373373
data: {
374374
expirationDate,
@@ -381,7 +381,7 @@ const create = context => {
381381
if (packageVersions.length > 1) {
382382
uses++;
383383
context.report({
384-
loc: comment.loc,
384+
loc: sourceCode.getLoc(comment),
385385
messageId: MESSAGE_ID_AVOID_MULTIPLE_PACKAGE_VERSIONS,
386386
data: {
387387
versions: packageVersions
@@ -400,7 +400,7 @@ const create = context => {
400400
const compare = semverComparisonForOperator(condition);
401401
if (packageVersion && compare(packageVersion, decidedPackageVersion)) {
402402
context.report({
403-
loc: comment.loc,
403+
loc: sourceCode.getLoc(comment),
404404
messageId: MESSAGE_ID_REACHED_PACKAGE_VERSION,
405405
data: {
406406
comparison: `${condition}${version}`,
@@ -426,7 +426,7 @@ const create = context => {
426426

427427
if (trigger) {
428428
context.report({
429-
loc: comment.loc,
429+
loc: sourceCode.getLoc(comment),
430430
messageId,
431431
data: {
432432
package: dependency.name,
@@ -452,7 +452,7 @@ const create = context => {
452452

453453
if (compare(targetPackageVersion, todoVersion)) {
454454
context.report({
455-
loc: comment.loc,
455+
loc: sourceCode.getLoc(comment),
456456
messageId: MESSAGE_ID_VERSION_MATCHES,
457457
data: {
458458
comparison: `${dependency.name} ${dependency.condition} ${dependency.version}`,
@@ -484,7 +484,7 @@ const create = context => {
484484

485485
if (compare(targetPackageEngineVersion, todoEngine)) {
486486
context.report({
487-
loc: comment.loc,
487+
loc: sourceCode.getLoc(comment),
488488
messageId: MESSAGE_ID_ENGINE_MATCHES,
489489
data: {
490490
comparison: `node${engine.condition}${engine.version}`,
@@ -508,7 +508,7 @@ const create = context => {
508508
if (parseArgument(testString).type !== 'unknowns') {
509509
uses++;
510510
context.report({
511-
loc: comment.loc,
511+
loc: sourceCode.getLoc(comment),
512512
messageId: MESSAGE_ID_MISSING_AT_SYMBOL,
513513
data: {
514514
original: unknown,
@@ -525,7 +525,7 @@ const create = context => {
525525
if (parseArgument(withoutWhitespace).type !== 'unknowns') {
526526
uses++;
527527
context.report({
528-
loc: comment.loc,
528+
loc: sourceCode.getLoc(comment),
529529
messageId: MESSAGE_ID_REMOVE_WHITESPACE,
530530
data: {
531531
original: unknown,

rules/no-abusive-eslint-disable.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const messages = {
66
const disableRegex = /^eslint-disable(?:-next-line|-line)?(?<ruleId>$|(?:\s+(?:@(?:[\w-]+\/){1,2})?[\w-]+)?)/;
77

88
/** @param {import('eslint').Rule.RuleContext} context */
9-
const create = () => ({
9+
const create = context => ({
1010
* Program(node) {
1111
for (const comment of node.comments) {
1212
const value = comment.value.trim();
@@ -16,15 +16,17 @@ const create = () => ({
1616
result // It's a eslint-disable comment
1717
&& !result.groups.ruleId // But it did not specify any rules
1818
) {
19+
const {sourceCode} = context;
20+
1921
yield {
2022
// Can't set it at the given location as the warning
2123
// will be ignored due to the disable comment
2224
loc: {
2325
start: {
24-
...comment.loc.start,
26+
...sourceCode.getLoc(comment).start,
2527
column: -1,
2628
},
27-
end: comment.loc.end,
29+
end: sourceCode.getLoc(comment).end,
2830
},
2931
messageId: MESSAGE_ID,
3032
};

rules/no-for-loop.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ const getRemovalRange = (node, sourceCode) => {
164164
const declarationNode = node.parent;
165165

166166
if (declarationNode.declarations.length === 1) {
167-
const {line} = declarationNode.loc.start;
167+
const {line} = sourceCode.getLoc(declarationNode).start;
168168
const lineText = sourceCode.lines[line - 1];
169169

170170
const isOnlyNodeOnLine = lineText.trim() === sourceCode.getText(declarationNode);

rules/no-static-only-class.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function switchClassToObject(node, sourceCode) {
152152
if (
153153
type === 'ClassExpression'
154154
&& parent.type === 'ReturnStatement'
155-
&& body.loc.start.line !== parent.loc.start.line
155+
&& sourceCode.getLoc(body).start.line !== sourceCode.getLoc(parent).start.line
156156
&& sourceCode.text.slice(classToken.range[1], body.range[0]).trim()
157157
) {
158158
yield fixer.replaceText(classToken, '{');

rules/no-typeof-undefined.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const create = context => {
9393

9494
const problem = {
9595
node: binaryExpression,
96-
loc: typeofToken.loc,
96+
loc: sourceCode.getLoc(typeofToken),
9797
messageId: MESSAGE_ID_ERROR,
9898
};
9999

rules/no-unnecessary-await.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const create = context => ({
4646
const awaitToken = sourceCode.getFirstToken(node);
4747
const problem = {
4848
node,
49-
loc: awaitToken.loc,
49+
loc: sourceCode.getLoc(awaitToken),
5050
messageId: MESSAGE_ID,
5151
};
5252

rules/no-useless-length-check.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,17 @@ const create = context => {
102102
getUselessLengthCheckNode(logicalExpression),
103103
),
104104
);
105+
const {sourceCode} = context;
105106

106107
for (const node of nodes) {
107108
yield {
108109
loc: {
109-
start: node.left.property.loc.start,
110-
end: node.loc.end,
110+
start: sourceCode.getLoc(node.left.property).start,
111+
end: sourceCode.getLoc(node).end,
111112
},
112113
messageId: zeroLengthChecks.has(node) ? 'zero' : 'non-zero',
113114
/** @param {import('eslint').Rule.RuleFixer} fixer */
114115
fix(fixer) {
115-
const {sourceCode} = context;
116116
const {left, right} = node.parent;
117117
const leftRange = getParenthesizedRange(left, sourceCode);
118118
const rightRange = getParenthesizedRange(right, sourceCode);

rules/no-useless-undefined.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ const create = context => {
251251
return {
252252
messageId,
253253
loc: {
254-
start: firstUndefined.loc.start,
255-
end: lastUndefined.loc.end,
254+
start: sourceCode.getLoc(firstUndefined).start,
255+
end: sourceCode.getLoc(lastUndefined).end,
256256
},
257257
fix(fixer) {
258258
let [start] = sourceCode.getRange(firstUndefined);

rules/prefer-array-flat-map.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ const create = context => ({
4646

4747
return {
4848
node: flatCallExpression,
49-
loc: {start: mapProperty.loc.start, end: flatCallExpression.loc.end},
49+
loc: {
50+
start: sourceCode.getLoc(mapProperty).start,
51+
end: sourceCode.getLoc(flatCallExpression).end,
52+
},
5053
messageId: MESSAGE_ID,
5154
* fix(fixer) {
5255
// Removes:

rules/prefer-default-parameters.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ const needsParentheses = (sourceCode, function_) => {
9191

9292
/** @param {import('eslint').Rule.RuleFixer} fixer */
9393
const fixDefaultExpression = (fixer, sourceCode, node) => {
94-
const {line} = node.loc.start;
95-
const {column} = node.loc.end;
94+
const {line} = sourceCode.getLoc(node).start;
95+
const {column} = sourceCode.getLoc(node).end;
9696
const nodeText = sourceCode.getText(node);
9797
const lineText = sourceCode.lines[line - 1];
9898
const isOnlyNodeOnLine = lineText.trim() === nodeText;

rules/prefer-string-raw.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ const create = context => {
5252
return;
5353
}
5454

55+
const {sourceCode} = context;
5556
const {raw} = node;
5657
if (
5758
raw.at(-2) === BACKSLASH
5859
|| !raw.includes(BACKSLASH + BACKSLASH)
5960
|| raw.includes('`')
6061
|| raw.includes('${')
61-
|| node.loc.start.line !== node.loc.end.line
62+
|| sourceCode.getLoc(node).start.line !== sourceCode.getLoc(node).end.line
6263
) {
6364
return;
6465
}
@@ -73,7 +74,7 @@ const create = context => {
7374
messageId: MESSAGE_ID,
7475
* fix(fixer) {
7576
yield fixer.replaceText(node, `String.raw\`${unescaped}\``);
76-
yield * fixSpaceAroundKeyword(fixer, node, context.sourceCode);
77+
yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
7778
},
7879
};
7980
});

rules/prefer-structured-clone.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ const create = context => {
4747

4848
const jsonParse = callExpression;
4949
const jsonStringify = callExpression.arguments[0];
50+
const {sourceCode} = context;
5051

5152
return {
5253
node: jsonParse,
5354
loc: {
54-
start: jsonParse.loc.start,
55-
end: jsonStringify.callee.loc.end,
55+
start: sourceCode.getLoc(jsonParse).start,
56+
end: sourceCode.getLoc(jsonStringify.callee).end,
5657
},
5758
messageId: MESSAGE_ID_ERROR,
5859
data: {
@@ -64,8 +65,6 @@ const create = context => {
6465
* fix(fixer) {
6566
yield fixer.replaceText(jsonParse.callee, 'structuredClone');
6667

67-
const {sourceCode} = context;
68-
6968
yield fixer.remove(jsonStringify.callee);
7069
yield * removeParentheses(jsonStringify.callee, fixer, sourceCode);
7170

rules/prefer-switch.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ const create = context => {
290290

291291
const problem = {
292292
loc: {
293-
start: node.loc.start,
294-
end: node.consequent.loc.start,
293+
start: sourceCode.getLoc(node).start,
294+
end: sourceCode.getLoc(node.consequent).start,
295295
},
296296
messageId: MESSAGE_ID,
297297
};

rules/prefer-ternary.js

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function getNodeBody(node) {
3434
return node;
3535
}
3636

37+
// eslint-disable-next-line internal/no-restricted-property-access -- Need fix
3738
const isSingleLineNode = node => node.loc.start.line === node.loc.end.line;
3839

3940
/** @param {import('eslint').Rule.RuleContext} context */

rules/require-array-join-separator.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ const create = context => ({
3838
const isPrototypeMethod = node.arguments.length === 1;
3939
return {
4040
loc: {
41-
start: penultimateToken.loc[isPrototypeMethod ? 'end' : 'start'],
42-
end: lastToken.loc.end,
41+
start: sourceCode.getLoc(penultimateToken)[isPrototypeMethod ? 'end' : 'start'],
42+
end: sourceCode.getLoc(lastToken).end,
4343
},
4444
messageId: MESSAGE_ID,
4545
/** @param {import('eslint').Rule.RuleFixer} fixer */

rules/require-number-to-fixed-digits-argument.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const create = context => ({
2929

3030
return {
3131
loc: {
32-
start: openingParenthesis.loc.start,
33-
end: closingParenthesis.loc.end,
32+
start: sourceCode.getLoc(openingParenthesis).start,
33+
end: sourceCode.getLoc(closingParenthesis).end,
3434
},
3535
messageId: MESSAGE_ID,
3636
/** @param {import('eslint').Rule.RuleFixer} fixer */

rules/require-post-message-target-origin.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ function create(context) {
4141

4242
return {
4343
loc: {
44-
start: penultimateToken.loc.end,
45-
end: lastToken.loc.end,
44+
start: sourceCode.getLoc(penultimateToken).end,
45+
end: sourceCode.getLoc(lastToken).end,
4646
},
4747
messageId: ERROR,
4848
suggest: replacements.map(code => ({

rules/switch-case-braces.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const create = context => {
5252
) {
5353
return {
5454
node,
55-
loc: sourceCode.getFirstToken(consequent[0]).loc,
55+
loc: sourceCode.getLoc(sourceCode.getFirstToken(consequent[0])),
5656
messageId: MESSAGE_ID_EMPTY_CLAUSE,
5757
fix: fixer => removeBraces(fixer, node, sourceCode),
5858
};
@@ -84,7 +84,7 @@ const create = context => {
8484
) {
8585
return {
8686
node,
87-
loc: sourceCode.getFirstToken(consequent[0]).loc,
87+
loc: sourceCode.getLoc(sourceCode.getFirstToken(consequent[0])),
8888
messageId: MESSAGE_ID_UNNECESSARY_BRACES,
8989
fix: fixer => removeBraces(fixer, node, sourceCode),
9090
};

0 commit comments

Comments
 (0)