Skip to content

Commit 090b3cf

Browse files
committed
tidy up
1 parent 0ce3227 commit 090b3cf

18 files changed

+43
-58
lines changed

packages/svelte/src/compiler/phases/2-analyze/visitors/AssignmentExpression.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import { validate_assignment } from './shared/utils.js';
99
* @param {Context} context
1010
*/
1111
export function AssignmentExpression(node, context) {
12-
const parent = /** @type {SvelteNode} */ (context.path.at(-1));
13-
if (parent.type !== 'ConstTag') {
14-
validate_assignment(node, node.left, context.state);
15-
}
12+
validate_assignment(node, node.left, context.state);
1613

1714
if (context.state.reactive_statement) {
1815
const id = node.left.type === 'MemberExpression' ? object(node.left) : node.left;

packages/svelte/src/compiler/phases/2-analyze/visitors/Attribute.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** @import { ArrowFunctionExpression, Expression, FunctionDeclaration, FunctionExpression } from 'estree' */
22
/** @import { Attribute, DelegatedEvent, RegularElement } from '#compiler' */
33
/** @import { Context } from '../types' */
4-
54
import { DelegatedEvents, is_capture_event } from '../../../../constants.js';
65
import {
76
get_attribute_chunks,
@@ -44,6 +43,7 @@ export function Attribute(node, context) {
4443
if (delegated_event.type === 'hoistable') {
4544
delegated_event.function.metadata.hoistable = true;
4645
}
46+
4747
node.metadata.delegated = delegated_event;
4848
}
4949
}

packages/svelte/src/compiler/phases/2-analyze/visitors/AwaitBlock.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export function AwaitBlock(node, context) {
2020
const match = context.state.analysis.source
2121
.substring(start - 10, start)
2222
.match(/{(\s*):then\s+$/);
23+
2324
if (match && match[1] !== '') {
2425
e.block_unexpected_character({ start: start - 10, end: start }, ':');
2526
}
@@ -30,6 +31,7 @@ export function AwaitBlock(node, context) {
3031
const match = context.state.analysis.source
3132
.substring(start - 10, start)
3233
.match(/{(\s*):catch\s+$/);
34+
3335
if (match && match[1] !== '') {
3436
e.block_unexpected_character({ start: start - 10, end: start }, ':');
3537
}

packages/svelte/src/compiler/phases/2-analyze/visitors/BindDirective.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,24 @@ export function BindDirective(node, context) {
6666
// i.e. one of their declarations is referenced in the binding. This allows group bindings to work
6767
// correctly when referencing a variable declared in an EachBlock by using the index of the each block
6868
// entries as keys.
69-
let i = context.path.length;
7069
const each_blocks = [];
7170
const [keypath, expression_ids] = extract_all_identifiers_from_expression(node.expression);
7271
let ids = expression_ids;
72+
73+
let i = context.path.length;
7374
while (i--) {
7475
const parent = context.path[i];
76+
7577
if (parent.type === 'EachBlock') {
7678
const references = ids.filter((id) => parent.metadata.declarations.has(id.name));
79+
7780
if (references.length > 0) {
7881
parent.metadata.contains_group_binding = true;
82+
7983
for (const binding of parent.metadata.references) {
8084
binding.mutated = true;
8185
}
86+
8287
each_blocks.push(parent);
8388
ids = ids.filter((id) => !references.includes(id));
8489
ids.push(...extract_all_identifiers_from_expression(parent.expression)[1]);
@@ -92,6 +97,7 @@ export function BindDirective(node, context) {
9297
// but this is a limitation of the current static analysis we do; it also never worked in Svelte 4)
9398
const bindings = expression_ids.map((id) => context.state.scope.get(id.name));
9499
let group_name;
100+
95101
outer: for (const [[key, b], group] of context.state.analysis.binding_groups) {
96102
if (b.length !== bindings.length || key !== keypath) continue;
97103
for (let i = 0; i < bindings.length; i++) {
@@ -149,6 +155,7 @@ export function BindDirective(node, context) {
149155
})
150156
.map(([property_name]) => property_name)
151157
.sort();
158+
152159
e.bind_invalid_name(
153160
node,
154161
node.name,
@@ -160,6 +167,7 @@ export function BindDirective(node, context) {
160167
const type = /** @type {Attribute | undefined} */ (
161168
parent.attributes.find((a) => a.type === 'Attribute' && a.name === 'type')
162169
);
170+
163171
if (type && !is_text_attribute(type)) {
164172
if (node.name !== 'value' || type.value === true) {
165173
e.attribute_invalid_type(type);
@@ -183,6 +191,7 @@ export function BindDirective(node, context) {
183191
!is_text_attribute(a) &&
184192
a.value !== true
185193
);
194+
186195
if (multiple) {
187196
e.attribute_invalid_multiple(multiple);
188197
}
@@ -200,6 +209,7 @@ export function BindDirective(node, context) {
200209
const contenteditable = /** @type {Attribute} */ (
201210
parent.attributes.find((a) => a.type === 'Attribute' && a.name === 'contenteditable')
202211
);
212+
203213
if (!contenteditable) {
204214
e.attribute_contenteditable_missing(node);
205215
} else if (!is_text_attribute(contenteditable) && contenteditable.value !== true) {
@@ -208,12 +218,14 @@ export function BindDirective(node, context) {
208218
}
209219
} else {
210220
const match = fuzzymatch(node.name, Object.keys(binding_properties));
221+
211222
if (match) {
212223
const property = binding_properties[match];
213224
if (!property.valid_elements || property.valid_elements.includes(parent.name)) {
214225
e.bind_invalid_name(node, node.name, `Did you mean '${match}'?`);
215226
}
216227
}
228+
217229
e.bind_invalid_name(node, node.name);
218230
}
219231
}

packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,5 @@ export function ClassBody(node, context) {
2323
}
2424
}
2525

26-
context.next({
27-
...context.state,
28-
private_derived_state
29-
});
26+
context.next({ ...context.state, private_derived_state });
3027
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/** @import { Component } from '#compiler' */
22
/** @import { Context } from '../types' */
3-
import { validate_component } from './shared/component.js';
3+
import { visit_component } from './shared/component.js';
44

55
/**
66
* @param {Component} node
77
* @param {Context} context
88
*/
99
export function Component(node, context) {
10-
validate_component(node, context);
11-
1210
const binding = context.state.scope.get(
1311
node.name.includes('.') ? node.name.slice(0, node.name.indexOf('.')) : node.name
1412
);
1513

1614
node.metadata.dynamic = binding !== null && binding.kind !== 'normal';
15+
16+
visit_component(node, context);
1717
}

packages/svelte/src/compiler/phases/2-analyze/visitors/ExportDefaultDeclaration.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/** @import { ExportDefaultDeclaration, Node } from 'estree' */
22
/** @import { Context } from '../types' */
3-
/** @import { Scope } from '../../scope' */
43
import * as e from '../../../errors.js';
54

65
/**

packages/svelte/src/compiler/phases/2-analyze/visitors/ExportNamedDeclaration.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@ export function ExportNamedDeclaration(node, context) {
2525

2626
for (const declarator of node.declaration.declarations) {
2727
for (const id of extract_identifiers(declarator.id)) {
28-
validate_export(node, context.state.scope, id.name);
28+
const binding = context.state.scope.get(id.name);
29+
if (!binding) continue;
30+
31+
if (binding.kind === 'derived') {
32+
e.derived_invalid_export(node);
33+
}
34+
35+
if ((binding.kind === 'state' || binding.kind === 'frozen_state') && binding.reassigned) {
36+
e.state_invalid_export(node);
37+
}
2938
}
3039
}
3140
}
@@ -81,22 +90,3 @@ export function ExportNamedDeclaration(node, context) {
8190
}
8291
}
8392
}
84-
85-
/**
86-
*
87-
* @param {Node} node
88-
* @param {Scope} scope
89-
* @param {string} name
90-
*/
91-
function validate_export(node, scope, name) {
92-
const binding = scope.get(name);
93-
if (!binding) return;
94-
95-
if (binding.kind === 'derived') {
96-
e.derived_invalid_export(node);
97-
}
98-
99-
if ((binding.kind === 'state' || binding.kind === 'frozen_state') && binding.reassigned) {
100-
e.state_invalid_export(node);
101-
}
102-
}

packages/svelte/src/compiler/phases/2-analyze/visitors/ExportSpecifier.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function ExportSpecifier(node, context) {
2222
context.state.analysis.needs_props = true;
2323

2424
const binding = /** @type {Binding} */ (context.state.scope.get(node.local.name));
25+
2526
if (
2627
binding !== null &&
2728
(binding.kind === 'state' ||

packages/svelte/src/compiler/phases/2-analyze/visitors/ExpressionStatement.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export function ExpressionStatement(node, context) {
1818
)
1919
) {
2020
const binding = context.state.scope.get(node.expression.callee.name);
21+
2122
if (binding?.kind === 'normal' && binding.declaration_kind === 'import') {
2223
const declaration = /** @type {ImportDeclaration} */ (binding.initial);
2324

packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,5 @@ export function RegularElement(node, context) {
160160
w.element_invalid_self_closing_tag(node, node.name);
161161
}
162162

163-
context.next({
164-
...context.state,
165-
parent_element: node.name
166-
});
163+
context.next({ ...context.state, parent_element: node.name });
167164
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/** @import { SvelteComponent } from '#compiler' */
22
/** @import { Context } from '../types' */
3-
import { validate_component } from './shared/component.js';
3+
import { visit_component } from './shared/component.js';
44

55
/**
66
* @param {SvelteComponent} node
77
* @param {Context} context
88
*/
99
export function SvelteComponent(node, context) {
10-
validate_component(node, context);
10+
visit_component(node, context);
1111
}

packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteElement.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,5 @@ export function SvelteElement(node, context) {
5858
}
5959
}
6060

61-
context.next({
62-
...context.state,
63-
parent_element: null
64-
});
61+
context.next({ ...context.state, parent_element: null });
6562
}

packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteFragment.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,5 @@ export function SvelteFragment(node, context) {
2323
}
2424
}
2525

26-
context.next({
27-
...context.state,
28-
parent_element: null
29-
});
26+
context.next({ ...context.state, parent_element: null });
3027
}

packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteHead.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import * as e from '../../../errors.js';
77
* @param {Context} context
88
*/
99
export function SvelteHead(node, context) {
10-
const attribute = node.attributes[0];
11-
if (attribute) {
10+
for (const attribute of node.attributes) {
1211
e.svelte_head_illegal_attribute(attribute);
1312
}
1413

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/** @import { SvelteSelf } from '#compiler' */
22
/** @import { Context } from '../types' */
3-
import { validate_component } from './shared/component.js';
3+
import { visit_component } from './shared/component.js';
44

55
/**
66
* @param {SvelteSelf} node
77
* @param {Context} context
88
*/
99
export function SvelteSelf(node, context) {
10-
validate_component(node, context);
10+
visit_component(node, context);
1111
}

packages/svelte/src/compiler/phases/2-analyze/visitors/Text.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import * as e from '../../../errors.js';
99
* @param {Context} context
1010
*/
1111
export function Text(node, context) {
12-
if (!node.parent) {
13-
return;
14-
}
15-
16-
if (context.state.parent_element && regex_not_whitespace.test(node.data)) {
12+
if (node.parent && context.state.parent_element && regex_not_whitespace.test(node.data)) {
1713
if (!is_tag_valid_with_parent('#text', context.state.parent_element)) {
1814
e.node_invalid_placement(node, 'Text node', context.state.parent_element);
1915
}

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
* @param {Component | SvelteComponent | SvelteSelf} node
1313
* @param {Context} context
1414
*/
15-
export function validate_component(node, context) {
15+
export function visit_component(node, context) {
1616
for (const attribute of node.attributes) {
1717
if (
1818
attribute.type !== 'Attribute' &&

0 commit comments

Comments
 (0)