Skip to content

chore: use JSDoc @import tag #12130

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

Merged
merged 1 commit into from
Jun 24, 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
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"search.exclude": {
"sites/svelte-5-preview/static/*": true
}
},
"typescript.tsdk": "node_modules/typescript/lib"
}
102 changes: 38 additions & 64 deletions packages/svelte/src/compiler/legacy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/** @import { Expression } from 'estree' */
/** @import { BaseNode, ConstTag, Root, SvelteNode, TemplateNode, Text } from '#compiler' */
/** @import * as Legacy from './types/legacy-nodes.js' */
import { walk } from 'zimmerframe';
import {
regex_ends_with_whitespaces,
Expand All @@ -8,7 +11,7 @@ import { extract_svelte_ignore } from './utils/extract_svelte_ignore.js';

/**
* Some of the legacy Svelte AST nodes remove whitespace from the start and end of their children.
* @param {import('./types/template.js').TemplateNode[]} nodes
* @param {TemplateNode[]} nodes
*/
function remove_surrounding_whitespace_nodes(nodes) {
const first = nodes.at(0);
Expand All @@ -33,16 +36,13 @@ function remove_surrounding_whitespace_nodes(nodes) {
/**
* Transform our nice modern AST into the monstrosity emitted by Svelte 4
* @param {string} source
* @param {import('#compiler').Root} ast
* @returns {import('./types/legacy-nodes.js').LegacyRoot}
* @param {Root} ast
* @returns {Legacy.LegacyRoot}
*/
export function convert(source, ast) {
const root =
/** @type {import('./types/template.js').SvelteNode | import('./types/legacy-nodes.js').LegacySvelteNode} */ (
ast
);
const root = /** @type {SvelteNode | Legacy.LegacySvelteNode} */ (ast);

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

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

start = first.start;
end = last.end;
Expand Down Expand Up @@ -229,25 +229,20 @@ export function convert(source, ast) {
end: node.end,
name: node.name,
attributes: node.attributes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
),
children: node.fragment.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
},
// @ts-ignore
ConstTag(node) {
if (
/** @type {import('./types/legacy-nodes.js').LegacyConstTag} */ (node).expression !==
undefined
) {
if (/** @type {Legacy.LegacyConstTag} */ (node).expression !== undefined) {
return node;
}

const modern_node = /** @type {import('#compiler').ConstTag} */ (node);
const modern_node = /** @type {ConstTag} */ (node);
const { id: left } = { ...modern_node.declaration.declarations[0] };
// @ts-ignore
delete left.typeAnnotation;
Expand All @@ -274,8 +269,7 @@ export function convert(source, ast) {
end: node.end,
expression: node.expression,
children: node.fragment.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
},
Expand Down Expand Up @@ -354,8 +348,7 @@ export function convert(source, ast) {
start,
end: end,
children: node.alternate.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
}
Expand All @@ -368,8 +361,7 @@ export function convert(source, ast) {
end: node.end,
expression: node.test,
children: node.consequent.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
),
else: elseblock,
elseif: node.elseif ? true : undefined
Expand Down Expand Up @@ -407,12 +399,10 @@ export function convert(source, ast) {
end: node.end,
name: node.name,
attributes: node.attributes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
),
children: node.fragment.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
},
Expand All @@ -433,12 +423,10 @@ export function convert(source, ast) {
start: node.start,
end: node.end,
attributes: node.attributes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
),
children: node.fragment.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
},
Expand All @@ -450,12 +438,10 @@ export function convert(source, ast) {
end: node.end,
expression: node.expression,
attributes: node.attributes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
),
children: node.fragment.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
},
Expand All @@ -466,17 +452,15 @@ export function convert(source, ast) {
start: node.start,
end: node.end,
attributes: node.attributes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
),
children: node.fragment.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
},
SvelteElement(node, { visit }) {
/** @type {import('estree').Expression | string} */
/** @type {Expression | string} */
let tag = node.tag;
if (
tag.type === 'Literal' &&
Expand All @@ -503,11 +487,10 @@ export function convert(source, ast) {
start: node.start,
end: node.end,
attributes: node.attributes.map(
(a) => /** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(a))
(a) => /** @type {Legacy.LegacyAttributeLike} */ (visit(a))
),
children: node.fragment.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
},
Expand All @@ -518,12 +501,10 @@ export function convert(source, ast) {
start: node.start,
end: node.end,
attributes: node.attributes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
),
children: node.fragment.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
},
Expand All @@ -534,8 +515,7 @@ export function convert(source, ast) {
start: node.start,
end: node.end,
attributes: node.attributes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
)
};
},
Expand All @@ -546,12 +526,10 @@ export function convert(source, ast) {
start: node.start,
end: node.end,
attributes: node.attributes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
),
children: node.fragment.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
},
Expand All @@ -562,20 +540,18 @@ export function convert(source, ast) {
start: node.start,
end: node.end,
attributes: node.attributes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
),
children: node.fragment.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
},
Text(node, { path }) {
const parent = path.at(-1);
if (parent?.type === 'RegularElement' && parent.name === 'style') {
// these text nodes are missing `raw` for some dumb reason
return /** @type {import('./types/template.js').Text} */ ({
return /** @type {Text} */ ({
type: 'Text',
start: node.start,
end: node.end,
Expand All @@ -590,12 +566,10 @@ export function convert(source, ast) {
start: node.start,
end: node.end,
attributes: node.attributes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyAttributeLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyAttributeLike} */ (visit(child))
),
children: node.fragment.nodes.map(
(child) =>
/** @type {import('./types/legacy-nodes.js').LegacyElementLike} */ (visit(child))
(child) => /** @type {Legacy.LegacyElementLike} */ (visit(child))
)
};
},
Expand Down
Loading