Skip to content

Commit 68e7724

Browse files
fix: typescript-eslint v6 compatibility (#548)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
1 parent 1d255cb commit 68e7724

File tree

14 files changed

+113
-87
lines changed

14 files changed

+113
-87
lines changed

.changeset/sweet-avocados-share.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-svelte": patch
3+
---
4+
5+
fix: typescript-eslint v6 compatibility

.github/workflows/NodeCI.yml

+25-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,32 @@ jobs:
7777
run: |+
7878
pnpm rm @sveltejs/kit
7979
rm -rf node_modules
80-
- name: Install ESLint ${{ matrix.eslint }}
80+
- name: Install svelte@3
81+
run: |+
82+
pnpm install -D svelte@3 @typescript-eslint/parser@5 @typescript-eslint/eslint-plugin@5
83+
rm -rf node_modules
84+
- name: Install Packages
85+
run: pnpm install
86+
- name: Test
87+
run: pnpm run test
88+
test-for-typescript-eslint-v5:
89+
name: Test for typescript-eslint v5
90+
runs-on: ${{ matrix.os }}
91+
strategy:
92+
matrix:
93+
os: [ubuntu-latest]
94+
node: [18]
95+
steps:
96+
- name: Checkout
97+
uses: actions/checkout@v3
98+
- uses: pnpm/action-setup@v2
99+
- name: Setup Node.js ${{ matrix.node }}
100+
uses: actions/setup-node@v3
101+
with:
102+
node-version: ${{ matrix.node }}
103+
- name: Install typescript-eslint v5
81104
run: |+
82-
pnpm install -D svelte@3
105+
pnpm install -D @typescript-eslint/parser@5 @typescript-eslint/eslint-plugin@5
83106
rm -rf node_modules
84107
- name: Install Packages
85108
run: pnpm install

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"postcss-safe-parser": "^6.0.0",
7777
"postcss-selector-parser": "^6.0.11",
7878
"semver": "^7.5.3",
79-
"svelte-eslint-parser": "^0.32.0"
79+
"svelte-eslint-parser": "^0.32.2"
8080
},
8181
"devDependencies": {
8282
"@1stg/browserslist-config": "^1.2.3",
@@ -112,8 +112,8 @@
112112
"@types/prismjs": "^1.26.0",
113113
"@types/semver": "^7.5.0",
114114
"@types/stylus": "^0.48.38",
115-
"@typescript-eslint/eslint-plugin": "^5.59.5",
116-
"@typescript-eslint/parser": "^5.59.5",
115+
"@typescript-eslint/eslint-plugin": "^6.0.0",
116+
"@typescript-eslint/parser": "^6.0.0",
117117
"@typescript/vfs": "^1.4.0",
118118
"acorn": "^8.8.2",
119119
"assert": "^2.0.0",
@@ -158,7 +158,7 @@
158158
"prism-svelte": "^0.5.0",
159159
"prismjs": "^1.25.0",
160160
"rimraf": "^5.0.0",
161-
"sass": "^1.51.0",
161+
"sass": "^1.64.0",
162162
"source-map-js": "^1.0.2",
163163
"stylelint": "^15.0.0",
164164
"stylelint-config-standard": "^34.0.0",
@@ -168,7 +168,7 @@
168168
"svelte-i18n": "^3.6.0",
169169
"tslib": "^2.5.0",
170170
"type-coverage": "^2.22.0",
171-
"typescript": "^5.0.0",
171+
"typescript": "~5.0.0",
172172
"vite": "^4.0.0",
173173
"vite-plugin-svelte-md": "^0.1.7",
174174
"yaml": "^2.1.1"

src/rules/indent-helpers/ts.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function defineVisitor(context: IndentContext): NodeListener {
3333
count: 2,
3434
includeComments: false,
3535
})
36-
const baseToken = sourceCode.getFirstToken(node.parent!)
36+
const baseToken = sourceCode.getFirstToken(node.parent)
3737
offsets.setOffsetToken([colonOrArrowToken, secondToken], 1, baseToken)
3838

