Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2b3a762

Browse filesBrowse files
authored
Fix an error when using default for generic (#222)
1 parent 7c9c24b commit 2b3a762
Copy full SHA for 2b3a762

File tree

11 files changed

+7407
-12
lines changed
Filter options

11 files changed

+7407
-12
lines changed

‎src/script/generic.ts

Copy file name to clipboardExpand all lines: src/script/generic.ts
+21-12Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,31 +124,40 @@ function getConstraint(node: TSESTree.TSTypeParameter, rawParam: string) {
124124
if (!node.constraint) {
125125
return "unknown"
126126
}
127-
let startIndex = rawParam.indexOf(node.name.name) + node.name.name.length
128-
while (startIndex < rawParam.length) {
129-
if (rawParam.startsWith("extends", startIndex)) {
130-
return rawParam.slice(startIndex + 7)
127+
let index = rawParam.indexOf(node.name.name) + node.name.name.length
128+
let startIndex: number | null = null
129+
while (index < rawParam.length) {
130+
if (startIndex == null) {
131+
if (rawParam.startsWith("extends", index)) {
132+
startIndex = index = index + 7
133+
continue
134+
}
135+
} else if (rawParam[index] === "=") {
136+
return rawParam.slice(startIndex, index)
131137
}
132-
if (rawParam.startsWith("//", startIndex)) {
133-
const lfIndex = rawParam.indexOf("\n", startIndex)
138+
if (rawParam.startsWith("//", index)) {
139+
const lfIndex = rawParam.indexOf("\n", index)
134140
if (lfIndex >= 0) {
135-
startIndex = lfIndex + 1
141+
index = lfIndex + 1
136142
continue
137143
}
138144
return "unknown"
139145
}
140-
if (rawParam.startsWith("/*", startIndex)) {
141-
const endIndex = rawParam.indexOf("*/", startIndex)
146+
if (rawParam.startsWith("/*", index)) {
147+
const endIndex = rawParam.indexOf("*/", index)
142148
if (endIndex >= 0) {
143-
startIndex = endIndex + 2
149+
index = endIndex + 2
144150
continue
145151
}
146152
return "unknown"
147153
}
148-
startIndex++
154+
index++
155+
}
156+
if (startIndex == null) {
157+
return "unknown"
149158
}
150159

151-
return "unknown"
160+
return rawParam.slice(startIndex)
152161
}
153162

154163
/** Remove variable def */

‎test/fixtures/ast/vue3.3-generic-6-with-default/ast.json

Copy file name to clipboardExpand all lines: test/fixtures/ast/vue3.3-generic-6-with-default/ast.json
+2,975Lines changed: 2975 additions & 0 deletions
Large diffs are not rendered by default.
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"sourceType": "module",
3+
"parser": {
4+
"ts": "@typescript-eslint/parser"
5+
}
6+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<script setup lang="ts" generic="
2+
T // Comments
3+
extends /* = */ Foo = number,
4+
// Comments
5+
U /* extends */
6+
extends
7+
Record&lt;
8+
/* = */
9+
string, // =
10+
T
11+
>
12+
= Record<string, number&gt;
13+
">
14+
type Foo = number | string
15+
const p = defineProps<{foo:T, bar: U}>()
16+
const foo = p.foo
17+
console.log(foo)
18+
</script>
19+
<template>
20+
{{foo}}
21+
</template>
+123Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
[
2+
"<script setup lang=\"ts\" generic=\"\n T // Comments\n extends /* = */ Foo = number,\n // Comments\n U /* extends */\n extends\n Record&lt;\n /* = */\n string, // =\n T\n >\n = Record<string, number&gt;\n\">",
3+
"type",
4+
"Foo",
5+
"=",
6+
"number",
7+
"|",
8+
"string",
9+
"const",
10+
"p",
11+
"=",
12+
"defineProps",
13+
"<",
14+
"{",
15+
"foo",
16+
":",
17+
"T",
18+
",",
19+
"bar",
20+
":",
21+
"U",
22+
"}",
23+
">",
24+
"(",
25+
")",
26+
"const",
27+
"foo",
28+
"=",
29+
"p",
30+
".",
31+
"foo",
32+
"console",
33+
".",
34+
"log",
35+
"(",
36+
"foo",
37+
")",
38+
"</script>",
39+
"<script",
40+
"setup",
41+
"lang",
42+
"=",
43+
"\"ts\"",
44+
"generic",
45+
"=",
46+
"\"",
47+
"T",
48+
"extends",
49+
"Foo",
50+
"=",
51+
"number",
52+
",",
53+
"U",
54+
"extends",
55+
"Record",
56+
"&lt;",
57+
"string",
58+
",",
59+
"T",
60+
">",
61+
"=",
62+
"Record",
63+
"<",
64+
"string",
65+
",",
66+
"number",
67+
"&gt;",
68+
"\"",
69+
">",
70+
"\n",
71+
"type",
72+
" ",
73+
"Foo",
74+
" ",
75+
"=",
76+
" ",
77+
"number",
78+
" ",
79+
"|",
80+
" ",
81+
"string",
82+
"\n",
83+
"const",
84+
" ",
85+
"p",
86+
" ",
87+
"=",
88+
" ",
89+
"defineProps<{foo:T,",
90+
" ",
91+
"bar:",
92+
" ",
93+
"U}>()",
94+
"\n",
95+
"const",
96+
" ",
97+
"foo",
98+
" ",
99+
"=",
100+
" ",
101+
"p.foo",
102+
"\n",
103+
"console.log(foo)",
104+
"\n",
105+
"</script",
106+
">",
107+
"\n",
108+
"<template",
109+
">",
110+
"\n",
111+
"{{",
112+
"foo",
113+
"}}",
114+
"\n",
115+
"</template",
116+
">",
117+
"// Comments",
118+
"/* = */",
119+
"// Comments",
120+
"/* extends */",
121+
"/* = */",
122+
"// ="
123+
]
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[
2+
{
3+
"type": "VElement",
4+
"text": "<template>\n{{foo}}\n</template>",
5+
"children": [
6+
{
7+
"type": "VStartTag",
8+
"text": "<template>",
9+
"children": []
10+
},
11+
{
12+
"type": "VText",
13+
"text": "\n",
14+
"children": []
15+
},
16+
{
17+
"type": "VExpressionContainer",
18+
"text": "{{foo}}",
19+
"children": [
20+
{
21+
"type": "Identifier",
22+
"text": "foo",
23+
"children": []
24+
}
25+
]
26+
},
27+
{
28+
"type": "VText",
29+
"text": "\n",
30+
"children": []
31+
},
32+
{
33+
"type": "VEndTag",
34+
"text": "</template>",
35+
"children": []
36+
}
37+
]
38+
}
39+
]

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.