Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error when using default for generic #222

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/script/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,31 +124,40 @@ function getConstraint(node: TSESTree.TSTypeParameter, rawParam: string) {
if (!node.constraint) {
return "unknown"
}
let startIndex = rawParam.indexOf(node.name.name) + node.name.name.length
while (startIndex < rawParam.length) {
if (rawParam.startsWith("extends", startIndex)) {
return rawParam.slice(startIndex + 7)
let index = rawParam.indexOf(node.name.name) + node.name.name.length
let startIndex: number | null = null
while (index < rawParam.length) {
if (startIndex == null) {
if (rawParam.startsWith("extends", index)) {
startIndex = index = index + 7
continue
}
} else if (rawParam[index] === "=") {
return rawParam.slice(startIndex, index)
}
if (rawParam.startsWith("//", startIndex)) {
const lfIndex = rawParam.indexOf("\n", startIndex)
if (rawParam.startsWith("//", index)) {
const lfIndex = rawParam.indexOf("\n", index)
if (lfIndex >= 0) {
startIndex = lfIndex + 1
index = lfIndex + 1
continue
}
return "unknown"
}
if (rawParam.startsWith("/*", startIndex)) {
const endIndex = rawParam.indexOf("*/", startIndex)
if (rawParam.startsWith("/*", index)) {
const endIndex = rawParam.indexOf("*/", index)
if (endIndex >= 0) {
startIndex = endIndex + 2
index = endIndex + 2
continue
}
return "unknown"
}
startIndex++
index++
}
if (startIndex == null) {
return "unknown"
}

return "unknown"
return rawParam.slice(startIndex)
}

/** Remove variable def */
Expand Down
2,975 changes: 2,975 additions & 0 deletions test/fixtures/ast/vue3.3-generic-6-with-default/ast.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sourceType": "module",
"parser": {
"ts": "@typescript-eslint/parser"
}
}
21 changes: 21 additions & 0 deletions test/fixtures/ast/vue3.3-generic-6-with-default/source.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script setup lang="ts" generic="
T // Comments
extends /* = */ Foo = number,
// Comments
U /* extends */
extends
Record&lt;
/* = */
string, // =
T
>
= Record<string, number&gt;
">
type Foo = number | string
const p = defineProps<{foo:T, bar: U}>()
const foo = p.foo
console.log(foo)
</script>
<template>
{{foo}}
</template>
123 changes: 123 additions & 0 deletions test/fixtures/ast/vue3.3-generic-6-with-default/token-ranges.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
[
"<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\">",
"type",
"Foo",
"=",
"number",
"|",
"string",
"const",
"p",
"=",
"defineProps",
"<",
"{",
"foo",
":",
"T",
",",
"bar",
":",
"U",
"}",
">",
"(",
")",
"const",
"foo",
"=",
"p",
".",
"foo",
"console",
".",
"log",
"(",
"foo",
")",
"</script>",
"<script",
"setup",
"lang",
"=",
"\"ts\"",
"generic",
"=",
"\"",
"T",
"extends",
"Foo",
"=",
"number",
",",
"U",
"extends",
"Record",
"&lt;",
"string",
",",
"T",
">",
"=",
"Record",
"<",
"string",
",",
"number",
"&gt;",
"\"",
">",
"\n",
"type",
" ",
"Foo",
" ",
"=",
" ",
"number",
" ",
"|",
" ",
"string",
"\n",
"const",
" ",
"p",
" ",
"=",
" ",
"defineProps<{foo:T,",
" ",
"bar:",
" ",
"U}>()",
"\n",
"const",
" ",
"foo",
" ",
"=",
" ",
"p.foo",
"\n",
"console.log(foo)",
"\n",
"</script",
">",
"\n",
"<template",
">",
"\n",
"{{",
"foo",
"}}",
"\n",
"</template",
">",
"// Comments",
"/* = */",
"// Comments",
"/* extends */",
"/* = */",
"// ="
]
39 changes: 39 additions & 0 deletions test/fixtures/ast/vue3.3-generic-6-with-default/tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[
{
"type": "VElement",
"text": "<template>\n{{foo}}\n</template>",
"children": [
{
"type": "VStartTag",
"text": "<template>",
"children": []
},
{
"type": "VText",
"text": "\n",
"children": []
},
{
"type": "VExpressionContainer",
"text": "{{foo}}",
"children": [
{
"type": "Identifier",
"text": "foo",
"children": []
}
]
},
{
"type": "VText",
"text": "\n",
"children": []
},
{
"type": "VEndTag",
"text": "</template>",
"children": []
}
]
}
]
Loading