Skip to content

Commit b696ef3

Browse files
authored
chore: use context.filename and context.physicalFilename (#1174)
1 parent 8e53e58 commit b696ef3

File tree

13 files changed

+25
-47
lines changed

13 files changed

+25
-47
lines changed

.changeset/petite-impalas-reply.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': patch
3+
---
4+
5+
chore: use `context.filename` and `context.physicalFilename` instead of compat functions.

packages/eslint-plugin-svelte/eslint.config.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ const config = [
7171
'no-restricted-properties': [
7272
'error',
7373
{ object: 'context', property: 'getSourceCode', message: 'Use `context.sourceCode`' },
74-
{ object: 'context', property: 'getFilename', message: 'Use src/utils/compat.ts' },
74+
{ object: 'context', property: 'getFilename', message: 'Use `context.filename`' },
7575
{
7676
object: 'context',
7777
property: 'getPhysicalFilename',
78-
message: 'Use src/utils/compat.ts'
78+
message: 'Use `context.physicalFilename`'
7979
},
8080
{ object: 'context', property: 'getCwd', message: 'Use src/utils/compat.ts' },
8181
{ object: 'context', property: 'getScope', message: 'Use src/utils/compat.ts' },

packages/eslint-plugin-svelte/src/rules/comment-directive.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { AST } from 'svelte-eslint-parser';
22
import { getShared } from '../shared/index.js';
33
import type { CommentDirectives } from '../shared/comment-directives.js';
44
import { createRule } from '../utils/index.js';
5-
import { getFilename } from '../utils/compat.js';
65

76
type RuleAndLocation = {
87
ruleId: string;
@@ -54,7 +53,7 @@ export default createRule('comment-directive', {
5453
type: 'problem'
5554
},
5655
create(context) {
57-
const shared = getShared(getFilename(context));
56+
const shared = getShared(context.filename);
5857
if (!shared) return {};
5958
const options = context.options[0] || {};
6059
const reportUnusedDisableDirectives = Boolean(options.reportUnusedDisableDirectives);

packages/eslint-plugin-svelte/src/rules/indent-helpers/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { isCommentToken } from '@eslint-community/eslint-utils';
88
import type { AnyToken, IndentOptions } from './commons.js';
99
import type { OffsetCalculator } from './offset-context.js';
1010
import { OffsetContext } from './offset-context.js';
11-
import { getFilename } from '../../utils/compat.js';
1211

1312
type IndentUserOptions = {
1413
indent?: number | 'tab';
@@ -78,7 +77,7 @@ export function defineVisitor(
7877
context: RuleContext,
7978
defaultOptions: Partial<IndentOptions>
8079
): RuleListener {
81-
if (!getFilename(context).endsWith('.svelte')) return {};
80+
if (!context.filename.endsWith('.svelte')) return {};
8281

8382
const options = parseOptions(context.options[0] || {}, defaultOptions);
8483
const sourceCode = context.sourceCode;

packages/eslint-plugin-svelte/src/rules/no-unused-props.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { TSESTree } from '@typescript-eslint/types';
44
import type ts from 'typescript';
55
import { findVariable } from '../utils/ast-utils.js';
66
import { toRegExp } from '../utils/regexp.js';
7-
import { getFilename } from '../utils/compat.js';
87

98
type PropertyPathArray = string[];
109

@@ -58,7 +57,7 @@ export default createRule('no-unused-props', {
5857
]
5958
},
6059
create(context) {
61-
const fileName = getFilename(context);
60+
const fileName = context.filename;
6261
const tools = getTypeScriptTools(context);
6362
if (!tools) {
6463
return {};

packages/eslint-plugin-svelte/src/rules/system.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { getShared } from '../shared/index.js';
22
import { createRule } from '../utils/index.js';
3-
import { getFilename } from '../utils/compat.js';
43
import { isRegExp, toRegExp } from '../utils/regexp.js';
54

65
export default createRule('system', {
@@ -15,7 +14,7 @@ export default createRule('system', {
1514
type: 'problem'
1615
},
1716
create(context) {
18-
const shared = getShared(getFilename(context));
17+
const shared = getShared(context.filename);
1918
if (!shared) return {};
2019

2120
const directives = shared.newCommentDirectives({

packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/transform/less.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type less from 'less';
33
import type { RuleContext } from '../../../types.js';
44
import type { TransformResult } from './types.js';
55
import { loadModule } from '../../../utils/load-module.js';
6-
import { getFilename } from '../../../utils/compat.js';
76

87
type Less = typeof less;
98
/**
@@ -26,7 +25,7 @@ export function transform(
2625
}
2726
const code = text.slice(...inputRange);
2827

29-
const filename = `${getFilename(context)}.less`;
28+
const filename = `${context.filename}.less`;
3029
try {
3130
let output: Awaited<ReturnType<Less['render']>> | undefined;
3231

packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/transform/postcss.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import postcss from 'postcss';
33
import postcssLoadConfig from 'postcss-load-config';
44
import type { RuleContext } from '../../../types.js';
55
import type { TransformResult } from './types.js';
6-
import { getCwd, getFilename } from '../../../utils/compat.js';
6+
import { getCwd } from '../../../utils/compat.js';
77

88
/**
99
* Transform with postcss
@@ -25,7 +25,7 @@ export function transform(
2525
}
2626
const code = text.slice(...inputRange);
2727

28-
const filename = `${getFilename(context)}.css`;
28+
const filename = `${context.filename}.css`;
2929
try {
3030
const configFilePath = postcssConfig?.configFilePath;
3131

packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/transform/stylus.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { RawSourceMap } from 'source-map-js';
44
import type { RuleContext } from '../../../types.js';
55
import type { TransformResult } from './types.js';
66
import { loadModule } from '../../../utils/load-module.js';
7-
import { getFilename } from '../../../utils/compat.js';
87

98
type Stylus = typeof stylus;
109
/**
@@ -27,7 +26,7 @@ export function transform(
2726
}
2827
const code = text.slice(...inputRange);
2928

30-
const filename = `${getFilename(context)}.stylus`;
29+
const filename = `${context.filename}.stylus`;
3130
try {
3231
let output: string | undefined;
3332

packages/eslint-plugin-svelte/src/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export type RuleContext = {
149149

150150
getDeclaredVariables(node: TSESTree.Node): Variable[];
151151

152-
getFilename(): string;
152+
filename: string;
153153

154154
getScope(): Scope;
155155

@@ -161,8 +161,8 @@ export type RuleContext = {
161161

162162
// eslint@6 does not have this method.
163163
getCwd?: () => string;
164-
// eslint@<7.11.0 does not have this method.
165-
getPhysicalFilename?: () => string;
164+
165+
physicalFilename: string;
166166
};
167167

168168
export type NodeOrToken = {

packages/eslint-plugin-svelte/src/utils/compat.ts

+1-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
1-
import {
2-
getFilename as getFilenameBase,
3-
getPhysicalFilename as getPhysicalFilenameBase,
4-
getCwd as getCwdBase
5-
} from 'eslint-compat-utils';
1+
import { getCwd as getCwdBase } from 'eslint-compat-utils';
62
import type { RuleContext } from '../types.js';
73

8-
/**
9-
* Gets the value of `context.filename`, but for older ESLint it returns the result of `context.getFilename()`.
10-
*/
11-
export function getFilename(context: RuleContext): string {
12-
return getFilenameBase(context as never);
13-
}
14-
/**
15-
* Gets the value of `context.physicalFilename`,
16-
* but for older ESLint it returns the result of `context.getPhysicalFilename()`.
17-
* Versions older than v7.28.0 return a value guessed from the result of `context.getFilename()`,
18-
* but it may be incorrect.
19-
*/
20-
export function getPhysicalFilename(context: RuleContext): string {
21-
return getPhysicalFilenameBase(context as never);
22-
}
23-
244
/**
255
* Gets the value of `context.cwd`, but for older ESLint it returns the result of `context.getCwd()`.
266
* Versions older than v6.6.0 return a value from the result of `process.cwd()`.

packages/eslint-plugin-svelte/src/utils/load-module.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { AST } from 'svelte-eslint-parser';
22
import Module from 'module';
33
import path from 'path';
44
import type { RuleContext } from '../types.js';
5-
import { getCwd, getFilename, getPhysicalFilename } from './compat.js';
5+
import { getCwd } from './compat.js';
66
const cache = new WeakMap<AST.SvelteProgram, Record<string, unknown>>();
77
const cache4b = new Map<string, unknown>();
88
/**
@@ -27,9 +27,9 @@ export function loadModule<R>(context: RuleContext, name: string): R | null {
2727
}
2828
for (const relativeTo of [
2929
// load from lint file name
30-
getFilename(context),
30+
context.filename,
3131
// load from lint file name (physical)
32-
getPhysicalFilename(context),
32+
context.physicalFilename,
3333
// load from this plugin module
3434
typeof __filename !== 'undefined' ? __filename : ''
3535
]) {

packages/eslint-plugin-svelte/src/utils/svelte-context.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import fs from 'fs';
33
import path from 'path';
44
import { getPackageJsons } from './get-package-json.js';
55
import { getNodeModule } from './get-node-module.js';
6-
import { getFilename } from './compat.js';
76
import { createCache } from './cache.js';
87
import { VERSION as SVELTE_VERSION } from 'svelte/compiler';
98

@@ -122,7 +121,7 @@ const svelteKitContextCache = createCache<Pick<
122121
function getSvelteKitContext(
123122
context: RuleContext
124123
): Pick<SvelteContext, 'svelteKitFileType' | 'svelteKitVersion'> {
125-
const filePath = getFilename(context);
124+
const filePath = context.filename;
126125

127126
const cached = svelteKitContextCache.get(filePath);
128127
if (cached) return cached;
@@ -151,7 +150,7 @@ function getSvelteKitContext(
151150
context.settings?.svelte?.kit?.files?.routes ??
152151
context.sourceCode.parserServices.svelteParseContext?.svelteConfig?.kit?.files?.routes
153152
)?.replace(/^\//, '') ?? 'src/routes';
154-
const projectRootDir = getProjectRootDir(getFilename(context)) ?? '';
153+
const projectRootDir = getProjectRootDir(context.filename) ?? '';
155154

156155
if (!filePath.startsWith(path.join(projectRootDir, routes))) {
157156
const result: Pick<SvelteContext, 'svelteKitFileType' | 'svelteKitVersion'> = {
@@ -288,7 +287,7 @@ const svelteContextCache = createCache<SvelteContext | null>();
288287
export function getSvelteContext(context: RuleContext): SvelteContext | null {
289288
const { parserServices } = context.sourceCode;
290289
const { svelteParseContext } = parserServices;
291-
const filePath = getFilename(context);
290+
const filePath = context.filename;
292291

293292
const cached = svelteContextCache.get(filePath);
294293
if (cached) return cached;

0 commit comments

Comments
 (0)