rafactor parse step

pull/6898/head
Yuichiro Yamashita 5 years ago
parent ca63111498
commit 85eab20b01

@ -117,7 +117,7 @@ function get_namespace(parent: Element, element: Element, explicit_namespace: st
} }
export default class Element extends Node { export default class Element extends Node {
type: 'Element' | 'DynamicElement'; type: 'Element';
name: string; name: string;
scope: TemplateScope; scope: TemplateScope;
attributes: Attribute[] = []; attributes: Attribute[] = [];
@ -140,10 +140,8 @@ export default class Element extends Node {
if (this.name === 'svelte:element') { if (this.name === 'svelte:element') {
if (typeof info.tag === 'string') { if (typeof info.tag === 'string') {
this.type = 'Element';
this.name = info.tag; this.name = info.tag;
} else { } else {
this.type = 'DynamicElement';
this.dynamic_tag_expr = new Expression(component, this, scope, info.tag); this.dynamic_tag_expr = new Expression(component, this, scope, info.tag);
} }
} }

@ -106,7 +106,7 @@ export default class InlineComponent extends Node {
if (child.type === 'SlotTemplate') { if (child.type === 'SlotTemplate') {
children.push(child); children.push(child);
info.children.splice(i, 1); info.children.splice(i, 1);
} else if ((child.type === 'Element' || child.type === 'DynamicElement' || child.type === 'InlineComponent' || child.type === 'Slot') && child.attributes.find(attribute => attribute.name === 'slot')) { } else if ((child.type === 'Element' || child.type === 'InlineComponent' || child.type === 'Slot') && child.attributes.find(attribute => attribute.name === 'slot')) {
const slot_template = { const slot_template = {
start: child.start, start: child.start,
end: child.end, end: child.end,

@ -25,7 +25,6 @@ function get_constructor(type) {
case 'AwaitBlock': return AwaitBlock; case 'AwaitBlock': return AwaitBlock;
case 'Body': return Body; case 'Body': return Body;
case 'Comment': return Comment; case 'Comment': return Comment;
case 'DynamicElement' : return Element;
case 'EachBlock': return EachBlock; case 'EachBlock': return EachBlock;
case 'Element': return Element; case 'Element': return Element;
case 'Head': return Head; case 'Head': return Head;

@ -248,7 +248,7 @@ export default class ElementWrapper extends Wrapper {
b`${node} = ${render_statement};` b`${node} = ${render_statement};`
); );
if (this.node.type === 'DynamicElement' && this.renderer.options.dev) { if (this.node.dynamic_tag_expr && this.renderer.options.dev) {
block.chunks.create.push(b`@validate_dynamic_element(${this.node.dynamic_tag_expr.manipulate(block)});`); block.chunks.create.push(b`@validate_dynamic_element(${this.node.dynamic_tag_expr.manipulate(block)});`);
if (renderer.options.hydratable) { if (renderer.options.hydratable) {
@ -354,7 +354,7 @@ export default class ElementWrapper extends Wrapper {
this.add_classes(block); this.add_classes(block);
this.add_manual_style_scoping(block); this.add_manual_style_scoping(block);
if (this.node.type === 'DynamicElement') { if (this.node.dynamic_tag_expr) {
const dependencies = this.node.dynamic_tag_expr.dynamic_dependencies(); const dependencies = this.node.dynamic_tag_expr.dynamic_dependencies();
if (dependencies.length) { if (dependencies.length) {
const condition = block.renderer.dirty( const condition = block.renderer.dirty(
@ -405,7 +405,7 @@ export default class ElementWrapper extends Wrapper {
return x`@element_is("${name}", ${is.render_chunks(block).reduce((lhs, rhs) => x`${lhs} + ${rhs}`)})`; return x`@element_is("${name}", ${is.render_chunks(block).reduce((lhs, rhs) => x`${lhs} + ${rhs}`)})`;
} }
const reference = this.node.type === 'DynamicElement' ? this.node.dynamic_tag_expr.manipulate(block) : `"${name}"`; const reference = this.node.dynamic_tag_expr ? this.node.dynamic_tag_expr.manipulate(block) : `"${name}"`;
return x`@element(${reference})`; return x`@element(${reference})`;
} }
@ -417,7 +417,7 @@ export default class ElementWrapper extends Wrapper {
const name = this.node.namespace const name = this.node.namespace
? this.node.name ? this.node.name
: this.node.name.toUpperCase(); : this.node.name.toUpperCase();
const reference = this.node.type === 'DynamicElement' ? this.node.dynamic_tag_expr.manipulate(block) : `"${name}"`; const reference = this.node.dynamic_tag_expr ? this.node.dynamic_tag_expr.manipulate(block) : `"${name}"`;
if (this.node.namespace === namespaces.svg) { if (this.node.namespace === namespaces.svg) {
return x`@claim_svg_element(${nodes}, ${reference}, { ${attributes} })`; return x`@claim_svg_element(${nodes}, ${reference}, { ${attributes} })`;

@ -117,8 +117,8 @@ export default class FragmentWrapper {
link(last_child, last_child = wrapper); link(last_child, last_child = wrapper);
} else { } else {
const Wrapper = (function () { const Wrapper = (function () {
if (child.type === 'DynamicElement' && child.dynamic_tag_expr.dynamic_dependencies().length === 0) { if (child.type === 'Element' && child.dynamic_tag_expr) {
return wrappers['Element']; return wrappers['DynamicElement'];
} else { } else {
return wrappers[child.type]; return wrappers[child.type];
} }

@ -113,7 +113,13 @@ export default class Renderer {
render(nodes: INode[], options: RenderOptions) { render(nodes: INode[], options: RenderOptions) {
nodes.forEach(node => { nodes.forEach(node => {
const handler = handlers[node.type]; const handler = (function () {
if (node.type === 'Element' && node.dynamic_tag_expr) {
return handlers['DynamicElement'];
} else {
return handlers[node.type];
}
}());
if (!handler) { if (!handler) {
throw new Error(`No handler for '${node.type}' nodes`); throw new Error(`No handler for '${node.type}' nodes`);

@ -47,7 +47,7 @@ interface BaseDirective extends BaseNode {
} }
export interface Element extends BaseNode { export interface Element extends BaseNode {
type: 'InlineComponent' | 'SlotTemplate' | 'Title' | 'Slot' | 'Element' | 'DynamicElement' | 'Head' | 'Options' | 'Window' | 'Body'; type: 'InlineComponent' | 'SlotTemplate' | 'Title' | 'Slot' | 'Element' | 'Head' | 'Options' | 'Window' | 'Body';
attributes: Array<BaseDirective | Attribute | SpreadAttribute>; attributes: Array<BaseDirective | Attribute | SpreadAttribute>;
name: string; name: string;
} }

@ -106,9 +106,9 @@ export default function tag(parser: Parser) {
if (meta_tags.has(name)) return meta_tags.get(name); if (meta_tags.has(name)) return meta_tags.get(name);
if ((/[A-Z]/.test(name[0]) || name === 'svelte:self' || name === 'svelte:component')) return 'InlineComponent'; if ((/[A-Z]/.test(name[0]) || name === 'svelte:self' || name === 'svelte:component')) return 'InlineComponent';
if (name === 'svelte:fragment') return 'SlotTemplate'; if (name === 'svelte:fragment') return 'SlotTemplate';
if (name === 'svelte:element') return 'DynamicElement';
if (name === 'title' && parent_is_head(parser.stack)) return 'Title'; if (name === 'title' && parent_is_head(parser.stack)) return 'Title';
if (name === 'slot' && !parser.customElement) return 'Slot'; if (name === 'slot' && !parser.customElement) return 'Slot';
if (name === 'svelte:element') return 'Element';
return 'Element'; return 'Element';
})(); })();

@ -7,7 +7,7 @@
{ {
"start": 0, "start": 0,
"end": 44, "end": 44,
"type": "DynamicElement", "type": "Element",
"name": "svelte:element", "name": "svelte:element",
"attributes": [], "attributes": [],
"children": [], "children": [],
@ -23,7 +23,7 @@
{ {
"start": 45, "start": 45,
"end": 101, "end": 101,
"type": "DynamicElement", "type": "Element",
"name": "svelte:element", "name": "svelte:element",
"attributes": [ "attributes": [
{ {
@ -47,4 +47,4 @@
} }
] ]
} }
} }

@ -7,7 +7,7 @@
{ {
"start": 0, "start": 0,
"end": 44, "end": 44,
"type": "DynamicElement", "type": "Element",
"name": "svelte:element", "name": "svelte:element",
"attributes": [], "attributes": [],
"children": [], "children": [],
@ -38,7 +38,7 @@
{ {
"start": 45, "start": 45,
"end": 101, "end": 101,
"type": "DynamicElement", "type": "Element",
"name": "svelte:element", "name": "svelte:element",
"children": [], "children": [],
"attributes": [ "attributes": [

Loading…
Cancel
Save