Skip to content

chore: use context.filename and context.physicalFilename #1174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/petite-impalas-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

chore: use `context.filename` and `context.physicalFilename` instead of compat functions.
4 changes: 2 additions & 2 deletions packages/eslint-plugin-svelte/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ const config = [
'no-restricted-properties': [
'error',
{ object: 'context', property: 'getSourceCode', message: 'Use `context.sourceCode`' },
{ object: 'context', property: 'getFilename', message: 'Use src/utils/compat.ts' },
{ object: 'context', property: 'getFilename', message: 'Use `context.filename`' },
{
object: 'context',
property: 'getPhysicalFilename',
message: 'Use src/utils/compat.ts'
message: 'Use `context.physicalFilename`'
},
{ object: 'context', property: 'getCwd', message: 'Use src/utils/compat.ts' },
{ object: 'context', property: 'getScope', message: 'Use src/utils/compat.ts' },
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin-svelte/src/rules/comment-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { AST } from 'svelte-eslint-parser';
import { getShared } from '../shared/index.js';
import type { CommentDirectives } from '../shared/comment-directives.js';
import { createRule } from '../utils/index.js';
import { getFilename } from '../utils/compat.js';

type RuleAndLocation = {
ruleId: string;
Expand Down Expand Up @@ -54,7 +53,7 @@ export default createRule('comment-directive', {
type: 'problem'
},
create(context) {
const shared = getShared(getFilename(context));
const shared = getShared(context.filename);
if (!shared) return {};
const options = context.options[0] || {};
const reportUnusedDisableDirectives = Boolean(options.reportUnusedDisableDirectives);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { isCommentToken } from '@eslint-community/eslint-utils';
import type { AnyToken, IndentOptions } from './commons.js';
import type { OffsetCalculator } from './offset-context.js';
import { OffsetContext } from './offset-context.js';
import { getFilename } from '../../utils/compat.js';

type IndentUserOptions = {
indent?: number | 'tab';
Expand Down Expand Up @@ -78,7 +77,7 @@ export function defineVisitor(
context: RuleContext,
defaultOptions: Partial<IndentOptions>
): RuleListener {
if (!getFilename(context).endsWith('.svelte')) return {};
if (!context.filename.endsWith('.svelte')) return {};

const options = parseOptions(context.options[0] || {}, defaultOptions);
const sourceCode = context.sourceCode;
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin-svelte/src/rules/no-unused-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import type ts from 'typescript';
import { findVariable } from '../utils/ast-utils.js';
import { toRegExp } from '../utils/regexp.js';
import { getFilename } from '../utils/compat.js';

type PropertyPathArray = string[];

Expand Down Expand Up @@ -58,7 +57,7 @@
]
},
create(context) {
const fileName = getFilename(context);
const fileName = context.filename;
const tools = getTypeScriptTools(context);
if (!tools) {
return {};
Expand All @@ -71,11 +70,11 @@

const options = context.options[0] ?? {};

// TODO: Remove in v4

Check warning on line 73 in packages/eslint-plugin-svelte/src/rules/no-unused-props.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: Remove in v4'
// MEMO: `ignorePatterns` was a property that only existed from v3.2.0 to v3.2.2.
// From v3.3.0, it was replaced with `ignorePropertyPatterns` and `ignoreTypePatterns`.
if (options.ignorePatterns != null && !isRemovedWarningShown) {
console.warn(

Check warning on line 77 in packages/eslint-plugin-svelte/src/rules/no-unused-props.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
'eslint-plugin-svelte: The `ignorePatterns` option in the `no-unused-props` rule has been removed. Please use `ignorePropertyPatterns` or/and `ignoreTypePatterns` instead.'
);
isRemovedWarningShown = true;
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin-svelte/src/rules/system.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getShared } from '../shared/index.js';
import { createRule } from '../utils/index.js';
import { getFilename } from '../utils/compat.js';
import { isRegExp, toRegExp } from '../utils/regexp.js';

export default createRule('system', {
Expand All @@ -15,7 +14,7 @@ export default createRule('system', {
type: 'problem'
},
create(context) {
const shared = getShared(getFilename(context));
const shared = getShared(context.filename);
if (!shared) return {};

const directives = shared.newCommentDirectives({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type less from 'less';
import type { RuleContext } from '../../../types.js';
import type { TransformResult } from './types.js';
import { loadModule } from '../../../utils/load-module.js';
import { getFilename } from '../../../utils/compat.js';

type Less = typeof less;
/**
Expand All @@ -26,7 +25,7 @@ export function transform(
}
const code = text.slice(...inputRange);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import postcss from 'postcss';
import postcssLoadConfig from 'postcss-load-config';
import type { RuleContext } from '../../../types.js';
import type { TransformResult } from './types.js';
import { getCwd, getFilename } from '../../../utils/compat.js';
import { getCwd } from '../../../utils/compat.js';

/**
* Transform with postcss
Expand All @@ -25,7 +25,7 @@ export function transform(
}
const code = text.slice(...inputRange);

const filename = `${getFilename(context)}.css`;
const filename = `${context.filename}.css`;
try {
const configFilePath = postcssConfig?.configFilePath;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { RawSourceMap } from 'source-map-js';
import type { RuleContext } from '../../../types.js';
import type { TransformResult } from './types.js';
import { loadModule } from '../../../utils/load-module.js';
import { getFilename } from '../../../utils/compat.js';

type Stylus = typeof stylus;
/**
Expand All @@ -27,7 +26,7 @@ export function transform(
}
const code = text.slice(...inputRange);

const filename = `${getFilename(context)}.stylus`;
const filename = `${context.filename}.stylus`;
try {
let output: string | undefined;

Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin-svelte/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export type RuleContext = {

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

getFilename(): string;
filename: string;

getScope(): Scope;

Expand All @@ -161,8 +161,8 @@ export type RuleContext = {

// eslint@6 does not have this method.
getCwd?: () => string;
// eslint@<7.11.0 does not have this method.
getPhysicalFilename?: () => string;

physicalFilename: string;
};

export type NodeOrToken = {
Expand Down
22 changes: 1 addition & 21 deletions packages/eslint-plugin-svelte/src/utils/compat.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
import {
getFilename as getFilenameBase,
getPhysicalFilename as getPhysicalFilenameBase,
getCwd as getCwdBase
} from 'eslint-compat-utils';
import { getCwd as getCwdBase } from 'eslint-compat-utils';
import type { RuleContext } from '../types.js';

/**
* Gets the value of `context.filename`, but for older ESLint it returns the result of `context.getFilename()`.
*/
export function getFilename(context: RuleContext): string {
return getFilenameBase(context as never);
}
/**
* Gets the value of `context.physicalFilename`,
* but for older ESLint it returns the result of `context.getPhysicalFilename()`.
* Versions older than v7.28.0 return a value guessed from the result of `context.getFilename()`,
* but it may be incorrect.
*/
export function getPhysicalFilename(context: RuleContext): string {
return getPhysicalFilenameBase(context as never);
}

/**
* Gets the value of `context.cwd`, but for older ESLint it returns the result of `context.getCwd()`.
* Versions older than v6.6.0 return a value from the result of `process.cwd()`.
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin-svelte/src/utils/load-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AST } from 'svelte-eslint-parser';
import Module from 'module';
import path from 'path';
import type { RuleContext } from '../types.js';
import { getCwd, getFilename, getPhysicalFilename } from './compat.js';
import { getCwd } from './compat.js';
const cache = new WeakMap<AST.SvelteProgram, Record<string, unknown>>();
const cache4b = new Map<string, unknown>();
/**
Expand All @@ -27,9 +27,9 @@ export function loadModule<R>(context: RuleContext, name: string): R | null {
}
for (const relativeTo of [
// load from lint file name
getFilename(context),
context.filename,
// load from lint file name (physical)
getPhysicalFilename(context),
context.physicalFilename,
// load from this plugin module
typeof __filename !== 'undefined' ? __filename : ''
]) {
Expand Down
7 changes: 3 additions & 4 deletions packages/eslint-plugin-svelte/src/utils/svelte-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import fs from 'fs';
import path from 'path';
import { getPackageJsons } from './get-package-json.js';
import { getNodeModule } from './get-node-module.js';
import { getFilename } from './compat.js';
import { createCache } from './cache.js';
import { VERSION as SVELTE_VERSION } from 'svelte/compiler';

Expand Down Expand Up @@ -122,7 +121,7 @@ const svelteKitContextCache = createCache<Pick<
function getSvelteKitContext(
context: RuleContext
): Pick<SvelteContext, 'svelteKitFileType' | 'svelteKitVersion'> {
const filePath = getFilename(context);
const filePath = context.filename;

const cached = svelteKitContextCache.get(filePath);
if (cached) return cached;
Expand Down Expand Up @@ -151,7 +150,7 @@ function getSvelteKitContext(
context.settings?.svelte?.kit?.files?.routes ??
context.sourceCode.parserServices.svelteParseContext?.svelteConfig?.kit?.files?.routes
)?.replace(/^\//, '') ?? 'src/routes';
const projectRootDir = getProjectRootDir(getFilename(context)) ?? '';
const projectRootDir = getProjectRootDir(context.filename) ?? '';

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

const cached = svelteContextCache.get(filePath);
if (cached) return cached;
Expand Down