Convert src/compiler/compile/nodes/Attribute.ts to JavaScript

pull/8569/head
Simon Holthausen 3 years ago
parent ba6bc462ae
commit a53413225b

@ -1,118 +1,136 @@
import { string_literal } from '../utils/stringify'; import { string_literal } from '../utils/stringify.js';
import add_to_set from '../utils/add_to_set'; import add_to_set from '../utils/add_to_set.js';
import Component from '../Component'; import Node from './shared/Node.js';
import Node from './shared/Node'; import Expression from './shared/Expression.js';
import Element from './Element';
import Text from './Text';
import Expression from './shared/Expression';
import TemplateScope from './shared/TemplateScope';
import { x } from 'code-red'; import { x } from 'code-red';
import { TemplateNode } from '../../interfaces';
/** @extends Node */
export default class Attribute extends Node { export default class Attribute extends Node {
type: 'Attribute' | 'Spread';
start: number; /** @type {'Attribute' | 'Spread'} */
end: number; type;
scope: TemplateScope;
/** @type {number} */
component: Component; start;
parent: Element;
name: string; /** @type {number} */
is_spread: boolean; end;
is_true: boolean;
is_static: boolean; /** @type {import('./shared/TemplateScope.js').default} */
expression?: Expression; scope;
chunks: Array<Text | Expression>;
dependencies: Set<string>; /** @type {import('../Component.js').default} */
component;
constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) {
/** @type {import('./Element.js').default} */
parent;
/** @type {string} */
name;
/** @type {boolean} */
is_spread;
/** @type {boolean} */
is_true;
/** @type {boolean} */
is_static;
/** @type {import('./shared/Expression.js').default} */
expression;
/** @type {Array<import('./Text.js').default | import('./shared/Expression.js').default>} */
chunks;
/** @type {Set<string>} */
dependencies;
/**
* @param {import('../Component.js').default} component *
* @param {import('./shared/Node.js').default} parent *
* @param {import('./shared/TemplateScope.js').default} scope *
* @param {import('../../interfaces.js').TemplateNode} info undefined
*/
constructor(component, parent, scope, info) {
super(component, parent, scope, info); super(component, parent, scope, info);
this.scope = scope; this.scope = scope;
if (info.type === 'Spread') { if (info.type === 'Spread') {
this.name = null; this.name = null;
this.is_spread = true; this.is_spread = true;
this.is_true = false; this.is_true = false;
this.expression = new Expression(component, this, scope, info.expression); this.expression = new Expression(component, this, scope, info.expression);
this.dependencies = this.expression.dependencies; this.dependencies = this.expression.dependencies;
this.chunks = null; this.chunks = null;
this.is_static = false; this.is_static = false;
} else { }
else {
this.name = info.name; this.name = info.name;
this.is_true = info.value === true; this.is_true = info.value === true;
this.is_static = true; this.is_static = true;
this.dependencies = new Set(); this.dependencies = new Set();
this.chunks = this.is_true this.chunks = this.is_true
? [] ? []
: info.value.map((node) => { : info.value.map(/** @param {any} node */ (node) => {
if (node.type === 'Text') return node; if (node.type === 'Text')
return node;
this.is_static = false; this.is_static = false;
const expression = new Expression(component, this, scope, node.expression); const expression = new Expression(component, this, scope, node.expression);
add_to_set(this.dependencies, expression.dependencies); add_to_set(this.dependencies, expression.dependencies);
return expression; return expression;
}); });
} }
if (this.dependencies.size > 0) { if (this.dependencies.size > 0) {
parent.cannot_use_innerhtml(); parent.cannot_use_innerhtml();
parent.not_static_content(); parent.not_static_content();
} }
} }
get_dependencies() { get_dependencies() {
if (this.is_spread) return this.expression.dynamic_dependencies(); if (this.is_spread)
return this.expression.dynamic_dependencies();
const dependencies: Set<string> = new Set(); /** @type {Set<string>} */
this.chunks.forEach((chunk) => { const dependencies = new Set();
this.chunks.forEach(/** @param {any} chunk */ (chunk) => {
if (chunk.type === 'Expression') { if (chunk.type === 'Expression') {
add_to_set(dependencies, chunk.dynamic_dependencies()); add_to_set(dependencies, chunk.dynamic_dependencies());
} }
}); });
return Array.from(dependencies); return Array.from(dependencies);
} }
/** @param {any} block */
get_value(block) { get_value(block) {
if (this.is_true) return x`true`; if (this.is_true)
if (this.chunks.length === 0) return x`""`; return x `true`;
if (this.chunks.length === 0)
return x `""`;
if (this.chunks.length === 1) { if (this.chunks.length === 1) {
return this.chunks[0].type === 'Text' return this.chunks[0].type === 'Text'
? string_literal((this.chunks[0] as Text).data) ? string_literal(( /** @type {import('./Text.js').default} */(this.chunks[0])).data)
: (this.chunks[0] as Expression).manipulate(block); : ( /** @type {import('./shared/Expression.js').default} */(this.chunks[0])).manipulate(block);
} }
let expression = this.chunks let expression = this.chunks
.map((chunk) => .map(/** @param {any} chunk */ (chunk) => chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block))
chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block) .reduce(/**
) * @param {any} lhs
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`); * @param {any} rhs
*/ (lhs, rhs) => x `${lhs} + ${rhs}`);
if (this.chunks[0].type !== 'Text') { if (this.chunks[0].type !== 'Text') {
expression = x `"" + ${expression}`; expression = x `"" + ${expression}`;
} }
return expression; return expression;
} }
get_static_value() { get_static_value() {
if (!this.is_static) return null; if (!this.is_static)
return null;
return this.is_true return this.is_true
? true ? true
: this.chunks[0] : this.chunks[0]
? // method should be called only when `is_static = true` ? // method should be called only when `is_static = true`
(this.chunks[0] as Text).data ( /** @type {import('./Text.js').default} */(this.chunks[0])).data
: ''; : '';
} }
should_cache() { should_cache() {
return this.is_static return this.is_static
? false ? false
@ -122,3 +140,7 @@ export default class Attribute extends Node {
: true; : true;
} }
} }

Loading…
Cancel
Save