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

Lines changed: 19 additions & 13 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 9 additions & 9 deletions
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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);

0 commit comments

Comments
 (0)