pull/864/head
Rich Harris 7 years ago
parent 4dbfc65e74
commit 7f39b5be16

@ -481,7 +481,7 @@ export default class Generator {
for (let i = 0; i < body.length; i += 1) { for (let i = 0; i < body.length; i += 1) {
const node = body[i]; const node = body[i];
if (node.type === 'ImportDeclaration') { if (node.type === 'ImportDeclaration') {
removeNode(this.code, js.content, node); removeNode(code, js.content, node);
imports.push(node); imports.push(node);
node.specifiers.forEach((specifier: Node) => { node.specifiers.forEach((specifier: Node) => {
@ -536,16 +536,21 @@ export default class Generator {
}; };
const addValue = (name: string, node: Node) => { const addValue = (name: string, node: Node) => {
if (node.type !== 'Identifier' || node.name !== name) { componentDefinition.addBlock(deindent`
componentDefinition.addBlock(deindent` var ${name} = [${node.start}-${node.end}];
var ${name} = [${node.start}-${node.end}]; `);
`);
}
}; };
const addDeclaration = (key: string, node: Node, disambiguator?: string) => { 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); let name = this.getUniqueName(key);
this.templateVars.set(disambiguator ? `${disambiguator}-${key}` : key, name); this.templateVars.set(qualified, name);
// deindent // deindent
const indentationLevel = getIndentationLevel(source, node.start); const indentationLevel = getIndentationLevel(source, node.start);
@ -553,8 +558,6 @@ export default class Generator {
removeIndentation(code, node.start, node.end, indentationLevel, indentExclusionRanges); removeIndentation(code, node.start, node.end, indentationLevel, indentExclusionRanges);
} }
// TODO disambiguate between different categories, and ensure
// no conflicts with existing aliases
if (node.type === 'ArrowFunctionExpression') { if (node.type === 'ArrowFunctionExpression') {
addArrowFunctionExpression(name, node); addArrowFunctionExpression(name, node);
} else if (node.type === 'FunctionExpression') { } else if (node.type === 'FunctionExpression') {
@ -566,24 +569,7 @@ export default class Generator {
if (templateProperties.components) { if (templateProperties.components) {
templateProperties.components.value.properties.forEach((property: Node) => { templateProperties.components.value.properties.forEach((property: Node) => {
// TODO replace all the guff below with this: addDeclaration(property.key.name, property.value, 'components');
// 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);
}
}); });
} }
@ -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 (indentationLevel) {
if (defaultExport) { if (defaultExport) {
removeIndentation(code, js.content.start, defaultExport.start, indentationLevel, indentExclusionRanges); 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; let a = js.content.start;
while (/\s/.test(source[a])) a += 1; while (/\s/.test(source[a])) a += 1;

@ -216,10 +216,7 @@ export default function visitComponent(
} }
} }
const expression = node.name === ':Self' const expression = node.name === ':Self' ? generator.name : `%components-${node.name}`;
? generator.name
: generator.importedComponents.get(node.name) ||
`@template.components.${node.name}`; // TODO this is out of date
block.builders.init.addBlock(deindent` block.builders.init.addBlock(deindent`
${statements.join('\n')} ${statements.join('\n')}

@ -161,10 +161,8 @@ export default function ssr(
}); });
} }
${templateProperties.components.value.properties.map(prop => { ${templateProperties.components.value.properties.map((prop: Node) => {
const { name } = prop.key; return `addComponent(%components-${prop.key.name});`;
const expression = generator.importedComponents.get(name) || `@template.components.${name}`;
return `addComponent(${expression});`;
})} })}
`} `}

@ -69,10 +69,7 @@ export default function visitComponent(
) )
.join(', '); .join(', ');
const expression = node.name === ':Self' const expression = node.name === ':Self' ? generator.name : `%components-${node.name}`;
? generator.name
: generator.importedComponents.get(node.name) ||
`@template.components.${node.name}`; // TODO out of date
bindings.forEach(binding => { bindings.forEach(binding => {
block.addBinding(binding, expression); block.addBinding(binding, expression);

Loading…
Cancel
Save