From e13c0f72587ace203c825786dc0542839b99d9ad Mon Sep 17 00:00:00 2001 From: halfnelson Date: Mon, 7 Dec 2020 16:26:55 +1000 Subject: [PATCH] Add namespace compiler option. --- src/compiler/compile/Component.ts | 3 ++- src/compiler/compile/index.ts | 13 +++++++++++- src/compiler/interfaces.ts | 1 + .../_config.js | 20 +++++++++++++++++++ .../main.svelte | 3 +++ test/validator/index.ts | 18 +++++++++++++++++ 6 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 test/runtime/samples/attribute-casing-foreign-namespace-compiler-option/_config.js create mode 100644 test/runtime/samples/attribute-casing-foreign-namespace-compiler-option/main.svelte diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index b2c8820351a7..73b3d915ac8f 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -1349,7 +1349,8 @@ function process_component_options(component: Component, nodes) { 'accessors' in component.compile_options ? component.compile_options.accessors : !!component.compile_options.customElement, - preserveWhitespace: !!component.compile_options.preserveWhitespace + preserveWhitespace: !!component.compile_options.preserveWhitespace, + namespace: component.compile_options.namespace }; const node = nodes.find(node => node.name === 'svelte:options'); diff --git a/src/compiler/compile/index.ts b/src/compiler/compile/index.ts index 842539fcde88..9f9b31917e38 100644 --- a/src/compiler/compile/index.ts +++ b/src/compiler/compile/index.ts @@ -6,6 +6,7 @@ import { CompileOptions, Warning } from '../interfaces'; import Component from './Component'; import fuzzymatch from '../utils/fuzzymatch'; import get_name_from_filename from './utils/get_name_from_filename'; +import { valid_namespaces } from '../utils/namespaces'; const valid_options = [ 'format', @@ -22,6 +23,7 @@ const valid_options = [ 'hydratable', 'legacy', 'customElement', + 'namespace', 'tag', 'css', 'loopGuardTimeout', @@ -30,7 +32,7 @@ const valid_options = [ ]; function validate_options(options: CompileOptions, warnings: Warning[]) { - const { name, filename, loopGuardTimeout, dev } = options; + const { name, filename, loopGuardTimeout, dev, namespace } = options; Object.keys(options).forEach(key => { if (!valid_options.includes(key)) { @@ -65,6 +67,15 @@ function validate_options(options: CompileOptions, warnings: Warning[]) { toString: () => message }); } + + if (namespace && valid_namespaces.indexOf(namespace) === -1) { + const match = fuzzymatch(namespace, valid_namespaces); + if (match) { + throw new Error(`Invalid namespace '${namespace}' (did you mean '${match}'?)`); + } else { + throw new Error(`Invalid namespace '${namespace}'`); + } + } } export default function compile(source: string, options: CompileOptions = {}) { diff --git a/src/compiler/interfaces.ts b/src/compiler/interfaces.ts index 689b59529d90..b9c95ae06db4 100644 --- a/src/compiler/interfaces.ts +++ b/src/compiler/interfaces.ts @@ -124,6 +124,7 @@ export interface CompileOptions { tag?: string; css?: boolean; loopGuardTimeout?: number; + namespace?: string; preserveComments?: boolean; preserveWhitespace?: boolean; diff --git a/test/runtime/samples/attribute-casing-foreign-namespace-compiler-option/_config.js b/test/runtime/samples/attribute-casing-foreign-namespace-compiler-option/_config.js new file mode 100644 index 000000000000..093710619c59 --- /dev/null +++ b/test/runtime/samples/attribute-casing-foreign-namespace-compiler-option/_config.js @@ -0,0 +1,20 @@ +// Test support for the native namespaces preserving attribute case (eg svelte-native, svelte-nodegui). + +export default { + html: ` + +