3939
const before = sourceCode.getTokenBefore(colonOrArrowToken)
@@ -636,7 +636,7 @@ export function defineVisitor(context: IndentContext): NodeListener {
636636
if (firstToken.type === "Punctuator") {
637637
// method
638638
leftParenToken = firstToken
639-
bodyBaseToken = sourceCode.getFirstToken(node.parent!)
639+
bodyBaseToken = sourceCode.getFirstToken(node.parent)
640640
} else {
641641
let nextToken = sourceCode.getTokenAfter(firstToken)
642642
let nextTokenOffset = 0
@@ -823,12 +823,16 @@ export function defineVisitor(context: IndentContext): NodeListener {
823823
includeComments: false,
824824
})!
825825
offsets.setOffsetToken(leftParenToken, 1, firstToken)
826-
const rightParenToken = sourceCode.getTokenAfter(node.parameter, {
826+
const argument =
827+
node.argument ||
828+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- typescript-eslint<v6 node
829+
(node as any).parameter
830+
const rightParenToken = sourceCode.getTokenAfter(argument, {
827831
filter: isClosingParenToken,
828832
includeComments: false,
829833
})!
830834
offsets.setOffsetElementList(
831-
[node.parameter],
835+
[argument],
832836
leftParenToken,
833837
rightParenToken,
834838
1,
@@ -954,7 +958,7 @@ export function defineVisitor(context: IndentContext): NodeListener {
954958
})
955959
offsets.setOffsetToken(secondToken, 0, atToken)
956960

957-
const parent = node.parent!
961+
const parent = node.parent
958962
const { decorators } = parent as { decorators?: TSESTree.Decorator[] }
959963
if (!decorators || decorators.length === 0) {
960964
return

tests/fixtures/rules/indent/invalid/ts/ts-interface01-errors.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@
7474
line: 21
7575
column: 1
7676
suggestions: null
77-
- message: Expected indentation of 6 spaces but found 0 spaces.
77+
- message: Expected indentation of 4 spaces but found 0 spaces.
7878
line: 22
7979
column: 1
8080
suggestions: null
8181
- message: Expected indentation of 6 spaces but found 0 spaces.
8282
line: 23
8383
column: 1
8484
suggestions: null
85-
- message: Expected indentation of 4 spaces but found 0 spaces.
85+
- message: Expected indentation of 6 spaces but found 0 spaces.
8686
line: 24
8787
column: 1
8888
suggestions: null

tests/fixtures/rules/indent/invalid/ts/ts-interface01-input.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ string
1717
}
1818
class
1919
Impl
20+
extends
21+
E
2022
implements
2123
I2
2224
,
2325
I
24-
extends
25-
E
2626
{
2727
public foo
2828
=

tests/fixtures/rules/indent/invalid/ts/ts-interface01-output.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
}
1818
class
1919
Impl
20+
extends
21+
E
2022
implements
2123
I2
2224
,
2325
I
24-
extends
25-
E
2626
{
2727
public foo
2828
=

tests/fixtures/rules/indent/invalid/ts/ts-interface03-errors.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@
1414
line: 6
1515
column: 1
1616
suggestions: null
17+
- message: Expected indentation of 4 spaces but found 0 spaces.
18+
line: 7
19+
column: 1
20+
suggestions: null
21+
- message: Expected indentation of 6 spaces but found 0 spaces.
22+
line: 8
23+
column: 1
24+
suggestions: null
25+
- message: Expected indentation of 8 spaces but found 0 spaces.
26+
line: 9
27+
column: 1
28+
suggestions: null
29+
- message: Expected indentation of 10 spaces but found 0 spaces.
30+
line: 10
31+
column: 1
32+
suggestions: null
33+
- message: Expected indentation of 8 spaces but found 0 spaces.
34+
line: 11
35+
column: 1
36+
suggestions: null
1737
- message: Expected indentation of 2 spaces but found 0 spaces.
1838
line: 12
1939
column: 1

tests/fixtures/rules/indent/invalid/ts/ts-interface03-input.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface Foo
44
<
55
T
66
>
7-
implements // visitor key is not provided
7+
extends
88
Bar
99
<
1010
T

tests/fixtures/rules/indent/invalid/ts/ts-interface03-output.svelte

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<
55
T
66
>
7-
implements // visitor key is not provided
8-
Bar
9-
<
10-
T
11-
>
7+
extends
8+
Bar
9+
<
10+
T
11+
>
1212
{ }
1313
</script>
1414

tests/src/rules/@typescript-eslint/original-tests/RuleTester.ts

-15
This file was deleted.

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

+36-15
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
11
// Original test cases
22
// https://github.com/typescript-eslint/typescript-eslint/blob/78467fc1bde9bd2db1e08b3d19f151f4adaff8a9/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts
33
/* eslint func-style: off, eslint-plugin/consistent-output: off -- respect original */
4-
import type {
5-
InvalidTestCase,
6-
TestCaseError,
7-
} from "@typescript-eslint/utils/dist/ts-eslint"
84
import * as path from "path"
5+
import { RuleTester } from "eslint"
96

107
import rule from "../../../../../src/rules/@typescript-eslint/no-unnecessary-condition"
11-
import { getFixturesRootDir, noFormat, RuleTester } from "./RuleTester"
8+
9+
function getFixturesRootDir(): string {
10+
return path.join(__dirname, "fixtures")
11+
}
1212

1313
const rootPath = getFixturesRootDir()
1414

1515
const ruleTester = new RuleTester({
16-
parser: "@typescript-eslint/parser",
16+
parser: require.resolve("@typescript-eslint/parser"),
1717
parserOptions: {
1818
tsconfigRootDir: rootPath,
1919
project: "./tsconfig.json",
2020
},
2121
})
2222

23+
function withFileName<
24+
TestCase extends RuleTester.ValidTestCase | RuleTester.InvalidTestCase,
25+
>(list: (string | TestCase)[]): TestCase[] {
26+
return list.map((e) => {
27+
if (typeof e === "string") {
28+
return { code: e, filename: path.join(rootPath, "file.ts") } as TestCase
29+
}
30+
if (e.filename) return e
31+
return {
32+
...e,
33+
filename: e.parserOptions?.tsconfigRootDir
34+
? path.join(e.parserOptions.tsconfigRootDir, "file.ts")
35+
: path.join(rootPath, "file.ts"),
36+
} as TestCase
37+
})
38+
}
39+
2340
const ruleError = (
2441
line: number,
2542
column: number,
2643
messageId: string,
27-
): TestCaseError<string> => ({
44+
): {
45+
messageId: string
46+
line: number
47+
column: number
48+
} => ({
2849
messageId,
2950
line,
3051
column,
@@ -39,13 +60,13 @@ const t1 = b1 && b2;
3960
const unnecessaryConditionTest = (
4061
condition: string,
4162
messageId: string,
42-
): InvalidTestCase<string, any> => ({
63+
): RuleTester.InvalidTestCase => ({
4364
code: necessaryConditionTest(condition),
4465
errors: [ruleError(4, 12, messageId)],
4566
})
4667

4768
ruleTester.run("no-unnecessary-conditionals", rule as any, {
48-
valid: [
69+
valid: withFileName([
4970
`
5071
declare const b1: boolean;
5172
declare const b2: boolean;
@@ -519,8 +540,8 @@ if (x) {
519540
tsconfigRootDir: path.join(rootPath, "unstrict"),
520541
},
521542
},
522-
],
523-
invalid: [
543+
]),
544+
invalid: withFileName([
524545
// Ensure that it's checking in all the right places
525546
{
526547
code: `
@@ -904,7 +925,7 @@ do {} while (true);
904925
],
905926
},
906927
{
907-
code: noFormat`
928+
code: `
908929
let foo = { bar: true };
909930
foo?.bar;
910931
foo ?. bar;
@@ -954,7 +975,7 @@ foo
954975
],
955976
},
956977
{
957-
code: noFormat`
978+
code: `
958979
let foo = () => {};
959980
foo?.();
960981
foo ?. ();
@@ -1004,7 +1025,7 @@ foo
10041025
],
10051026
},
10061027
{
1007-
code: noFormat`
1028+
code: `
10081029
let foo = () => {};
10091030
foo?.(bar);
10101031
foo ?. (bar);
@@ -1540,5 +1561,5 @@ if (x) {
15401561
},
15411562
],
15421563
},
1543-
],
1564+
]),
15441565
})

0 commit comments

Comments
 (0)