Skip to content

Commit f834dc2

Browse files
authored
refactor: remove context compat functions (#532)
This change is in support of the larger migration to TypeScript., and a followup to the removal of support for ESLint v8. The context compatibility functions in the `utils` module were no longer necessary. I originally did this in the TypeScript branch, but that PR's going to be large enough. So, I'm trying to peel off smaller independent changes I can land separately first.
1 parent f8775e4 commit f834dc2

33 files changed

+327
-269
lines changed

lib/rules/consistent-output.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author Teddy Katz
44
*/
55

6-
import * as utils from '../utils.js';
6+
import { getKeyName, getTestInfo } from '../utils.js';
77

88
// ------------------------------------------------------------------------------
99
// Rule Definition
@@ -44,13 +44,13 @@ const rule = {
4444

4545
return {
4646
Program(ast) {
47-
utils.getTestInfo(context, ast).forEach((testRun) => {
47+
getTestInfo(context, ast).forEach((testRun) => {
4848
const readableCases = testRun.invalid.filter(
4949
(testCase) => testCase.type === 'ObjectExpression',
5050
);
5151
const casesWithoutOutput = readableCases.filter(
5252
(testCase) =>
53-
!testCase.properties.map(utils.getKeyName).includes('output'),
53+
!testCase.properties.map(getKeyName).includes('output'),
5454
);
5555

5656
if (

lib/rules/fixer-return.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
* @author 薛定谔的猫<[email protected]>
44
*/
55

6-
// ------------------------------------------------------------------------------
7-
// Requirements
8-
// ------------------------------------------------------------------------------
9-
10-
import * as utils from '../utils.js';
116
import { getStaticValue } from '@eslint-community/eslint-utils';
127

8+
import {
9+
getContextIdentifiers,
10+
isAutoFixerFunction,
11+
isSuggestionFixerFunction,
12+
} from '../utils.js';
13+
1314
// ------------------------------------------------------------------------------
1415
// Rule Definition
1516
// ------------------------------------------------------------------------------
@@ -70,15 +71,14 @@ const rule = {
7071
* Check if a returned/yielded node is likely to be a fix or not.
7172
* A fix is an object created by fixer.replaceText() for example and returned by the fix function.
7273
* @param {ASTNode} node - node to check
73-
* @param {Context} context
7474
* @returns {boolean}
7575
*/
7676
function isFix(node) {
7777
if (node.type === 'ArrayExpression' && node.elements.length === 0) {
7878
// An empty array is not a fix.
7979
return false;
8080
}
81-
const scope = utils.getScope(context);
81+
const scope = context.sourceCode.getScope(node);
8282
const staticValue = getStaticValue(node, scope);
8383
if (!staticValue) {
8484
// If we can't find a static value, assume it's a real fix value.
@@ -96,8 +96,8 @@ const rule = {
9696

9797
return {
9898
Program(ast) {
99-
const sourceCode = utils.getSourceCode(context);
100-
contextIdentifiers = utils.getContextIdentifiers(
99+
const sourceCode = context.sourceCode;
100+
contextIdentifiers = getContextIdentifiers(
101101
sourceCode.scopeManager,
102102
ast,
103103
);
@@ -111,8 +111,8 @@ const rule = {
111111
hasYieldWithFixer: false,
112112
hasReturnWithFixer: false,
113113
shouldCheck:
114-
utils.isAutoFixerFunction(node, contextIdentifiers) ||
115-
utils.isSuggestionFixerFunction(node, contextIdentifiers),
114+
isAutoFixerFunction(node, contextIdentifiers) ||
115+
isSuggestionFixerFunction(node, contextIdentifiers),
116116
node,
117117
};
118118
},
@@ -146,7 +146,7 @@ const rule = {
146146
// Ensure the current (arrow) fixer function returned a fix.
147147
'ArrowFunctionExpression:exit'(node) {
148148
if (funcInfo.shouldCheck) {
149-
const sourceCode = utils.getSourceCode(context);
149+
const sourceCode = context.sourceCode;
150150
const loc = sourceCode.getTokenBefore(node.body).loc; // Show violation on arrow (=>).
151151
if (node.expression) {
152152
// When the return is implied (no curly braces around the body), we have to check the single body node directly.

lib/rules/meta-property-ordering.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
* @fileoverview Enforces the order of meta properties
33
*/
44

5-
import * as utils from '../utils.js';
6-
7-
const { getKeyName, getRuleInfo } = utils;
5+
import { getKeyName, getRuleInfo } from '../utils.js';
86

97
const defaultOrder = [
108
'type',
@@ -48,7 +46,7 @@ const rule = {
4846
},
4947

5048
create(context) {
51-
const sourceCode = utils.getSourceCode(context);
49+
const sourceCode = context.sourceCode;
5250
const ruleInfo = getRuleInfo(sourceCode);
5351
if (!ruleInfo) {
5452
return {};

lib/rules/no-deprecated-context-methods.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author Teddy Katz
44
*/
55

6-
import * as utils from '../utils.js';
6+
import { getContextIdentifiers } from '../utils.js';
77

88
const DEPRECATED_PASSTHROUGHS = {
99
getSource: 'getText',
@@ -52,15 +52,15 @@ const rule = {
5252
},
5353

5454
create(context) {
55-
const sourceCode = utils.getSourceCode(context);
55+
const sourceCode = context.sourceCode;
5656

5757
// ----------------------------------------------------------------------
5858
// Public
5959
// ----------------------------------------------------------------------
6060

6161
return {
6262
'Program:exit'(ast) {
63-
[...utils.getContextIdentifiers(sourceCode.scopeManager, ast)]
63+
[...getContextIdentifiers(sourceCode.scopeManager, ast)]
6464
.filter(
6565
(contextId) =>
6666
contextId.parent.type === 'MemberExpression' &&

lib/rules/no-deprecated-report-api.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author Teddy Katz
44
*/
55

6-
import * as utils from '../utils.js';
6+
import { getContextIdentifiers, getReportInfo } from '../utils.js';
77

88
// ------------------------------------------------------------------------------
99
// Rule Definition
@@ -28,7 +28,7 @@ const rule = {
2828
},
2929

3030
create(context) {
31-
const sourceCode = utils.getSourceCode(context);
31+
const sourceCode = context.sourceCode;
3232
let contextIdentifiers;
3333

3434
// ----------------------------------------------------------------------
@@ -37,7 +37,7 @@ const rule = {
3737

3838
return {
3939
Program(ast) {
40-
contextIdentifiers = utils.getContextIdentifiers(
40+
contextIdentifiers = getContextIdentifiers(
4141
sourceCode.scopeManager,
4242
ast,
4343
);
@@ -58,7 +58,7 @@ const rule = {
5858
fix(fixer) {
5959
const openingParen = sourceCode.getTokenBefore(node.arguments[0]);
6060
const closingParen = sourceCode.getLastToken(node);
61-
const reportInfo = utils.getReportInfo(node, context);
61+
const reportInfo = getReportInfo(node, context);
6262

6363
if (!reportInfo) {
6464
return null;

lib/rules/no-identical-tests.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author 薛定谔的猫<[email protected]>
44
*/
55

6-
import * as utils from '../utils.js';
6+
import { getTestInfo } from '../utils.js';
77

88
// ------------------------------------------------------------------------------
99
// Rule Definition
@@ -30,7 +30,7 @@ const rule = {
3030
// ----------------------------------------------------------------------
3131
// Public
3232
// ----------------------------------------------------------------------
33-
const sourceCode = utils.getSourceCode(context);
33+
const sourceCode = context.sourceCode;
3434

3535
// ----------------------------------------------------------------------
3636
// Helpers
@@ -52,7 +52,7 @@ const rule = {
5252

5353
return {
5454
Program(ast) {
55-
utils.getTestInfo(context, ast).forEach((testRun) => {
55+
getTestInfo(context, ast).forEach((testRun) => {
5656
[testRun.valid, testRun.invalid].forEach((tests) => {
5757
const cache = new Set();
5858
tests.forEach((test) => {

lib/rules/no-meta-replaced-by.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @fileoverview Disallows the usage of `meta.replacedBy` property
33
*/
44

5-
import * as utils from '../utils.js';
5+
import { evaluateObjectProperties, getKeyName, getRuleInfo } from '../utils.js';
66

77
// ------------------------------------------------------------------------------
88
// Rule Definition
@@ -25,8 +25,8 @@ const rule = {
2525
},
2626
},
2727
create(context) {
28-
const sourceCode = utils.getSourceCode(context);
29-
const ruleInfo = utils.getRuleInfo(sourceCode);
28+
const sourceCode = context.sourceCode;
29+
const ruleInfo = getRuleInfo(sourceCode);
3030

3131
if (!ruleInfo) {
3232
return {};
@@ -40,12 +40,10 @@ const rule = {
4040
return;
4141
}
4242

43-
const replacedByNode = utils
44-
.evaluateObjectProperties(metaNode, sourceCode.scopeManager)
45-
.find(
46-
(p) =>
47-
p.type === 'Property' && utils.getKeyName(p) === 'replacedBy',
48-
);
43+
const replacedByNode = evaluateObjectProperties(
44+
metaNode,
45+
sourceCode.scopeManager,
46+
).find((p) => p.type === 'Property' && getKeyName(p) === 'replacedBy');
4947

5048
if (!replacedByNode) {
5149
return;

lib/rules/no-meta-schema-default.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { getStaticValue } from '@eslint-community/eslint-utils';
2-
import * as utils from '../utils.js';
2+
3+
import {
4+
getMetaSchemaNode,
5+
getMetaSchemaNodeProperty,
6+
getRuleInfo,
7+
} from '../utils.js';
38

49
// ------------------------------------------------------------------------------
510
// Rule Definition
@@ -23,22 +28,19 @@ const rule = {
2328
},
2429

2530
create(context) {
26-
const sourceCode = utils.getSourceCode(context);
31+
const sourceCode = context.sourceCode;
2732
const { scopeManager } = sourceCode;
28-
const ruleInfo = utils.getRuleInfo(sourceCode);
33+
const ruleInfo = getRuleInfo(sourceCode);
2934
if (!ruleInfo) {
3035
return {};
3136
}
3237

33-
const schemaNode = utils.getMetaSchemaNode(ruleInfo.meta, scopeManager);
38+
const schemaNode = getMetaSchemaNode(ruleInfo.meta, scopeManager);
3439
if (!schemaNode) {
3540
return {};
3641
}
3742

38-
const schemaProperty = utils.getMetaSchemaNodeProperty(
39-
schemaNode,
40-
scopeManager,
41-
);
43+
const schemaProperty = getMetaSchemaNodeProperty(schemaNode, scopeManager);
4244

4345
if (schemaProperty?.type === 'ObjectExpression') {
4446
checkSchemaElement(schemaProperty, true);

lib/rules/no-missing-message-ids.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import * as utils from '../utils.js';
1+
import {
2+
collectReportViolationAndSuggestionData,
3+
findPossibleVariableValues,
4+
getContextIdentifiers,
5+
getMessagesNode,
6+
getMessageIdNodeById,
7+
getReportInfo,
8+
getRuleInfo,
9+
} from '../utils.js';
210

311
// ------------------------------------------------------------------------------
412
// Rule Definition
@@ -24,14 +32,14 @@ const rule = {
2432
},
2533

2634
create(context) {
27-
const sourceCode = utils.getSourceCode(context);
35+
const sourceCode = context.sourceCode;
2836
const { scopeManager } = sourceCode;
29-
const ruleInfo = utils.getRuleInfo(sourceCode);
37+
const ruleInfo = getRuleInfo(sourceCode);
3038
if (!ruleInfo) {
3139
return {};
3240
}
3341

34-
const messagesNode = utils.getMessagesNode(ruleInfo, scopeManager);
42+
const messagesNode = getMessagesNode(ruleInfo, scopeManager);
3543

3644
let contextIdentifiers;
3745

@@ -42,45 +50,40 @@ const rule = {
4250

4351
return {
4452
Program(ast) {
45-
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
53+
contextIdentifiers = getContextIdentifiers(scopeManager, ast);
4654
},
4755

4856
CallExpression(node) {
49-
const scope = utils.getScope(context);
57+
const scope = sourceCode.getScope(node);
5058
// Check for messageId properties used in known calls to context.report();
5159
if (
5260
node.callee.type === 'MemberExpression' &&
5361
contextIdentifiers.has(node.callee.object) &&
5462
node.callee.property.type === 'Identifier' &&
5563
node.callee.property.name === 'report'
5664
) {
57-
const reportInfo = utils.getReportInfo(node, context);
65+
const reportInfo = getReportInfo(node, context);
5866
if (!reportInfo) {
5967
return;
6068
}
6169

6270
const reportMessagesAndDataArray =
63-
utils.collectReportViolationAndSuggestionData(reportInfo);
71+
collectReportViolationAndSuggestionData(reportInfo);
6472
for (const { messageId } of reportMessagesAndDataArray.filter(
6573
(obj) => obj.messageId,
6674
)) {
6775
const values =
6876
messageId.type === 'Literal'
6977
? [messageId]
70-
: utils.findPossibleVariableValues(messageId, scopeManager);
78+
: findPossibleVariableValues(messageId, scopeManager);
7179

7280
// Look for any possible string values we found for this messageId.
7381
values.forEach((val) => {
7482
if (
7583
val.type === 'Literal' &&
7684
typeof val.value === 'string' &&
7785
val.value !== '' &&
78-
!utils.getMessageIdNodeById(
79-
val.value,
80-
ruleInfo,
81-
scopeManager,
82-
scope,
83-
)
86+
!getMessageIdNodeById(val.value, ruleInfo, scopeManager, scope)
8487
)
8588
// Couldn't find this messageId in `meta.messages`.
8689
context.report({

0 commit comments

Comments
 (0)