diff --git a/src/compiler/compile/create_module.ts b/src/compiler/compile/create_module.ts index ce6443e899..632206652a 100644 --- a/src/compiler/compile/create_module.ts +++ b/src/compiler/compile/create_module.ts @@ -41,6 +41,32 @@ function edit_source(source, sveltePath) { : source; } +function get_internal_globals( + globals: Array<{ name: string; alias: Identifier }>, + helpers: Array<{ name: string; alias: Identifier }> +) { + return globals.length > 0 && { + type: 'VariableDeclaration', + kind: 'const', + declarations: [{ + type: 'VariableDeclarator', + id: { + type: 'ObjectPattern', + properties: globals.map(g => ({ + type: 'Property', + method: false, + shorthand: false, + computed: false, + key: { type: 'Identifier', name: g.name }, + value: g.alias, + kind: 'init' + })) + }, + init: helpers.find(({ name }) => name === 'globals').alias + }] + }; +} + function esm( program: any, name: Identifier, @@ -62,26 +88,7 @@ function esm( source: { type: 'Literal', value: internal_path } }; - const internal_globals = globals.length > 0 && { - type: 'VariableDeclaration', - kind: 'const', - declarations: [{ - type: 'VariableDeclarator', - id: { - type: 'ObjectPattern', - properties: globals.map(g => ({ - type: 'Property', - method: false, - shorthand: false, - computed: false, - key: { type: 'Identifier', name: g.name }, - value: g.alias, - kind: 'init' - })) - }, - init: helpers.find(({ name }) => name === 'globals').alias - }] - }; + const internal_globals = get_internal_globals(globals, helpers); // edit user imports imports.forEach(node => { @@ -143,26 +150,7 @@ function cjs( }] }; - const internal_globals = globals.length > 0 && { - type: 'VariableDeclaration', - kind: 'const', - declarations: [{ - type: 'VariableDeclarator', - id: { - type: 'ObjectPattern', - properties: globals.map(g => ({ - type: 'Property', - method: false, - shorthand: false, - computed: false, - key: { type: 'Identifier', name: g.name }, - value: g.alias, - kind: 'init' - })) - }, - init: helpers.find(({ name }) => name === 'globals').alias - }] - }; + const internal_globals = get_internal_globals(globals, helpers); const user_requires = imports.map(node => ({ type: 'VariableDeclaration', diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index cc1e05f82b..3d966f8765 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -75,7 +75,6 @@ export default function dom( const props = component.vars.filter(variable => !variable.module && variable.export_name); const writable_props = props.filter(variable => variable.writable); - /* eslint-disable @typescript-eslint/indent,indent */ const set = (uses_props || writable_props.length > 0 || component.slots.size > 0) ? x` ${$$props} => { @@ -88,7 +87,6 @@ export default function dom( } ` : null; - /* eslint-enable @typescript-eslint/indent,indent */ const accessors = []; diff --git a/src/compiler/compile/render_dom/wrappers/Element/StyleAttribute.ts b/src/compiler/compile/render_dom/wrappers/Element/StyleAttribute.ts index 9dc36dabf5..06ead11a3c 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/StyleAttribute.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/StyleAttribute.ts @@ -87,14 +87,12 @@ function optimize_style(value: Array) { const remaining_data = chunk.data.slice(offset); if (remaining_data) { - /* eslint-disable @typescript-eslint/no-object-literal-type-assertion */ chunks[0] = { start: chunk.start + offset, end: chunk.end, type: 'Text', data: remaining_data } as Text; - /* eslint-enable @typescript-eslint/no-object-literal-type-assertion */ } else { chunks.shift(); } diff --git a/src/compiler/compile/render_dom/wrappers/IfBlock.ts b/src/compiler/compile/render_dom/wrappers/IfBlock.ts index d2d4c79486..dfc2228bc5 100644 --- a/src/compiler/compile/render_dom/wrappers/IfBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/IfBlock.ts @@ -263,7 +263,6 @@ export default class IfBlockWrapper extends Wrapper { ? x`${current_block_type}(#ctx)` : x`${current_block_type} && ${current_block_type}(#ctx)`; - /* eslint-disable @typescript-eslint/indent,indent */ if (this.needs_update) { block.chunks.init.push(b` function ${select_block_type}(#ctx, #dirty) { @@ -287,7 +286,6 @@ export default class IfBlockWrapper extends Wrapper { } `); } - /* eslint-enable @typescript-eslint/indent,indent */ block.chunks.init.push(b` let ${current_block_type} = ${select_block_type}(#ctx, -1); @@ -375,7 +373,6 @@ export default class IfBlockWrapper extends Wrapper { block.add_variable(current_block_type_index); block.add_variable(name); - /* eslint-disable @typescript-eslint/indent,indent */ block.chunks.init.push(b` const ${if_block_creators} = [ ${this.branches.map(branch => branch.block.name)} @@ -407,7 +404,6 @@ export default class IfBlockWrapper extends Wrapper { } `} `); - /* eslint-enable @typescript-eslint/indent,indent */ if (has_else) { block.chunks.init.push(b` diff --git a/src/compiler/compile/render_dom/wrappers/Slot.ts b/src/compiler/compile/render_dom/wrappers/Slot.ts index 0443ecc2e5..5f2d331839 100644 --- a/src/compiler/compile/render_dom/wrappers/Slot.ts +++ b/src/compiler/compile/render_dom/wrappers/Slot.ts @@ -6,39 +6,10 @@ 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; @@ -125,7 +96,7 @@ export default class SlotWrapper extends Wrapper { renderer.blocks.push(b` const ${get_slot_changes_fn} = #dirty => ${changes}; - const ${get_slot_context_fn} = #ctx => ${get_slot_data(block, this.node.values)}; + const ${get_slot_context_fn} = #ctx => ${get_slot_data(this.node.values, block)}; `); } else { get_slot_changes_fn = 'null'; diff --git a/src/compiler/compile/render_ssr/handlers/Slot.ts b/src/compiler/compile/render_ssr/handlers/Slot.ts index 897efeb382..3b1e199c75 100644 --- a/src/compiler/compile/render_ssr/handlers/Slot.ts +++ b/src/compiler/compile/render_ssr/handlers/Slot.ts @@ -1,37 +1,7 @@ import Renderer, { RenderOptions } from '../Renderer'; import Slot from '../../nodes/Slot'; -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; -} +import { x } from 'code-red'; +import get_slot_data from '../../utils/get_slot_data'; export default function(node: Slot, renderer: Renderer, options: RenderOptions) { const slot_data = get_slot_data(node.values); diff --git a/src/compiler/compile/utils/get_slot_data.ts b/src/compiler/compile/utils/get_slot_data.ts index 87811684a3..a118d52023 100644 --- a/src/compiler/compile/utils/get_slot_data.ts +++ b/src/compiler/compile/utils/get_slot_data.ts @@ -3,7 +3,7 @@ import { p, x } from 'code-red'; import { string_literal } from './stringify'; import Block from '../render_dom/Block'; -export default function get_slot_data(block: Block, values: Map) { +export default function get_slot_data(values: Map, block: Block = null) { return { type: 'ObjectExpression', properties: Array.from(values.values()) @@ -15,13 +15,12 @@ export default function get_slot_data(block: Block, values: Map chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block)) + .map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : (block ? chunk.manipulate(block) : chunk.node)) .reduce((lhs, rhs) => x`${lhs} + ${rhs}`); if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') { diff --git a/src/compiler/parse/index.ts b/src/compiler/parse/index.ts index 5416038a53..e552374698 100644 --- a/src/compiler/parse/index.ts +++ b/src/compiler/parse/index.ts @@ -65,11 +65,11 @@ export class Parser { } if (this.html.children.length) { - let start = this.html.children[0] && this.html.children[0].start; - while (/\s/.test(template[start])) start += 1; + let start = this.html.children[0].start; + while (whitespace.test(template[start])) start += 1; - let end = this.html.children[this.html.children.length - 1] && this.html.children[this.html.children.length - 1].end; - while (/\s/.test(template[end - 1])) end -= 1; + let end = this.html.children[this.html.children.length - 1].end; + while (whitespace.test(template[end - 1])) end -= 1; this.html.start = start; this.html.end = end; diff --git a/src/compiler/parse/read/expression.ts b/src/compiler/parse/read/expression.ts index 1126711fe2..c8e955b719 100644 --- a/src/compiler/parse/read/expression.ts +++ b/src/compiler/parse/read/expression.ts @@ -13,7 +13,6 @@ export default function read_expression(parser: Parser): Node { const end = start + name.length; if (literals.has(name)) { - // eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion return { type: 'Literal', start, @@ -23,7 +22,6 @@ export default function read_expression(parser: Parser): Node { } as SimpleLiteral; } - // eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion return { type: 'Identifier', start, diff --git a/src/compiler/parse/read/script.ts b/src/compiler/parse/read/script.ts index 0c416be6e8..130c346ba1 100644 --- a/src/compiler/parse/read/script.ts +++ b/src/compiler/parse/read/script.ts @@ -1,5 +1,4 @@ import * as acorn from '../acorn'; -import repeat from '../../utils/repeat'; import { Parser } from '../index'; import { Script } from '../../interfaces'; import { Node, Program } from 'estree'; @@ -38,8 +37,7 @@ export default function read_script(parser: Parser, start: number, attributes: N message: `