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

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

@ -1,124 +1,146 @@
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) {
super(component, parent, scope, info); /** @type {import('./Element.js').default} */
this.scope = scope; parent;
if (info.type === 'Spread') { /** @type {string} */
this.name = null; name;
this.is_spread = true;
this.is_true = false; /** @type {boolean} */
is_spread;
this.expression = new Expression(component, this, scope, info.expression);
this.dependencies = this.expression.dependencies; /** @type {boolean} */
this.chunks = null; is_true;
this.is_static = false; /** @type {boolean} */
} else { is_static;
this.name = info.name;
this.is_true = info.value === true; /** @type {import('./shared/Expression.js').default} */
this.is_static = true; expression;
this.dependencies = new Set(); /** @type {Array<import('./Text.js').default | import('./shared/Expression.js').default>} */
chunks;
this.chunks = this.is_true
? [] /** @type {Set<string>} */
: info.value.map((node) => { dependencies;
if (node.type === 'Text') return node;
/**
this.is_static = false; * @param {import('../Component.js').default} component *
* @param {import('./shared/Node.js').default} parent *
const expression = new Expression(component, this, scope, node.expression); * @param {import('./shared/TemplateScope.js').default} scope *
* @param {import('../../interfaces.js').TemplateNode} info undefined
add_to_set(this.dependencies, expression.dependencies); */
return expression; constructor(component, parent, scope, info) {
}); super(component, parent, scope, info);
} this.scope = scope;
if (info.type === 'Spread') {
if (this.dependencies.size > 0) { this.name = null;
parent.cannot_use_innerhtml(); this.is_spread = true;
parent.not_static_content(); this.is_true = false;
} this.expression = new Expression(component, this, scope, info.expression);
} this.dependencies = this.expression.dependencies;
this.chunks = null;
get_dependencies() { this.is_static = false;
if (this.is_spread) return this.expression.dynamic_dependencies(); }
else {
const dependencies: Set<string> = new Set(); this.name = info.name;
this.chunks.forEach((chunk) => { this.is_true = info.value === true;
if (chunk.type === 'Expression') { this.is_static = true;
add_to_set(dependencies, chunk.dynamic_dependencies()); this.dependencies = new Set();
} this.chunks = this.is_true
}); ? []
: info.value.map(/** @param {any} node */ (node) => {
return Array.from(dependencies); if (node.type === 'Text')
} return node;
this.is_static = false;
get_value(block) { const expression = new Expression(component, this, scope, node.expression);
if (this.is_true) return x`true`; add_to_set(this.dependencies, expression.dependencies);
if (this.chunks.length === 0) return x`""`; return expression;
});
if (this.chunks.length === 1) { }
return this.chunks[0].type === 'Text' if (this.dependencies.size > 0) {
? string_literal((this.chunks[0] as Text).data) parent.cannot_use_innerhtml();
: (this.chunks[0] as Expression).manipulate(block); parent.not_static_content();
} }
}
let expression = this.chunks get_dependencies() {
.map((chunk) => if (this.is_spread)
chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block) return this.expression.dynamic_dependencies();
)
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`); /** @type {Set<string>} */
const dependencies = new Set();
if (this.chunks[0].type !== 'Text') { this.chunks.forEach(/** @param {any} chunk */ (chunk) => {
expression = x`"" + ${expression}`; if (chunk.type === 'Expression') {
} add_to_set(dependencies, chunk.dynamic_dependencies());
}
return expression; });
} return Array.from(dependencies);
}
get_static_value() {
if (!this.is_static) return null; /** @param {any} block */
get_value(block) {
return this.is_true if (this.is_true)
? true return x `true`;
: this.chunks[0] if (this.chunks.length === 0)
? // method should be called only when `is_static = true` return x `""`;
(this.chunks[0] as Text).data if (this.chunks.length === 1) {
: ''; return this.chunks[0].type === 'Text'
} ? string_literal(( /** @type {import('./Text.js').default} */(this.chunks[0])).data)
: ( /** @type {import('./shared/Expression.js').default} */(this.chunks[0])).manipulate(block);
should_cache() { }
return this.is_static let expression = this.chunks
? false .map(/** @param {any} chunk */ (chunk) => chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block))
: this.chunks.length === 1 .reduce(/**
? // @ts-ignore todo: probably error * @param {any} lhs
this.chunks[0].node.type !== 'Identifier' || this.scope.names.has(this.chunks[0].node.name) * @param {any} rhs
: true; */ (lhs, rhs) => x `${lhs} + ${rhs}`);
} if (this.chunks[0].type !== 'Text') {
expression = x `"" + ${expression}`;
}
return expression;
}
get_static_value() {
if (!this.is_static)
return null;
return this.is_true
? true
: this.chunks[0]
? // method should be called only when `is_static = true`
( /** @type {import('./Text.js').default} */(this.chunks[0])).data
: '';
}
should_cache() {
return this.is_static
? false
: this.chunks.length === 1
? // @ts-ignore todo: probably error
this.chunks[0].node.type !== 'Identifier' || this.scope.names.has(this.chunks[0].node.name)
: true;
}
} }

Loading…
Cancel
Save