All files / src/compiler/phases/1-parse/read options.js

51% Statements 127/249
63.26% Branches 31/49
100% Functions 3/3
50.6% Lines 125/247

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 2482x 2x 2x 2x 2x 2x 2x 2x 2x 2x 30x 30x 30x 30x 30x 30x 30x     30x 30x 30x     30x 30x 30x 30x 30x 7x   7x 7x     7x 7x 7x 30x       30x 4x 4x 4x 4x 4x   4x 3x 3x 3x 3x 3x 4x 1x 1x 1x     1x 1x                                               4x 4x                                                                                                       4x 4x               4x 4x             30x 9x 1x 9x 9x     8x 9x 2x 7x 5x 6x 1x 1x 7x 7x 7x 30x 6x   6x 6x     6x 6x 6x 30x                   30x 4x   4x 4x     4x 4x 4x 30x   30x 30x 26x 26x 26x 2x 2x 2x 2x 2x 29x 29x 29x 29x 28x     28x 21x 1x 1x 8x 8x 2x 2x 2x 2x 2x 2x 3x 3x     3x 1x 1x 3x 3x 3x 3x 3x  
import { namespace_svg } from '../../../../constants.js';
import { error } from '../../../errors.js';
 
const regex_valid_tag_name = /^[a-zA-Z][a-zA-Z0-9]*-[a-zA-Z0-9-]+$/;
 
/**
 * @param {import('#compiler').SvelteOptionsRaw} node
 * @returns {import('#compiler').Root['options']}
 */
