Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
revert ordering changes
  • Loading branch information
43081j committed Jun 6, 2019
commit d22422ca2f14f506831d05b1c926ae7076aee82e
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"no-inner-declarations": 0,
"@typescript-eslint/indent": [2, "tab", { "SwitchCase": 1 }],
"@typescript-eslint/camelcase": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/array-type": ["error", "array-simple"],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": "off",
Expand Down
228 changes: 114 additions & 114 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,120 +67,6 @@ function remove_node(code: MagicString, start: number, end: number, body: Node,
return;
}

function process_component_options(component: Component, nodes) {
const component_options: ComponentOptions = {
immutable: component.compile_options.immutable || false,
accessors: 'accessors' in component.compile_options
? component.compile_options.accessors
: !!component.compile_options.customElement,
preserveWhitespace: !!component.compile_options.preserveWhitespace
};

const node = nodes.find(node => node.name === 'svelte:options');

function get_value(attribute, code, message) {
const { value } = attribute;
const chunk = value[0];

if (!chunk) return true;

if (value.length > 1) {
component.error(attribute, { code, message });
}

if (chunk.type === 'Text') return chunk.data;

if (chunk.expression.type !== 'Literal') {
component.error(attribute, { code, message });
}

return chunk.expression.value;
}

if (node) {
node.attributes.forEach(attribute => {
if (attribute.type === 'Attribute') {
const { name } = attribute;

switch (name) {
case 'tag': {
const code = 'invalid-tag-attribute';
const message = `'tag' must be a string literal`;
const tag = get_value(attribute, code, message);

if (typeof tag !== 'string' && tag !== null) component.error(attribute, { code, message });

if (tag && !/^[a-zA-Z][a-zA-Z0-9]*-[a-zA-Z0-9-]+$/.test(tag)) {
component.error(attribute, {
code: `invalid-tag-property`,
message: `tag name must be two or more words joined by the '-' character`
});
}

component_options.tag = tag;
break;
}

case 'namespace': {
const code = 'invalid-namespace-attribute';
const message = `The 'namespace' attribute must be a string literal representing a valid namespace`;
const ns = get_value(attribute, code, message);

if (typeof ns !== 'string') component.error(attribute, { code, message });

if (valid_namespaces.indexOf(ns) === -1) {
const match = fuzzymatch(ns, valid_namespaces);
if (match) {
component.error(attribute, {
code: `invalid-namespace-property`,
message: `Invalid namespace '${ns}' (did you mean '${match}'?)`
});
} else {
component.error(attribute, {
code: `invalid-namespace-property`,
message: `Invalid namespace '${ns}'`
});
}
}

component_options.namespace = ns;
break;
}

case 'accessors':
case 'immutable':
case 'preserveWhitespace':
{
const code = `invalid-${name}-value`;
const message = `${name} attribute must be true or false`;
const value = get_value(attribute, code, message);

if (typeof value !== 'boolean') component.error(attribute, { code, message });

component_options[name] = value;
break;
}

default:
component.error(attribute, {
code: `invalid-options-attribute`,
message: `<svelte:options> unknown attribute`
});
}
}

else {
component.error(attribute, {
code: `invalid-options-attribute`,
message: `<svelte:options> can only have static 'tag', 'namespace', 'accessors', 'immutable' and 'preserveWhitespace' attributes`
});
}
});
}

return component_options;
}

export default class Component {
stats: Stats;
warnings: Warning[];
Expand Down Expand Up @@ -1351,3 +1237,117 @@ export default class Component {
});
}
}

