Skip to content

Commit a415b02

Browse files
committed
refactor: replace usages of context.getPhysicalFilename
1 parent 2263fb6 commit a415b02

16 files changed

+37
-32
lines changed

src/context.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ export function getFilename(context) {
22
return context.filename ?? context.getFilename();
33
}
44

5+
export function getPhysicalFilename(context) {
6+
return context.getPhysicalFilename?.() ?? getFilename(context);
7+
}
8+
59
export function getSourceCode(context) {
610
return context.sourceCode ?? context.getSourceCode();
711
}

src/core/packagePath.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { dirname } from 'path';
22
import pkgUp from 'eslint-module-utils/pkgUp';
33
import readPkgUp from 'eslint-module-utils/readPkgUp';
4-
import { getFilename } from '../context';
4+
import { getPhysicalFilename } from '../context';
55

66
export function getFilePackagePath(filePath) {
77
const fp = pkgUp({ cwd: filePath });
88
return dirname(fp);
99
}
1010

1111
export function getContextPackagePath(context) {
12-
return getFilePackagePath(context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context));
12+
return getFilePackagePath(getPhysicalFilename(context));
1313
}
1414

1515
export function getFilePackageName(filePath) {

src/rules/named.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from 'path';
22
import ExportMapBuilder from '../exportMap/builder';
33
import docsUrl from '../docsUrl';
4-
import { getFilename } from '../context';
4+
import { getFilename, getPhysicalFilename } from '../context';
55

66
module.exports = {
77
meta: {
@@ -68,7 +68,7 @@ module.exports = {
6868
if (!deepLookup.found) {
6969
if (deepLookup.path.length > 1) {
7070
const deepPath = deepLookup.path
71-
.map((i) => path.relative(path.dirname(context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context)), i.path))
71+
.map((i) => path.relative(path.dirname(getPhysicalFilename(context)), i.path))
7272
.join(' -> ');
7373

7474
context.report(im[key], `${name} not found via ${deepPath}`);

src/rules/newline-after-import.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import isStaticRequire from '../core/staticRequire';
77
import docsUrl from '../docsUrl';
88

99
import debug from 'debug';
10-
import { getFilename, getScope } from '../context';
10+
import { getPhysicalFilename, getScope } from '../context';
11+
1112
const log = debug('eslint-plugin-import:rules:newline-after-import');
1213

1314
//------------------------------------------------------------------------------
@@ -194,7 +195,7 @@ module.exports = {
194195
}
195196
},
196197
'Program:exit'() {
197-
log('exit processing for', context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context));
198+
log('exit processing for', getPhysicalFilename(context));
198199

199200
requireCalls.forEach((node, index) => {
200201
// todo: this probably isn't correct...

src/rules/no-absolute-path.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'path';
22
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
33
import { isAbsolute } from '../core/importType';
44
import docsUrl from '../docsUrl';
5-
import { getFilename } from '../context';
5+
import { getPhysicalFilename } from '../context';
66

77
module.exports = {
88
meta: {
@@ -23,7 +23,7 @@ module.exports = {
2323
node: source,
2424
message: 'Do not import modules using an absolute path',
2525
fix(fixer) {
26-
const resolvedContext = context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context);
26+
const resolvedContext = getPhysicalFilename(context);
2727
// node.js and web imports work with posix style paths ("/")
2828
let relativePath = path.posix.relative(path.dirname(resolvedContext), source.value);
2929
if (!relativePath.startsWith('.')) {

src/rules/no-cycle.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ExportMapBuilder from '../exportMap/builder';
88
import { isExternalModule } from '../core/importType';
99
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
1010
import docsUrl from '../docsUrl';
11-
import { getFilename } from '../context';
11+
import { getPhysicalFilename } from '../context';
1212

1313
const traversed = new Set();
1414

@@ -52,7 +52,7 @@ module.exports = {
5252
},
5353

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

5858
const options = context.options[0] || {};

src/rules/no-extraneous-dependencies.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import moduleVisitor from 'eslint-module-utils/moduleVisitor';
77
import importType from '../core/importType';
88
import { getFilePackageName } from '../core/packagePath';
99
import docsUrl from '../docsUrl';
10-
import { getFilename } from '../context';
10+
import { getPhysicalFilename } from '../context';
1111

1212
const depFieldCache = new Map();
1313

@@ -80,7 +80,7 @@ function getDependencies(context, packageDir) {
8080
});
8181
} else {
8282
const packageJsonPath = pkgUp({
83-
cwd: context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context),
83+
cwd: getPhysicalFilename(context),
8484
normalize: false,
8585
});
8686

@@ -279,7 +279,7 @@ module.exports = {
279279

280280
create(context) {
281281
const options = context.options[0] || {};
282-
const filename = context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context);
282+
const filename = getPhysicalFilename(context);
283283
const deps = getDependencies(context, options.packageDir) || extractDepFields({});
284284

285285
const depsOptions = {

src/rules/no-import-module-exports.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import minimatch from 'minimatch';
22
import path from 'path';
33
import pkgUp from 'eslint-module-utils/pkgUp';
4-
import { getFilename, getSourceCode } from '../context';
4+
import { getPhysicalFilename, getSourceCode } from '../context';
55

66
function getEntryPoint(context) {
7-
const pkgPath = pkgUp({ cwd: context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context) });
7+
const pkgPath = pkgUp({ cwd: getPhysicalFilename(context) });
88
try {
99
return require.resolve(path.dirname(pkgPath));
1010
} catch (error) {
@@ -51,7 +51,7 @@ module.exports = {
5151
let alreadyReported = false;
5252

5353
function report(node) {
54-
const fileName = context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context);
54+
const fileName = getPhysicalFilename(context);
5555
const isEntryPoint = entryPoint === fileName;
5656
const isIdentifier = node.object.type === 'Identifier';
5757
const hasKeywords = (/^(module|exports)$/).test(node.object.name);

src/rules/no-relative-packages.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import resolve from 'eslint-module-utils/resolve';
55
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
66
import importType from '../core/importType';
77
import docsUrl from '../docsUrl';
8-
import { getFilename } from '../context';
8+
import { getPhysicalFilename } from '../context';
99

1010
/** @param {string} filePath */
1111
function toPosixPath(filePath) {
@@ -27,7 +27,7 @@ function checkImportForRelativePackage(context, importPath, node) {
2727
}
2828

2929
const resolvedImport = resolve(importPath, context);
30-
const resolvedContext = context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context);
30+
const resolvedContext = getPhysicalFilename(context);
3131

3232
if (!resolvedImport || !resolvedContext) {
3333
return;

src/rules/no-relative-parent-imports.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { basename, dirname, relative } from 'path';
44
import resolve from 'eslint-module-utils/resolve';
55

66
import importType from '../core/importType';
7-
import { getFilename } from '../context';
7+
import { getPhysicalFilename } from '../context';
88

99
module.exports = {
1010
meta: {
@@ -18,7 +18,7 @@ module.exports = {
1818
},
1919

2020
create: function noRelativePackages(context) {
21-
const myPath = context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context);
21+
const myPath = getPhysicalFilename(context);
2222
if (myPath === '<text>') { return {}; } // can't check a non-file
2323

2424
function checkSourceValue(sourceNode) {

src/rules/no-restricted-paths.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import isGlob from 'is-glob';
66
import { Minimatch } from 'minimatch';
77
import docsUrl from '../docsUrl';
88
import importType from '../core/importType';
9-
import { getFilename } from '../context';
9+
import { getPhysicalFilename } from '../context';
1010

1111
const containsPath = (filepath, target) => {
1212
const relative = path.relative(target, filepath);
@@ -86,7 +86,7 @@ module.exports = {
8686
const options = context.options[0] || {};
8787
const restrictedPaths = options.zones || [];
8888
const basePath = options.basePath || process.cwd();
89-
const currentFilename = context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context);
89+
const currentFilename = getPhysicalFilename(context);
9090
const matchingZones = restrictedPaths.filter(
9191
(zone) => [].concat(zone.target)
9292
.map((target) => path.resolve(basePath, target))

src/rules/no-self-import.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import resolve from 'eslint-module-utils/resolve';
77
import moduleVisitor from 'eslint-module-utils/moduleVisitor';
88
import docsUrl from '../docsUrl';
9-
import { getFilename } from '../context';
9+
import { getPhysicalFilename } from '../context';
1010

1111
function isImportingSelf(context, node, requireName) {
12-
const filePath = context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context);
12+
const filePath = getPhysicalFilename(context);
1313

1414
// If the input is from stdin, this test can't fail
1515
if (filePath !== '<text>' && filePath === resolve(requireName, context)) {

src/rules/no-unassigned-import.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import minimatch from 'minimatch';
33

44
import isStaticRequire from '../core/staticRequire';
55
import docsUrl from '../docsUrl';
6-
import { getFilename } from '../context';
6+
import { getPhysicalFilename } from '../context';
77

88
function report(context, node) {
99
context.report({
@@ -32,7 +32,7 @@ function testIsAllow(globs, filename, source) {
3232

3333
function create(context) {
3434
const options = context.options[0] || {};
35-
const filename = context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context);
35+
const filename = getPhysicalFilename(context);
3636
const isAllow = (source) => testIsAllow(options.allow, filename, source);
3737

3838
return {

src/rules/no-unused-modules.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import flatMap from 'array.prototype.flatmap';
1616
import ExportMapBuilder from '../exportMap/builder';
1717
import recursivePatternCapture from '../exportMap/patternCapture';
1818
import docsUrl from '../docsUrl';
19-
import { getFilename } from '../context';
19+
import { getPhysicalFilename } from '../context';
2020

2121
let FileEnumerator;
2222
let listFilesToProcess;
@@ -477,7 +477,7 @@ module.exports = {
477477
doPreparation(src, ignoreExports, context);
478478
}
479479

480-
const file = context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context);
480+
const file = getPhysicalFilename(context);
481481

482482
const checkExportPresence = (node) => {
483483
if (!missingExports) {

src/rules/no-useless-path-segments.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import moduleVisitor from 'eslint-module-utils/moduleVisitor';
88
import resolve from 'eslint-module-utils/resolve';
99
import path from 'path';
1010
import docsUrl from '../docsUrl';
11-
import { getFilename } from '../context';
11+
import { getPhysicalFilename } from '../context';
1212

1313
/**
1414
* convert a potentially relative path from node utils into a true
@@ -61,7 +61,7 @@ module.exports = {
6161
},
6262

6363
create(context) {
64-
const currentDir = path.dirname(context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context));
64+
const currentDir = path.dirname(getPhysicalFilename(context));
6565
const options = context.options[0];
6666

6767
function checkSourceValue(source) {

utils/resolve.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports.__esModule = true;
55
const fs = require('fs');
66
const Module = require('module');
77
const path = require('path');
8-
const { getFilename } = require('../src/context');
8+
const { getFilename, getPhysicalFilename } = require('../src/context');
99

1010
const hashObject = require('./hash').hashObject;
1111
const ModuleCache = require('./ModuleCache').default;
@@ -230,7 +230,7 @@ const erroredContexts = new Set();
230230
*/
231231
function resolve(p, context) {
232232
try {
233-
return relative(p, context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context), context.settings);
233+
return relative(p, getPhysicalFilename(context), context.settings);
234234
} catch (err) {
235235
if (!erroredContexts.has(context)) {
236236
// The `err.stack` string starts with `err.name` followed by colon and `err.message`.

0 commit comments

Comments
 (0)