Skip to content

Commit fcba4fe

Browse files
authored
Fix wrong scope for generics with multiple script blocks (#201)
1 parent 4b402ee commit fcba4fe

File tree

4 files changed

+92
-70
lines changed

4 files changed

+92
-70
lines changed

src/script-setup/scope-analyzer.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,10 @@ function analyzeScriptSetupVariables(
321321
)
322322

323323
const genericDefineNames = new Set<string>()
324-
const scriptElement = df.children.find(isScriptElement)
325-
if (
326-
scriptElement &&
327-
isScriptSetupElement(scriptElement) &&
328-
findGenericDirective(scriptElement)
329-
) {
330-
for (const variable of scriptElement.variables) {
324+
const scriptElements = df.children.filter(isScriptElement)
325+
const scriptSetupElement = scriptElements.find(isScriptSetupElement)
326+
if (scriptSetupElement && findGenericDirective(scriptSetupElement)) {
327+
for (const variable of scriptSetupElement.variables) {
331328
if (variable.kind === "generic") {
332329
genericDefineNames.add(variable.id.name)
333330
}

test/fixtures/ast/vue3.3-generic-2/scope.json

+25-21
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,31 @@
907907
"init": null
908908
}
909909
]
910+
},
911+
{
912+
"name": "T",
913+
"identifiers": [],
914+
"defs": [],
915+
"references": [
916+
{
917+
"identifier": {
918+
"type": "Identifier",
919+
"name": "T",
920+
"loc": {
921+
"start": {
922+
"line": 5,
923+
"column": 27
924+
},
925+
"end": {
926+
"line": 5,
927+
"column": 28
928+
}
929+
}
930+
},
931+
"from": "module",
932+
"init": null
933+
}
934+
]
910935
}
911936
],
912937
"references": [],
@@ -1296,7 +1321,6 @@
12961321
}
12971322
},
12981323
"from": "module",
1299-
"resolved": null,
13001324
"init": null
13011325
},
13021326
{
@@ -1451,7 +1475,6 @@
14511475
}
14521476
},
14531477
"from": "module",
1454-
"resolved": null,
14551478
"init": null
14561479
},
14571480
{
@@ -1477,25 +1500,6 @@
14771500
}
14781501
],
14791502
"through": [
1480-
{
1481-
"identifier": {
1482-
"type": "Identifier",
1483-
"name": "T",
1484-
"loc": {
1485-
"start": {
1486-
"line": 5,
1487-
"column": 27
1488-
},
1489-
"end": {
1490-
"line": 5,
1491-
"column": 28
1492-
}
1493-
}
1494-
},
1495-
"from": "module",
1496-
"resolved": null,
1497-
"init": null
1498-
},
14991503
{
15001504
"identifier": {
15011505
"type": "Identifier",

test/fixtures/ast/vue3.3-generic-3/scope.json

+50-42
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,56 @@
907907
"init": null
908908
}
909909
]
910+
},
911+
{
912+
"name": "T",
913+
"identifiers": [],
914+
"defs": [],
915+
"references": [
916+
{
917+
"identifier": {
918+
"type": "Identifier",
919+
"name": "T",
920+
"loc": {
921+
"start": {
922+
"line": 5,
923+
"column": 27
924+
},
925+
"end": {
926+
"line": 5,
927+
"column": 28
928+
}
929+
}
930+
},
931+
"from": "module",
932+
"init": null
933+
}
934+
]
935+
},
936+
{
937+
"name": "U",
938+
"identifiers": [],
939+
"defs": [],
940+
"references": [
941+
{
942+
"identifier": {
943+
"type": "Identifier",
944+
"name": "U",
945+
"loc": {
946+
"start": {
947+
"line": 5,
948+
"column": 35
949+
},
950+
"end": {
951+
"line": 5,
952+
"column": 36
953+
}
954+
}
955+
},
956+
"from": "module",
957+
"init": null
958+
}
959+
]
910960
}
911961
],
912962
"references": [],
@@ -1296,7 +1346,6 @@
12961346
}
12971347
},
12981348
"from": "module",
1299-
"resolved": null,
13001349
"init": null
13011350
},
13021351
{
@@ -1315,7 +1364,6 @@
13151364
}
13161365
},
13171366
"from": "module",
1318-
"resolved": null,
13191367
"init": null
13201368
},
13211369
{
@@ -1470,7 +1518,6 @@
14701518
}
14711519
},
14721520
"from": "module",
1473-
"resolved": null,
14741521
"init": null
14751522
},
14761523
{
@@ -1489,7 +1536,6 @@
14891536
}
14901537
},
14911538
"from": "module",
1492-
"resolved": null,
14931539
"init": null
14941540
},
14951541
{
@@ -1515,44 +1561,6 @@
15151561
}
15161562
],
15171563
"through": [
1518-
{
1519-
"identifier": {
1520-
"type": "Identifier",
1521-
"name": "T",
1522-
"loc": {
1523-
"start": {
1524-
"line": 5,
1525-
"column": 27
1526-
},
1527-
"end": {
1528-
"line": 5,
1529-
"column": 28
1530-
}
1531-
}
1532-
},
1533-
"from": "module",
1534-
"resolved": null,
1535-
"init": null
1536-
},
1537-
{
1538-
"identifier": {
1539-
"type": "Identifier",
1540-
"name": "U",
1541-
"loc": {
1542-
"start": {
1543-
"line": 5,
1544-
"column": 35
1545-
},
1546-
"end": {
1547-
"line": 5,
1548-
"column": 36
1549-
}
1550-
}
1551-
},
1552-
"from": "module",
1553-
"resolved": null,
1554-
"init": null
1555-
},
15561564
{
15571565
"identifier": {
15581566
"type": "Identifier",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script lang="ts">
2+
export default {}
3+
</script>
4+
<script setup lang="ts" generic="T extends Foo | Bar">
5+
type Foo = number | string
6+
type Bar = number[] | string[]
7+
interface Props {
8+
msg?: T
9+
}
10+
defineProps<Props>(), {
11+
msg: 'hello',
12+
}
13+
</script>

0 commit comments

Comments
 (0)