Skip to content

Commit 879b011

Browse files
authored
chore: use JSDoc @import tag (#12130)
1 parent 7e462ee commit 879b011

File tree

2 files changed

+40
-65
lines changed

2 files changed

+40
-65
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"search.exclude": {
33
"sites/svelte-5-preview/static/*": true
4-
}
4+
},
5+
"typescript.tsdk": "node_modules/typescript/lib"
56
}

packages/svelte/src/compiler/legacy.js

Lines changed: 38 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/** @import { Expression } from 'estree' */
2+
/** @import { BaseNode, ConstTag, Root, SvelteNode, TemplateNode, Text } from '#compiler' */
3+
/** @import * as Legacy from './types/legacy-nodes.js' */
14
import { walk } from 'zimmerframe';
25
import {
36
regex_ends_with_whitespaces,
@@ -8,7 +11,7 @@ import { extract_svelte_ignore } from './utils/extract_svelte_ignore.js';
811

912
/**
1013
* Some of the legacy Svelte AST nodes remove whitespace from the start and end of their children.
11-
* @param {import('./types/template.js').TemplateNode[]} nodes
14+
* @param {TemplateNode[]} nodes
1215
*/
1316
function remove_surrounding_whitespace_nodes(nodes) {
1417
const first = nodes.at(0);
@@ -33,16 +36,13 @@ function remove_surrounding_whitespace_nodes(nodes) {
3336
/**
3437
* Transform our nice modern AST into the monstrosity emitted by Svelte 4
3538
* @param {string} source
36-
* @param {import('#compiler').Root} ast
37-
* @returns {import('./types/legacy-nodes.js').LegacyRoot}
39+
* @param {Root} ast
40+
* @returns {Legacy.LegacyRoot}
3841
*/
3942
export function convert(source, ast) {
40-
const root =
41-
/** @type {import('./types/template.js').SvelteNode | import('./types/legacy-nodes.js').LegacySvelteNode} */ (
42-
ast
43-
);
43+
const root = /** @type {SvelteNode | Legacy.LegacySvelteNode} */ (ast);
4444

45-
return /** @type {import('./types/legacy-nodes.js').LegacyRoot} */ (
45+
return /** @type {Legacy.LegacyRoot} */ (
4646
walk(root, null, {
4747
_(node, { next }) {
4848
// @ts-ignore
@@ -74,8 +74,8 @@ export function convert(source, ast) {
7474
let end = null;
7575

7676
if (node.fragment.nodes.length > 0) {
77-
const first = /** @type {import('#compiler').BaseNode} */ (node.fragment.nodes.at(0));
78-
const last = /** @type {import('#compiler').BaseNode} */ (node.fragment.nodes.at(-1));
77+
const first = /** @type {BaseNode} */ (node.fragment.nodes.at(0));
78+
const last = /** @type {BaseNode} */ (node.fragment.nodes.at(-1));
7979

8080
start = first.start;
8181
end = last.end;
@@ -229,25 +229,20 @@ export function convert(source, ast) {
229229
end: node.end,
230230
name: node.name,
231231
attributes: node.attributes.map(
232-
(child) =>
233-
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
232+
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
234233
),
235234
children: node.fragment.nodes.map(
236-
(child) =>
237-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
235+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
238236
)
239237
};
240238
},
241239
// @ts-ignore
242240
ConstTag(node) {
243-
if (
244-
/** @type {import('./types/legacy-nodes.js').LegacyConstTag} */ (node).expression !==
245-
undefined
246-
) {
241+
if (/** @type {Legacy.LegacyConstTag} */ (node).expression !== undefined) {
247242
return node;
248243
}
249244

250-
const modern_node = /** @type {import('#compiler').ConstTag} */ (node);
245+
const modern_node = /** @type {ConstTag} */ (node);
251246
const { id: left } = { ...modern_node.declaration.declarations[0] };
252247
// @ts-ignore
253248
delete left.typeAnnotation;
@@ -274,8 +269,7 @@ export function convert(source, ast) {
274269
end: node.end,
275270
expression: node.expression,
276271
children: node.fragment.nodes.map(
277-
(child) =>
278-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
272+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
279273
)
280274
};
281275
},
@@ -354,8 +348,7 @@ export function convert(source, ast) {
354348
start,
355349
end: end,
356350
children: node.alternate.nodes.map(
357-
(child) =>
358-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
351+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
359352
)
360353
};
361354
}
@@ -368,8 +361,7 @@ export function convert(source, ast) {
368361
end: node.end,
369362
expression: node.test,
370363
children: node.consequent.nodes.map(
371-
(child) =>
372-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
364+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
373365
),
374366
else: elseblock,
375367
elseif: node.elseif ? true : undefined
@@ -407,12 +399,10 @@ export function convert(source, ast) {
407399
end: node.end,
408400
name: node.name,
409401
attributes: node.attributes.map(
410-
(child) =>
411-
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
402+
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
412403
),
413404
children: node.fragment.nodes.map(
414-
(child) =>
415-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
405+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
416406
)
417407
};
418408
},
@@ -433,12 +423,10 @@ export function convert(source, ast) {
433423
start: node.start,
434424
end: node.end,
435425
attributes: node.attributes.map(
436-
(child) =>
437-
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
426+
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
438427
),
439428
children: node.fragment.nodes.map(
440-
(child) =>
441-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
429+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
442430
)
443431
};
444432
},
@@ -450,12 +438,10 @@ export function convert(source, ast) {
450438
end: node.end,
451439
expression: node.expression,
452440
attributes: node.attributes.map(
453-
(child) =>
454-
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
441+
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
455442
),
456443
children: node.fragment.nodes.map(
457-
(child) =>
458-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
444+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
459445
)
460446
};
461447
},
@@ -466,17 +452,15 @@ export function convert(source, ast) {
466452
start: node.start,
467453
end: node.end,
468454
attributes: node.attributes.map(
469-
(child) =>
470-
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
455+
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
471456
),
472457
children: node.fragment.nodes.map(
473-
(child) =>
474-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
458+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
475459
)
476460
};
477461
},
478462
SvelteElement(node, { visit }) {
479-
/** @type {import('estree').Expression | string} */
463+
/** @type {Expression | string} */
480464
let tag = node.tag;
481465
if (
482466
tag.type === 'Literal' &&
@@ -503,11 +487,10 @@ export function convert(source, ast) {
503487
start: node.start,
504488
end: node.end,
505489
attributes: node.attributes.map(
506-
(a) => /** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(a))
490+
(a) => /** @type {Legacy.LegacyAttributeLike} */ (visit(a))
507491
),
508492
children: node.fragment.nodes.map(
509-
(child) =>
510-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
493+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
511494
)
512495
};
513496
},
@@ -518,12 +501,10 @@ export function convert(source, ast) {
518501
start: node.start,
519502
end: node.end,
520503
attributes: node.attributes.map(
521-
(child) =>
522-
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
504+
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
523505
),
524506
children: node.fragment.nodes.map(
525-
(child) =>
526-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
507+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
527508
)
528509
};
529510
},
@@ -534,8 +515,7 @@ export function convert(source, ast) {
534515
start: node.start,
535516
end: node.end,
536517
attributes: node.attributes.map(
537-
(child) =>
538-
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
518+
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
539519
)
540520
};
541521
},
@@ -546,12 +526,10 @@ export function convert(source, ast) {
546526
start: node.start,
547527
end: node.end,
548528
attributes: node.attributes.map(
549-
(child) =>
550-
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
529+
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
551530
),
552531
children: node.fragment.nodes.map(
553-
(child) =>
554-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
532+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
555533
)
556534
};
557535
},
@@ -562,20 +540,18 @@ export function convert(source, ast) {
562540
start: node.start,
563541
end: node.end,
564542
attributes: node.attributes.map(
565-
(child) =>
566-
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
543+
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
567544
),
568545
children: node.fragment.nodes.map(
569-
(child) =>
570-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
546+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
571547
)
572548
};
573549
},
574550
Text(node, { path }) {
575551
const parent = path.at(-1);
576552
if (parent?.type === 'RegularElement' && parent.name === 'style') {
577553
// these text nodes are missing `raw` for some dumb reason
578-
return /** @type {import('./types/template.js').Text} */ ({
554+
return /** @type {Text} */ ({
579555
type: 'Text',
580556
start: node.start,
581557
end: node.end,
@@ -590,12 +566,10 @@ export function convert(source, ast) {
590566
start: node.start,
591567
end: node.end,
592568
attributes: node.attributes.map(
593-
(child) =>
594-
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
569+
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
595570
),
596571
children: node.fragment.nodes.map(
597-
(child) =>
598-
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
572+
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
599573
)
600574
};
601575
},

0 commit comments

Comments
 (0)