Skip to content

Commit 8d32602

Browse files
committed
simplify options reading
1 parent 50f5986 commit 8d32602

File tree

4 files changed

+10
-62
lines changed

4 files changed

+10
-62
lines changed

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

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -133,47 +133,12 @@ export default function read_options(node) {
133133

134134
const shadow = properties.find(([name]) => name === 'shadow')?.[1];
135135
if (shadow) {
136-
if (shadow.type === 'Literal') {
137-
if (shadow.value !== 'open' && shadow.value !== 'none') {
138-
e.svelte_options_invalid_customelement_shadow(attribute);
139-
}
136+
if (shadow.type === 'Literal' && (shadow.value === 'open' || shadow.value === 'none')) {
140137
ce.shadow = shadow.value;
141-
} else if (shadow.type === 'ObjectExpression') {
142-
ce.shadow = { mode: 'open' };
143-
for (const property of /** @type {ObjectExpression} */ (shadow).properties) {
144-
if (
145-
property.type !== 'Property' ||
146-
property.computed ||
147-
property.key.type !== 'Identifier' ||
148-
property.value.type !== 'Literal'
149-
) {
150-
e.svelte_options_invalid_customelement_shadow(attribute);
151-
}
152-
153-
if (property.key.name === 'mode') {
154-
if (!['open', 'closed'].includes(/** @type {string} */ (property.value.value))) {
155-
e.svelte_options_invalid_customelement_shadow(attribute);
156-
}
157-
ce.shadow.mode = /** @type {any} */ (property.value.value);
158-
} else if (property.key.name === 'slotAssignment') {
159-
if (!['named', 'manual'].includes(/** @type {string} */ (property.value.value))) {
160-
e.svelte_options_invalid_customelement_shadow(attribute);
161-
}
162-
ce.shadow.slotAssignment = /** @type {any} */ (property.value.value);
163-
} else if (
164-
property.key.name === 'clonable' ||
165-
property.key.name === 'delegatesFocus' ||
166-
property.key.name === 'serializable'
167-
) {
168-
if (typeof property.value.value !== 'boolean') {
169-
e.svelte_options_invalid_customelement_shadow(attribute);
170-
}
171-
ce.shadow[property.key.name] = property.value.value;
172-
} else {
173-
e.svelte_options_invalid_customelement_shadow(attribute);
174-
}
175-
}
176-
} else if (shadow.type === 'ArrowFunctionExpression') {
138+
} else if (
139+
shadow.type === 'ObjectExpression' ||
140+
shadow.type === 'ArrowFunctionExpression'
141+
) {
177142
ce.shadow = shadow;
178143
} else {
179144
e.svelte_options_invalid_customelement_shadow(attribute);

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -645,22 +645,13 @@ export function client_component(analysis, options) {
645645
);
646646

647647
/** @type {ESTree.ObjectExpression | ESTree.ArrowFunctionExpression | undefined} */
648-
let shadow_root_init = undefined;
648+
let shadow_root_init;
649649
if (typeof ce === 'boolean' || ce.shadow === 'open' || ce.shadow === undefined) {
650650
shadow_root_init = b.object([b.init('mode', b.literal('open'))]);
651651
} else if (ce.shadow === 'none') {
652652
shadow_root_init = undefined;
653-
} else if ('type' in ce.shadow && ce.shadow.type === 'ArrowFunctionExpression') {
654-
shadow_root_init = /** @type {ESTree.ArrowFunctionExpression} */ (ce.shadow);
655-
} else if (typeof ce.shadow === 'object') {
656-
/** @type {ESTree.Property[]} */
657-
const shadow_root_init_props = Object.entries(ce.shadow).map(([key, value]) =>
658-
b.init(key, b.literal(value))
659-
);
660-
661-
shadow_root_init = shadow_root_init_props.length
662-
? b.object(shadow_root_init_props)
663-
: undefined;
653+
} else {
654+
shadow_root_init = ce.shadow;
664655
}
665656

666657
const create_ce = b.call(

packages/svelte/src/compiler/types/template.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,7 @@ export namespace AST {
8888
css?: 'injected';
8989
customElement?: {
9090
tag?: string;
91-
shadow?:
92-
| 'open'
93-
| 'none'
94-
| (ShadowRootInit & { clonable?: boolean })
95-
| ArrowFunctionExpression;
91+
shadow?: 'open' | 'none' | ObjectExpression | ArrowFunctionExpression | undefined;
9692
props?: Record<
9793
string,
9894
{

packages/svelte/types/index.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,11 +1224,7 @@ declare module 'svelte/compiler' {
12241224
css?: 'injected';
12251225
customElement?: {
12261226
tag?: string;
1227-
shadow?:
1228-
| 'open'
1229-
| 'none'
1230-
| (ShadowRootInit & { clonable?: boolean })
1231-
| ArrowFunctionExpression;
1227+
shadow?: 'open' | 'none' | ObjectExpression | ArrowFunctionExpression | undefined;
12321228
props?: Record<
12331229
string,
12341230
{

0 commit comments

Comments
 (0)