Skip to content

Commit 8745198

Browse files
committed
fix: correct typing to Svelte AST
1 parent 3fdae82 commit 8745198

7 files changed

Lines changed: 189 additions & 155 deletions

File tree

.changeset/kind-bees-wash.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@svelte-put/preprocess-auto-slug': patch
3+
'@svelte-put/preprocess-markdown': patch
4+
'@svelte-put/preprocess-external-link': patch
5+
'@svelte-put/inline-svg': patch
6+
'@svelte-put/preaction': patch
7+
---
8+
9+
update typing to Svelte AST (following [svelte@5.0.0-next.243](https://github.com/sveltejs/svelte/releases/tag/svelte%405.0.0-next.243) where AST has been exposed back to public typedef)

packages/inline-svg/src/preprocessor/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function inlineSvg(uSources, uConfig) {
3535
const ast = parseSvelteMarkup(content, { filename, modern: true });
3636

3737
walk(
38-
/** @type {import('svelte/compiler').ElementLike} */ (
38+
/** @type {import('svelte/compiler').AST.RegularElement} */ (
3939
/** @type {unknown} */ (ast.fragment)
4040
),
4141
null,

packages/inline-svg/src/preprocessor/internals.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import path from 'path';
1414
*/
1515

1616
/** @package */
17-
export const DEFAULT_SOURCES_CONFIG = /** @satisfies {ResolvedSourceDefinition} */({
17+
export const DEFAULT_SOURCES_CONFIG = /** @satisfies {ResolvedSourceDefinition} */ ({
1818
directories: [],
1919
attributes: {},
2020
});
@@ -78,7 +78,7 @@ export function resolveSources(sources) {
7878
*/
7979

8080
/** @package */
81-
export const DEFAULT_INLINE_SVG_CONFIG = /** @type {ResolvedPreprocessorConfig} */({
81+
export const DEFAULT_INLINE_SVG_CONFIG = /** @type {ResolvedPreprocessorConfig} */ ({
8282
inlineSrcAttributeName: 'inline-src',
8383
keepInlineSrcAttribute: false,
8484
});
@@ -126,14 +126,14 @@ export function findSvgSrc(filename, directories, inlineSrc) {
126126

127127
/**
128128
* @param {string} source
129-
* @param {import('svelte/compiler').RegularElement} node
129+
* @param {import('svelte/compiler').AST.RegularElement} node
130130
* @param {string} attributeName
131131
* @returns {string | undefined}
132132
*/
133133
export function getAttribute(source, node, attributeName) {
134-
const attr = /** @type {import('svelte/compiler').Attribute} */(node.attributes.find(
135-
(attr) => attr.type === 'Attribute' && attr.name === attributeName,
136-
));
134+
const attr = /** @type {import('svelte/compiler').AST.Attribute} */ (
135+
node.attributes.find((attr) => attr.type === 'Attribute' && attr.name === attributeName)
136+
);
137137
if (attr) {
138138
let raw = source.slice(attr.start + attributeName.length + 1, attr.end);
139139
if (raw.startsWith('"') && raw.endsWith('"')) {

packages/preaction/src/preprocess.js

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,63 +24,77 @@ export function preaction() {
2424
if (!script) continue;
2525
const str = s.slice(script.start, script.end);
2626
if (!str.includes(PACKAGE_NAME)) continue;
27-
walk(/** @type {import('estree').ImportDeclaration} */(/** @type {unknown} */(script)), null, {
28-
ImportDeclaration(node, { next }) {
29-
if (node.source.value !== PACKAGE_NAME) return next();
30-
const applyAction = node.specifiers.find(
31-
(specifier) => specifier.type === 'ImportSpecifier' && specifier.imported.name === 'apply',
32-
);
33-
if (applyAction) applyActionName = applyAction.local.name;
27+
walk(
28+
/** @type {import('estree').ImportDeclaration} */ (/** @type {unknown} */ (script)),
29+
null,
30+
{
31+
ImportDeclaration(node, { next }) {
32+
if (node.source.value !== PACKAGE_NAME) return next();
33+
const applyAction = node.specifiers.find(
34+
(specifier) =>
35+
specifier.type === 'ImportSpecifier' &&
36+
specifier.imported.type === 'Identifier' &&
37+
specifier.imported.name === 'apply',
38+
);
39+
if (applyAction) applyActionName = applyAction.local.name;
40+
},
3441
},
35-
});
42+
);
3643
break;
3744
}
3845
if (!applyActionName) return;
3946

4047
/** @type {string[]} */
4148
const declarations = [];
4249

43-
// TODO: adjust typing when https://github.com/sveltejs/svelte/issues/12292 is resolved
44-
// walk(/** @type {import('svelte/compiler').ElementLike} */ (ast.fragment), null, {
45-
walk(/** @type {any} */ (ast.fragment), null, {
46-
RegularElement(node, { next }) {
47-
const uses = node.attributes.filter(
48-
(attr) => attr.type === 'UseDirective' && attr.name === applyActionName,
49-
);
50-
if (!uses.length) return next();
51-
52-
const firstAttribute = node.attributes[0];
53-
54-
for (const use of uses) {
55-
// 0 - generate unique name for eaction
56-
const name = `__preaction_${count++}`;
57-
58-
// 1 - add to declarations: const preaction_#count = preaction(param)
59-
const expression = s.slice(use.expression.start, use.expression.end);
60-
declarations.push(`const ${name} = ${expression}`);
61-
62-
// 2 - transform use directive: use:preaction_#count.action={param}
63-
const arg = use.expression.arguments[0];
64-
let argStr = arg ? `={${s.slice(arg.start, arg.end)}}` : '';
65-
s.overwrite(use.start, use.end, `use:${name}.action${argStr}`);
66-
67-
// 3 - add attribute spread to start of tag: {...preaction_#count.attributes}
68-
s.prependLeft(
69-
firstAttribute.start,
70-
`{...(${name}.attributes ?? {})} `,
50+
walk(
51+
/** @type {import('svelte/compiler').AST.RegularElement} */ (
52+
/** @type {unknown} */ (ast.fragment)
53+
),
54+
null,
55+
{
56+
RegularElement(node, { next }) {
57+
const uses = /** @type {import('svelte/compiler').AST.UseDirective[]} */ (
58+
node.attributes.filter(
59+
(attr) => attr.type === 'UseDirective' && attr.name === applyActionName,
60+
)
7161
);
72-
}
62+
if (!uses.length) return next();
63+
64+
const firstAttribute = node.attributes[0];
65+
66+
for (const use of uses) {
67+
// 0 - generate unique name for preaction
68+
const name = `__preaction_${count++}`;
69+
70+
if (!use.expression || use.expression.type !== 'CallExpression') continue;
71+
72+
// 1 - add to declarations: const preaction_#count = preaction(param)
73+
const { start: expStart, end: expEnd } = /** @type {any} */ (use.expression);
74+
const expression = s.slice(expStart, expEnd);
75+
declarations.push(`const ${name} = ${expression}`);
7376

74-
return next();
77+
// 2 - transform use directive: use:preaction_#count.action={param}
78+
const arg = use.expression.arguments[0];
79+
const { start: argStart, end: argEnd } = /** @type {any} */ (arg);
80+
let argStr = arg ? `={${s.slice(argStart, argEnd)}}` : '';
81+
s.overwrite(use.start, use.end, `use:${name}.action${argStr}`);
82+
83+
// 3 - add attribute spread to start of tag: {...preaction_#count.attributes}
84+
s.prependLeft(firstAttribute.start, `{...(${name}.attributes ?? {})} `);
85+
}
86+
87+
return next();
88+
},
7589
},
76-
});
90+
);
7791

7892
// add declarations to end of script tag
7993
const declarationsStr = '\n' + declarations.join('\n') + '\n';
8094
if (ast.instance) {
81-
s.prependLeft(/** @type {any} */(ast.instance.content).end, declarationsStr)
95+
s.prependLeft(/** @type {any} */ (ast.instance.content).end, declarationsStr);
8296
} else {
83-
s.append(`<script>${declarationsStr}</script>`)
97+
s.append(`<script>${declarationsStr}</script>`);
8498
}
8599

86100
return {
@@ -92,4 +106,3 @@ export function preaction() {
92106
}
93107

94108
export default preaction;
95-

0 commit comments

Comments
 (0)