Convert src/compiler/compile/nodes/shared/Node.ts to JavaScript

pull/8569/head
Simon Holthausen 3 years ago
parent ca30dff13d
commit 7ddd050304

@ -1,81 +1,122 @@
import Attribute from '../Attribute';
import Component from '../../Component';
import { INode } from '../interfaces';
import Text from '../Text';
import { TemplateNode } from '../../../interfaces';
export default class Node { export default class Node {
readonly start: number;
readonly end: number; /**
readonly component: Component; * @readonly
readonly parent: INode; * @type {number}
readonly type: string; */
start;
prev?: INode;
next?: INode; /**
* @readonly
can_use_innerhtml: boolean; * @type {number}
is_static_content: boolean; */
var: string; end;
attributes: Attribute[];
/**
constructor(component: Component, parent: Node, _scope, info: TemplateNode) { * @readonly
this.start = info.start; * @type {import('../../Component.js').default}
this.end = info.end; */
this.type = info.type; component;
// this makes properties non-enumerable, which makes logging /**
// bearable. might have a performance cost. TODO remove in prod? * @readonly
Object.defineProperties(this, { * @type {import('../interfaces.js').INode}
component: { */
value: component parent;
},
parent: { /**
value: parent * @readonly
} * @type {string}
}); */
type;
this.can_use_innerhtml = true;
this.is_static_content = true; /** @type {import('../interfaces.js').INode} */
} prev;
cannot_use_innerhtml() { /** @type {import('../interfaces.js').INode} */
if (this.can_use_innerhtml !== false) { next;
this.can_use_innerhtml = false;
if (this.parent) this.parent.cannot_use_innerhtml(); /** @type {boolean} */
} can_use_innerhtml;
}
/** @type {boolean} */
not_static_content() { is_static_content;
this.is_static_content = false;
if (this.parent) this.parent.not_static_content(); /** @type {string} */
} var;
find_nearest(selector: RegExp) { /** @type {import('../Attribute.js').default[]} */
if (selector.test(this.type)) return this; attributes;
if (this.parent) return this.parent.find_nearest(selector);
} /**
* @param {import('../../Component.js').default} component *
get_static_attribute_value(name: string) { * @param {Node} parent *
const attribute = * @param {any} _scope *
this.attributes && * @param {import('../../../interfaces.js').TemplateNode} info undefined
this.attributes.find( */
(attr: Attribute) => attr.type === 'Attribute' && attr.name.toLowerCase() === name constructor(component, parent, _scope, info) {
); this.start = info.start;
this.end = info.end;
if (!attribute) return null; this.type = info.type;
// this makes properties non-enumerable, which makes logging
if (attribute.is_true) return true; // bearable. might have a performance cost. TODO remove in prod?
if (attribute.chunks.length === 0) return ''; Object.defineProperties(this, {
component: {
if (attribute.chunks.length === 1 && attribute.chunks[0].type === 'Text') { value: component
return (attribute.chunks[0] as Text).data; },
} parent: {
value: parent
return null; }
} });
this.can_use_innerhtml = true;
has_ancestor(type: string) { this.is_static_content = true;
return this.parent ? this.parent.type === type || this.parent.has_ancestor(type) : false; }
} cannot_use_innerhtml() {
if (this.can_use_innerhtml !== false) {
this.can_use_innerhtml = false;
if (this.parent)
this.parent.cannot_use_innerhtml();
}
}
not_static_content() {
this.is_static_content = false;
if (this.parent)
this.parent.not_static_content();
}
/** @param {RegExp} selector */
find_nearest(selector) {
if (selector.test(this.type))
return this;
if (this.parent)
return this.parent.find_nearest(selector);
}
/** @param {string} name */
get_static_attribute_value(name) {
const attribute = this.attributes &&
this.attributes.find(
/** @param {import('../Attribute.js').default} attr */
(attr) => attr.type === 'Attribute' && attr.name.toLowerCase() === name);
if (!attribute)
return null;
if (attribute.is_true)
return true;
if (attribute.chunks.length === 0)
return '';
if (attribute.chunks.length === 1 && attribute.chunks[0].type === 'Text') {
return ( /** @type {import('../Text.js').default} */(attribute.chunks[0])).data;
}
return null;
}
/** @param {string} type */
has_ancestor(type) {
return this.parent ? this.parent.type === type || this.parent.has_ancestor(type) : false;
}
} }

@ -20,6 +20,8 @@ import Title from '../Title.js';
import Window from '../Window.js'; import Window from '../Window.js';
import { push_array } from '../../../utils/push_array.js'; import { push_array } from '../../../utils/push_array.js';
/** @typedef {ReturnType<typeof map_children>} Children */
/** @param {any} type */ /** @param {any} type */
function get_constructor(type) { function get_constructor(type) {
switch (type) { switch (type) {
@ -94,5 +96,3 @@ export default function map_children(component, parent, scope, children) {
} }
); );
} }
/** @typedef {Class<map_children>>} Children */

Loading…
Cancel
Save