Skip to content

Commit ef5f965

Browse files
authored
feat: use eslint-compat-utils (#605)
1 parent ff28fd3 commit ef5f965

Some content is hidden

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

51 files changed

+187
-77
lines changed

.changeset/slimy-jokes-reflect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-svelte": minor
3+
---
4+
5+
feat: use eslint-compat-utils

.eslintrc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ module.exports = {
133133
}
134134
]
135135
}
136+
],
137+
'no-restricted-properties': [
138+
'error',
139+
{ object: 'context', property: 'getSourceCode', message: 'Use src/utils/compat.ts' },
140+
{ object: 'context', property: 'getFilename', message: 'Use src/utils/compat.ts' },
141+
{
142+
object: 'context',
143+
property: 'getPhysicalFilename',
144+
message: 'Use src/utils/compat.ts'
145+
},
146+
{ object: 'context', property: 'getCwd', message: 'Use src/utils/compat.ts' },
147+
{ object: 'context', property: 'getScope', message: 'Use src/utils/compat.ts' },
148+
{ object: 'context', property: 'parserServices', message: 'Use src/utils/compat.ts' }
136149
]
137150
}
138151
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"@eslint-community/eslint-utils": "^4.2.0",
7070
"@jridgewell/sourcemap-codec": "^1.4.14",
7171
"debug": "^4.3.1",
72+
"eslint-compat-utils": "^0.1.2",
7273
"esutils": "^2.0.3",
7374
"known-css-properties": "^0.29.0",
7475
"postcss": "^8.4.5",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
isTupleType
2323
} from '../../utils/ts-utils';
2424
import type { TS, TSTools } from '../../utils/ts-utils';
25+
import { getSourceCode } from '../../utils/compat';
2526

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

157158
const { service, ts } = tools;
158159
const checker = service.program.getTypeChecker();
159-
const sourceCode = context.getSourceCode();
160+
const sourceCode = getSourceCode(context);
160161
const compilerOptions = service.program.getCompilerOptions();
161162
const isStrictNullChecks = compilerOptions.strict
162163
? compilerOptions.strictNullChecks !== false

src/rules/block-lang.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createRule } from '../utils';
22
import { getLangValue } from '../utils/ast-utils';
33
import type { SvelteScriptElement, SvelteStyleElement } from 'svelte-eslint-parser/lib/ast';
4+
import { getSourceCode } from '../utils/compat';
45

56
export default createRule('block-lang', {
67
meta: {
@@ -56,7 +57,7 @@ export default createRule('block-lang', {
5657
type: 'suggestion'
5758
},
5859
create(context) {
59-
if (!context.parserServices.isSvelte) {
60+
if (!getSourceCode(context).parserServices.isSvelte) {
6061
return {};
6162
}
6263
const enforceScriptPresent: boolean = context.options[0]?.enforceScriptPresent ?? false;

src/rules/comment-directive.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { AST } from 'svelte-eslint-parser';
22
import { getShared } from '../shared';
33
import type { CommentDirectives } from '../shared/comment-directives';
44
import { createRule } from '../utils';
5+
import { getFilename, getSourceCode } from '../utils/compat';
56

67
type RuleAndLocation = {
78
ruleId: string;
@@ -53,7 +54,7 @@ export default createRule('comment-directive', {
5354
type: 'problem'
5455
},
5556
create(context) {
56-
const shared = getShared(context.getFilename());
57+
const shared = getShared(getFilename(context));
5758
if (!shared) return {};
5859
const options = context.options[0] || {};
5960
const reportUnusedDisableDirectives = Boolean(options.reportUnusedDisableDirectives);
@@ -62,7 +63,7 @@ export default createRule('comment-directive', {
6263
reportUnusedDisableDirectives
6364
});
6465

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

6768
/**
6869
* Parse a given comment.

src/rules/first-attribute-linebreak.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AST } from 'svelte-eslint-parser';
22
import { createRule } from '../utils';
3+
import { getSourceCode } from '../utils/compat';
34

45
export default createRule('first-attribute-linebreak', {
56
meta: {
@@ -29,7 +30,7 @@ export default createRule('first-attribute-linebreak', {
2930
create(context) {
3031
const multiline: 'below' | 'beside' = context.options[0]?.multiline || 'below';
3132
const singleline: 'below' | 'beside' = context.options[0]?.singleline || 'beside';
32-
const sourceCode = context.getSourceCode();
33+
const sourceCode = getSourceCode(context);
3334

3435
/**
3536
* Report attribute

src/rules/html-quotes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createRule } from '../utils';
33
import type { QuoteAndRange } from '../utils/ast-utils';
44
import { getMustacheTokens } from '../utils/ast-utils';
55
import { getAttributeValueQuoteAndRange } from '../utils/ast-utils';
6+
import { getSourceCode } from '../utils/compat';
67

78
const QUOTE_CHARS = {
89
double: '"',
@@ -48,7 +49,7 @@ export default createRule('html-quotes', {
4849
type: 'layout' // "problem",
4950
},
5051
create(context) {
51-
const sourceCode = context.getSourceCode();
52+
const sourceCode = getSourceCode(context);
5253
const preferQuote: 'double' | 'single' = context.options[0]?.prefer ?? 'double';
5354
const dynamicQuote = context.options[0]?.dynamic?.quoted ? preferQuote : 'unquoted';
5455
const avoidInvalidUnquotedInHTML = Boolean(

src/rules/html-self-closing.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { AST } from 'svelte-eslint-parser';
22
import { createRule } from '../utils';
33
import { getNodeName, isVoidHtmlElement } from '../utils/ast-utils';
4+
import { getSourceCode } from '../utils/compat';
45

56
const TYPE_MESSAGES = {
67
normal: 'HTML elements',
@@ -126,9 +127,9 @@ export default createRule('html-self-closing', {
126127
context.report({
127128
node,
128129
loc: {
129-
start: context
130-
.getSourceCode()
131-
.getLocFromIndex(node.startTag.range[1] - (node.startTag.selfClosing ? 2 : 1)),
130+
start: getSourceCode(context).getLocFromIndex(
131+
node.startTag.range[1] - (node.startTag.selfClosing ? 2 : 1)
132+
),
132133
end: node.loc.end
133134
},
134135
messageId: shouldBeClosed ? 'requireClosing' : 'disallowClosing',

src/rules/indent-helpers/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { isCommentToken } from '@eslint-community/eslint-utils';
99
import type { AnyToken, IndentOptions } from './commons';
1010
import type { OffsetCalculator } from './offset-context';
1111
import { OffsetContext } from './offset-context';
12+
import { getFilename, getSourceCode } from '../../utils/compat';
1213

1314
type IndentUserOptions = {
1415
indent?: number | 'tab';
@@ -78,10 +79,10 @@ export function defineVisitor(
7879
context: RuleContext,
7980
defaultOptions: Partial<IndentOptions>
8081
): RuleListener {
81-
if (!context.getFilename().endsWith('.svelte')) return {};
82+
if (!getFilename(context).endsWith('.svelte')) return {};
8283

8384
const options = parseOptions(context.options[0] || {}, defaultOptions);
84-
const sourceCode = context.getSourceCode();
85+
const sourceCode = getSourceCode(context);
8586
const offsets = new OffsetContext({ sourceCode, options });
8687

8788
/**

0 commit comments

Comments
 (0)