Skip to content

Commit 167b337

Browse files
committed
more
1 parent c853d6a commit 167b337

File tree

1 file changed

+35
-51
lines changed

1 file changed

+35
-51
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/javascript-runes.js

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/** @import { CallExpression, Expression, Identifier, Literal, MethodDefinition, PropertyDefinition, VariableDeclarator } from 'estree' */
2+
/** @import { Binding } from '#compiler' */
3+
/** @import { ComponentVisitors, StateField } from '../types.js' */
14
import { get_rune } from '../../../scope.js';
25
import { is_hoistable_function, transform_inspect_rune } from '../../utils.js';
36
import * as b from '../../../../utils/builders.js';
@@ -12,13 +15,13 @@ import {
1215
import { extract_paths } from '../../../../utils/ast.js';
1316
import { regex_invalid_identifier_chars } from '../../../patterns.js';
1417

15-
/** @type {import('../types.js').ComponentVisitors} */
18+
/** @type {ComponentVisitors} */
1619
export const javascript_visitors_runes = {
1720
ClassBody(node, { state, visit }) {
18-
/** @type {Map<string, import('../types.js').StateField>} */
21+
/** @type {Map<string, StateField>} */
1922
const public_state = new Map();
2023

21-
/** @type {Map<string, import('../types.js').StateField>} */
24+
/** @type {Map<string, StateField>} */
2225
const private_state = new Map();
2326

2427
/** @type {string[]} */
@@ -46,7 +49,7 @@ export const javascript_visitors_runes = {
4649
rune === '$derived' ||
4750
rune === '$derived.by'
4851
) {
49-
/** @type {import('../types.js').StateField} */
52+
/** @type {StateField} */
5053
const field = {
5154
kind:
5255
rune === '$state'
@@ -81,7 +84,7 @@ export const javascript_visitors_runes = {
8184
field.id = b.private_id(deconflicted);
8285
}
8386

84-
/** @type {Array<import('estree').MethodDefinition | import('estree').PropertyDefinition>} */
87+
/** @type {Array<MethodDefinition | PropertyDefinition>} */
8588
const body = [];
8689

8790
const child_state = { ...state, public_state, private_state };
@@ -104,7 +107,7 @@ export const javascript_visitors_runes = {
104107
let value = null;
105108

106109
if (definition.value.arguments.length > 0) {
107-
const init = /** @type {import('estree').Expression} **/ (
110+
const init = /** @type {Expression} **/ (
108111
visit(definition.value.arguments[0], child_state)
109112
);
110113

@@ -182,7 +185,7 @@ export const javascript_visitors_runes = {
182185
}
183186
}
184187

185-
body.push(/** @type {import('estree').MethodDefinition} **/ (visit(definition, child_state)));
188+
body.push(/** @type {MethodDefinition} **/ (visit(definition, child_state)));
186189
}
187190

188191
if (state.options.dev && public_state.size > 0) {
@@ -225,15 +228,11 @@ export const javascript_visitors_runes = {
225228
if (init != null && is_hoistable_function(init)) {
226229
const hoistable_function = visit(init);
227230
state.hoisted.push(
228-
b.declaration(
229-
'const',
230-
declarator.id,
231-
/** @type {import('estree').Expression} */ (hoistable_function)
232-
)
231+
b.declaration('const', declarator.id, /** @type {Expression} */ (hoistable_function))
233232
);
234233
continue;
235234
}
236-
declarations.push(/** @type {import('estree').VariableDeclarator} */ (visit(declarator)));
235+
declarations.push(/** @type {VariableDeclarator} */ (visit(declarator)));
237236
continue;
238237
}
239238

@@ -246,7 +245,7 @@ export const javascript_visitors_runes = {
246245
}
247246

248247
if (declarator.id.type === 'Identifier') {
249-
/** @type {import('estree').Expression[]} */
248+
/** @type {Expression[]} */
250249
const args = [b.id('$$props'), b.array(seen.map((name) => b.literal(name)))];
251250

252251
if (state.options.dev) {
@@ -260,20 +259,16 @@ export const javascript_visitors_runes = {
260259

261260
for (const property of declarator.id.properties) {
262261
if (property.type === 'Property') {
263-
const key = /** @type {import('estree').Identifier | import('estree').Literal} */ (
264-
property.key
265-
);
262+
const key = /** @type {Identifier | Literal} */ (property.key);
266263
const name = key.type === 'Identifier' ? key.name : /** @type {string} */ (key.value);
267264

268265
seen.push(name);
269266

270267
let id =
271268
property.value.type === 'AssignmentPattern' ? property.value.left : property.value;
272269
assert.equal(id.type, 'Identifier');
273-
const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
274-
let initial =
275-
binding.initial &&
276-
/** @type {import('estree').Expression} */ (visit(binding.initial));
270+
const binding = /** @type {Binding} */ (state.scope.get(id.name));
271+
let initial = binding.initial && /** @type {Expression} */ (visit(binding.initial));
277272
// We're adding proxy here on demand and not within the prop runtime function so that
278273
// people not using proxied state anywhere in their code don't have to pay the additional bundle size cost
279274
if (
@@ -289,14 +284,12 @@ export const javascript_visitors_runes = {
289284
}
290285
} else {
291286
// RestElement
292-
/** @type {import('estree').Expression[]} */
287+
/** @type {Expression[]} */
293288
const args = [b.id('$$props'), b.array(seen.map((name) => b.literal(name)))];
294289

295290
if (state.options.dev) {
296291
// include rest name, so we can provide informative error messages
297-
args.push(
298-
b.literal(/** @type {import('estree').Identifier} */ (property.argument).name)
299-
);
292+
args.push(b.literal(/** @type {Identifier} */ (property.argument).name));
300293
}
301294

302295
declarations.push(b.declarator(property.argument, b.call('$.rest_props', ...args)));
@@ -308,19 +301,17 @@ export const javascript_visitors_runes = {
308301
continue;
309302
}
310303

311-
const args = /** @type {import('estree').CallExpression} */ (init).arguments;
304+
const args = /** @type {CallExpression} */ (init).arguments;
312305
const value =
313-
args.length === 0
314-
? b.id('undefined')
315-
: /** @type {import('estree').Expression} */ (visit(args[0]));
306+
args.length === 0 ? b.id('undefined') : /** @type {Expression} */ (visit(args[0]));
316307

317308
if (rune === '$state' || rune === '$state.frozen') {
318309
/**
319-
* @param {import('estree').Identifier} id
320-
* @param {import('estree').Expression} value
310+
* @param {Identifier} id
311+
* @param {Expression} value
321312
*/
322313
const create_state_declarator = (id, value) => {
323-
const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
314+
const binding = /** @type {Binding} */ (state.scope.get(id.name));
324315
if (should_proxy_or_freeze(value, state.scope)) {
325316
value = b.call(rune === '$state' ? '$.proxy' : '$.freeze', value);
326317
}
@@ -341,9 +332,7 @@ export const javascript_visitors_runes = {
341332
b.declarator(b.id(tmp), value),
342333
...paths.map((path) => {
343334
const value = path.expression?.(b.id(tmp));
344-
const binding = state.scope.get(
345-
/** @type {import('estree').Identifier} */ (path.node).name
346-
);
335+
const binding = state.scope.get(/** @type {Identifier} */ (path.node).name);
347336
return b.declarator(
348337
path.node,
349338
binding?.kind === 'state' || binding?.kind === 'frozen_state'
@@ -426,7 +415,7 @@ export const javascript_visitors_runes = {
426415
const func = context.visit(node.expression.arguments[0]);
427416
return {
428417
...node,
429-
expression: b.call('$.user_effect', /** @type {import('estree').Expression} */ (func))
418+
expression: b.call('$.user_effect', /** @type {Expression} */ (func))
430419
};
431420
}
432421

@@ -441,7 +430,7 @@ export const javascript_visitors_runes = {
441430
const func = context.visit(node.expression.arguments[0]);
442431
return {
443432
...node,
444-
expression: b.call('$.user_pre_effect', /** @type {import('estree').Expression} */ (func))
433+
expression: b.call('$.user_pre_effect', /** @type {Expression} */ (func))
445434
};
446435
}
447436
}
@@ -460,24 +449,19 @@ export const javascript_visitors_runes = {
460449
}
461450

462451
if (rune === '$state.snapshot') {
463-
return b.call(
464-
'$.snapshot',
465-
/** @type {import('estree').Expression} */ (context.visit(node.arguments[0]))
466-
);
452+
return b.call('$.snapshot', /** @type {Expression} */ (context.visit(node.arguments[0])));
467453
}
468454

469455
if (rune === '$state.is') {
470456
return b.call(
471457
'$.is',
472-
/** @type {import('estree').Expression} */ (context.visit(node.arguments[0])),
473-
/** @type {import('estree').Expression} */ (context.visit(node.arguments[1]))
458+
/** @type {Expression} */ (context.visit(node.arguments[0])),
459+
/** @type {Expression} */ (context.visit(node.arguments[1]))
474460
);
475461
}
476462

477463
if (rune === '$effect.root') {
478-
const args = /** @type {import('estree').Expression[]} */ (
479-
node.arguments.map((arg) => context.visit(arg))
480-
);
464+
const args = /** @type {Expression[]} */ (node.arguments.map((arg) => context.visit(arg)));
481465
return b.call('$.effect_root', ...args);
482466
}
483467

@@ -494,17 +478,17 @@ export const javascript_visitors_runes = {
494478
if (operator === '===' || operator === '!==') {
495479
return b.call(
496480
'$.strict_equals',
497-
/** @type {import('estree').Expression} */ (visit(node.left)),
498-
/** @type {import('estree').Expression} */ (visit(node.right)),
481+
/** @type {Expression} */ (visit(node.left)),
482+
/** @type {Expression} */ (visit(node.right)),
499483
operator === '!==' && b.literal(false)
500484
);
501485
}
502486

503487
if (operator === '==' || operator === '!=') {
504488
return b.call(
505489
'$.equals',
506-
/** @type {import('estree').Expression} */ (visit(node.left)),
507-
/** @type {import('estree').Expression} */ (visit(node.right)),
490+
/** @type {Expression} */ (visit(node.left)),
491+
/** @type {Expression} */ (visit(node.right)),
508492
operator === '!=' && b.literal(false)
509493
);
510494
}
@@ -515,7 +499,7 @@ export const javascript_visitors_runes = {
515499
};
516500

517501
/**
518-
* @param {import('estree').Identifier | import('estree').PrivateIdentifier | import('estree').Literal} node
502+
* @param {Identifier | PrivateIdentifier | Literal} node
519503
*/
520504
function get_name(node) {
521505
if (node.type === 'Literal') {

0 commit comments

Comments
 (0)