Skip to content

Commit

Permalink
refactor: add utils.getSourceCode(), utils.getScope(), utils.getparse…
Browse files Browse the repository at this point in the history
…rServices(), utils.getFilename()
  • Loading branch information
aladdin-add committed Dec 18, 2024
1 parent 8d32bf9 commit c53b4eb
Show file tree
Hide file tree
Showing 32 changed files with 78 additions and 56 deletions.
7 changes: 3 additions & 4 deletions lib/rules/fixer-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ module.exports = {
* @returns {boolean}
*/
function isFix(node) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: use context.sourceCode when dropping eslint < v9
if (node.type === 'ArrayExpression' && node.elements.length === 0) {
// An empty array is not a fix.
return false;
}
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
const scope = utils.getScope(context);
const staticValue = getStaticValue(node, scope);
if (!staticValue) {
// If we can't find a static value, assume it's a real fix value.
Expand All @@ -99,7 +98,7 @@ module.exports = {

return {
Program(ast) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
contextIdentifiers = utils.getContextIdentifiers(
sourceCode.scopeManager,
ast,
Expand Down Expand Up @@ -149,7 +148,7 @@ module.exports = {
// Ensure the current (arrow) fixer function returned a fix.
'ArrowFunctionExpression:exit'(node) {
if (funcInfo.shouldCheck) {
const sourceCode = context.sourceCode || context.getSourceCode();
const sourceCode = utils.getSourceCode(context);
const loc = sourceCode.getTokenBefore(node.body).loc; // Show violation on arrow (=>).
if (node.expression) {
// When the return is implied (no curly braces around the body), we have to check the single body node directly.
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/meta-property-ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

'use strict';

const { getKeyName, getRuleInfo } = require('../utils');
const utils = require('../utils');
const { getKeyName, getRuleInfo } = utils;

const defaultOrder = [
'type',
Expand Down Expand Up @@ -48,7 +49,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const ruleInfo = getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-deprecated-context-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);

// ----------------------------------------------------------------------
// Public
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-deprecated-report-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
let contextIdentifiers;

// ----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);

// ----------------------------------------------------------------------
// Helpers
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-meta-schema-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const { scopeManager } = sourceCode;
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-missing-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const { scopeManager } = sourceCode;
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
Expand All @@ -48,7 +48,7 @@ module.exports = {
},

CallExpression(node) {
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
const scope = utils.getScope(context);
// Check for messageId properties used in known calls to context.report();
if (
node.callee.type === 'MemberExpression' &&
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-missing-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const { scopeManager } = sourceCode;

let contextIdentifiers;
Expand All @@ -48,7 +48,7 @@ module.exports = {
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
},
CallExpression(node) {
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
const scope = utils.getScope(context);
if (
node.callee.type === 'MemberExpression' &&
contextIdentifiers.has(node.callee.object) &&
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/no-only-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ module.exports = {
{
messageId: 'removeOnly',
*fix(fixer) {
const sourceCode =
context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);

const tokenBefore =
sourceCode.getTokenBefore(onlyProperty);
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/no-property-in-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const utils = require('../utils');

const defaultTypedNodeSourceFileTesters = [
/@types[/\\]estree[/\\]index\.d\.ts/,
/@typescript-eslint[/\\]types[/\\]dist[/\\]generated[/\\]ast-spec\.d\.ts/,
Expand Down Expand Up @@ -88,7 +90,7 @@ module.exports = {
'BinaryExpression[operator=in]'(node) {
// TODO: Switch this to ESLintUtils.getParserServices with typescript-eslint@>=6
// https://github.com/eslint-community/eslint-plugin-eslint-plugin/issues/269
const services = (context.sourceCode || context).parserServices;
const services = utils.getparserServices(context);
if (!services.program) {
throw new Error(
'You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.',
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-unused-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const { scopeManager } = sourceCode;
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
Expand All @@ -47,7 +47,7 @@ module.exports = {
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
},

'Program:exit'(ast) {
'Program:exit'() {
if (hasSeenUnknownMessageId || !hasSeenViolationReport) {
/*
Bail out when the rule is likely to have false positives.
Expand All @@ -57,7 +57,7 @@ module.exports = {
return;
}

const scope = sourceCode.getScope?.(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
const scope = utils.getScope(context);

const messageIdNodesUnused = messageIdNodes.filter(
(node) => !messageIdsUsed.has(utils.getKeyName(node, scope)),
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-unused-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const { scopeManager } = sourceCode;

let contextIdentifiers;
Expand All @@ -47,7 +47,7 @@ module.exports = {
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
},
CallExpression(node) {
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
const scope = utils.getScope(context);
if (
node.callee.type === 'MemberExpression' &&
contextIdentifiers.has(node.callee.object) &&
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-useless-token-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);

// ----------------------------------------------------------------------
// Helpers
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/prefer-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
Expand All @@ -43,7 +43,7 @@ module.exports = {

return {
Program(ast) {
const scope = sourceCode.getScope?.(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
const scope = utils.getScope(context);
contextIdentifiers = utils.getContextIdentifiers(
sourceCode.scopeManager,
ast,
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-object-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
// Public
// ----------------------------------------------------------------------

const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-output-null.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
// Public
// ----------------------------------------------------------------------

const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);

return {
Program(ast) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = {
create(context) {
let contextIdentifiers;

const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const { scopeManager } = sourceCode;

// ----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-replace-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
let funcInfo = {
upper: null,
codePath: null,
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/report-message-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = {
}
}

const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
Expand All @@ -75,7 +75,7 @@ module.exports = {

return {
Program(ast) {
const scope = sourceCode.getScope?.(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
const scope = utils.getScope(context);
contextIdentifiers = utils.getContextIdentifiers(
sourceCode.scopeManager,
ast,
Expand Down Expand Up @@ -103,7 +103,7 @@ module.exports = {
.forEach((it) => processMessageNode(it, scope));
},
CallExpression(node) {
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
const scope = utils.getScope(context);
if (
node.callee.type === 'MemberExpression' &&
contextIdentifiers.has(node.callee.object) &&
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-meta-default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const { scopeManager } = sourceCode;
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/require-meta-docs-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
}

return {
Program(ast) {
const scope = sourceCode.getScope?.(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
Program() {
const scope = utils.getScope(context);
const { scopeManager } = sourceCode;

const {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-meta-docs-recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/require-meta-docs-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = {
*/
create(context) {
const options = context.options[0] || {};
const filename = context.filename || context.getFilename(); // TODO: just use context.filename when dropping eslint < v9
const filename = utils.getFilename(context);
const ruleName =
filename === '<input>'
? undefined
Expand All @@ -77,15 +77,15 @@ module.exports = {
);
}

const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
}

return {
Program(ast) {
const scope = sourceCode.getScope?.(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
Program() {
const scope = utils.getScope(context);
const { scopeManager } = sourceCode;

const {
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/require-meta-fixable.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = {
const catchNoFixerButFixableProperty =
context.options[0] && context.options[0].catchNoFixerButFixableProperty;

const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const { scopeManager } = sourceCode;
const ruleInfo = utils.getRuleInfo(sourceCode);
let contextIdentifiers;
Expand Down Expand Up @@ -79,8 +79,8 @@ module.exports = {
usesFixFunctions = true;
}
},
'Program:exit'(ast) {
const scope = sourceCode.getScope?.(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
'Program:exit'() {
const scope = utils.getScope(context);
const metaFixableProp =
ruleInfo &&
utils
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/require-meta-has-suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const sourceCode = utils.getSourceCode(context);
const { scopeManager } = sourceCode;
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
Expand All @@ -44,7 +44,7 @@ module.exports = {
* @returns {boolean} whether this property should be considered to contain suggestions
*/
function doesPropertyContainSuggestions(node) {
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
const scope = utils.getScope(context);
const staticValue = getStaticValue(node.value, scope);
if (
!staticValue ||
Expand Down Expand Up @@ -95,8 +95,8 @@ module.exports = {
ruleReportsSuggestions = true;
}
},
'Program:exit'(node) {
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
'Program:exit'() {
const scope = utils.getScope(context);
const metaNode = ruleInfo && ruleInfo.meta;
const hasSuggestionsProperty = utils
.evaluateObjectProperties(metaNode, scopeManager)
Expand Down
Loading

0 comments on commit c53b4eb

Please sign in to comment.