Lint and format src/compiler/compile/nodes/Attribute.js

pull/8569/head
Simon Holthausen 3 years ago
parent c0f8129278
commit eb599ce778

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