compile/render-dom and other remaining typings

pull/2838/head
Bogdan Savluk 6 years ago
parent 231603df7b
commit 24abeb1d28

8
package-lock.json generated

@ -1,6 +1,6 @@
{ {
"name": "svelte", "name": "svelte",
"version": "3.4.0", "version": "3.4.2",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -4179,9 +4179,9 @@
"dev": true "dev": true
}, },
"typescript": { "typescript": {
"version": "3.2.4", "version": "3.4.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.2.4.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz",
"integrity": "sha512-0RNDbSdEokBeEAkgNbxJ+BLwSManFy9TeXz8uW+48j/xhEXv1ePME60olyzw2XzUqUBNAYFeJadIqAgNqIACwg==", "integrity": "sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==",
"dev": true "dev": true
}, },
"uglify-js": { "uglify-js": {

@ -84,7 +84,7 @@
"tiny-glob": "^0.2.1", "tiny-glob": "^0.2.1",
"ts-node": "^8.0.2", "ts-node": "^8.0.2",
"tslib": "^1.8.0", "tslib": "^1.8.0",
"typescript": "^3.0.1" "typescript": "^3.4.0"
}, },
"nyc": { "nyc": {
"include": [ "include": [

@ -776,7 +776,7 @@ export default class Component {
}); });
} }
invalidate(name, value) { invalidate(name, value?) {
const variable = this.var_lookup.get(name); const variable = this.var_lookup.get(name);
if (variable && (variable.subscribable && variable.reassigned)) { if (variable && (variable.subscribable && variable.reassigned)) {

@ -55,7 +55,7 @@ function validate_options(options: CompileOptions, warnings: Warning[]) {
} }
} }
function get_name(filename) { function get_name(filename: string) {
if (!filename) return null; if (!filename) return null;
const parts = filename.split(/[\/\\]/); const parts = filename.split(/[\/\\]/);
@ -105,4 +105,4 @@ export default function compile(source: string, options: CompileOptions = {}) {
: render_dom(component, options); : render_dom(component, options);
return component.generate(js); return component.generate(js);
} }

@ -223,7 +223,7 @@ export default class Expression {
} }
// TODO move this into a render-dom wrapper? // TODO move this into a render-dom wrapper?
render(block: Block) { render(block?: Block) {
if (this.rendered) return this.rendered; if (this.rendered) return this.rendered;
const { const {

@ -9,7 +9,7 @@ export interface BlockOptions {
renderer?: Renderer; renderer?: Renderer;
comment?: string; comment?: string;
key?: string; key?: string;
bindings?: Map<string, () => { object: string, property: string, snippet: string }>; bindings?: Map<string, { object: string, property: string, snippet: string }>;
dependencies?: Set<string>; dependencies?: Set<string>;
} }

@ -3,7 +3,6 @@ import { CompileOptions } from '../../interfaces';
import Component from '../Component'; import Component from '../Component';
import FragmentWrapper from './wrappers/Fragment'; import FragmentWrapper from './wrappers/Fragment';
import CodeBuilder from '../utils/CodeBuilder'; import CodeBuilder from '../utils/CodeBuilder';
import SlotWrapper from './wrappers/Slot';
export default class Renderer { export default class Renderer {
component: Component; // TODO Maybe Renderer shouldn't know about Component? component: Component; // TODO Maybe Renderer shouldn't know about Component?
@ -18,6 +17,7 @@ export default class Renderer {
fragment: FragmentWrapper; fragment: FragmentWrapper;
file_var: string; file_var: string;
locate: (c: number) => { line: number; column: number; };
constructor(component: Component, options: CompileOptions) { constructor(component: Component, options: CompileOptions) {
this.component = component; this.component = component;
@ -58,4 +58,4 @@ export default class Renderer {
this.fragment.render(this.block, null, 'nodes'); this.fragment.render(this.block, null, 'nodes');
} }
} }

@ -8,7 +8,7 @@ import deindent from '../../utils/deindent';
import ElseBlock from '../../nodes/ElseBlock'; import ElseBlock from '../../nodes/ElseBlock';
import { attach_head } from '../../utils/tail'; import { attach_head } from '../../utils/tail';
class ElseBlockWrapper extends Wrapper { export class ElseBlockWrapper extends Wrapper {
node: ElseBlock; node: ElseBlock;
block: Block; block: Block;
fragment: FragmentWrapper; fragment: FragmentWrapper;
@ -83,6 +83,7 @@ export default class EachBlockWrapper extends Wrapper {
this.block = block.child({ this.block = block.child({
comment: create_debugging_comment(this.node, this.renderer.component), comment: create_debugging_comment(this.node, this.renderer.component),
name: renderer.component.get_unique_name('create_each_block'), name: renderer.component.get_unique_name('create_each_block'),
// @ts-ignore todo: probably error
key: node.key as string, key: node.key as string,
bindings: new Map(block.bindings) bindings: new Map(block.bindings)
@ -310,7 +311,9 @@ export default class EachBlockWrapper extends Wrapper {
} }
block.builders.init.add_block(deindent` block.builders.init.add_block(deindent`
const ${get_key} = ctx => ${this.node.key.render()}; const ${get_key} = ctx => ${
// @ts-ignore todo: probably error
this.node.key.render()};
for (var #i = 0; #i < ${this.vars.each_block_value}.${length}; #i += 1) { for (var #i = 0; #i < ${this.vars.each_block_value}.${length}; #i += 1) {
let child_ctx = ${this.vars.get_each_context}(ctx, ${this.vars.each_block_value}, #i); let child_ctx = ${this.vars.get_each_context}(ctx, ${this.vars.each_block_value}, #i);

@ -4,6 +4,7 @@ import fix_attribute_casing from './fix_attribute_casing';
import ElementWrapper from './index'; import ElementWrapper from './index';
import { stringify } from '../../../utils/stringify'; import { stringify } from '../../../utils/stringify';
import deindent from '../../../utils/deindent'; import deindent from '../../../utils/deindent';
import Expression from '../../../nodes/shared/Expression';
export default class AttributeWrapper { export default class AttributeWrapper {
node: Attribute; node: Attribute;
@ -21,7 +22,9 @@ export default class AttributeWrapper {
// special case — <option value={foo}> — see below // special case — <option value={foo}> — see below
if (this.parent.node.name === 'option' && node.name === 'value') { if (this.parent.node.name === 'option' && node.name === 'value') {
let select: ElementWrapper = this.parent; let select: ElementWrapper = this.parent;
while (select && (select.node.type !== 'Element' || select.node.name !== 'select')) select = select.parent; while (select && (select.node.type !== 'Element' || select.node.name !== 'select'))
// @ts-ignore todo: doublecheck this, but looks to be correct
select = select.parent;
if (select && select.select_binding_dependencies) { if (select && select.select_binding_dependencies) {
select.select_binding_dependencies.forEach(prop => { select.select_binding_dependencies.forEach(prop => {
@ -47,7 +50,7 @@ export default class AttributeWrapper {
(element.node.name === 'option' || // TODO check it's actually bound (element.node.name === 'option' || // TODO check it's actually bound
(element.node.name === 'input' && (element.node.name === 'input' &&
element.node.bindings.find( element.node.bindings.find(
(binding: Binding) => (binding) =>
/checked|group/.test(binding.name) /checked|group/.test(binding.name)
))); )));
@ -78,13 +81,13 @@ export default class AttributeWrapper {
// DRY it out if that's possible without introducing crazy indirection // DRY it out if that's possible without introducing crazy indirection
if (this.node.chunks.length === 1) { if (this.node.chunks.length === 1) {
// single {tag} — may be a non-string // single {tag} — may be a non-string
value = this.node.chunks[0].render(block); value = (this.node.chunks[0] as Expression).render(block);
} else { } else {
// '{foo} {bar}' — treat as string concatenation // '{foo} {bar}' — treat as string concatenation
value = value =
(this.node.chunks[0].type === 'Text' ? '' : `"" + `) + (this.node.chunks[0].type === 'Text' ? '' : `"" + `) +
this.node.chunks this.node.chunks
.map((chunk: Node) => { .map((chunk) => {
if (chunk.type === 'Text') { if (chunk.type === 'Text') {
return stringify(chunk.data); return stringify(chunk.data);
} else { } else {

@ -1,14 +1,15 @@
import Attribute from '../../../nodes/Attribute'; import Attribute from '../../../nodes/Attribute';
import Block from '../../Block'; import Block from '../../Block';
import AttributeWrapper from './Attribute'; import AttributeWrapper from './Attribute';
import Node from '../../../nodes/shared/Node';
import ElementWrapper from '.'; import ElementWrapper from '.';
import { stringify } from '../../../utils/stringify'; import { stringify } from '../../../utils/stringify';
import add_to_set from '../../../utils/add_to_set'; import add_to_set from '../../../utils/add_to_set';
import Expression from '../../../nodes/shared/Expression';
import Text from '../../../nodes/Text';
export interface StyleProp { export interface StyleProp {
key: string; key: string;
value: Node[]; value: (Text|Expression)[];
} }
export default class StyleAttributeWrapper extends AttributeWrapper { export default class StyleAttributeWrapper extends AttributeWrapper {
@ -28,7 +29,7 @@ export default class StyleAttributeWrapper extends AttributeWrapper {
value = value =
((prop.value.length === 1 || prop.value[0].type === 'Text') ? '' : `"" + `) + ((prop.value.length === 1 || prop.value[0].type === 'Text') ? '' : `"" + `) +
prop.value prop.value
.map((chunk: Node) => { .map((chunk) => {
if (chunk.type === 'Text') { if (chunk.type === 'Text') {
return stringify(chunk.data); return stringify(chunk.data);
} else { } else {
@ -54,7 +55,7 @@ export default class StyleAttributeWrapper extends AttributeWrapper {
); );
} }
} else { } else {
value = stringify(prop.value[0].data); value = stringify((prop.value[0] as Text).data);
} }
block.builders.hydrate.add_line( block.builders.hydrate.add_line(
@ -64,8 +65,8 @@ export default class StyleAttributeWrapper extends AttributeWrapper {
} }
} }
function optimize_style(value: Node[]) { function optimize_style(value: (Text|Expression)[]) {
const props: { key: string, value: Node[] }[] = []; const props: StyleProp[] = [];
let chunks = value.slice(); let chunks = value.slice();
while (chunks.length) { while (chunks.length) {
@ -87,7 +88,7 @@ function optimize_style(value: Node[]) {
end: chunk.end, end: chunk.end,
type: 'Text', type: 'Text',
data: remaining_data data: remaining_data
}; } as Text;
} else { } else {
chunks.shift(); chunks.shift();
} }
@ -101,8 +102,8 @@ function optimize_style(value: Node[]) {
return props; return props;
} }
function get_style_value(chunks: Node[]) { function get_style_value(chunks: (Text | Expression)[]) {
const value: Node[] = []; const value: (Text|Expression)[] = [];
let in_url = false; let in_url = false;
let quote_mark = null; let quote_mark = null;
@ -141,7 +142,7 @@ function get_style_value(chunks: Node[]) {
start: chunk.start, start: chunk.start,
end: chunk.start + c, end: chunk.start + c,
data: chunk.data.slice(0, c) data: chunk.data.slice(0, c)
}); } as Text);
} }
while (/[;\s]/.test(chunk.data[c])) c += 1; while (/[;\s]/.test(chunk.data[c])) c += 1;
@ -153,7 +154,7 @@ function get_style_value(chunks: Node[]) {
end: chunk.end, end: chunk.end,
type: 'Text', type: 'Text',
data: remaining_data data: remaining_data
}); } as Text);
break; break;
} }
@ -170,6 +171,6 @@ function get_style_value(chunks: Node[]) {
}; };
} }
function is_dynamic(value: Node[]) { function is_dynamic(value: (Text|Expression)[]) {
return value.length > 1 || value[0].type !== 'Text'; return value.length > 1 || value[0].type !== 'Text';
} }

@ -20,20 +20,19 @@ import add_event_handlers from '../shared/add_event_handlers';
import add_actions from '../shared/add_actions'; import add_actions from '../shared/add_actions';
import create_debugging_comment from '../shared/create_debugging_comment'; import create_debugging_comment from '../shared/create_debugging_comment';
import { get_context_merger } from '../shared/get_context_merger'; import { get_context_merger } from '../shared/get_context_merger';
import Slot from '../../../nodes/Slot';
const events = [ const events = [
{ {
event_names: ['input'], event_names: ['input'],
filter: (node: Element, name: string) => filter: (node: Element, name: string) =>
node.name === 'textarea' || node.name === 'textarea' ||
node.name === 'input' && !/radio|checkbox|range/.test(node.get_static_attribute_value('type')) node.name === 'input' && !/radio|checkbox|range/.test(node.get_static_attribute_value('type') as string)
}, },
{ {
event_names: ['change'], event_names: ['change'],
filter: (node: Element, name: string) => filter: (node: Element, name: string) =>
node.name === 'select' || node.name === 'select' ||
node.name === 'input' && /radio|checkbox/.test(node.get_static_attribute_value('type')) node.name === 'input' && /radio|checkbox/.test(node.get_static_attribute_value('type') as string)
}, },
{ {
event_names: ['change', 'input'], event_names: ['change', 'input'],
@ -135,7 +134,7 @@ export default class ElementWrapper extends Wrapper {
} }
if (owner && owner.node.type === 'InlineComponent') { if (owner && owner.node.type === 'InlineComponent') {
const name = attribute.get_static_value(); const name = attribute.get_static_value() as string;
if (!(owner as InlineComponentWrapper).slots.has(name)) { if (!(owner as InlineComponentWrapper).slots.has(name)) {
const child_block = block.child({ const child_block = block.child({
@ -275,6 +274,7 @@ export default class ElementWrapper extends Wrapper {
if (!this.node.namespace && this.can_use_innerhtml && this.fragment.nodes.length > 0) { if (!this.node.namespace && this.can_use_innerhtml && this.fragment.nodes.length > 0) {
if (this.fragment.nodes.length === 1 && this.fragment.nodes[0].node.type === 'Text') { if (this.fragment.nodes.length === 1 && this.fragment.nodes[0].node.type === 'Text') {
block.builders.create.add_line( block.builders.create.add_line(
// @ts-ignore todo: should it be this.fragment.nodes[0].node.data instead?
`${node}.textContent = ${stringify(this.fragment.nodes[0].data)};` `${node}.textContent = ${stringify(this.fragment.nodes[0].data)};`
); );
} else { } else {
@ -324,7 +324,7 @@ export default class ElementWrapper extends Wrapper {
function to_html(wrapper: ElementWrapper | TextWrapper) { function to_html(wrapper: ElementWrapper | TextWrapper) {
if (wrapper.node.type === 'Text') { if (wrapper.node.type === 'Text') {
const { parent } = wrapper.node; const parent = wrapper.node.parent as Element;
const raw = parent && ( const raw = parent && (
parent.name === 'script' || parent.name === 'script' ||
@ -349,7 +349,7 @@ export default class ElementWrapper extends Wrapper {
if (is_void(wrapper.node.name)) return open + '>'; if (is_void(wrapper.node.name)) return open + '>';
return `${open}>${wrapper.fragment.nodes.map(to_html).join('')}</${wrapper.node.name}>`; return `${open}>${(wrapper as ElementWrapper).fragment.nodes.map(to_html).join('')}</${wrapper.node.name}>`;
} }
if (renderer.options.dev) { if (renderer.options.dev) {
@ -376,8 +376,8 @@ export default class ElementWrapper extends Wrapper {
get_claim_statement(nodes: string) { get_claim_statement(nodes: string) {
const attributes = this.node.attributes const attributes = this.node.attributes
.filter((attr: Node) => attr.type === 'Attribute') .filter((attr) => attr.type === 'Attribute')
.map((attr: Node) => `${quote_name_if_necessary(attr.name)}: true`) .map((attr) => `${quote_name_if_necessary(attr.name)}: true`)
.join(', '); .join(', ');
const name = this.node.namespace const name = this.node.namespace
@ -553,12 +553,13 @@ export default class ElementWrapper extends Wrapper {
} }
add_attributes(block: Block) { add_attributes(block: Block) {
// @ts-ignore todo:
if (this.node.attributes.find(attr => attr.type === 'Spread')) { if (this.node.attributes.find(attr => attr.type === 'Spread')) {
this.add_spread_attributes(block); this.add_spread_attributes(block);
return; return;
} }
this.attributes.forEach((attribute: Attribute) => { this.attributes.forEach((attribute) => {
if (attribute.node.name === 'class' && attribute.node.is_dynamic) { if (attribute.node.name === 'class' && attribute.node.is_dynamic) {
this.class_dependencies.push(...attribute.node.dependencies); this.class_dependencies.push(...attribute.node.dependencies);
} }
@ -814,27 +815,28 @@ export default class ElementWrapper extends Wrapper {
}); });
} }
add_css_class(class_name = this.component.stylesheet.id) { // todo: looks to be dead code copypasted from Element.add_css_class in src/compile/nodes/Element.ts
const class_attribute = this.attributes.find(a => a.name === 'class'); // add_css_class(class_name = this.component.stylesheet.id) {
if (class_attribute && !class_attribute.is_true) { // const class_attribute = this.attributes.find(a => a.name === 'class');
if (class_attribute.chunks.length === 1 && class_attribute.chunks[0].type === 'Text') { // if (class_attribute && !class_attribute.is_true) {
(class_attribute.chunks[0] as Text).data += ` ${class_name}`; // if (class_attribute.chunks.length === 1 && class_attribute.chunks[0].type === 'Text') {
} else { // (class_attribute.chunks[0] as Text).data += ` ${class_name}`;
(class_attribute.chunks as Node[]).push( // } else {
new Text(this.component, this, this.scope, { // (class_attribute.chunks as Node[]).push(
type: 'Text', // new Text(this.component, this, this.scope, {
data: ` ${class_name}` // type: 'Text',
}) // data: ` ${class_name}`
); // })
} // );
} else { // }
this.attributes.push( // } else {
new Attribute(this.component, this, this.scope, { // this.attributes.push(
type: 'Attribute', // new Attribute(this.component, this, this.scope, {
name: 'class', // type: 'Attribute',
value: [{ type: 'Text', data: class_name }] // name: 'class',
}) // value: [{ type: 'Text', data: class_name }]
); // })
} // );
} // }
// }
} }

@ -13,7 +13,7 @@ import Slot from './Slot';
import Text from './Text'; import Text from './Text';
import Title from './Title'; import Title from './Title';
import Window from './Window'; import Window from './Window';
import Node from '../../nodes/shared/Node'; import { INode } from '../../nodes/interfaces';
import TextWrapper from './Text'; import TextWrapper from './Text';
import Renderer from '../Renderer'; import Renderer from '../Renderer';
import Block from '../Block'; import Block from '../Block';
@ -49,7 +49,7 @@ export default class FragmentWrapper {
constructor( constructor(
renderer: Renderer, renderer: Renderer,
block: Block, block: Block,
nodes: Node[], nodes: INode[],
parent: Wrapper, parent: Wrapper,
strip_whitespace: boolean, strip_whitespace: boolean,
next_sibling: Wrapper next_sibling: Wrapper
@ -85,6 +85,7 @@ export default class FragmentWrapper {
// *unless* there is no whitespace between this node and its next sibling // *unless* there is no whitespace between this node and its next sibling
if (this.nodes.length === 0) { if (this.nodes.length === 0) {
const should_trim = ( const should_trim = (
// @ts-ignore todo:
next_sibling ? (next_sibling.node.type === 'Text' && /^\s/.test(next_sibling.data)) : !child.has_ancestor('EachBlock') next_sibling ? (next_sibling.node.type === 'Text' && /^\s/.test(next_sibling.data)) : !child.has_ancestor('EachBlock')
); );
@ -96,6 +97,7 @@ export default class FragmentWrapper {
// glue text nodes (which could e.g. be separated by comments) together // glue text nodes (which could e.g. be separated by comments) together
if (last_child && last_child.node.type === 'Text') { if (last_child && last_child.node.type === 'Text') {
// @ts-ignore todo: probably error, should it be last_child.node.data?
last_child.data = data + last_child.data; last_child.data = data + last_child.data;
continue; continue;
} }

@ -96,7 +96,7 @@ export default class IfBlockWrapper extends Wrapper {
if (branch.block.has_outros) has_outros = true; if (branch.block.has_outros) has_outros = true;
if (is_else_if(node.else)) { if (is_else_if(node.else)) {
create_branches(node.else.children[0]); create_branches(node.else.children[0] as IfBlock);
} else if (node.else) { } else if (node.else) {
const branch = new IfBlockBranch( const branch = new IfBlockBranch(
renderer, renderer,
@ -452,4 +452,4 @@ export default class IfBlockWrapper extends Wrapper {
block.builders.destroy.add_line(`${if_name}${name}.d(${parent_node ? '' : 'detaching'});`); block.builders.destroy.add_line(`${if_name}${name}.d(${parent_node ? '' : 'detaching'});`);
} }
} }

@ -142,7 +142,7 @@ export default class InlineComponentWrapper extends Wrapper {
if (this.fragment) { if (this.fragment) {
const default_slot = this.slots.get('default'); const default_slot = this.slots.get('default');
this.fragment.nodes.forEach((child: Wrapper) => { this.fragment.nodes.forEach((child) => {
child.render(default_slot.block, null, 'nodes'); child.render(default_slot.block, null, 'nodes');
}); });
} }
@ -505,4 +505,4 @@ export default class InlineComponentWrapper extends Wrapper {
); );
} }
} }
} }

@ -1,13 +1,14 @@
import Renderer from '../Renderer'; import Renderer from '../Renderer';
import Block from '../Block'; import Block from '../Block';
import Node from '../../nodes/shared/Node';
import Tag from './shared/Tag'; import Tag from './shared/Tag';
import Wrapper from './shared/Wrapper'; import Wrapper from './shared/Wrapper';
import MustacheTag from '../../nodes/MustacheTag';
import RawMustacheTag from '../../nodes/RawMustacheTag';
export default class MustacheTagWrapper extends Tag { export default class MustacheTagWrapper extends Tag {
var = 't'; var = 't';
constructor(renderer: Renderer, block: Block, parent: Wrapper, node: Node) { constructor(renderer: Renderer, block: Block, parent: Wrapper, node: MustacheTag | RawMustacheTag) {
super(renderer, block, parent, node); super(renderer, block, parent, node);
this.cannot_use_innerhtml(); this.cannot_use_innerhtml();
} }
@ -25,4 +26,4 @@ export default class MustacheTagWrapper extends Tag {
parent_node parent_node
); );
} }
} }

@ -1,9 +1,10 @@
import Renderer from '../Renderer'; import Renderer from '../Renderer';
import Block from '../Block'; import Block from '../Block';
import Node from '../../nodes/shared/Node';
import Tag from './shared/Tag'; import Tag from './shared/Tag';
import Wrapper from './shared/wrapper'; import Wrapper from './shared/wrapper';
import deindent from '../../utils/deindent'; import deindent from '../../utils/deindent';
import MustacheTag from '../../nodes/MustacheTag';
import RawMustacheTag from '../../nodes/RawMustacheTag';
export default class RawMustacheTagWrapper extends Tag { export default class RawMustacheTagWrapper extends Tag {
var = 'raw'; var = 'raw';
@ -12,7 +13,7 @@ export default class RawMustacheTagWrapper extends Tag {
renderer: Renderer, renderer: Renderer,
block: Block, block: Block,
parent: Wrapper, parent: Wrapper,
node: Node node: MustacheTag | RawMustacheTag
) { ) {
super(renderer, block, parent, node); super(renderer, block, parent, node);
this.cannot_use_innerhtml(); this.cannot_use_innerhtml();
@ -100,4 +101,4 @@ export default class RawMustacheTagWrapper extends Tag {
add_anchor_after(); add_anchor_after();
} }
} }
} }

@ -9,7 +9,6 @@ import add_to_set from '../../utils/add_to_set';
import get_slot_data from '../../utils/get_slot_data'; import get_slot_data from '../../utils/get_slot_data';
import { stringify_props } from '../../utils/stringify_props'; import { stringify_props } from '../../utils/stringify_props';
import Expression from '../../nodes/shared/Expression'; import Expression from '../../nodes/shared/Expression';
import Attribute from '../../nodes/Attribute';
export default class SlotWrapper extends Wrapper { export default class SlotWrapper extends Wrapper {
node: Slot; node: Slot;

@ -4,6 +4,7 @@ import Block from '../Block';
import Title from '../../nodes/Title'; import Title from '../../nodes/Title';
import { stringify } from '../../utils/stringify'; import { stringify } from '../../utils/stringify';
import add_to_set from '../../utils/add_to_set'; import add_to_set from '../../utils/add_to_set';
import Text from '../../nodes/Text';
export default class TitleWrapper extends Wrapper { export default class TitleWrapper extends Wrapper {
node: Title; node: Title;
@ -31,6 +32,7 @@ export default class TitleWrapper extends Wrapper {
// DRY it out if that's possible without introducing crazy indirection // DRY it out if that's possible without introducing crazy indirection
if (this.node.children.length === 1) { if (this.node.children.length === 1) {
// single {tag} — may be a non-string // single {tag} — may be a non-string
// @ts-ignore todo: check this
const { expression } = this.node.children[0]; const { expression } = this.node.children[0];
value = expression.render(block); value = expression.render(block);
add_to_set(all_dependencies, expression.dependencies); add_to_set(all_dependencies, expression.dependencies);
@ -39,16 +41,18 @@ export default class TitleWrapper extends Wrapper {
value = value =
(this.node.children[0].type === 'Text' ? '' : `"" + `) + (this.node.children[0].type === 'Text' ? '' : `"" + `) +
this.node.children this.node.children
.map((chunk: Node) => { .map((chunk) => {
if (chunk.type === 'Text') { if (chunk.type === 'Text') {
return stringify(chunk.data); return stringify(chunk.data);
} else { } else {
// @ts-ignore todo: check this
const snippet = chunk.expression.render(block); const snippet = chunk.expression.render(block);
// @ts-ignore todo: check this
chunk.expression.dependencies.forEach(d => { chunk.expression.dependencies.forEach(d => {
all_dependencies.add(d); all_dependencies.add(d);
}); });
// @ts-ignore todo: check this
return chunk.expression.get_precedence() <= 13 ? `(${snippet})` : snippet; return chunk.expression.get_precedence() <= 13 ? `(${snippet})` : snippet;
} }
}) })
@ -88,8 +92,8 @@ export default class TitleWrapper extends Wrapper {
); );
} }
} else { } else {
const value = stringify(this.node.children[0].data); const value = stringify((this.node.children[0] as Text).data);
block.builders.hydrate.add_line(`document.title = ${value};`); block.builders.hydrate.add_line(`document.title = ${value};`);
} }
} }
} }

@ -6,6 +6,7 @@ import deindent from '../../utils/deindent';
import add_event_handlers from './shared/add_event_handlers'; import add_event_handlers from './shared/add_event_handlers';
import Window from '../../nodes/Window'; import Window from '../../nodes/Window';
import add_actions from './shared/add_actions'; import add_actions from './shared/add_actions';
import { INode } from '../../nodes/interfaces';
const associated_events = { const associated_events = {
innerWidth: 'resize', innerWidth: 'resize',
@ -33,7 +34,7 @@ const readonly = new Set([
export default class WindowWrapper extends Wrapper { export default class WindowWrapper extends Wrapper {
node: Window; node: Window;
constructor(renderer: Renderer, block: Block, parent: Wrapper, node: Node) { constructor(renderer: Renderer, block: Block, parent: Wrapper, node: INode) {
super(renderer, block, parent, node); super(renderer, block, parent, node);
} }

@ -1,11 +1,11 @@
import Renderer from '../../Renderer'; import Renderer from '../../Renderer';
import Node from '../../../nodes/shared/Node';
import Block from '../../Block'; import Block from '../../Block';
import { INode } from '../../../nodes/interfaces';
export default class Wrapper { export default class Wrapper {
renderer: Renderer; renderer: Renderer;
parent: Wrapper; parent: Wrapper;
node: Node; node: INode;
prev: Wrapper | null; prev: Wrapper | null;
next: Wrapper | null; next: Wrapper | null;
@ -17,7 +17,7 @@ export default class Wrapper {
renderer: Renderer, renderer: Renderer,
block: Block, block: Block,
parent: Wrapper, parent: Wrapper,
node: Node node: INode
) { ) {
this.node = node; this.node = node;
@ -75,4 +75,8 @@ export default class Wrapper {
this.node.type === 'MustacheTag' this.node.type === 'MustacheTag'
); );
} }
}
render(block: Block, parent_node: string, parent_nodes: string){
throw Error('Wrapper class is not renderable');
}
}

@ -1,8 +1,9 @@
import Component from '../../../Component'; import Component from '../../../Component';
import { Node } from '../../../../interfaces'; import { Node } from '../../../../interfaces';
import { INode } from '../../../nodes/interfaces';
export default function create_debugging_comment( export default function create_debugging_comment(
node: Node, node: INode,
component: Component component: Component
) { ) {
const { locate, source } = component; const { locate, source } = component;
@ -19,6 +20,7 @@ export default function create_debugging_comment(
d = node.children.length ? node.children[0].start : node.start; d = node.children.length ? node.children[0].start : node.start;
while (source[d - 1] !== '>') d -= 1; while (source[d - 1] !== '>') d -= 1;
} else { } else {
// @ts-ignore
d = node.expression ? node.expression.node.end : c; d = node.expression ? node.expression.node.end : c;
while (source[d] !== '}') d += 1; while (source[d] !== '}') d += 1;
while (source[d] === '}') d += 1; while (source[d] === '}') d += 1;

@ -74,6 +74,7 @@ export default async function preprocess(
preprocessor: PreprocessorGroup | PreprocessorGroup[], preprocessor: PreprocessorGroup | PreprocessorGroup[],
options?: { filename?: string } options?: { filename?: string }
) { ) {
// @ts-ignore todo: doublecheck
const filename = (options && options.filename) || preprocessor.filename; // legacy const filename = (options && options.filename) || preprocessor.filename; // legacy
const dependencies = []; const dependencies = [];

Loading…
Cancel
Save