diff --git a/src/compiler/compile/render_dom/Renderer.ts b/src/compiler/compile/render_dom/Renderer.ts index 27895e2f9c..c015bcfdb5 100644 --- a/src/compiler/compile/render_dom/Renderer.ts +++ b/src/compiler/compile/render_dom/Renderer.ts @@ -167,18 +167,6 @@ export default class Renderer { const variable = this.component.var_lookup.get(head); - // // TODO this feels woolly. might encounter false positive - // // if each context shadows top-level var - // if (variable) { - // this.component.add_reference(name); // TODO we can probably remove most other occurrences of this - - // if (!variable.hoistable) { - // head = x`#ctx[${i}]`; - // } - // } else if (i !== undefined) { - // head = x`#ctx[${i}]`; - // } - if (variable) { this.component.add_reference(name); // TODO we can probably remove most other occurrences of this } diff --git a/src/compiler/compile/render_dom/wrappers/Slot.ts b/src/compiler/compile/render_dom/wrappers/Slot.ts index 123b6776e8..a477c6c006 100644 --- a/src/compiler/compile/render_dom/wrappers/Slot.ts +++ b/src/compiler/compile/render_dom/wrappers/Slot.ts @@ -6,10 +6,39 @@ import FragmentWrapper from './Fragment'; import { b, p, x } from 'code-red'; import { sanitize } from '../../../utils/names'; import add_to_set from '../../utils/add_to_set'; -import get_slot_data from '../../utils/get_slot_data'; import Expression from '../../nodes/shared/Expression'; import is_dynamic from './shared/is_dynamic'; import { Identifier, ObjectExpression } from 'estree'; +import Attribute from '../../nodes/Attribute'; +import { string_literal } from '../../utils/stringify'; + +function get_slot_data(block: Block, values: Map) { + return { + type: 'ObjectExpression', + properties: Array.from(values.values()) + .filter(attribute => attribute.name !== 'name') + .map(attribute => { + const value = get_value(block, attribute); + return p`${attribute.name}: ${value}`; + }) + }; +} + +// TODO fairly sure this is duplicated at least once +function get_value(block: Block, attribute: Attribute) { + if (attribute.is_true) return x`true`; + if (attribute.chunks.length === 0) return x`""`; + + let value = attribute.chunks + .map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block)) + .reduce((lhs, rhs) => x`${lhs} + ${rhs}`); + + if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') { + value = x`"" + ${value}`; + } + + return value; +} export default class SlotWrapper extends Wrapper { node: Slot; diff --git a/src/compiler/compile/render_ssr/handlers/Slot.ts b/src/compiler/compile/render_ssr/handlers/Slot.ts index 9f8d617c53..897efeb382 100644 --- a/src/compiler/compile/render_ssr/handlers/Slot.ts +++ b/src/compiler/compile/render_ssr/handlers/Slot.ts @@ -1,7 +1,37 @@ -import get_slot_data from '../../utils/get_slot_data'; import Renderer, { RenderOptions } from '../Renderer'; import Slot from '../../nodes/Slot'; -import { x } from 'code-red'; +import { x, p } from 'code-red'; +import Attribute from '../../nodes/Attribute'; +import { string_literal } from '../../utils/stringify'; + +// TODO this is *almost* but not quite duplicated with non-SSR +function get_slot_data(values: Map) { + return { + type: 'ObjectExpression', + properties: Array.from(values.values()) + .filter(attribute => attribute.name !== 'name') + .map(attribute => { + const value = get_value(attribute); + return p`${attribute.name}: ${value}`; + }) + }; +} + +// TODO fairly sure this is duplicated at least once +function get_value(attribute: Attribute) { + if (attribute.is_true) return x`true`; + if (attribute.chunks.length === 0) return x`""`; + + let value = attribute.chunks + .map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : chunk.node) + .reduce((lhs, rhs) => x`${lhs} + ${rhs}`); + + if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') { + value = x`"" + ${value}`; + } + + return value; +} export default function(node: Slot, renderer: Renderer, options: RenderOptions) { const slot_data = get_slot_data(node.values); diff --git a/test/runtime/index.js b/test/runtime/index.js index 79dfd9e050..fc989d352f 100644 --- a/test/runtime/index.js +++ b/test/runtime/index.js @@ -26,7 +26,7 @@ process.on('unhandledRejection', err => { unhandled_rejection = err; }); -describe.only("runtime", () => { +describe("runtime", () => { before(() => { svelte = loadSvelte(false); svelte$ = loadSvelte(true); diff --git a/test/sourcemaps/samples/basic/test.js b/test/sourcemaps/samples/basic/test.js index f2ce4198fd..7059761674 100644 --- a/test/sourcemaps/samples/basic/test.js +++ b/test/sourcemaps/samples/basic/test.js @@ -1,31 +1,31 @@ export function test({ assert, smc, locateInSource, locateInGenerated }) { - const expected = locateInSource( 'foo.bar.baz' ); + const expected = locateInSource('foo.bar.baz'); let start; let actual; - start = locateInGenerated('foo.bar.baz'); + start = locateInGenerated('ctx[0].bar.baz'); actual = smc.originalPositionFor({ line: start.line + 1, column: start.column }); - assert.deepEqual( actual, { + assert.deepEqual(actual, { source: 'input.svelte', name: null, line: expected.line + 1, column: expected.column }); - start = locateInGenerated( 'foo.bar.baz', start.character + 1 ); + start = locateInGenerated('ctx[0].bar.baz', start.character + 1); actual = smc.originalPositionFor({ line: start.line + 1, column: start.column }); - assert.deepEqual( actual, { + assert.deepEqual(actual, { source: 'input.svelte', name: null, line: expected.line + 1,