Skip to content

Commit 847d2e5

Browse files
committed
fix: typescript-eslint v6 compatibility
1 parent 80df60b commit 847d2e5

File tree

12 files changed

+103
-83
lines changed

12 files changed

+103
-83
lines changed

.github/workflows/NodeCI.yml

+24-1
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,37 @@ jobs:
7777
run: |+
7878
pnpm rm @sveltejs/kit
7979
rm -rf node_modules
80-
- name: Install ESLint ${{ matrix.eslint }}
80+
- name: Install svelte@3
8181
run: |+
8282
pnpm install -D svelte@3
8383
rm -rf node_modules
8484
- name: Install Packages
8585
run: pnpm install
8686
- name: Test
8787
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
104+
run: |+
105+
pnpm install -D @typescript-eslint/parser@5 @typescript-eslint/eslint-plugin@5
106+
rm -rf node_modules
107+
- name: Install Packages
108+
run: pnpm install
109+
- name: Test
110+
run: pnpm run test
88111
update-resources:
89112
runs-on: ubuntu-latest
90113
steps:

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"postcss-safe-parser": "^6.0.0",
7878
"postcss-selector-parser": "^6.0.11",
7979
"semver": "^7.5.3",
80-
"svelte-eslint-parser": "^0.32.0"
80+
"svelte-eslint-parser": "^0.32.2"
8181
},
8282
"devDependencies": {
8383
"@1stg/browserslist-config": "^1.2.3",
@@ -159,7 +159,7 @@
159159
"prism-svelte": "^0.5.0",
160160
"prismjs": "^1.25.0",
161161
"rimraf": "^5.0.0",
162-
"sass": "^1.51.0",
162+
"sass": "^1.64.0",
163163
"simple-git-hooks": "^2.8.0",
164164
"source-map-js": "^1.0.2",
165165
"stylelint": "^15.0.0",

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
})

tests/src/rules/indent.ts

-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { RuleTester } from "eslint"
2-
import path from "path"
32
import rule from "../../../src/rules/indent"
43
import { loadTestCases } from "../../utils/utils"
54

@@ -15,35 +14,3 @@ const tester = new RuleTester({
1514
})
1615

1716
tester.run("indent", rule as any, loadTestCases("indent"))
18-
19-
const testerForTsParserV4 = new RuleTester({
20-
parserOptions: {
21-
ecmaVersion: 2020,
22-
sourceType: "module",
23-
parser: {
24-
ts: "@typescript-eslint/parser-v4",
25-
js: "espree",
26-
},
27-
},
28-
})
29-
30-
describe("use @typescript-eslint/parser@4", () => {
31-
testerForTsParserV4.run(
32-
"indent",
33-
rule as any,
34-
loadTestCases("indent", {
35-
filter(filename) {
36-
if (
37-
filename.endsWith("ts-class03-input.svelte") ||
38-
filename.endsWith("ts-decorator01-input.svelte") ||
39-
filename.endsWith("ts-decorator02-input.svelte")
40-
) {
41-
// Decorator nodes in ts4.8 are incompatible.
42-
return false
43-
}
44-
45-
return path.basename(path.dirname(filename)) === "ts"
46-
},
47-
}),
48-
)
49-
})

0 commit comments

Comments
 (0)