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

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

@ -6,7 +6,6 @@ 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;
@ -63,22 +62,22 @@ export default class Attribute extends Node {
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(/** @param {any} node */ (node) => { : info.value.map(
if (node.type === 'Text') /** @param {any} node */ (node) => {
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();
@ -86,49 +85,54 @@ export default class Attribute extends Node {
} }
} }
get_dependencies() { get_dependencies() {
if (this.is_spread) if (this.is_spread) return this.expression.dynamic_dependencies();
return this.expression.dynamic_dependencies();
/** @type {Set<string>} */ /** @type {Set<string>} */
const dependencies = new Set(); const dependencies = new Set();
this.chunks.forEach(/** @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)
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(( /** @type {import('./Text.js').default} */(this.chunks[0])).data) ? string_literal(/** @type {import('./Text.js').default} */ (this.chunks[0]).data)
: ( /** @type {import('./shared/Expression.js').default} */(this.chunks[0])).manipulate(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) =>
chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block)
)
.reduce(
/**
* @param {any} lhs * @param {any} lhs
* @param {any} rhs * @param {any} rhs
*/ (lhs, rhs) => x `${lhs} + ${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) if (!this.is_static) return null;
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`
( /** @type {import('./Text.js').default} */(this.chunks[0])).data /** @type {import('./Text.js').default} */ (this.chunks[0]).data
: ''; : '';
} }
should_cache() { should_cache() {
@ -140,7 +144,3 @@ export default class Attribute extends Node {
: true; : true;
} }
} }

Loading…
Cancel
Save