Skip to content

Commit 8e53e58

Browse files
authored
chore: use context.sourceCode instead of compat (#1173)
1 parent 3bdf5d0 commit 8e53e58

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+85
-143
lines changed

.changeset/happy-donuts-fold.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.sourceCode` directly rather than a compatibility helper.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const config = [
7070
],
7171
'no-restricted-properties': [
7272
'error',
73-
{ object: 'context', property: 'getSourceCode', message: 'Use src/utils/compat.ts' },
73+
{ object: 'context', property: 'getSourceCode', message: 'Use `context.sourceCode`' },
7474
{ object: 'context', property: 'getFilename', message: 'Use src/utils/compat.ts' },
7575
{
7676
object: 'context',

packages/eslint-plugin-svelte/src/rules/@typescript-eslint/no-unnecessary-condition.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
isTupleType
2323
} from '../../utils/ts-utils/index.js';
2424
import type { TS, TSTools } from '../../utils/ts-utils/index.js';
25-
import { getSourceCode } from '../../utils/compat.js';
2625

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

158157
const { service, ts } = tools;
159158
const checker = service.program.getTypeChecker();
160-
const sourceCode = getSourceCode(context);
159+
const sourceCode = context.sourceCode;
161160
const compilerOptions = service.program.getCompilerOptions();
162161
const isStrictNullChecks = compilerOptions.strict
163162
? compilerOptions.strictNullChecks !== false

packages/eslint-plugin-svelte/src/rules/block-lang.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { createRule } from '../utils/index.js';
22
import { findAttribute, getLangValue } from '../utils/ast-utils.js';
33
import type { SvelteScriptElement, SvelteStyleElement } from 'svelte-eslint-parser/lib/ast';
4-
import { getSourceCode } from '../utils/compat.js';
54
import type { SuggestionReportDescriptor, SourceCode } from '../types.js';
65

76
export default createRule('block-lang', {
@@ -59,7 +58,7 @@ export default createRule('block-lang', {
5958
hasSuggestions: true
6059
},
6160
create(context) {
62-
if (!getSourceCode(context).parserServices.isSvelte) {
61+
if (!context.sourceCode.parserServices.isSvelte) {
6362
return {};
6463
}
6564
const enforceScriptPresent: boolean = context.options[0]?.enforceScriptPresent ?? false;
@@ -91,7 +90,7 @@ export default createRule('block-lang', {
9190
message: `The <script> block should be present and its lang attribute should be ${prettyPrintLangs(
9291
allowedScriptLangs
9392
)}.`,
94-
suggest: buildAddLangSuggestions(allowedScriptLangs, 'script', getSourceCode(context))
93+
suggest: buildAddLangSuggestions(allowedScriptLangs, 'script', context.sourceCode)
9594
});
9695
}
9796
for (const scriptNode of scriptNodes) {
@@ -106,7 +105,7 @@ export default createRule('block-lang', {
106105
}
107106
}
108107
if (styleNodes.length === 0 && enforceStylePresent) {
109-
const sourceCode = getSourceCode(context);
108+
const sourceCode = context.sourceCode;
110109
context.report({
111110
loc: { line: 1, column: 1 },
112111
message: `The <style> block should be present and its lang attribute should be ${prettyPrintLangs(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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, getSourceCode } from '../utils/compat.js';
5+
import { getFilename } from '../utils/compat.js';
66

77
type RuleAndLocation = {
88
ruleId: string;
@@ -63,7 +63,7 @@ export default createRule('comment-directive', {
6363
reportUnusedDisableDirectives
6464
});
6565

66-
const sourceCode = getSourceCode(context);
66+
const sourceCode = context.sourceCode;
6767

6868
/**
6969
* Parse a given comment.

packages/eslint-plugin-svelte/src/rules/consistent-selector-style.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
Tag as SelectorTag
88
} from 'postcss-selector-parser';
99
import { findClassesInAttribute } from '../utils/ast-utils.js';
10-
import { getSourceCode } from '../utils/compat.js';
1110
import { createRule } from '../utils/index.js';
1211

1312
export default createRule('consistent-selector-style', {
@@ -48,7 +47,7 @@ export default createRule('consistent-selector-style', {
4847
type: 'suggestion'
4948
},
5049
create(context) {
51-
const sourceCode = getSourceCode(context);
50+
const sourceCode = context.sourceCode;
5251
if (
5352
!sourceCode.parserServices.isSvelte ||
5453
sourceCode.parserServices.getStyleSelectorAST === undefined ||

packages/eslint-plugin-svelte/src/rules/first-attribute-linebreak.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { AST } from 'svelte-eslint-parser';
22
import { createRule } from '../utils/index.js';
3-
import { getSourceCode } from '../utils/compat.js';
43

54
export default createRule('first-attribute-linebreak', {
65
meta: {
@@ -30,7 +29,7 @@ export default createRule('first-attribute-linebreak', {
3029
create(context) {
3130
const multiline: 'below' | 'beside' = context.options[0]?.multiline || 'below';
3231
const singleline: 'below' | 'beside' = context.options[0]?.singleline || 'beside';
33-
const sourceCode = getSourceCode(context);
32+
const sourceCode = context.sourceCode;
3433

3534
/**
3635
* Report attribute

packages/eslint-plugin-svelte/src/rules/html-closing-bracket-new-line.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { AST } from 'svelte-eslint-parser';
22
import { createRule } from '../utils/index.js';
3-
import { getSourceCode } from '../utils/compat.js';
43
import type { SourceCode } from '../types.js';
54

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

126-
const sourceCode = getSourceCode(context);
125+
const sourceCode = context.sourceCode;
127126

128127
return {
129128
'SvelteStartTag, SvelteEndTag'(node: ExpectedNode) {

packages/eslint-plugin-svelte/src/rules/html-closing-bracket-spacing.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default createRule('html-closing-bracket-spacing', {
4040
selfClosingTag: 'always',
4141
...ctx.options[0]
4242
};
43-
const src = ctx.getSourceCode();
43+
const src = ctx.sourceCode;
4444

4545
/**
4646
* Returns true if string contains newline characters

packages/eslint-plugin-svelte/src/rules/html-quotes.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { createRule } from '../utils/index.js';
33
import type { QuoteAndRange } from '../utils/ast-utils.js';
44
import { getMustacheTokens } from '../utils/ast-utils.js';
55
import { getAttributeValueQuoteAndRange } from '../utils/ast-utils.js';
6-
import { getSourceCode } from '../utils/compat.js';
76

87
const QUOTE_CHARS = {
98
double: '"',
@@ -49,7 +48,7 @@ export default createRule('html-quotes', {
4948
type: 'layout' // "problem",
5049
},
5150
create(context) {
52-
const sourceCode = getSourceCode(context);
51+
const sourceCode = context.sourceCode;
5352
const preferQuote: 'double' | 'single' = context.options[0]?.prefer ?? 'double';
5453
const dynamicQuote = context.options[0]?.dynamic?.quoted ? preferQuote : 'unquoted';
5554
const avoidInvalidUnquotedInHTML = Boolean(

packages/eslint-plugin-svelte/src/rules/html-self-closing.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
isSvgElement,
77
isMathMLElement
88
} from '../utils/ast-utils.js';
9-
import { getSourceCode } from '../utils/compat.js';
109

1110
const TYPE_MESSAGES = {
1211
normal: 'HTML elements',
@@ -160,7 +159,7 @@ export default createRule('html-self-closing', {
160159
context.report({
161160
node,
162161
loc: {
163-
start: getSourceCode(context).getLocFromIndex(
162+
start: context.sourceCode.getLocFromIndex(
164163
node.startTag.range[1] - (node.startTag.selfClosing ? 2 : 1)
165164
),
166165
end: node.loc.end

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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, getSourceCode } from '../../utils/compat.js';
11+
import { getFilename } from '../../utils/compat.js';
1212

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

8383
const options = parseOptions(context.options[0] || {}, defaultOptions);
84-
const sourceCode = getSourceCode(context);
84+
const sourceCode = context.sourceCode;
8585
const offsets = new OffsetContext({ sourceCode, options });
8686

8787
/**

packages/eslint-plugin-svelte/src/rules/infinite-reactive-loop.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import { createRule } from '../utils/index.js';
55
import type { RuleContext } from '../types.js';
66
import { findVariable } from '../utils/ast-utils.js';
77
import { traverseNodes } from 'svelte-eslint-parser';
8-
import { getSourceCode } from '../utils/compat.js';
98

109
/**
1110
* Get usage of `tick`
1211
*/
1312
function extractTickReferences(
1413
context: RuleContext
1514
): { node: TSESTree.CallExpression; name: string }[] {
16-
const referenceTracker = new ReferenceTracker(getSourceCode(context).scopeManager.globalScope!);
15+
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope!);
1716
const a = referenceTracker.iterateEsmReferences({
1817
svelte: {
1918
[ReferenceTracker.ESM]: true,
@@ -36,7 +35,7 @@ function extractTickReferences(
3635
function extractTaskReferences(
3736
context: RuleContext
3837
): { node: TSESTree.CallExpression; name: string }[] {
39-
const referenceTracker = new ReferenceTracker(getSourceCode(context).scopeManager.globalScope!);
38+
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope!);
4039
const a = referenceTracker.iterateGlobalReferences({
4140
setTimeout: { [ReferenceTracker.CALL]: true },
4241
setInterval: { [ReferenceTracker.CALL]: true },
@@ -123,7 +122,7 @@ function isPromiseThenOrCatchBody(node: TSESTree.Node): boolean {
123122
* Get all reactive variable reference.
124123
*/
125124
function getReactiveVariableReferences(context: RuleContext) {
126-
const scopeManager = getSourceCode(context).scopeManager;
125+
const scopeManager = context.sourceCode.scopeManager;
127126
// Find the top-level (module or global) scope.
128127
// Any variable defined at the top-level (module scope or global scope) can be made reactive.
129128
const toplevelScope =

packages/eslint-plugin-svelte/src/rules/max-attributes-per-line.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { AST } from 'svelte-eslint-parser';
22
import { createRule } from '../utils/index.js';
3-
import { getSourceCode } from '../utils/compat.js';
43

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

6362
/**
6463
* Report attributes

packages/eslint-plugin-svelte/src/rules/mustache-spacing.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { AST } from 'svelte-eslint-parser';
22
import { isClosingBraceToken, isOpeningBraceToken } from '@eslint-community/eslint-utils';
33
import { createRule } from '../utils/index.js';
44
import { getMustacheTokens } from '../utils/ast-utils.js';
5-
import { getSourceCode } from '../utils/compat.js';
65
type DeepPartial<T> = {
76
[P in keyof T]?: DeepPartial<T[P]>;
87
};
@@ -74,7 +73,7 @@ export default createRule('mustache-spacing', {
7473
},
7574
create(context) {
7675
const options = parseOptions(context.options[0]);
77-
const sourceCode = getSourceCode(context);
76+
const sourceCode = context.sourceCode;
7877

7978
function verifyBraces(
8079
openingBrace: AST.Token | AST.Comment,

packages/eslint-plugin-svelte/src/rules/no-dupe-else-if-blocks.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { AST } from 'svelte-eslint-parser';
22
import type { TSESTree } from '@typescript-eslint/types';
33
import { createRule } from '../utils/index.js';
44
import { equalTokens } from '../utils/ast-utils.js';
5-
import { getSourceCode } from '../utils/compat.js';
65

76
// ------------------------------------------------------------------------------
87
// Helpers
@@ -82,7 +81,7 @@ export default createRule('no-dupe-else-if-blocks', {
8281
type: 'problem' // "problem",
8382
},
8483
create(context) {
85-
const sourceCode = getSourceCode(context);
84+
const sourceCode = context.sourceCode;
8685

8786
/**
8887
* Determines whether the two given nodes are considered to be equal. In particular, given that the nodes

packages/eslint-plugin-svelte/src/rules/no-dupe-on-directives.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { AST } from 'svelte-eslint-parser';
22
import type { TSESTree } from '@typescript-eslint/types';
33
import { createRule } from '../utils/index.js';
44
import { equalTokens } from '../utils/ast-utils.js';
5-
import { getSourceCode } from '../utils/compat.js';
65

76
export default createRule('no-dupe-on-directives', {
87
meta: {
@@ -19,7 +18,7 @@ export default createRule('no-dupe-on-directives', {
1918
type: 'problem'
2019
},
2120
create(context) {
22-
const sourceCode = getSourceCode(context);
21+
const sourceCode = context.sourceCode;
2322

2423
const directiveDataMap = new Map<
2524
string, // event type

packages/eslint-plugin-svelte/src/rules/no-dupe-use-directives.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { AST } from 'svelte-eslint-parser';
22
import type { TSESTree } from '@typescript-eslint/types';
33
import { createRule } from '../utils/index.js';
44
import { equalTokens, getAttributeKeyText } from '../utils/ast-utils.js';
5-
import { getSourceCode } from '../utils/compat.js';
65

76
export default createRule('no-dupe-use-directives', {
87
meta: {
@@ -19,7 +18,7 @@ export default createRule('no-dupe-use-directives', {
1918
type: 'problem'
2019
},
2120
create(context) {
22-
const sourceCode = getSourceCode(context);
21+
const sourceCode = context.sourceCode;
2322

2423
const directiveDataMap = new Map<
2524
string, // key text

packages/eslint-plugin-svelte/src/rules/no-dynamic-slot-name.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
getAttributeValueQuoteAndRange,
77
getStringIfConstant
88
} from '../utils/ast-utils.js';
9-
import { getSourceCode } from '../utils/compat.js';
109

1110
export default createRule('no-dynamic-slot-name', {
1211
meta: {
@@ -28,7 +27,7 @@ export default createRule('no-dynamic-slot-name', {
2827
}
2928
},
3029
create(context) {
31-
const sourceCode = getSourceCode(context);
30+
const sourceCode = context.sourceCode;
3231
return {
3332
"SvelteElement[name.name='slot'] > SvelteStartTag.startTag > SvelteAttribute[key.name='name']"(
3433
node: AST.SvelteAttribute

packages/eslint-plugin-svelte/src/rules/no-extra-reactive-curlies.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { TSESTree } from '@typescript-eslint/types';
22
import { createRule } from '../utils/index.js';
3-
import { getSourceCode } from '../utils/compat.js';
43

54
export default createRule('no-extra-reactive-curlies', {
65
meta: {
@@ -24,7 +23,7 @@ export default createRule('no-extra-reactive-curlies', {
2423
[`SvelteReactiveStatement > BlockStatement[body.length=1]`]: (
2524
node: TSESTree.BlockStatement
2625
) => {
27-
const source = getSourceCode(context);
26+
const source = context.sourceCode;
2827

2928
return context.report({
3029
node,

packages/eslint-plugin-svelte/src/rules/no-goto-without-base.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { TSESTree } from '@typescript-eslint/types';
22
import { createRule } from '../utils/index.js';
33
import { ReferenceTracker } from '@eslint-community/eslint-utils';
4-
import { getSourceCode } from '../utils/compat.js';
54
import { findVariable } from '../utils/ast-utils.js';
65
import type { RuleContext } from '../types.js';
76

@@ -24,9 +23,7 @@ export default createRule('no-goto-without-base', {
2423
create(context) {
2524
return {
2625
Program() {
27-
const referenceTracker = new ReferenceTracker(
28-
getSourceCode(context).scopeManager.globalScope!
29-
);
26+
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope!);
3027
const basePathNames = extractBasePathReferences(referenceTracker, context);
3128
for (const gotoCall of extractGotoReferences(referenceTracker)) {
3229
if (gotoCall.arguments.length < 1) {

packages/eslint-plugin-svelte/src/rules/no-immutable-reactive-statements.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { createRule } from '../utils/index.js';
33
import type { Scope, Variable, Reference, Definition } from '@typescript-eslint/scope-manager';
44
import type { TSESTree } from '@typescript-eslint/types';
55
import { findVariable, iterateIdentifiers } from '../utils/ast-utils.js';
6-
import { getSourceCode } from '../utils/compat.js';
76

87
export default createRule('no-immutable-reactive-statements', {
98
meta: {
@@ -20,7 +19,7 @@ export default createRule('no-immutable-reactive-statements', {
2019
type: 'suggestion'
2120
},
2221
create(context) {
23-
const scopeManager = getSourceCode(context).scopeManager;
22+
const scopeManager = context.sourceCode.scopeManager;
2423
const globalScope = scopeManager.globalScope;
2524
const toplevelScope =
2625
globalScope?.childScopes.find((scope) => scope.type === 'module') || globalScope;

0 commit comments

Comments
 (0)