Skip to content

Commit

Permalink
[Refactor] use contextCompat helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 5, 2024
1 parent 756fe8e commit a7b4348
Show file tree
Hide file tree
Showing 26 changed files with 93 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"debug": "^3.2.7",
"doctrine": "^2.1.0",
"eslint-import-resolver-node": "^0.3.9",
"eslint-module-utils": "^2.9.0",
"eslint-module-utils": "^2.10.0",
"hasown": "^2.0.2",
"is-core-module": "^2.15.1",
"is-glob": "^4.0.3",
Expand Down
3 changes: 2 additions & 1 deletion src/core/packagePath.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { dirname } from 'path';
import { getPhysicalFilename } from 'eslint-module-utils/contextCompat';
import pkgUp from 'eslint-module-utils/pkgUp';
import readPkgUp from 'eslint-module-utils/readPkgUp';

Expand All @@ -8,7 +9,7 @@ export function getFilePackagePath(filePath) {
}

export function getContextPackagePath(context) {
return getFilePackagePath(context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename());
return getFilePackagePath(getPhysicalFilename(context));
}

export function getFilePackageName(filePath) {
Expand Down
4 changes: 3 additions & 1 deletion src/rules/consistent-type-specifier-style.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getSourceCode } from 'eslint-module-utils/contextCompat';

import docsUrl from '../docsUrl';

