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

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

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