From e99be35da68db7a68b9d64339adf0ca0353efee2 Mon Sep 17 00:00:00 2001
From: KazariEX <1364035137@qq.com>
Date: Thu, 5 Dec 2024 17:36:18 +0800
Subject: [PATCH 1/4] fix: avoid unchecked index access when parsing
`defineEmits`
---
.../language-core/lib/parsers/scriptSetupRanges.ts | 13 +++++++++----
.../tsc/passedFixtures/vue3/#5027/main.vue | 3 +++
2 files changed, 12 insertions(+), 4 deletions(-)
create mode 100644 test-workspace/tsc/passedFixtures/vue3/#5027/main.vue
diff --git a/packages/language-core/lib/parsers/scriptSetupRanges.ts b/packages/language-core/lib/parsers/scriptSetupRanges.ts
index 433674d2f1..0fc8f48db1 100644
--- a/packages/language-core/lib/parsers/scriptSetupRanges.ts
+++ b/packages/language-core/lib/parsers/scriptSetupRanges.ts
@@ -306,12 +306,17 @@ export function parseScriptSetupRanges(
if (ts.isVariableDeclaration(parent)) {
emits.name = getNodeText(ts, parent.name, ast);
}
- if (node.typeArguments?.length && ts.isTypeLiteralNode(node.typeArguments[0]) && node.typeArguments[0].members.at(0)) {
+ if (node.typeArguments?.length && ts.isTypeLiteralNode(node.typeArguments[0])) {
for (const member of node.typeArguments[0].members) {
- if (ts.isCallSignatureDeclaration(member) && member.parameters[0].type && ts.isUnionTypeNode(member.parameters[0].type)) {
- emits.define.hasUnionTypeArg = true;
- return;
+ if (!ts.isCallSignatureDeclaration(member)) {
+ continue;
+ }
+ const type = member.parameters[0]?.type;
+ if (!type || !ts.isUnionTypeNode(type)) {
+ continue;
}
+ emits.define.hasUnionTypeArg = true;
+ return;
}
}
}
diff --git a/test-workspace/tsc/passedFixtures/vue3/#5027/main.vue b/test-workspace/tsc/passedFixtures/vue3/#5027/main.vue
new file mode 100644
index 0000000000..1e0d9389cd
--- /dev/null
+++ b/test-workspace/tsc/passedFixtures/vue3/#5027/main.vue
@@ -0,0 +1,3 @@
+
\ No newline at end of file
From 1604f4a2631c94a3af1a70e34f54f40daa1aa215 Mon Sep 17 00:00:00 2001
From: KazariEX <1364035137@qq.com>
Date: Thu, 5 Dec 2024 17:40:05 +0800
Subject: [PATCH 2/4] refactor: simplify
---
.../language-core/lib/parsers/scriptSetupRanges.ts | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)
diff --git a/packages/language-core/lib/parsers/scriptSetupRanges.ts b/packages/language-core/lib/parsers/scriptSetupRanges.ts
index 0fc8f48db1..0614a2b99d 100644
--- a/packages/language-core/lib/parsers/scriptSetupRanges.ts
+++ b/packages/language-core/lib/parsers/scriptSetupRanges.ts
@@ -308,15 +308,13 @@ export function parseScriptSetupRanges(
}
if (node.typeArguments?.length && ts.isTypeLiteralNode(node.typeArguments[0])) {
for (const member of node.typeArguments[0].members) {
- if (!ts.isCallSignatureDeclaration(member)) {
- continue;
- }
- const type = member.parameters[0]?.type;
- if (!type || !ts.isUnionTypeNode(type)) {
- continue;
+ if (ts.isCallSignatureDeclaration(member)) {
+ const type = member.parameters[0]?.type;
+ if (type && ts.isUnionTypeNode(type)) {
+ emits.define.hasUnionTypeArg = true;
+ break;
+ }
}
- emits.define.hasUnionTypeArg = true;
- return;
}
}
}
From e89fec3d7454a7c3415028b53d2c7a2f075cef6f Mon Sep 17 00:00:00 2001
From: KazariEX <1364035137@qq.com>
Date: Thu, 5 Dec 2024 17:40:40 +0800
Subject: [PATCH 3/4] chore: lint
---
packages/language-core/lib/parsers/scriptSetupRanges.ts | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/packages/language-core/lib/parsers/scriptSetupRanges.ts b/packages/language-core/lib/parsers/scriptSetupRanges.ts
index 0614a2b99d..a95ab04f0a 100644
--- a/packages/language-core/lib/parsers/scriptSetupRanges.ts
+++ b/packages/language-core/lib/parsers/scriptSetupRanges.ts
@@ -380,7 +380,8 @@ export function parseScriptSetupRanges(
}
}
}
- } else if (vueCompilerOptions.composibles.useTemplateRef.includes(callText) && node.arguments.length && !node.typeArguments?.length) {
+ }
+ else if (vueCompilerOptions.composibles.useTemplateRef.includes(callText) && node.arguments.length && !node.typeArguments?.length) {
const define = parseDefineFunction(node);
let name;
if (ts.isVariableDeclaration(parent)) {
From 52baa61d51026bace2f76ade986f5d72b1481d66 Mon Sep 17 00:00:00 2001
From: KazariEX <1364035137@qq.com>
Date: Thu, 5 Dec 2024 17:45:21 +0800
Subject: [PATCH 4/4] test: fix
---
test-workspace/tsc/passedFixtures/vue3/#5027/main.vue | 1 +
1 file changed, 1 insertion(+)
diff --git a/test-workspace/tsc/passedFixtures/vue3/#5027/main.vue b/test-workspace/tsc/passedFixtures/vue3/#5027/main.vue
index 1e0d9389cd..c0afbc8e68 100644
--- a/test-workspace/tsc/passedFixtures/vue3/#5027/main.vue
+++ b/test-workspace/tsc/passedFixtures/vue3/#5027/main.vue
@@ -1,3 +1,4 @@
\ No newline at end of file