function isComma(token) {
Expand Down Expand Up @@ -55,7 +57,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

if (context.options[0] === 'prefer-inline') {
return {
Expand Down
4 changes: 3 additions & 1 deletion src/rules/dynamic-import-chunkname.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { getSourceCode } from 'eslint-module-utils/contextCompat';
import vm from 'vm';

import docsUrl from '../docsUrl';

module.exports = {
Expand Down Expand Up @@ -43,7 +45,7 @@ module.exports = {
const eagerModeRegex = new RegExp(eagerModeFormat);

function run(node, arg) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const leadingComments = sourceCode.getCommentsBefore
? sourceCode.getCommentsBefore(arg) // This method is available in ESLint >= 4.
: sourceCode.getComments(arg).leading; // This method is deprecated in ESLint 7.
Expand Down
6 changes: 4 additions & 2 deletions src/rules/first.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getDeclaredVariables, getSourceCode } from 'eslint-module-utils/contextCompat';

import docsUrl from '../docsUrl';

function getImportValue(node) {
Expand Down Expand Up @@ -38,7 +40,7 @@ module.exports = {
}
const absoluteFirst = context.options[0] === 'absolute-first';
const message = 'Import in body of module; reorder to top.';
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const originSourceCode = sourceCode.getText();
let nonImportCount = 0;
let anyExpressions = false;
Expand Down Expand Up @@ -66,7 +68,7 @@ module.exports = {
}
}
if (nonImportCount > 0) {
for (const variable of context.getDeclaredVariables(node)) {
for (const variable of getDeclaredVariables(context, node)) {
if (!shouldSort) { break; }
const references = variable.references;
if (references.length) {
Expand Down
6 changes: 4 additions & 2 deletions src/rules/named.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as path from 'path';
import { getFilename, getPhysicalFilename } from 'eslint-module-utils/contextCompat';

import ExportMapBuilder from '../exportMap/builder';
import docsUrl from '../docsUrl';

Expand Down Expand Up @@ -67,7 +69,7 @@ module.exports = {
if (!deepLookup.found) {
if (deepLookup.path.length > 1) {
const deepPath = deepLookup.path
.map((i) => path.relative(path.dirname(context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename()), i.path))
.map((i) => path.relative(path.dirname(getPhysicalFilename(context)), i.path))
.join(' -> ');

context.report(im[key], `${name} not found via ${deepPath}`);
Expand Down Expand Up @@ -121,7 +123,7 @@ module.exports = {
if (!deepLookup.found) {
if (deepLookup.path.length > 1) {
const deepPath = deepLookup.path
.map((i) => path.relative(path.dirname(context.getFilename()), i.path))
.map((i) => path.relative(path.dirname(getFilename(context)), i.path))
.join(' -> ');

context.report(im.key, `${im.key.name} not found via ${deepPath}`);
Expand Down
4 changes: 3 additions & 1 deletion src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* @author Radek Benkel
*/

import { getPhysicalFilename } from 'eslint-module-utils/contextCompat';

import isStaticRequire from '../core/staticRequire';
import docsUrl from '../docsUrl';

Expand Down Expand Up @@ -193,7 +195,7 @@ module.exports = {
}
},
'Program:exit'() {
log('exit processing for', context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename());
log('exit processing for', getPhysicalFilename(context));
const scopeBody = getScopeBody(context.getScope());
log('got scope:', scopeBody);

Expand Down
5 changes: 3 additions & 2 deletions src/rules/no-absolute-path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import path from 'path';
import { getPhysicalFilename } from 'eslint-module-utils/contextCompat';
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';

import { isAbsolute } from '../core/importType';
import docsUrl from '../docsUrl';

Expand All @@ -22,9 +24,8 @@ module.exports = {
node: source,
message: 'Do not import modules using an absolute path',
fix(fixer) {
const resolvedContext = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
// node.js and web imports work with posix style paths ("/")
let relativePath = path.posix.relative(path.dirname(resolvedContext), source.value);
let relativePath = path.posix.relative(path.dirname(getPhysicalFilename(context)), source.value);
if (!relativePath.startsWith('.')) {
relativePath = `./${relativePath}`;
}
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* @author Jamund Ferguson
*/

import { getScope } from 'eslint-module-utils/contextCompat';

import docsUrl from '../docsUrl';

//------------------------------------------------------------------------------
Expand All @@ -23,7 +25,7 @@ module.exports = {
create(context) {
return {
CallExpression(node) {
if (context.getScope().type !== 'module') { return; }
if (getScope(context, node).type !== 'module') { return; }

if (node.callee.type !== 'Identifier') { return; }
if (node.callee.name !== 'require' && node.callee.name !== 'define') { return; }
Expand Down
6 changes: 4 additions & 2 deletions src/rules/no-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* @author Jamund Ferguson
*/

import { getScope } from 'eslint-module-utils/contextCompat';

import docsUrl from '../docsUrl';

const EXPORT_MESSAGE = 'Expected "export" or "export default"';
Expand Down Expand Up @@ -107,7 +109,7 @@ module.exports = {

// exports.
if (node.object.name === 'exports') {
const isInScope = context.getScope()
const isInScope = getScope(context, node)
.variables
.some((variable) => variable.name === 'exports');
if (!isInScope) {
Expand All @@ -117,7 +119,7 @@ module.exports = {

},
CallExpression(call) {
if (!validateScope(context.getScope())) { return; }
if (!validateScope(getScope(context, call))) { return; }

if (call.callee.type !== 'Identifier') { return; }
if (call.callee.name !== 'require') { return; }
Expand Down
6 changes: 4 additions & 2 deletions src/rules/no-cycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
* @author Ben Mosher
*/

import { getPhysicalFilename } from 'eslint-module-utils/contextCompat';
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
import resolve from 'eslint-module-utils/resolve';

import ExportMapBuilder from '../exportMap/builder';
import StronglyConnectedComponentsBuilder from '../scc';
import { isExternalModule } from '../core/importType';
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
import docsUrl from '../docsUrl';

const traversed = new Set();
Expand Down Expand Up @@ -57,7 +59,7 @@ module.exports = {
},

create(context) {
const myPath = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
const myPath = getPhysicalFilename(context);
if (myPath === '<text>') { return {}; } // can't cycle-check a non-file

const options = context.options[0] || {};
Expand Down
6 changes: 4 additions & 2 deletions src/rules/no-default-export.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getSourceCode } from 'eslint-module-utils/contextCompat';

import docsUrl from '../docsUrl';

module.exports = {
Expand All @@ -22,15 +24,15 @@ module.exports = {

return {
ExportDefaultDeclaration(node) {
const { loc } = context.getSourceCode().getFirstTokens(node)[1] || {};
const { loc } = getSourceCode(context).getFirstTokens(node)[1] || {};
context.report({ node, message: preferNamed, loc });
},

ExportNamedDeclaration(node) {
node.specifiers
.filter((specifier) => (specifier.exported.name || specifier.exported.value) === 'default')
.forEach((specifier) => {
const { loc } = context.getSourceCode().getFirstTokens(node)[1] || {};
const { loc } = getSourceCode(context).getFirstTokens(node)[1] || {};
if (specifier.type === 'ExportDefaultSpecifier') {
context.report({ node, message: preferNamed, loc });
} else if (specifier.type === 'ExportSpecifier') {
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getSourceCode } from 'eslint-module-utils/contextCompat';
import resolve from 'eslint-module-utils/resolve';
import semver from 'semver';
import flatMap from 'array.prototype.flatmap';
Expand Down Expand Up @@ -260,7 +261,7 @@ function checkImports(imported, context) {
if (nodes.length > 1) {
const message = `'${module}' imported multiple times.`;
const [first, ...rest] = nodes;
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const fix = getFix(first, rest, sourceCode, context);

context.report({
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-empty-named-blocks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getSourceCode } from 'eslint-module-utils/contextCompat';

import docsUrl from '../docsUrl';

function getEmptyBlockRange(tokens, index) {
Expand Down Expand Up @@ -72,7 +74,7 @@ module.exports = {
fix(fixer) {
// Remove the empty block and the 'from' token, leaving the import only for its side
// effects, e.g. `import 'mod'`
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const fromToken = program.tokens.find((t) => t.value === 'from');
const importToken = program.tokens.find((t) => t.value === 'import');
const hasSpaceAfterFrom = sourceCode.isSpaceBetween(fromToken, sourceCode.getTokenAfter(fromToken));
Expand Down
8 changes: 5 additions & 3 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import path from 'path';
import fs from 'fs';
import pkgUp from 'eslint-module-utils/pkgUp';
import minimatch from 'minimatch';
import { getPhysicalFilename } from 'eslint-module-utils/contextCompat';
import pkgUp from 'eslint-module-utils/pkgUp';
import resolve from 'eslint-module-utils/resolve';
import moduleVisitor from 'eslint-module-utils/moduleVisitor';

import importType from '../core/importType';
import { getFilePackageName } from '../core/packagePath';
import docsUrl from '../docsUrl';
Expand Down Expand Up @@ -84,7 +86,7 @@ function getDependencies(context, packageDir) {
});
} else {
const packageJsonPath = pkgUp({
cwd: context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename(),
cwd: getPhysicalFilename(context),
normalize: false,
});

Expand Down Expand Up @@ -283,7 +285,7 @@ module.exports = {

create(context) {
const options = context.options[0] || {};
const filename = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
const filename = getPhysicalFilename(context);
const deps = getDependencies(context, options.packageDir) || extractDepFields({});

const depsOptions = {
Expand Down
7 changes: 4 additions & 3 deletions src/rules/no-import-module-exports.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import minimatch from 'minimatch';
import path from 'path';
import { getPhysicalFilename, getSourceCode } from 'eslint-module-utils/contextCompat';
import pkgUp from 'eslint-module-utils/pkgUp';

function getEntryPoint(context) {
const pkgPath = pkgUp({ cwd: context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename() });
const pkgPath = pkgUp({ cwd: getPhysicalFilename(context) });
try {
return require.resolve(path.dirname(pkgPath));
} catch (error) {
Expand All @@ -14,7 +15,7 @@ function getEntryPoint(context) {
}

function findScope(context, identifier) {
const { scopeManager } = context.getSourceCode();
const { scopeManager } = getSourceCode(context);

return scopeManager && scopeManager.scopes.slice().reverse().find((scope) => scope.variables.some((variable) => variable.identifiers.some((node) => node.name === identifier)));
}
Expand Down Expand Up @@ -50,7 +51,7 @@ module.exports = {
let alreadyReported = false;

function report(node) {
const fileName = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
const fileName = getPhysicalFilename(context);
const isEntryPoint = entryPoint === fileName;
const isIdentifier = node.object.type === 'Identifier';
const hasKeywords = (/^(module|exports)$/).test(node.object.name);
Expand Down
6 changes: 4 additions & 2 deletions src/rules/no-mutable-exports.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getScope } from 'eslint-module-utils/contextCompat';

import docsUrl from '../docsUrl';

module.exports = {
Expand Down Expand Up @@ -32,15 +34,15 @@ module.exports = {
}

function handleExportDefault(node) {
const scope = context.getScope();
const scope = getScope(context, node);

if (node.declaration.name) {
checkDeclarationsInScope(scope, node.declaration.name);
}
}

function handleExportNamed(node) {
const scope = context.getScope();
const scope = getScope(context, node);

if (node.declaration) {
checkDeclaration(node.declaration);
Expand Down
6 changes: 4 additions & 2 deletions src/rules/no-namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/

import minimatch from 'minimatch';
import { getScope, getSourceCode } from 'eslint-module-utils/contextCompat';

import docsUrl from '../docsUrl';

/**
Expand Down Expand Up @@ -108,7 +110,7 @@ module.exports = {
return;
}

const scopeVariables = context.getScope().variables;
const scopeVariables = getScope(context, node).variables;
const namespaceVariable = scopeVariables.find((variable) => variable.defs[0].node === node);
const namespaceReferences = namespaceVariable.references;
const namespaceIdentifiers = namespaceReferences.map((reference) => reference.identifier);
Expand All @@ -118,7 +120,7 @@ module.exports = {
node,
message: `Unexpected namespace import.`,
fix: canFix && ((fixer) => {
const scopeManager = context.getSourceCode().scopeManager;
const { scopeManager } = getSourceCode(context);
const fixes = [];

// Pass 1: Collect variable names that are already in scope for each reference we want
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-relative-packages.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import readPkgUp from 'eslint-module-utils/readPkgUp';

import { getPhysicalFilename } from 'eslint-module-utils/contextCompat';
import resolve from 'eslint-module-utils/resolve';
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
import importType from '../core/importType';
Expand All @@ -26,7 +27,7 @@ function checkImportForRelativePackage(context, importPath, node) {
}

const resolvedImport = resolve(importPath, context);
const resolvedContext = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
const resolvedContext = getPhysicalFilename(context);

if (!resolvedImport || !resolvedContext) {
return;
Expand Down
Loading

0 comments on commit a7b4348

Please sign in to comment.