Skip to content

chore: improve constants #12695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/svelte/src/compiler/phases/1-parse/read/options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @import { ObjectExpression } from 'estree' */
/** @import { SvelteOptionsRaw, Root, SvelteOptions } from '#compiler' */
import { namespace_mathml, namespace_svg } from '../../../../constants.js';
import { NAMESPACE_MATHML, NAMESPACE_SVG } from '../../../../constants.js';
import * as e from '../../../errors.js';

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

if (value === namespace_svg) {
if (value === NAMESPACE_SVG) {
component_options.namespace = 'svg';
} else if (value === namespace_mathml) {
} else if (value === NAMESPACE_MATHML) {
component_options.namespace = 'mathml';
} else if (
value === 'html' ||
Expand Down
17 changes: 8 additions & 9 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import * as e from '../../errors.js';
import * as w from '../../warnings.js';
import { is_text_attribute } from '../../utils/ast.js';
import * as b from '../../utils/builders.js';
import { ReservedKeywords, Runes } from '../constants.js';
import { Scope, ScopeRoot, create_scopes, get_rune } from '../scope.js';
import check_graph_for_cycles from './utils/check_graph_for_cycles.js';
import { create_attribute } from '../nodes.js';
import { analyze_css } from './css/css-analyze.js';
import { prune } from './css/css-prune.js';
import { hash } from '../../../utils.js';
import { hash, is_rune } from '../../../utils.js';
import { warn_unused } from './css/css-warn.js';
import { extract_svelte_ignore } from '../../utils/extract_svelte_ignore.js';
import { ignore_map, ignore_stack, pop_ignore, push_ignore } from '../../state.js';
Expand Down Expand Up @@ -203,6 +202,8 @@ function get_component_name(filename) {
return name[0].toUpperCase() + name.slice(1);
}

const RESERVED = ['$$props', '$$restProps', '$$slots'];

/**
* @param {Program} ast
* @param {ValidatedModuleCompileOptions} options
Expand All @@ -212,7 +213,7 @@ export function analyze_module(ast, options) {
const { scope, scopes } = create_scopes(ast, new ScopeRoot(), false, null);

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

// create synthetic bindings for store subscriptions
for (const [name, references] of module.scope.references) {
if (name[0] !== '$' || ReservedKeywords.includes(name)) continue;
if (name[0] !== '$' || RESERVED.includes(name)) continue;
if (name === '$' || name[1] === '$') {
e.global_reference_invalid(references[0].node, name);
}
Expand All @@ -269,7 +270,7 @@ export function analyze_component(root, source, options) {
// is referencing a rune and not a global store.
if (
options.runes === false ||
!Runes.includes(/** @type {any} */ (name)) ||
!is_rune(name) ||
(declaration !== null &&
// const state = $state(0) is valid
(get_rune(declaration.initial, instance.scope) === null ||
Expand Down Expand Up @@ -307,7 +308,7 @@ export function analyze_component(root, source, options) {
if (options.runes !== false) {
if (declaration === null && /[a-z]/.test(store_name[0])) {
e.global_reference_invalid(references[0].node, name);
} else if (declaration !== null && Runes.includes(/** @type {any} */ (name))) {
} else if (declaration !== null && is_rune(name)) {
for (const { node, path } of references) {
if (path.at(-1)?.type === 'CallExpression') {
w.store_rune_conflict(node, store_name);
Expand Down Expand Up @@ -339,9 +340,7 @@ export function analyze_component(root, source, options) {

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

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

// TODO remove all the ?? stuff, we don't need it now that we're validating the config
/** @type {ComponentAnalysis} */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/** @import { ArrowFunctionExpression, Expression, FunctionDeclaration, FunctionExpression } from 'estree' */
/** @import { Attribute, DelegatedEvent, RegularElement } from '#compiler' */
/** @import { Context } from '../types' */
import { DelegatedEvents } from '../../../../constants.js';
import { is_capture_event } from '../../../../utils.js';
import { is_capture_event, is_delegated } from '../../../../utils.js';
import {
get_attribute_chunks,
get_attribute_expression,
Expand Down Expand Up @@ -60,7 +59,7 @@ export function Attribute(node, context) {
*/
function get_delegated_event(event_name, handler, context) {
// Handle delegated event handlers. Bail-out if not a delegated event.
if (!handler || !DelegatedEvents.includes(event_name)) {
if (!handler || !is_delegated(event_name)) {
return null;
}

Expand Down Expand Up @@ -119,7 +118,7 @@ function get_delegated_event(event_name, handler, context) {
if (
element.type !== 'RegularElement' ||
element.metadata.has_spread ||
!DelegatedEvents.includes(event_name)
!is_delegated(event_name)
) {
return non_hoistable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { validate_no_const_assignment } from './shared/utils.js';
import * as e from '../../../errors.js';
import * as w from '../../../warnings.js';
import { binding_properties } from '../../bindings.js';
import { ContentEditableBindings, SVGElements } from '../../constants.js';
import fuzzymatch from '../../1-parse/utils/fuzzymatch.js';
import { is_content_editable_binding, is_svg } from '../../../../utils.js';

/**
* @param {BindDirective} node
Expand Down Expand Up @@ -197,15 +197,15 @@ export function BindDirective(node, context) {
}
}

if (node.name === 'offsetWidth' && SVGElements.includes(parent.name)) {
if (node.name === 'offsetWidth' && is_svg(parent.name)) {
e.bind_invalid_target(
node,
node.name,
`non-<svg> elements. Use 'clientWidth' for <svg> instead`
);
}

if (ContentEditableBindings.includes(node.name)) {
if (is_content_editable_binding(node.name)) {
const contenteditable = /** @type {Attribute} */ (
parent.attributes.find((a) => a.type === 'Attribute' && a.name === 'contenteditable')
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/** @import { Expression, Identifier } from 'estree' */
/** @import { SvelteNode } from '#compiler' */
/** @import { Context } from '../types' */
import is_reference from 'is-reference';
import { Runes } from '../../constants.js';
import { should_proxy_or_freeze } from '../../3-transform/client/utils.js';
import * as e from '../../../errors.js';
import * as w from '../../../warnings.js';
import { is_rune } from '../../../../utils.js';

/**
* @param {Identifier} node
Expand Down Expand Up @@ -34,7 +33,7 @@ export function Identifier(node, context) {

if (context.state.analysis.runes) {
if (
Runes.includes(/** @type {Runes[number]} */ (node.name)) &&
is_rune(node.name) &&
context.state.scope.get(node.name) === null &&
context.state.scope.get(node.name.slice(1)) === null
) {
Expand All @@ -49,7 +48,7 @@ export function Identifier(node, context) {
current = parent;
parent = /** @type {Expression} */ (context.path[--i]);

if (!Runes.includes(/** @type {Runes[number]} */ (name))) {
if (!is_rune(name)) {
if (name === '$effect.active') {
e.rune_renamed(parent, '$effect.active', '$effect.tracking');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/** @import { RegularElement } from '#compiler' */
/** @import { Context } from '../types' */
import { is_void } from '../../../../utils.js';
import { is_mathml, is_svg, is_void } from '../../../../utils.js';
import {
is_tag_valid_with_ancestor,
is_tag_valid_with_parent
} from '../../../../html-tree-validation.js';
import * as e from '../../../errors.js';
import * as w from '../../../warnings.js';
import { MathMLElements, SVGElements } from '../../constants.js';
import { create_attribute } from '../../nodes.js';
import { regex_starts_with_newline } from '../../patterns.js';
import { check_element } from './shared/a11y.js';
Expand Down Expand Up @@ -92,8 +91,8 @@ export function RegularElement(node, context) {
);

if (context.state.options.namespace !== 'foreign') {
if (SVGElements.includes(node.name)) node.metadata.svg = true;
else if (MathMLElements.includes(node.name)) node.metadata.mathml = true;
node.metadata.svg = is_svg(node.name);
node.metadata.mathml = is_mathml(node.name);
}

if (context.state.parent_element) {
Expand Down Expand Up @@ -159,7 +158,7 @@ export function RegularElement(node, context) {
context.state.analysis.source[node.end - 2] === '/' &&
context.state.options.namespace !== 'foreign' &&
!is_void(node_name) &&
!SVGElements.includes(node_name)
!is_svg(node_name)
) {
w.element_invalid_self_closing_tag(node, node.name);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @import { Attribute, SvelteElement, Text } from '#compiler' */
/** @import { Context } from '../types' */
import { namespace_mathml, namespace_svg } from '../../../../constants.js';
import { NAMESPACE_MATHML, NAMESPACE_SVG } from '../../../../constants.js';
import { is_text_attribute } from '../../../utils/ast.js';
import { check_element } from './shared/a11y.js';
import { validate_element } from './shared/element.js';
Expand All @@ -23,8 +23,8 @@ export function SvelteElement(node, context) {
);

if (xmlns) {
node.metadata.svg = xmlns.value[0].data === namespace_svg;
node.metadata.mathml = xmlns.value[0].data === namespace_mathml;
node.metadata.svg = xmlns.value[0].data === NAMESPACE_SVG;
node.metadata.mathml = xmlns.value[0].data === NAMESPACE_MATHML;
} else {
let i = context.path.length;
while (i--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {
import * as w from '../../../../warnings.js';
import fuzzymatch from '../../../1-parse/utils/fuzzymatch.js';
import { is_event_attribute, is_text_attribute } from '../../../../utils/ast.js';
import { ContentEditableBindings } from '../../../constants.js';
import { walk } from 'zimmerframe';
import { list } from '../../../../utils/string.js';
import { is_content_editable_binding } from '../../../../../utils.js';

const aria_roles = roles_map.keys();
const abstract_roles = aria_roles.filter((role) => roles_map.get(role)?.abstract);
Expand Down Expand Up @@ -720,10 +720,7 @@ export function check_element(node, state) {
has_contenteditable_attr = true;
}
}
} else if (
attribute.type === 'BindDirective' &&
ContentEditableBindings.includes(attribute.name)
) {
} else if (attribute.type === 'BindDirective' && is_content_editable_binding(attribute.name)) {
has_contenteditable_binding = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ import { get_attribute_expression, is_expression_attribute } from '../../../../u
import { regex_illegal_attribute_character } from '../../../patterns.js';
import * as e from '../../../../errors.js';
import * as w from '../../../../warnings.js';
import { EventModifiers } from '../../../constants.js';
import {
validate_attribute,
validate_attribute_name,
validate_slot_attribute
} from './attribute.js';

const EVENT_MODIFIERS = [
'preventDefault',
'stopPropagation',
'stopImmediatePropagation',
'capture',
'once',
'passive',
'nonpassive',
'self',
'trusted'
];

/**
* @param {import('#compiler').RegularElement | SvelteElement} node
* @param {Context} context
Expand Down Expand Up @@ -122,8 +133,8 @@ export function validate_element(node, context) {
let has_passive_modifier = false;
let conflicting_passive_modifier = '';
for (const modifier of attribute.modifiers) {
if (!EventModifiers.includes(modifier)) {
const list = `${EventModifiers.slice(0, -1).join(', ')} or ${EventModifiers.at(-1)}`;
if (!EVENT_MODIFIERS.includes(modifier)) {
const list = `${EVENT_MODIFIERS.slice(0, -1).join(', ')} or ${EVENT_MODIFIERS.at(-1)}`;
e.event_handler_invalid_modifier(attribute, list);
}
if (modifier === 'passive') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
/** @import { SourceLocation } from '#shared' */
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
/** @import { Scope } from '../../../scope' */
import { DOMBooleanAttributes } from '../../../../../constants.js';
import { is_void } from '../../../../../utils.js';
import {
is_boolean_attribute,
is_dom_property,
is_load_error_element,
is_void
} from '../../../../../utils.js';
import { escape_html } from '../../../../../escaping.js';
import { dev, is_ignored, locator } from '../../../../state.js';
import {
Expand All @@ -13,7 +17,6 @@ import {
is_text_attribute
} from '../../../../utils/ast.js';
import * as b from '../../../../utils/builders.js';
import { DOMProperties, LoadErrorElements } from '../../../constants.js';
import { is_custom_element_node } from '../../../nodes.js';
import { clean_nodes, determine_namespace_for_children } from '../../utils.js';
import { serialize_get_binding } from '../utils.js';
Expand Down Expand Up @@ -130,7 +133,7 @@ export function RegularElement(node, context) {
attributes.push(attribute);
needs_input_reset = true;
needs_content_reset = true;
if (LoadErrorElements.includes(node.name)) {
if (is_load_error_element(node.name)) {
might_need_event_replaying = true;
}
} else if (attribute.type === 'ClassDirective') {
Expand All @@ -155,7 +158,7 @@ export function RegularElement(node, context) {
) {
has_content_editable_binding = true;
}
} else if (attribute.type === 'UseDirective' && LoadErrorElements.includes(node.name)) {
} else if (attribute.type === 'UseDirective' && is_load_error_element(node.name)) {
might_need_event_replaying = true;
}
context.visit(attribute);
Expand Down Expand Up @@ -211,7 +214,7 @@ export function RegularElement(node, context) {
if (is_event_attribute(attribute)) {
if (
(attribute.name === 'onload' || attribute.name === 'onerror') &&
LoadErrorElements.includes(node.name)
is_load_error_element(node.name)
) {
might_need_event_replaying = true;
}
Expand All @@ -238,7 +241,7 @@ export function RegularElement(node, context) {
// to create the elements it needs.
context.state.template.push(
` ${attribute.name}${
DOMBooleanAttributes.includes(name) && literal_value === true
is_boolean_attribute(name) && literal_value === true
? ''
: `="${literal_value === true ? '' : escape_html(literal_value, true)}"`
}`
Expand Down Expand Up @@ -599,7 +602,7 @@ function serialize_element_attribute_update_assignment(element, node_id, attribu
update = b.stmt(b.call('$.set_value', node_id, value));
} else if (name === 'checked') {
update = b.stmt(b.call('$.set_checked', node_id, value));
} else if (DOMProperties.includes(name)) {
} else if (is_dom_property(name)) {
update = b.stmt(b.assignment('=', b.member(node_id, b.id(name)), value));
} else {
const callee = name.startsWith('xlink') ? '$.set_xlink_attribute' : '$.set_attribute';
Expand Down
Loading
Loading