0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00
This commit is contained in:
Rich Harris 2017-09-20 11:42:07 -04:00
parent 4dbfc65e74
commit 7f39b5be16
4 changed files with 17 additions and 53 deletions

View File

@ -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;

View File

@ -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')}

View File

@ -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});`;
})}
`}

View File

@ -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);