Skip to content

Commit 5126826

Browse files
committed
tidy up
1 parent 29ce01e commit 5126826

File tree

12 files changed

+245
-217
lines changed

12 files changed

+245
-217
lines changed

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import * as e from '../../errors.js';
77
import * as w from '../../warnings.js';
88
import { is_text_attribute } from '../../utils/ast.js';
99
import * as b from '../../utils/builders.js';
10-
import { Runes } from '../constants.js';
1110
import { Scope, ScopeRoot, create_scopes, get_rune } from '../scope.js';
1211
import check_graph_for_cycles from './utils/check_graph_for_cycles.js';
1312
import { create_attribute } from '../nodes.js';
1413
import { analyze_css } from './css/css-analyze.js';
1514
import { prune } from './css/css-prune.js';
16-
import { hash } from '../../../utils.js';
15+
import { hash, is_rune } from '../../../utils.js';
1716
import { warn_unused } from './css/css-warn.js';
1817
import { extract_svelte_ignore } from '../../utils/extract_svelte_ignore.js';
1918
import { ignore_map, ignore_stack, pop_ignore, push_ignore } from '../../state.js';
@@ -271,7 +270,7 @@ export function analyze_component(root, source, options) {
271270
// is referencing a rune and not a global store.
272271
if (
273272
options.runes === false ||
274-
!Runes.includes(/** @type {any} */ (name)) ||
273+
!is_rune(name) ||
275274
(declaration !== null &&
276275
// const state = $state(0) is valid
277276
(get_rune(declaration.initial, instance.scope) === null ||
@@ -309,7 +308,7 @@ export function analyze_component(root, source, options) {
309308
if (options.runes !== false) {
310309
if (declaration === null && /[a-z]/.test(store_name[0])) {
311310
e.global_reference_invalid(references[0].node, name);
312-
} else if (declaration !== null && Runes.includes(/** @type {any} */ (name))) {
311+
} else if (declaration !== null && is_rune(name)) {
313312
for (const { node, path } of references) {
314313
if (path.at(-1)?.type === 'CallExpression') {
315314
w.store_rune_conflict(node, store_name);
@@ -341,9 +340,7 @@ export function analyze_component(root, source, options) {
341340

342341
const component_name = get_component_name(options.filename ?? 'Component');
343342

344-
const runes =
345-
options.runes ??
346-
Array.from(module.scope.references).some(([name]) => Runes.includes(/** @type {any} */ (name)));
343+
const runes = options.runes ?? Array.from(module.scope.references.keys()).some(is_rune);
347344

348345
// TODO remove all the ?? stuff, we don't need it now that we're validating the config
349346
/** @type {ComponentAnalysis} */

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { validate_no_const_assignment } from './shared/utils.js';
99
import * as e from '../../../errors.js';
1010
import * as w from '../../../warnings.js';
1111
import { binding_properties } from '../../bindings.js';
12-
import { CONTENT_EDITABLE_BINDINGS, SVGElements } from '../../constants.js';
1312
import fuzzymatch from '../../1-parse/utils/fuzzymatch.js';
13+
import { is_content_editable_binding, is_svg } from '../../../../utils.js';
1414

1515
/**
1616
* @param {BindDirective} node
@@ -197,15 +197,15 @@ export function BindDirective(node, context) {
197197
}
198198
}
199199

200-
if (node.name === 'offsetWidth' && SVGElements.includes(parent.name)) {
200+
if (node.name === 'offsetWidth' && is_svg(parent.name)) {
201201
e.bind_invalid_target(
202202
node,
203203
node.name,
204204
`non-<svg> elements. Use 'clientWidth' for <svg> instead`
205205
);
206206
}
207207

208-
if (CONTENT_EDITABLE_BINDINGS.includes(node.name)) {
208+
if (is_content_editable_binding(node.name)) {
209209
const contenteditable = /** @type {Attribute} */ (
210210
parent.attributes.find((a) => a.type === 'Attribute' && a.name === 'contenteditable')
211211
);

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/** @import { Expression, Identifier } from 'estree' */
2-
/** @import { SvelteNode } from '#compiler' */
32
/** @import { Context } from '../types' */
43
import is_reference from 'is-reference';
5-
import { Runes } from '../../constants.js';
64
import { should_proxy_or_freeze } from '../../3-transform/client/utils.js';
75
import * as e from '../../../errors.js';
86
import * as w from '../../../warnings.js';
7+
import { is_rune } from '../../../../utils.js';
98

109
/**
1110
* @param {Identifier} node
@@ -34,7 +33,7 @@ export function Identifier(node, context) {
3433

3534
if (context.state.analysis.runes) {
3635
if (
37-
Runes.includes(/** @type {Runes[number]} */ (node.name)) &&
36+
is_rune(node.name) &&
3837
context.state.scope.get(node.name) === null &&
3938
context.state.scope.get(node.name.slice(1)) === null
4039
) {
@@ -49,7 +48,7 @@ export function Identifier(node, context) {
4948
current = parent;
5049
parent = /** @type {Expression} */ (context.path[--i]);
5150

52-
if (!Runes.includes(/** @type {Runes[number]} */ (name))) {
51+
if (!is_rune(name)) {
5352
if (name === '$effect.active') {
5453
e.rune_renamed(parent, '$effect.active', '$effect.tracking');
5554
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/** @import { RegularElement } from '#compiler' */
22
/** @import { Context } from '../types' */
3-
import { is_void } from '../../../../utils.js';
3+
import { is_mathml, is_svg, is_void } from '../../../../utils.js';
44
import {
55
is_tag_valid_with_ancestor,
66
is_tag_valid_with_parent
77
} from '../../../../html-tree-validation.js';
88
import * as e from '../../../errors.js';
99
import * as w from '../../../warnings.js';
10-
import { MathMLElements, SVGElements } from '../../constants.js';
1110
import { create_attribute } from '../../nodes.js';
1211
import { regex_starts_with_newline } from '../../patterns.js';
1312
import { check_element } from './shared/a11y.js';
@@ -92,8 +91,8 @@ export function RegularElement(node, context) {
9291
);
9392

9493
if (context.state.options.namespace !== 'foreign') {
95-
if (SVGElements.includes(node.name)) node.metadata.svg = true;
96-
else if (MathMLElements.includes(node.name)) node.metadata.mathml = true;
94+
node.metadata.svg = is_svg(node.name);
95+
node.metadata.mathml = is_mathml(node.name);
9796
}
9897

9998
if (context.state.parent_element) {
@@ -159,7 +158,7 @@ export function RegularElement(node, context) {
159158
context.state.analysis.source[node.end - 2] === '/' &&
160159
context.state.options.namespace !== 'foreign' &&
161160
!is_void(node_name) &&
162-
!SVGElements.includes(node_name)
161+
!is_svg(node_name)
163162
) {
164163
w.element_invalid_self_closing_tag(node, node.name);
165164
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import {
1414
import * as w from '../../../../warnings.js';
1515
import fuzzymatch from '../../../1-parse/utils/fuzzymatch.js';
1616
import { is_event_attribute, is_text_attribute } from '../../../../utils/ast.js';
17-
import { CONTENT_EDITABLE_BINDINGS } from '../../../constants.js';
1817
import { walk } from 'zimmerframe';
1918
import { list } from '../../../../utils/string.js';
19+
import { is_content_editable_binding } from '../../../../../utils.js';
2020

2121
const aria_roles = roles_map.keys();
2222
const abstract_roles = aria_roles.filter((role) => roles_map.get(role)?.abstract);
@@ -720,10 +720,7 @@ export function check_element(node, state) {
720720
has_contenteditable_attr = true;
721721
}
722722
}
723-
} else if (
724-
attribute.type === 'BindDirective' &&
725-
CONTENT_EDITABLE_BINDINGS.includes(attribute.name)
726-
) {
723+
} else if (attribute.type === 'BindDirective' && is_content_editable_binding(attribute.name)) {
727724
has_contenteditable_binding = true;
728725
}
729726
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@ import { get_attribute_expression, is_expression_attribute } from '../../../../u
44
import { regex_illegal_attribute_character } from '../../../patterns.js';
55
import * as e from '../../../../errors.js';
66
import * as w from '../../../../warnings.js';
7-
import { EventModifiers } from '../../../constants.js';
87
import {
98
validate_attribute,
109
validate_attribute_name,
1110
validate_slot_attribute
1211
} from './attribute.js';
1312

13+
const EVENT_MODIFIERS = [
14+
'preventDefault',
15+
'stopPropagation',
16+
'stopImmediatePropagation',
17+
'capture',
18+
'once',
19+
'passive',
20+
'nonpassive',
21+
'self',
22+
'trusted'
23+
];
24+
1425
/**
1526
* @param {import('#compiler').RegularElement | SvelteElement} node
1627
* @param {Context} context
@@ -122,8 +133,8 @@ export function validate_element(node, context) {
122133
let has_passive_modifier = false;
123134
let conflicting_passive_modifier = '';
124135
for (const modifier of attribute.modifiers) {
125-
if (!EventModifiers.includes(modifier)) {
126-
const list = `${EventModifiers.slice(0, -1).join(', ')} or ${EventModifiers.at(-1)}`;
136+
if (!EVENT_MODIFIERS.includes(modifier)) {
137+
const list = `${EVENT_MODIFIERS.slice(0, -1).join(', ')} or ${EVENT_MODIFIERS.at(-1)}`;
127138
e.event_handler_invalid_modifier(attribute, list);
128139
}
129140
if (modifier === 'passive') {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
/** @import { SourceLocation } from '#shared' */
44
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
55
/** @import { Scope } from '../../../scope' */
6-
import { is_boolean_attribute, is_dom_property, is_void } from '../../../../../utils.js';
6+
import {
7+
is_boolean_attribute,
8+
is_dom_property,
9+
is_load_error_element,
10+
is_void
11+
} from '../../../../../utils.js';
712
import { escape_html } from '../../../../../escaping.js';
813
import { dev, is_ignored, locator } from '../../../../state.js';
914
import {
@@ -12,7 +17,6 @@ import {
1217
is_text_attribute
1318
} from '../../../../utils/ast.js';
1419
import * as b from '../../../../utils/builders.js';
15-
import { LoadErrorElements } from '../../../constants.js';
1620
import { is_custom_element_node } from '../../../nodes.js';
1721
import { clean_nodes, determine_namespace_for_children } from '../../utils.js';
1822
import { serialize_get_binding } from '../utils.js';
@@ -129,7 +133,7 @@ export function RegularElement(node, context) {
129133
attributes.push(attribute);
130134
needs_input_reset = true;
131135
needs_content_reset = true;
132-
if (LoadErrorElements.includes(node.name)) {
136+
if (is_load_error_element(node.name)) {
133137
might_need_event_replaying = true;
134138
}
135139
} else if (attribute.type === 'ClassDirective') {
@@ -154,7 +158,7 @@ export function RegularElement(node, context) {
154158
) {
155159
has_content_editable_binding = true;
156160
}
157-
} else if (attribute.type === 'UseDirective' && LoadErrorElements.includes(node.name)) {
161+
} else if (attribute.type === 'UseDirective' && is_load_error_element(node.name)) {
158162
might_need_event_replaying = true;
159163
}
160164
context.visit(attribute);
@@ -210,7 +214,7 @@ export function RegularElement(node, context) {
210214
if (is_event_attribute(attribute)) {
211215
if (
212216
(attribute.name === 'onload' || attribute.name === 'onerror') &&
213-
LoadErrorElements.includes(node.name)
217+
is_load_error_element(node.name)
214218
) {
215219
might_need_event_replaying = true;
216220
}

packages/svelte/src/compiler/phases/3-transform/server/visitors/shared/element.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
is_text_attribute
88
} from '../../../../../utils/ast.js';
99
import { binding_properties } from '../../../../bindings.js';
10-
import { CONTENT_EDITABLE_BINDINGS, LoadErrorElements } from '../../../../constants.js';
1110
import {
1211
create_attribute,
1312
create_expression_metadata,
@@ -20,7 +19,11 @@ import {
2019
ELEMENT_PRESERVE_ATTRIBUTE_CASE
2120
} from '../../../../../../constants.js';
2221
import { serialize_attribute_value } from './utils.js';
23-
import { is_boolean_attribute } from '../../../../../../utils.js';
22+
import {
23+
is_boolean_attribute,
24+
is_content_editable_binding,
25+
is_load_error_element
26+
} from '../../../../../../utils.js';
2427

2528
const WHITESPACE_INSENSITIVE_ATTRIBUTES = ['class', 'style'];
2629

@@ -75,7 +78,7 @@ export function serialize_element_attributes(node, context) {
7578
} else if (is_event_attribute(attribute)) {
7679
if (
7780
(attribute.name === 'onload' || attribute.name === 'onerror') &&
78-
LoadErrorElements.includes(node.name)
81+
is_load_error_element(node.name)
7982
) {
8083
events_to_capture.add(attribute.name);
8184
}
@@ -106,7 +109,7 @@ export function serialize_element_attributes(node, context) {
106109
const binding = binding_properties[attribute.name];
107110
if (binding?.omit_in_ssr) continue;
108111

109-
if (CONTENT_EDITABLE_BINDINGS.includes(attribute.name)) {
112+
if (is_content_editable_binding(attribute.name)) {
110113
content = /** @type {Expression} */ (context.visit(attribute.expression));
111114
} else if (attribute.name === 'value' && node.name === 'textarea') {
112115
content = b.call(
@@ -168,12 +171,12 @@ export function serialize_element_attributes(node, context) {
168171
} else if (attribute.type === 'SpreadAttribute') {
169172
attributes.push(attribute);
170173
has_spread = true;
171-
if (LoadErrorElements.includes(node.name)) {
174+
if (is_load_error_element(node.name)) {
172175
events_to_capture.add('onload');
173176
events_to_capture.add('onerror');
174177
}
175178
} else if (attribute.type === 'UseDirective') {
176-
if (LoadErrorElements.includes(node.name)) {
179+
if (is_load_error_element(node.name)) {
177180
events_to_capture.add('onload');
178181
events_to_capture.add('onerror');
179182
}

0 commit comments

Comments
 (0)