From 7f39b5be165c9314a545f16f6465f72d2ff15c89 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 20 Sep 2017 11:42:07 -0400 Subject: [PATCH] tidy up --- src/generators/Generator.ts | 54 +++++-------------- src/generators/dom/visitors/Component.ts | 5 +- src/generators/server-side-rendering/index.ts | 6 +-- .../visitors/Component.ts | 5 +- 4 files changed, 17 insertions(+), 53 deletions(-) diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index bd781c05fc..1255773897 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -481,7 +481,7 @@ export default class Generator { for (let i = 0; i < body.length; i += 1) { const node = body[i]; if (node.type === 'ImportDeclaration') { - removeNode(this.code, js.content, node); + removeNode(code, js.content, node); imports.push(node); node.specifiers.forEach((specifier: Node) => { @@ -536,16 +536,21 @@ export default class Generator { }; const addValue = (name: string, node: Node) => { - if (node.type !== 'Identifier' || node.name !== name) { - componentDefinition.addBlock(deindent` - var ${name} = [✂${node.start}-${node.end}✂]; - `); - } + componentDefinition.addBlock(deindent` + var ${name} = [✂${node.start}-${node.end}✂]; + `); }; const addDeclaration = (key: string, node: Node, disambiguator?: string) => { + const qualified = disambiguator ? `${disambiguator}-${key}` : key; + + if (node.type === 'Identifier' && node.name === key) { + this.templateVars.set(qualified, key); + return; + } + let name = this.getUniqueName(key); - this.templateVars.set(disambiguator ? `${disambiguator}-${key}` : key, name); + this.templateVars.set(qualified, name); // deindent const indentationLevel = getIndentationLevel(source, node.start); @@ -553,8 +558,6 @@ export default class Generator { removeIndentation(code, node.start, node.end, indentationLevel, indentExclusionRanges); } - // TODO disambiguate between different categories, and ensure - // no conflicts with existing aliases if (node.type === 'ArrowFunctionExpression') { addArrowFunctionExpression(name, node); } else if (node.type === 'FunctionExpression') { @@ -566,24 +569,7 @@ export default class Generator { if (templateProperties.components) { templateProperties.components.value.properties.forEach((property: Node) => { - // TODO replace all the guff below with this: - // addValue(property.key.name, property.value); - - const key = property.key.name; - const value = source.slice( - property.value.start, - property.value.end - ); - - if (key !== value) { - const alias = this.alias(key); - componentDefinition.addLine( - `var ${alias} = [✂${property.value.start}-${property.value.end}✂];` - ); - this.importedComponents.set(key, alias); - } else { - this.importedComponents.set(key, key); - } + addDeclaration(property.key.name, property.value, 'components'); }); } @@ -677,15 +663,6 @@ export default class Generator { } } - // if we do need to keep it, then we need to replace `export default` - // if (defaultExport) { - // this.code.overwrite( - // defaultExport.start, - // defaultExport.declaration.start, - // `var ${this.alias('template')} = ` - // ); - // } - if (indentationLevel) { if (defaultExport) { removeIndentation(code, js.content.start, defaultExport.start, indentationLevel, indentExclusionRanges); @@ -695,11 +672,6 @@ export default class Generator { } } - if (js.content.body.length === 0) { - // if there's no need to include user code, remove it altogether - this.code.remove(js.content.start, js.content.end); - } - let a = js.content.start; while (/\s/.test(source[a])) a += 1; diff --git a/src/generators/dom/visitors/Component.ts b/src/generators/dom/visitors/Component.ts index 67f647a755..2263aab1fb 100644 --- a/src/generators/dom/visitors/Component.ts +++ b/src/generators/dom/visitors/Component.ts @@ -216,10 +216,7 @@ export default function visitComponent( } } - const expression = node.name === ':Self' - ? generator.name - : generator.importedComponents.get(node.name) || - `@template.components.${node.name}`; // TODO this is out of date + const expression = node.name === ':Self' ? generator.name : `%components-${node.name}`; block.builders.init.addBlock(deindent` ${statements.join('\n')} diff --git a/src/generators/server-side-rendering/index.ts b/src/generators/server-side-rendering/index.ts index 58d410fbe6..8971110d57 100644 --- a/src/generators/server-side-rendering/index.ts +++ b/src/generators/server-side-rendering/index.ts @@ -161,10 +161,8 @@ export default function ssr( }); } - ${templateProperties.components.value.properties.map(prop => { - const { name } = prop.key; - const expression = generator.importedComponents.get(name) || `@template.components.${name}`; - return `addComponent(${expression});`; + ${templateProperties.components.value.properties.map((prop: Node) => { + return `addComponent(%components-${prop.key.name});`; })} `} diff --git a/src/generators/server-side-rendering/visitors/Component.ts b/src/generators/server-side-rendering/visitors/Component.ts index 8c527ac361..0ab6604be3 100644 --- a/src/generators/server-side-rendering/visitors/Component.ts +++ b/src/generators/server-side-rendering/visitors/Component.ts @@ -69,10 +69,7 @@ export default function visitComponent( ) .join(', '); - const expression = node.name === ':Self' - ? generator.name - : generator.importedComponents.get(node.name) || - `@template.components.${node.name}`; // TODO out of date + const expression = node.name === ':Self' ? generator.name : `%components-${node.name}`; bindings.forEach(binding => { block.addBinding(binding, expression);