export default function read_options(node) {
	/** @type {import('#compiler').SvelteOptions} */
	const component_options = {
		start: node.start,
		end: node.end
	};
 
	if (!node) {
		return component_options;
	}
 
	for (const attribute of node.attributes) {
		if (attribute.type !== 'Attribute') {
			error(attribute, 'invalid-svelte-option-attribute');
		}
 
		const { name } = attribute;
 
		switch (name) {
			case 'runes': {
				const value = get_static_value(attribute, () =>
					error(attribute, 'invalid-svelte-option-runes')
				);
				if (typeof value !== 'boolean') {
					error(attribute, 'invalid-svelte-option-runes');
				}
				component_options.runes = value;
				break;
			}
			case 'tag': {
				error(attribute, 'tag-option-deprecated');
				break; // eslint doesn't know this is unnecessary
			}
			case 'customElement': {
				/** @type {import('#compiler').SvelteOptions['customElement']} */
				const ce = { tag: '' };
 
				const { value } = attribute;
				if (value === true) {
					error(attribute, 'invalid-svelte-option-customElement');
				} else if (value[0].type === 'Text') {
					const tag = get_static_value(attribute, () => error(attribute, 'invalid-tag-property'));
					validate_tag(attribute, tag);
					ce.tag = tag;
					component_options.customElement = ce;
					break;
				} else if (value[0].expression.type !== 'ObjectExpression') {
					// Before Svelte 4 it was necessary to explicitly set customElement to null or else you'd get a warning.
					// This is no longer necessary, but for backwards compat just skip in this case now.
					if (value[0].expression.type === 'Literal' && value[0].expression.value === null) {
						break;
					}
					error(attribute, 'invalid-svelte-option-customElement');
				}

				/** @type {Array<[string, any]>} */
				const properties = [];
				for (const property of value[0].expression.properties) {
					if (
						property.type !== 'Property' ||
						property.computed ||
						property.key.type !== 'Identifier'
					) {
						error(attribute, 'invalid-svelte-option-customElement');
					}
					properties.push([property.key.name, property.value]);
				}

				const tag = properties.find(([name]) => name === 'tag');
				if (tag) {
					const tag_value = tag[1]?.value;
					validate_tag(tag, tag_value);
					ce.tag = tag_value;
				} else {
					error(attribute, 'invalid-svelte-option-customElement');
				}

				const props = properties.find(([name]) => name === 'props')?.[1];
				if (props) {
					if (props.type !== 'ObjectExpression') {
						error(attribute, 'invalid-customElement-props-attribute');
					}
					ce.props = {};
					for (const property of /** @type {import('estree').ObjectExpression} */ (props)
						.properties) {
						if (
							property.type !== 'Property' ||
							property.computed ||
							property.key.type !== 'Identifier' ||
							property.value.type !== 'ObjectExpression'
						) {
							error(attribute, 'invalid-customElement-props-attribute');
						}
						ce.props[property.key.name] = {};
						for (const prop of property.value.properties) {
							if (
								prop.type !== 'Property' ||
								prop.computed ||
								prop.key.type !== 'Identifier' ||
								prop.value.type !== 'Literal'
							) {
								error(attribute, 'invalid-customElement-props-attribute');
							}

							if (prop.key.name === 'type') {
								if (
									['String', 'Number', 'Boolean', 'Array', 'Object'].indexOf(
										/** @type {string} */ (prop.value.value)
									) === -1
								) {
									error(attribute, 'invalid-customElement-props-attribute');
								}
								ce.props[property.key.name].type = /** @type {any} */ (prop.value.value);
							} else if (prop.key.name === 'reflect') {
								if (typeof prop.value.value !== 'boolean') {
									error(attribute, 'invalid-customElement-props-attribute');
								}
								ce.props[property.key.name].reflect = prop.value.value;
							} else if (prop.key.name === 'attribute') {
								if (typeof prop.value.value !== 'string') {
									error(attribute, 'invalid-customElement-props-attribute');
								}
								ce.props[property.key.name].attribute = prop.value.value;
							} else {
								error(attribute, 'invalid-customElement-props-attribute');
							}
						}
					}
				}

				const shadow = properties.find(([name]) => name === 'shadow')?.[1];
				if (shadow) {
					const shadowdom = shadow?.value;
					if (shadowdom !== 'open' && shadowdom !== 'none') {
						error(shadow, 'invalid-customElement-shadow-attribute');
					}
					ce.shadow = shadowdom;
				}

				const extend = properties.find(([name]) => name === 'extend')?.[1];
				if (extend) {
					ce.extend = extend;
				}

				component_options.customElement = ce;
				break;
			}
			case 'namespace': {
				const value = get_static_value(attribute, () =>
					error(attribute, 'invalid-svelte-option-namespace')
				);
				if (typeof value !== 'string') {
					error(attribute, 'invalid-svelte-option-namespace');
				}
 
				if (value === namespace_svg) {
					component_options.namespace = 'svg';
				} else if (value === 'html' || value === 'svg' || value === 'foreign') {
					component_options.namespace = value;
				} else {
					error(attribute, 'invalid-svelte-option-namespace');
				}
 
				break;
			}
			case 'immutable': {
				const value = get_static_value(attribute, () =>
					error(attribute, 'invalid-svelte-option-immutable')
				);
				if (typeof value !== 'boolean') {
					error(attribute, 'invalid-svelte-option-immutable');
				}
				component_options.immutable = value;
				break;
			}
			case 'preserveWhitespace': {
				const value = get_static_value(attribute, () =>
					error(attribute, 'invalid-svelte-option-preserveWhitespace')
				);
				if (typeof value !== 'boolean') {
					error(attribute, 'invalid-svelte-option-preserveWhitespace');
				}
				component_options.preserveWhitespace = value;
				break;
			}
			case 'accessors': {
				const value = get_static_value(attribute, () =>
					error(attribute, 'invalid-svelte-option-accessors')
				);
				if (typeof value !== 'boolean') {
					error(attribute, 'invalid-svelte-option-accessors');
				}
				component_options.accessors = value;
				break;
			}
			default:
				error(attribute, 'unknown-svelte-option-attribute', name);
		}
	}
 
	return component_options;
}
 
/**
 * @param {any} attribute
 * @param {(attribute: any) => never} error
 */
function get_static_value(attribute, error) {
	const { value } = attribute;
	const chunk = value[0];
	if (!chunk) return true;
	if (value.length > 1) {
		error(attribute);
	}
	if (chunk.type === 'Text') return chunk.data;
	if (chunk.expression.type !== 'Literal') {
		error(attribute);
	}
	return chunk.expression.value;
}
 
/**
 * @param {any} attribute
 * @param {string} tag
 * @returns {asserts tag is string}
 */
function validate_tag(attribute, tag) {
	if (typeof tag !== 'string' && tag !== null) {
		error(attribute, 'invalid-tag-property');
	}
	if (tag && !regex_valid_tag_name.test(tag)) {
		error(attribute, 'invalid-tag-property');
	}
	// TODO do we still need this?
	// if (tag && !component.compile_options.customElement) {
	// 	component.warn(attribute, compiler_warnings.missing_custom_element_compile_options);
	// }
}