fix compile/render-ssr typings

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

@ -12,6 +12,7 @@ import Tag from './handlers/Tag';
import Text from './handlers/Text';
import Title from './handlers/Title';
import { AppendTarget, CompileOptions } from '../../interfaces';
import { INode } from '../nodes/interfaces';
type Handler = (node: any, renderer: Renderer, options: CompileOptions) => void;
@ -36,6 +37,10 @@ const handlers: Record<string, Handler> = {
Window: noop
};
export interface RenderOptions extends CompileOptions{
locate: (c: number) => { line: number; column: number; };
};
export default class Renderer {
has_bindings = false;
code = '';
@ -51,7 +56,7 @@ export default class Renderer {
}
}
render(nodes, options) {
render(nodes: INode[], options: RenderOptions) {
nodes.forEach(node => {
const handler = handlers[node.type];

@ -1,8 +1,8 @@
import Renderer from '../Renderer';
import { CompileOptions } from '../../../interfaces';
import Renderer, { RenderOptions } from '../Renderer';
import { snip } from '../../utils/snip';
import AwaitBlock from '../../nodes/AwaitBlock';
export default function(node, renderer: Renderer, options: CompileOptions) {
export default function(node: AwaitBlock, renderer: Renderer, options: RenderOptions) {
renderer.append('${(function(__value) { if(@is_promise(__value)) return `');
renderer.render(node.pending.children, options);

@ -1,7 +1,7 @@
import Renderer from '../Renderer';
import { CompileOptions } from '../../../interfaces';
import Renderer, { RenderOptions } from '../Renderer';
import Comment from '../../nodes/Comment';
export default function(node, renderer: Renderer, options: CompileOptions) {
export default function(node: Comment, renderer: Renderer, options: RenderOptions) {
if (options.preserveComments) {
renderer.append(`<!--${node.data}-->`);
}

@ -1,9 +1,10 @@
import { stringify } from '../../utils/stringify';
export default function(node, renderer, options) {
import DebugTag from '../../nodes/DebugTag';
import Renderer, { RenderOptions } from '../Renderer';
export default function(node: DebugTag, renderer: Renderer, options: RenderOptions) {
if (!options.dev) return;
const filename = options.file || null;
const filename = options.filename || null;
const { line, column } = options.locate(node.start + 1);
const obj = node.expressions.length === 0

@ -1,6 +1,8 @@
import { snip } from '../../utils/snip';
import Renderer, { RenderOptions } from '../Renderer';
import EachBlock from '../../nodes/EachBlock';
export default function(node, renderer, options) {
export default function(node: EachBlock, renderer: Renderer, options: RenderOptions) {
const snippet = snip(node.expression);
const { start, end } = node.context_node;

@ -5,6 +5,9 @@ import Node from '../../nodes/shared/Node';
import { snip } from '../../utils/snip';
import { stringify_attribute } from '../../utils/stringify_attribute';
import { get_slot_scope } from './shared/get_slot_scope';
import Renderer, { RenderOptions } from '../Renderer';
import Element from '../../nodes/Element';
import Text from '../../nodes/Text';
// source: https://gist.github.com/ArjanSchouten/0b8574a6ad7f5065a5e7
const boolean_attributes = new Set([
@ -47,15 +50,17 @@ const boolean_attributes = new Set([
'translate'
]);
export default function(node, renderer, options) {
export default function(node: Element, renderer: Renderer, options: RenderOptions & {
slot_scopes: Map<any, any>;
}) {
let opening_tag = `<${node.name}`;
let textarea_contents; // awkward special case
const slot = node.get_static_attribute_value('slot');
const component = node.find_nearest(/InlineComponent/);
if (slot && component) {
const slot = node.attributes.find((attribute: Node) => attribute.name === 'slot');
const slot_name = slot.chunks[0].data;
const slot = node.attributes.find((attribute) => attribute.name === 'slot');
const slot_name = (slot.chunks[0] as Text).data;
const target = renderer.targets[renderer.targets.length - 1];
target.slot_stack.push(slot_name);
target.slots[slot_name] = '';

@ -1,4 +1,7 @@
export default function(node, renderer, options) {
import Renderer, { RenderOptions } from '../Renderer';
import Head from '../../nodes/Head';
export default function(node: Head, renderer: Renderer, options: RenderOptions) {
renderer.append('${($$result.head += `');
renderer.render(node.children, options);

@ -1,5 +1,7 @@
import { snip } from '../../utils/snip';
import Renderer, { RenderOptions } from '../Renderer';
import RawMustacheTag from '../../nodes/RawMustacheTag';
export default function(node, renderer, options) {
export default function(node: RawMustacheTag, renderer: Renderer, options: RenderOptions) {
renderer.append('${' + snip(node.expression) + '}');
}

@ -1,6 +1,7 @@
import { snip } from '../../utils/snip';
export default function(node, renderer, options) {
import IfBlock from '../../nodes/IfBlock';
import Renderer, { RenderOptions } from '../Renderer';
export default function(node: IfBlock, renderer: Renderer, options: RenderOptions) {
const snippet = snip(node.expression);
renderer.append('${ ' + snippet + ' ? `');

@ -1,14 +1,17 @@
import { escape, escape_template, stringify } from '../../utils/stringify';
import { quote_name_if_necessary } from '../../../utils/names';
import { snip } from '../../utils/snip';
import Renderer from '../Renderer';
import Renderer, { RenderOptions } from '../Renderer';
import { stringify_props } from '../../utils/stringify_props';
import { get_slot_scope } from './shared/get_slot_scope';
import { AppendTarget } from '../../../interfaces';
import InlineComponent from '../../nodes/InlineComponent';
import { INode } from '../../nodes/interfaces';
import Text from '../../nodes/Text';
function stringify_attribute(chunk: Node) {
function stringify_attribute(chunk: INode) {
if (chunk.type === 'Text') {
return escape_template(escape(chunk.data));
return escape_template(escape((chunk as Text).data));
}
return '${@escape(' + snip(chunk) + ')}';
@ -30,7 +33,7 @@ function get_attribute_value(attribute) {
return '`' + attribute.chunks.map(stringify_attribute).join('') + '`';
}
export default function(node, renderer: Renderer, options) {
export default function(node: InlineComponent, renderer: Renderer, options: RenderOptions) {
const binding_props = [];
const binding_fns = [];

@ -1,7 +1,9 @@
import { quote_prop_if_necessary } from '../../../utils/names';
import get_slot_data from '../../utils/get_slot_data';
import Renderer, { RenderOptions } from '../Renderer';
import Slot from '../../nodes/Slot';
export default function(node, renderer, options) {
export default function(node: Slot, renderer: Renderer, options: RenderOptions) {
const prop = quote_prop_if_necessary(node.slot_name);
const slot_data = get_slot_data(node.values, true);

@ -1,6 +1,6 @@
import { snip } from '../../utils/snip';
export default function(node, renderer, options) {
import Renderer, { RenderOptions } from '../Renderer';
export default function(node, renderer: Renderer, options: RenderOptions) {
const snippet = snip(node.expression);
renderer.append(

@ -1,11 +1,14 @@
import { escape_html, escape_template, escape } from '../../utils/stringify';
import Renderer, { RenderOptions } from '../Renderer';
import Text from '../../nodes/Text';
import Element from '../../nodes/Element';
export default function(node, renderer, options) {
export default function(node: Text, renderer: Renderer, options: RenderOptions) {
let text = node.data;
if (
!node.parent ||
node.parent.type !== 'Element' ||
(node.parent.name !== 'script' && node.parent.name !== 'style')
((node.parent as Element).name !== 'script' && (node.parent as Element).name !== 'style')
) {
// unless this Text node is inside a <script> or <style> element, escape &,<,>
text = escape_html(text);

@ -1,4 +1,7 @@
export default function(node, renderer, options) {
import Renderer, { RenderOptions } from '../Renderer';
import Title from '../../nodes/Title';
export default function(node: Title, renderer: Renderer, options: RenderOptions) {
renderer.append(`<title>`);
renderer.render(node.children, options);

@ -4,6 +4,8 @@ import { CompileOptions } from '../../interfaces';
import { stringify } from '../utils/stringify';
import Renderer from './Renderer';
import { extract_names } from '../utils/scope';
import { INode } from '../nodes/interfaces';
import Text from '../nodes/Text';
export default function ssr(
component: Component,
@ -151,10 +153,10 @@ export default function ssr(
`).trim();
}
function trim(nodes) {
function trim(nodes: INode[]) {
let start = 0;
for (; start < nodes.length; start += 1) {
const node = nodes[start];
const node = nodes[start] as Text;
if (node.type !== 'Text') break;
node.data = node.data.replace(/^\s+/, '');
@ -163,7 +165,7 @@ function trim(nodes) {
let end = nodes.length;
for (; end > start; end -= 1) {
const node = nodes[end - 1];
const node = nodes[end - 1] as Text;
if (node.type !== 'Text') break;
node.data = node.data.replace(/\s+$/, '');

Loading…
Cancel
Save