Skip to content

chore: use context.sourceCode instead of compat #1173

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 5 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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/happy-donuts-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

chore: use `context.sourceCode` directly rather than a compatibility helper.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
isTupleType
} from '../../utils/ts-utils/index.js';
import type { TS, TSTools } from '../../utils/ts-utils/index.js';
import { getSourceCode } from '../../utils/compat.js';

/**
* Returns all types of a union type or an array containing `type` itself if it's no union type.
Expand Down Expand Up @@ -157,7 +156,7 @@ export default createRule('@typescript-eslint/no-unnecessary-condition', {

const { service, ts } = tools;
const checker = service.program.getTypeChecker();
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;
const compilerOptions = service.program.getCompilerOptions();
const isStrictNullChecks = compilerOptions.strict
? compilerOptions.strictNullChecks !== false
Expand Down
7 changes: 3 additions & 4 deletions packages/eslint-plugin-svelte/src/rules/block-lang.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createRule } from '../utils/index.js';
import { findAttribute, getLangValue } from '../utils/ast-utils.js';
import type { SvelteScriptElement, SvelteStyleElement } from 'svelte-eslint-parser/lib/ast';
import { getSourceCode } from '../utils/compat.js';
import type { SuggestionReportDescriptor, SourceCode } from '../types.js';

export default createRule('block-lang', {
Expand Down Expand Up @@ -59,7 +58,7 @@ export default createRule('block-lang', {
hasSuggestions: true
},
create(context) {
if (!getSourceCode(context).parserServices.isSvelte) {
if (!context.sourceCode.parserServices.isSvelte) {
return {};
}
const enforceScriptPresent: boolean = context.options[0]?.enforceScriptPresent ?? false;
Expand Down Expand Up @@ -91,7 +90,7 @@ export default createRule('block-lang', {
message: `The <script> block should be present and its lang attribute should be ${prettyPrintLangs(
allowedScriptLangs
)}.`,
suggest: buildAddLangSuggestions(allowedScriptLangs, 'script', getSourceCode(context))
suggest: buildAddLangSuggestions(allowedScriptLangs, 'script', context.sourceCode)
});
}
for (const scriptNode of scriptNodes) {
Expand All @@ -106,7 +105,7 @@ export default createRule('block-lang', {
}
}
if (styleNodes.length === 0 && enforceStylePresent) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;
context.report({
loc: { line: 1, column: 1 },
message: `The <style> block should be present and its lang attribute should be ${prettyPrintLangs(
Expand Down
4 changes: 2 additions & 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,7 @@ 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, getSourceCode } from '../utils/compat.js';
import { getFilename } from '../utils/compat.js';

type RuleAndLocation = {
ruleId: string;
Expand Down Expand Up @@ -63,7 +63,7 @@ export default createRule('comment-directive', {
reportUnusedDisableDirectives
});

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

/**
* Parse a given comment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
Tag as SelectorTag
} from 'postcss-selector-parser';
import { findClassesInAttribute } from '../utils/ast-utils.js';
import { getSourceCode } from '../utils/compat.js';
import { createRule } from '../utils/index.js';

export default createRule('consistent-selector-style', {
Expand Down Expand Up @@ -48,7 +47,7 @@ export default createRule('consistent-selector-style', {
type: 'suggestion'
},
create(context) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;
if (
!sourceCode.parserServices.isSvelte ||
sourceCode.parserServices.getStyleSelectorAST === undefined ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { AST } from 'svelte-eslint-parser';
import { createRule } from '../utils/index.js';
import { getSourceCode } from '../utils/compat.js';

export default createRule('first-attribute-linebreak', {
meta: {
Expand Down Expand Up @@ -30,7 +29,7 @@ export default createRule('first-attribute-linebreak', {
create(context) {
const multiline: 'below' | 'beside' = context.options[0]?.multiline || 'below';
const singleline: 'below' | 'beside' = context.options[0]?.singleline || 'beside';
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;

/**
* Report attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { AST } from 'svelte-eslint-parser';
import { createRule } from '../utils/index.js';
import { getSourceCode } from '../utils/compat.js';
import type { SourceCode } from '../types.js';

type ExpectedNode = AST.SvelteStartTag | AST.SvelteEndTag;
Expand Down Expand Up @@ -123,7 +122,7 @@ export default createRule('html-closing-bracket-new-line', {
options.singleline ??= 'never';
options.multiline ??= 'always';

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

return {
'SvelteStartTag, SvelteEndTag'(node: ExpectedNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default createRule('html-closing-bracket-spacing', {
selfClosingTag: 'always',
...ctx.options[0]
};
const src = ctx.getSourceCode();
const src = ctx.sourceCode;

/**
* Returns true if string contains newline characters
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin-svelte/src/rules/html-quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { createRule } from '../utils/index.js';
import type { QuoteAndRange } from '../utils/ast-utils.js';
import { getMustacheTokens } from '../utils/ast-utils.js';
import { getAttributeValueQuoteAndRange } from '../utils/ast-utils.js';
import { getSourceCode } from '../utils/compat.js';

const QUOTE_CHARS = {
double: '"',
Expand Down Expand Up @@ -49,7 +48,7 @@ export default createRule('html-quotes', {
type: 'layout' // "problem",
},
create(context) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;
const preferQuote: 'double' | 'single' = context.options[0]?.prefer ?? 'double';
const dynamicQuote = context.options[0]?.dynamic?.quoted ? preferQuote : 'unquoted';
const avoidInvalidUnquotedInHTML = Boolean(
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin-svelte/src/rules/html-self-closing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
isSvgElement,
isMathMLElement
} from '../utils/ast-utils.js';
import { getSourceCode } from '../utils/compat.js';

const TYPE_MESSAGES = {
normal: 'HTML elements',
Expand Down Expand Up @@ -160,7 +159,7 @@ export default createRule('html-self-closing', {
context.report({
node,
loc: {
start: getSourceCode(context).getLocFromIndex(
start: context.sourceCode.getLocFromIndex(
node.startTag.range[1] - (node.startTag.selfClosing ? 2 : 1)
),
end: node.loc.end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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, getSourceCode } from '../../utils/compat.js';
import { getFilename } from '../../utils/compat.js';

type IndentUserOptions = {
indent?: number | 'tab';
Expand Down Expand Up @@ -81,7 +81,7 @@ export function defineVisitor(
if (!getFilename(context).endsWith('.svelte')) return {};

const options = parseOptions(context.options[0] || {}, defaultOptions);
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;
const offsets = new OffsetContext({ sourceCode, options });

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import { createRule } from '../utils/index.js';
import type { RuleContext } from '../types.js';
import { findVariable } from '../utils/ast-utils.js';
import { traverseNodes } from 'svelte-eslint-parser';
import { getSourceCode } from '../utils/compat.js';

/**
* Get usage of `tick`
*/
function extractTickReferences(
context: RuleContext
): { node: TSESTree.CallExpression; name: string }[] {
const referenceTracker = new ReferenceTracker(getSourceCode(context).scopeManager.globalScope!);
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope!);
const a = referenceTracker.iterateEsmReferences({
svelte: {
[ReferenceTracker.ESM]: true,
Expand All @@ -36,7 +35,7 @@ function extractTickReferences(
function extractTaskReferences(
context: RuleContext
): { node: TSESTree.CallExpression; name: string }[] {
const referenceTracker = new ReferenceTracker(getSourceCode(context).scopeManager.globalScope!);
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope!);
const a = referenceTracker.iterateGlobalReferences({
setTimeout: { [ReferenceTracker.CALL]: true },
setInterval: { [ReferenceTracker.CALL]: true },
Expand Down Expand Up @@ -123,7 +122,7 @@ function isPromiseThenOrCatchBody(node: TSESTree.Node): boolean {
* Get all reactive variable reference.
*/
function getReactiveVariableReferences(context: RuleContext) {
const scopeManager = getSourceCode(context).scopeManager;
const scopeManager = context.sourceCode.scopeManager;
// Find the top-level (module or global) scope.
// Any variable defined at the top-level (module scope or global scope) can be made reactive.
const toplevelScope =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { AST } from 'svelte-eslint-parser';
import { createRule } from '../utils/index.js';
import { getSourceCode } from '../utils/compat.js';

/**
* Check whether the component is declared in a single line or not.
Expand Down Expand Up @@ -58,7 +57,7 @@ export default createRule('max-attributes-per-line', {
create(context) {
const multilineMaximum = context.options[0]?.multiline ?? 1;
const singlelineMaximum = context.options[0]?.singleline ?? 1;
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;

/**
* Report attributes
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin-svelte/src/rules/mustache-spacing.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 { isClosingBraceToken, isOpeningBraceToken } from '@eslint-community/eslint-utils';
import { createRule } from '../utils/index.js';
import { getMustacheTokens } from '../utils/ast-utils.js';
import { getSourceCode } from '../utils/compat.js';
type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
Expand Down Expand Up @@ -74,7 +73,7 @@ export default createRule('mustache-spacing', {
},
create(context) {
const options = parseOptions(context.options[0]);
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;

function verifyBraces(
openingBrace: AST.Token | AST.Comment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { AST } from 'svelte-eslint-parser';
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils/index.js';
import { equalTokens } from '../utils/ast-utils.js';
import { getSourceCode } from '../utils/compat.js';

// ------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -82,7 +81,7 @@ export default createRule('no-dupe-else-if-blocks', {
type: 'problem' // "problem",
},
create(context) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;

/**
* Determines whether the two given nodes are considered to be equal. In particular, given that the nodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { AST } from 'svelte-eslint-parser';
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils/index.js';
import { equalTokens } from '../utils/ast-utils.js';
import { getSourceCode } from '../utils/compat.js';

export default createRule('no-dupe-on-directives', {
meta: {
Expand All @@ -19,7 +18,7 @@ export default createRule('no-dupe-on-directives', {
type: 'problem'
},
create(context) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;

const directiveDataMap = new Map<
string, // event type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { AST } from 'svelte-eslint-parser';
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils/index.js';
import { equalTokens, getAttributeKeyText } from '../utils/ast-utils.js';
import { getSourceCode } from '../utils/compat.js';

export default createRule('no-dupe-use-directives', {
meta: {
Expand All @@ -19,7 +18,7 @@ export default createRule('no-dupe-use-directives', {
type: 'problem'
},
create(context) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;

const directiveDataMap = new Map<
string, // key text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getAttributeValueQuoteAndRange,
getStringIfConstant
} from '../utils/ast-utils.js';
import { getSourceCode } from '../utils/compat.js';

export default createRule('no-dynamic-slot-name', {
meta: {
Expand All @@ -28,7 +27,7 @@ export default createRule('no-dynamic-slot-name', {
}
},
create(context) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;
return {
"SvelteElement[name.name='slot'] > SvelteStartTag.startTag > SvelteAttribute[key.name='name']"(
node: AST.SvelteAttribute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils/index.js';
import { getSourceCode } from '../utils/compat.js';

export default createRule('no-extra-reactive-curlies', {
meta: {
Expand All @@ -24,7 +23,7 @@ export default createRule('no-extra-reactive-curlies', {
[`SvelteReactiveStatement > BlockStatement[body.length=1]`]: (
node: TSESTree.BlockStatement
) => {
const source = getSourceCode(context);
const source = context.sourceCode;

return context.report({
node,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils/index.js';
import { ReferenceTracker } from '@eslint-community/eslint-utils';
import { getSourceCode } from '../utils/compat.js';
import { findVariable } from '../utils/ast-utils.js';
import type { RuleContext } from '../types.js';

Expand All @@ -24,9 +23,7 @@ export default createRule('no-goto-without-base', {
create(context) {
return {
Program() {
const referenceTracker = new ReferenceTracker(
getSourceCode(context).scopeManager.globalScope!
);
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope!);
const basePathNames = extractBasePathReferences(referenceTracker, context);
for (const gotoCall of extractGotoReferences(referenceTracker)) {
if (gotoCall.arguments.length < 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { createRule } from '../utils/index.js';
import type { Scope, Variable, Reference, Definition } from '@typescript-eslint/scope-manager';
import type { TSESTree } from '@typescript-eslint/types';
import { findVariable, iterateIdentifiers } from '../utils/ast-utils.js';
import { getSourceCode } from '../utils/compat.js';

export default createRule('no-immutable-reactive-statements', {
meta: {
Expand All @@ -20,7 +19,7 @@ export default createRule('no-immutable-reactive-statements', {
type: 'suggestion'
},
create(context) {
const scopeManager = getSourceCode(context).scopeManager;
const scopeManager = context.sourceCode.scopeManager;
const globalScope = scopeManager.globalScope;
const toplevelScope =
globalScope?.childScopes.find((scope) => scope.type === 'module') || globalScope;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils/index.js';
import { ReferenceTracker } from '@eslint-community/eslint-utils';
import { getSourceCode } from '../utils/compat.js';
import { findVariable } from '../utils/ast-utils.js';
import type { RuleContext } from '../types.js';
import type { SvelteLiteral } from 'svelte-eslint-parser/lib/ast';
Expand Down Expand Up @@ -48,9 +47,7 @@ export default createRule('no-navigation-without-base', {
let basePathNames: Set<TSESTree.Identifier> = new Set<TSESTree.Identifier>();
return {
Program() {
const referenceTracker = new ReferenceTracker(
getSourceCode(context).scopeManager.globalScope!
);
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope!);
basePathNames = extractBasePathReferences(referenceTracker, context);
const {
goto: gotoCalls,
Expand Down
Loading
Loading