function process_component_options(component: Component, nodes) {
const component_options: ComponentOptions = {
immutable: component.compile_options.immutable || false,
accessors: 'accessors' in component.compile_options
? component.compile_options.accessors
: !!component.compile_options.customElement,
preserveWhitespace: !!component.compile_options.preserveWhitespace
};

const node = nodes.find(node => node.name === 'svelte:options');

function get_value(attribute, code, message) {
const { value } = attribute;
const chunk = value[0];

if (!chunk) return true;

if (value.length > 1) {
component.error(attribute, { code, message });
}

if (chunk.type === 'Text') return chunk.data;

if (chunk.expression.type !== 'Literal') {
component.error(attribute, { code, message });
}

return chunk.expression.value;
}

if (node) {
node.attributes.forEach(attribute => {
if (attribute.type === 'Attribute') {
const { name } = attribute;

switch (name) {
case 'tag': {
const code = 'invalid-tag-attribute';
const message = `'tag' must be a string literal`;
const tag = get_value(attribute, code, message);

if (typeof tag !== 'string' && tag !== null) component.error(attribute, { code, message });

if (tag && !/^[a-zA-Z][a-zA-Z0-9]*-[a-zA-Z0-9-]+$/.test(tag)) {
component.error(attribute, {
code: `invalid-tag-property`,
message: `tag name must be two or more words joined by the '-' character`
});
}

component_options.tag = tag;
break;
}

case 'namespace': {
const code = 'invalid-namespace-attribute';
const message = `The 'namespace' attribute must be a string literal representing a valid namespace`;
const ns = get_value(attribute, code, message);

if (typeof ns !== 'string') component.error(attribute, { code, message });

if (valid_namespaces.indexOf(ns) === -1) {
const match = fuzzymatch(ns, valid_namespaces);
if (match) {
component.error(attribute, {
code: `invalid-namespace-property`,
message: `Invalid namespace '${ns}' (did you mean '${match}'?)`
});
} else {
component.error(attribute, {
code: `invalid-namespace-property`,
message: `Invalid namespace '${ns}'`
});
}
}

component_options.namespace = ns;
break;
}

case 'accessors':
case 'immutable':
case 'preserveWhitespace':
{
const code = `invalid-${name}-value`;
const message = `${name} attribute must be true or false`;
const value = get_value(attribute, code, message);

if (typeof value !== 'boolean') component.error(attribute, { code, message });

component_options[name] = value;
break;
}

default:
component.error(attribute, {
code: `invalid-options-attribute`,
message: `<svelte:options> unknown attribute`
});
}
}

else {
component.error(attribute, {
code: `invalid-options-attribute`,
message: `<svelte:options> can only have static 'tag', 'namespace', 'accessors', 'immutable' and 'preserveWhitespace' attributes`
});
}
});
}

return component_options;
}
48 changes: 24 additions & 24 deletions src/compiler/compile/create_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@ import list from '../utils/list';
import { ModuleFormat, Node } from '../interfaces';
import { stringify_props } from './utils/stringify_props';

const wrappers = { esm, cjs };

interface Export {
name: string;
as: string;
}

export default function create_module(
code: string,
format: ModuleFormat,
name: string,
banner: string,
sveltePath = 'svelte',
helpers: Array<{ name: string; alias: string }>,
imports: Node[],
module_exports: Export[],
source: string
): string {
const internal_path = `${sveltePath}/internal`;

if (format === 'esm') {
return esm(code, name, banner, sveltePath, internal_path, helpers, imports, module_exports, source);
}

if (format === 'cjs') return cjs(code, name, banner, sveltePath, internal_path, helpers, imports, module_exports);

throw new Error(`options.format is invalid (must be ${list(Object.keys(wrappers))})`);
}

function edit_source(source, sveltePath) {
return source === 'svelte' || source.startsWith('svelte/')
? source.replace('svelte', sveltePath)
Expand Down Expand Up @@ -109,27 +133,3 @@ function cjs(

${exports}`;
}

const wrappers = { esm, cjs };

export default function create_module(
code: string,
format: ModuleFormat,
name: string,
banner: string,
sveltePath = 'svelte',
helpers: Array<{ name: string; alias: string }>,
imports: Node[],
module_exports: Export[],
source: string
): string {
const internal_path = `${sveltePath}/internal`;

if (format === 'esm') {
return esm(code, name, banner, sveltePath, internal_path, helpers, imports, module_exports, source);
}

if (format === 'cjs') return cjs(code, name, banner, sveltePath, internal_path, helpers, imports, module_exports);

throw new Error(`options.format is invalid (must be ${list(Object.keys(wrappers))})`);
}
Loading