Skip to content

Commit 79ef645

Browse files
authored
chore: improve constants (#12695)
* chore: consistently uppercase constants * expose helpers * use helpers * tidy up * why was that there * fix * tweak * fix * tidy up * cheat, to preserve treeshakeability
1 parent f984307 commit 79ef645

File tree

20 files changed

+421
-372
lines changed

20 files changed

+421
-372
lines changed

packages/svelte/src/compiler/phases/1-parse/read/options.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @import { ObjectExpression } from 'estree' */
22
/** @import { SvelteOptionsRaw, Root, SvelteOptions } from '#compiler' */
3-
import { namespace_mathml, namespace_svg } from '../../../../constants.js';
3+
import { NAMESPACE_MATHML, NAMESPACE_SVG } from '../../../../constants.js';
44
import * as e from '../../../errors.js';
55

66
const regex_valid_tag_name = /^[a-zA-Z][a-zA-Z0-9]*-[a-zA-Z0-9-]+$/;
@@ -155,9 +155,9 @@ export default function read_options(node) {
155155
case 'namespace': {
156156
const value = get_static_value(attribute);
157157

158-
if (value === namespace_svg) {
158+
if (value === NAMESPACE_SVG) {
159159
component_options.namespace = 'svg';
160-
} else if (value === namespace_mathml) {
160+
} else if (value === NAMESPACE_MATHML) {
161161
component_options.namespace = 'mathml';
162162
} else if (
163163
value === 'html' ||

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

Lines changed: 8 additions & 9 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 { ReservedKeywords, 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';
@@ -203,6 +202,8 @@ function get_component_name(filename) {
203202
return name[0].toUpperCase() + name.slice(1);
204203
}
205204

205+
const RESERVED = ['$$props', '$$restProps', '$$slots'];
206+
206207
/**
207208
* @param {Program} ast
208209
* @param {ValidatedModuleCompileOptions} options
@@ -212,7 +213,7 @@ export function analyze_module(ast, options) {
212213
const { scope, scopes } = create_scopes(ast, new ScopeRoot(), false, null);
213214

214215
for (const [name, references] of scope.references) {
215-
if (name[0] !== '$' || ReservedKeywords.includes(name)) continue;
216+
if (name[0] !== '$' || RESERVED.includes(name)) continue;
216217
if (name === '$' || name[1] === '$') {
217218
e.global_reference_invalid(references[0].node, name);
218219
}
@@ -257,7 +258,7 @@ export function analyze_component(root, source, options) {
257258

258259
// create synthetic bindings for store subscriptions
259260
for (const [name, references] of module.scope.references) {
260-
if (name[0] !== '$' || ReservedKeywords.includes(name)) continue;
261+
if (name[0] !== '$' || RESERVED.includes(name)) continue;
261262
if (name === '$' || name[1] === '$') {
262263
e.global_reference_invalid(references[0].node, name);
263264
}
@@ -269,7 +270,7 @@ export function analyze_component(root, source, options) {
269270
// is referencing a rune and not a global store.
270271
if (
271272
options.runes === false ||
272-
!Runes.includes(/** @type {any} */ (name)) ||
273+
!is_rune(name) ||
273274
(declaration !== null &&
274275
// const state = $state(0) is valid
275276
(get_rune(declaration.initial, instance.scope) === null ||
@@ -307,7 +308,7 @@ export function analyze_component(root, source, options) {
307308
if (options.runes !== false) {
308309
if (declaration === null && /[a-z]/.test(store_name[0])) {
309310
e.global_reference_invalid(references[0].node, name);
310-
} else if (declaration !== null && Runes.includes(/** @type {any} */ (name))) {
311+
} else if (declaration !== null && is_rune(name)) {
311312
for (const { node, path } of references) {
312313
if (path.at(-1)?.type === 'CallExpression') {
313314
w.store_rune_conflict(node, store_name);
@@ -339,9 +340,7 @@ export function analyze_component(root, source, options) {
339340

340341
const component_name = get_component_name(options.filename ?? 'Component');
341342

342-
const runes =
343-
options.runes ??
344-
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);
345344

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

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/** @import { ArrowFunctionExpression, Expression, FunctionDeclaration, FunctionExpression } from 'estree' */
22
/** @import { Attribute, DelegatedEvent, RegularElement } from '#compiler' */
33
/** @import { Context } from '../types' */
4-
import { DelegatedEvents } from '../../../../constants.js';
5-
import { is_capture_event } from '../../../../utils.js';
4+
import { is_capture_event, is_delegated } from '../../../../utils.js';
65
import {
76
get_attribute_chunks,
87
get_attribute_expression,
@@ -60,7 +59,7 @@ export function Attribute(node, context) {
6059
*/
6160
function get_delegated_event(event_name, handler, context) {
6261
// Handle delegated event handlers. Bail-out if not a delegated event.
63-
if (!handler || !DelegatedEvents.includes(event_name)) {
62+
if (!handler || !is_delegated(event_name)) {
6463
return null;
6564
}
6665

@@ -119,7 +118,7 @@ function get_delegated_event(event_name, handler, context) {
119118
if (
120119
element.type !== 'RegularElement' ||
121120
element.metadata.has_spread ||
122-
!DelegatedEvents.includes(event_name)
121+
!is_delegated(event_name)
123122
) {
124123
return non_hoistable;
125124
}

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 { ContentEditableBindings, 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 (ContentEditableBindings.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/SvelteElement.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @import { Attribute, SvelteElement, Text } from '#compiler' */
22
/** @import { Context } from '../types' */
3-
import { namespace_mathml, namespace_svg } from '../../../../constants.js';
3+
import { NAMESPACE_MATHML, NAMESPACE_SVG } from '../../../../constants.js';
44
import { is_text_attribute } from '../../../utils/ast.js';
55
import { check_element } from './shared/a11y.js';
66
import { validate_element } from './shared/element.js';
@@ -23,8 +23,8 @@ export function SvelteElement(node, context) {
2323
);
2424

2525
if (xmlns) {
26-
node.metadata.svg = xmlns.value[0].data === namespace_svg;
27-
node.metadata.mathml = xmlns.value[0].data === namespace_mathml;
26+
node.metadata.svg = xmlns.value[0].data === NAMESPACE_SVG;
27+
node.metadata.mathml = xmlns.value[0].data === NAMESPACE_MATHML;
2828
} else {
2929
let i = context.path.length;
3030
while (i--) {

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 { ContentEditableBindings } 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-
ContentEditableBindings.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: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
/** @import { SourceLocation } from '#shared' */
44
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
55
/** @import { Scope } from '../../../scope' */
6-
import { DOMBooleanAttributes } from '../../../../../constants.js';
7-
import { 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';
812
import { escape_html } from '../../../../../escaping.js';
913
import { dev, is_ignored, locator } from '../../../../state.js';
1014
import {
@@ -13,7 +17,6 @@ import {
1317
is_text_attribute
1418
} from '../../../../utils/ast.js';
1519
import * as b from '../../../../utils/builders.js';
16-
import { DOMProperties, LoadErrorElements } from '../../../constants.js';
1720
import { is_custom_element_node } from '../../../nodes.js';
1821
import { clean_nodes, determine_namespace_for_children } from '../../utils.js';
1922
import { serialize_get_binding } from '../utils.js';
@@ -130,7 +133,7 @@ export function RegularElement(node, context) {
130133
attributes.push(attribute);
131134
needs_input_reset = true;
132135
needs_content_reset = true;
133-
if (LoadErrorElements.includes(node.name)) {
136+
if (is_load_error_element(node.name)) {
134137
might_need_event_replaying = true;
135138
}
136139
} else if (attribute.type === 'ClassDirective') {
@@ -155,7 +158,7 @@ export function RegularElement(node, context) {
155158
) {
156159
has_content_editable_binding = true;
157160
}
158-
} else if (attribute.type === 'UseDirective' && LoadErrorElements.includes(node.name)) {
161+
} else if (attribute.type === 'UseDirective' && is_load_error_element(node.name)) {
159162
might_need_event_replaying = true;
160163
}
161164
context.visit(attribute);
@@ -211,7 +214,7 @@ export function RegularElement(node, context) {
211214
if (is_event_attribute(attribute)) {
212215
if (
213216
(attribute.name === 'onload' || attribute.name === 'onerror') &&
214-
LoadErrorElements.includes(node.name)
217+
is_load_error_element(node.name)
215218
) {
216219
might_need_event_replaying = true;
217220
}
@@ -238,7 +241,7 @@ export function RegularElement(node, context) {
238241
// to create the elements it needs.
239242
context.state.template.push(
240243
` ${attribute.name}${
241-
DOMBooleanAttributes.includes(name) && literal_value === true
244+
is_boolean_attribute(name) && literal_value === true
242245
? ''
243246
: `="${literal_value === true ? '' : escape_html(literal_value, true)}"`
244247
}`
@@ -599,7 +602,7 @@ function serialize_element_attribute_update_assignment(element, node_id, attribu
599602
update = b.stmt(b.call('$.set_value', node_id, value));
600603
} else if (name === 'checked') {
601604
update = b.stmt(b.call('$.set_checked', node_id, value));
602-
} else if (DOMProperties.includes(name)) {
605+
} else if (is_dom_property(name)) {
603606
update = b.stmt(b.assignment('=', b.member(node_id, b.id(name)), value));
604607
} else {
605608
const callee = name.startsWith('xlink') ? '$.set_xlink_attribute' : '$.set_attribute';

0 commit comments

Comments
 (0)