typescript stuff

pull/3539/head
Richard Harris 6 years ago
parent 7efcca82cc
commit 682707ff60

@ -4,7 +4,7 @@ import map_children from './shared/map_children';
import TemplateScope from './shared/TemplateScope'; import TemplateScope from './shared/TemplateScope';
import AbstractBlock from './shared/AbstractBlock'; import AbstractBlock from './shared/AbstractBlock';
import Element from './Element'; import Element from './Element';
import { x, p } from 'code-red'; import { x } from 'code-red';
import { Node, Identifier, RestElement } from 'estree'; import { Node, Identifier, RestElement } from 'estree';
interface Context { interface Context {
@ -39,7 +39,7 @@ function unpack_destructuring(contexts: Context[], node: Node, modifier: (node:
argument: property.key as Identifier argument: property.key as Identifier
}; };
node.properties[i] = replacement; node.properties[i] = replacement as any;
unpack_destructuring( unpack_destructuring(
contexts, contexts,

@ -17,6 +17,7 @@ export interface BlockOptions {
snippet: Node; snippet: Node;
store: string; store: string;
tail: Node; tail: Node;
modifier: (node: Node) => Node;
}>; }>;
dependencies?: Set<string>; dependencies?: Set<string>;
} }
@ -41,6 +42,7 @@ export default class Block {
snippet: Node; snippet: Node;
store: string; store: string;
tail: Node; tail: Node;
modifier: (node: Node) => Node;
}>; }>;
chunks: { chunks: {

@ -1,5 +1,4 @@
import { b, x, p } from 'code-red'; import { b, x, p } from 'code-red';
import { escape } from '../utils/stringify';
import Component from '../Component'; import Component from '../Component';
import Renderer from './Renderer'; import Renderer from './Renderer';
import { CompileOptions } from '../../interfaces'; import { CompileOptions } from '../../interfaces';
@ -8,7 +7,7 @@ import add_to_set from '../utils/add_to_set';
import { extract_names } from '../utils/scope'; import { extract_names } from '../utils/scope';
import { invalidate } from '../utils/invalidate'; import { invalidate } from '../utils/invalidate';
import Block from './Block'; import Block from './Block';
import { ClassDeclaration, FunctionExpression, Node, LabeledStatement, Statement } from 'estree'; import { ClassDeclaration, FunctionExpression, Node, Statement } from 'estree';
export default function dom( export default function dom(
component: Component, component: Component,

@ -7,7 +7,7 @@ import Renderer from '../../Renderer';
import flatten_reference from '../../../utils/flatten_reference'; import flatten_reference from '../../../utils/flatten_reference';
import EachBlock from '../../../nodes/EachBlock'; import EachBlock from '../../../nodes/EachBlock';
import { changed } from '../shared/changed'; import { changed } from '../shared/changed';
import { Node, Identifier, MemberExpression } from 'estree'; import { Node, Identifier } from 'estree';
export default class BindingWrapper { export default class BindingWrapper {
node: Binding; node: Binding;

@ -15,7 +15,7 @@ import TemplateScope from '../../../nodes/shared/TemplateScope';
import is_dynamic from '../shared/is_dynamic'; import is_dynamic from '../shared/is_dynamic';
import bind_this from '../shared/bind_this'; import bind_this from '../shared/bind_this';
import { changed } from '../shared/changed'; import { changed } from '../shared/changed';
import { Node, Identifier, Expression, ObjectExpression, Property } from 'estree'; import { Node, Identifier, ObjectExpression } from 'estree';
export default class InlineComponentWrapper extends Wrapper { export default class InlineComponentWrapper extends Wrapper {
var: Identifier; var: Identifier;

@ -66,7 +66,7 @@ export default class SlotWrapper extends Wrapper {
get_slot_changes = renderer.component.get_unique_name(`get_${sanitize(slot_name)}_slot_changes`); get_slot_changes = renderer.component.get_unique_name(`get_${sanitize(slot_name)}_slot_changes`);
get_slot_context = renderer.component.get_unique_name(`get_${sanitize(slot_name)}_slot_context`); get_slot_context = renderer.component.get_unique_name(`get_${sanitize(slot_name)}_slot_context`);
const context = get_slot_data(this.node.values, false); const context = get_slot_data(this.node.values);
const changes = x`{}` as ObjectExpression; const changes = x`{}` as ObjectExpression;
const dependencies = new Set(); const dependencies = new Set();

@ -1,7 +1,7 @@
import Renderer, { RenderOptions } from '../Renderer'; import Renderer, { RenderOptions } from '../Renderer';
import Comment from '../../nodes/Comment'; import Comment from '../../nodes/Comment';
export default function(node: Comment, renderer: Renderer, options: RenderOptions) { export default function(_node: Comment, _renderer: Renderer, _options: RenderOptions) {
// TODO preserve comments // TODO preserve comments
// if (options.preserveComments) { // if (options.preserveComments) {

@ -1,12 +1,12 @@
import { is_void, quote_prop_if_necessary } from '../../../utils/names'; import { is_void } from '../../../utils/names';
import Attribute from '../../nodes/Attribute'; import Attribute from '../../nodes/Attribute';
import Class from '../../nodes/Class'; import Class from '../../nodes/Class';
import { get_attribute_value, get_class_attribute_value } from './shared/get_attribute_value'; import { get_attribute_value, get_class_attribute_value } from './shared/get_attribute_value';
import { get_slot_scope } from './shared/get_slot_scope'; import { get_slot_scope } from './shared/get_slot_scope';
import Renderer, { RenderOptions } from '../Renderer'; import Renderer, { RenderOptions } from '../Renderer';
import Element from '../../nodes/Element'; import Element from '../../nodes/Element';
import Text from '../../nodes/Text';
import { x } from 'code-red'; import { x } from 'code-red';
import Expression from '../../nodes/shared/Expression';
// source: https://gist.github.com/ArjanSchouten/0b8574a6ad7f5065a5e7 // source: https://gist.github.com/ArjanSchouten/0b8574a6ad7f5065a5e7
const boolean_attributes = new Set([ const boolean_attributes = new Set([
@ -54,7 +54,6 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
}) { }) {
// awkward special case // awkward special case
let node_contents; let node_contents;
let value;
const contenteditable = ( const contenteditable = (
node.name !== 'textarea' && node.name !== 'textarea' &&
@ -112,12 +111,12 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
attribute.chunks[0].type !== 'Text' attribute.chunks[0].type !== 'Text'
) { ) {
// a boolean attribute with one non-Text chunk // a boolean attribute with one non-Text chunk
args.push(x`{ ${attribute.name}: ${attribute.chunks[0].node} }`); args.push(x`{ ${attribute.name}: ${(attribute.chunks[0] as Expression).node} }`);
} else if (attribute.name === 'class' && class_expression) { } else if (attribute.name === 'class' && class_expression) {
// Add class expression // Add class expression
args.push(x`{ ${attribute.name}: [${get_class_attribute_value(attribute)}, ${class_expression}].join(' ').trim() }`); args.push(x`{ ${attribute.name}: [${get_class_attribute_value(attribute)}, ${class_expression}].join(' ').trim() }`);
} else { } else {
args.push(x`{ ${attribute.name}: ${attribute.name === 'class' ? get_class_attribute_value(attribute) : get_attribute_value(attribute, true)} }`); args.push(x`{ ${attribute.name}: ${attribute.name === 'class' ? get_class_attribute_value(attribute) : get_attribute_value(attribute)} }`);
} }
} }
}); });
@ -138,7 +137,7 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
) { ) {
// a boolean attribute with one non-Text chunk // a boolean attribute with one non-Text chunk
renderer.add_string(` `); renderer.add_string(` `);
renderer.add_expression(x`${attribute.chunks[0].node} ? "${attribute.name}" : ""`); renderer.add_expression(x`${(attribute.chunks[0] as Expression).node} ? "${attribute.name}" : ""`);
} else if (attribute.name === 'class' && class_expression) { } else if (attribute.name === 'class' && class_expression) {
add_class_attribute = false; add_class_attribute = false;
renderer.add_string(` class="`); renderer.add_string(` class="`);
@ -146,7 +145,7 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
renderer.add_string(`"`); renderer.add_string(`"`);
} else if (attribute.chunks.length === 1 && attribute.chunks[0].type !== 'Text') { } else if (attribute.chunks.length === 1 && attribute.chunks[0].type !== 'Text') {
const { name } = attribute; const { name } = attribute;
const snippet = attribute.chunks[0].node; const snippet = (attribute.chunks[0] as Expression).node;
renderer.add_expression(x`@add_attribute("${name}", ${snippet}, ${boolean_attributes.has(name) ? 1 : 0})`); renderer.add_expression(x`@add_attribute("${name}", ${snippet}, ${boolean_attributes.has(name) ? 1 : 0})`);
} else { } else {
renderer.add_string(` ${attribute.name}="`); renderer.add_string(` ${attribute.name}="`);
@ -167,7 +166,9 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
// TODO server-render group bindings // TODO server-render group bindings
} else if (contenteditable && (name === 'textContent' || name === 'innerHTML')) { } else if (contenteditable && (name === 'textContent' || name === 'innerHTML')) {
node_contents = expression.node; node_contents = expression.node;
value = name === 'textContent' ? x`@escape($$value)` : x`$$value`;
// TODO where was this used?
// value = name === 'textContent' ? x`@escape($$value)` : x`$$value`;
} else if (binding.name === 'value' && node.name === 'textarea') { } else if (binding.name === 'value' && node.name === 'textarea') {
const snippet = expression.node; const snippet = expression.node;
node_contents = x`${snippet} || ""`; node_contents = x`${snippet} || ""`;

@ -1,5 +1,4 @@
import { string_literal } from '../../utils/stringify'; import { string_literal } from '../../utils/stringify';
import { quote_name_if_necessary } from '../../../utils/names';
import Renderer, { RenderOptions } from '../Renderer'; import Renderer, { RenderOptions } from '../Renderer';
import { get_slot_scope } from './shared/get_slot_scope'; import { get_slot_scope } from './shared/get_slot_scope';
import InlineComponent from '../../nodes/InlineComponent'; import InlineComponent from '../../nodes/InlineComponent';

@ -4,7 +4,7 @@ import Slot from '../../nodes/Slot';
import { x } from 'code-red'; import { x } from 'code-red';
export default function(node: Slot, renderer: Renderer, options: RenderOptions) { export default function(node: Slot, renderer: Renderer, options: RenderOptions) {
const slot_data = get_slot_data(node.values, true); const slot_data = get_slot_data(node.values);
renderer.push(); renderer.push();
renderer.render(node.children, options); renderer.render(node.children, options);

@ -6,6 +6,7 @@ import Renderer from './Renderer';
import { INode as TemplateNode } from '../nodes/interfaces'; // TODO import { INode as TemplateNode } from '../nodes/interfaces'; // TODO
import Text from '../nodes/Text'; import Text from '../nodes/Text';
import { extract_names } from '../utils/scope'; import { extract_names } from '../utils/scope';
import { LabeledStatement, Statement, ExpressionStatement, AssignmentExpression } from 'estree';
export default function ssr( export default function ssr(
component: Component, component: Component,
@ -72,7 +73,9 @@ export default function ssr(
: []; : [];
const reactive_declarations = component.reactive_declarations.map(d => { const reactive_declarations = component.reactive_declarations.map(d => {
let statement = b`${d.node.body}`; const body: Statement = (d.node as LabeledStatement).body;
let statement = b`${body}`;
if (d.declaration) { if (d.declaration) {
const declared = extract_names(d.declaration); const declared = extract_names(d.declaration);
@ -90,12 +93,14 @@ export default function ssr(
declared.length > injected.length declared.length > injected.length
); );
const { left, right } = (body as ExpressionStatement).expression as AssignmentExpression;
statement = separate statement = separate
? b` ? b`
${injected.map(name => b`let ${name};`)} ${injected.map(name => b`let ${name};`)}
${statement}` ${statement}`
: b` : b`
let ${d.node.body.expression.left} = ${d.node.body.expression.right}`; let ${left} = ${right}`;
} }
} }

@ -2,7 +2,7 @@ import Attribute from '../nodes/Attribute';
import { p, x } from 'code-red'; import { p, x } from 'code-red';
import { string_literal } from './stringify'; import { string_literal } from './stringify';
export default function get_slot_data(values: Map<string, Attribute>, is_ssr: boolean) { export default function get_slot_data(values: Map<string, Attribute>) {
return { return {
type: 'ObjectExpression', type: 'ObjectExpression',
properties: Array.from(values.values()) properties: Array.from(values.values())

@ -117,16 +117,6 @@ export function is_valid(str: string): boolean {
return true; return true;
} }
export function quote_name_if_necessary(name: string) {
if (!is_valid(name)) return `"${name}"`;
return name;
}
export function quote_prop_if_necessary(name: string) {
if (!is_valid(name)) return `["${name}"]`;
return `.${name}`;
}
export function sanitize(name: string) { export function sanitize(name: string) {
return name return name
.replace(/[^a-zA-Z0-9_]+/g, '_') .replace(/[^a-zA-Z0-9_]+/g, '_')

Loading…
Cancel
Save