From b02cc849f98f56a1d2c241117b70ec1b60bc3ab2 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Thu, 29 Mar 2018 00:05:59 -0400 Subject: [PATCH] remove some stuff we wont need --- src/generators/nodes/Component.ts | 19 +-- src/generators/nodes/Element.ts | 7 +- src/generators/nodes/Spread.ts | 200 ------------------------------ src/generators/nodes/index.ts | 2 - src/validate/html/a11y.ts | 2 + 5 files changed, 8 insertions(+), 222 deletions(-) delete mode 100644 src/generators/nodes/Spread.ts diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 47eb8175f0..bababef766 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -48,10 +48,6 @@ export default class Component extends Node { } }); - if (this.spread) { - block.addDependencies(this.spread.metadata.dependencies); - } - this.var = block.getUniqueName( ( this.name === ':Self' ? this.generator.name : @@ -230,7 +226,7 @@ export default class Component extends Node { } }); - componentInitialData = name_initial_data; + componentInitProperties.push(`data: ${name_initial_data}`); const initialisers = [ 'state = #component.get()', @@ -254,21 +250,10 @@ export default class Component extends Node { }); `; } else if (initialProps.length) { - componentInitialData = initialPropString; + componentInitProperties.push(`data: ${initialPropString}`); } } - if (this.spread) { - const initialData = this.spread.renderForComponent(block, updates); - componentInitialData = componentInitialData ? - `@assign({}, ${initialData}, ${componentInitialData})` : - initialData; - } - - if (componentInitialData) { - componentInitProperties.push(`data: ${componentInitialData}`); - } - const isDynamicComponent = this.name === ':Component'; const switch_vars = isDynamicComponent && { diff --git a/src/generators/nodes/Element.ts b/src/generators/nodes/Element.ts index a96cbb7265..9cd3e0efb9 100644 --- a/src/generators/nodes/Element.ts +++ b/src/generators/nodes/Element.ts @@ -242,14 +242,15 @@ export default class Element extends Node { } this.addBindings(block, allUsedContexts); - this.addAttributes(block); const eventHandlerUsesComponent = this.addEventHandlers(block, allUsedContexts); this.addRefs(block); this.addTransitions(block); this.addActions(block); - if (this.spread) { - this.spread.renderForElement(block); + if (this.attributes.find(attr => attr.type === 'Spread')) { + this.addSpreadAttributes(block); + } else { + this.addAttributes(block); } if (allUsedContexts.size || eventHandlerUsesComponent) { diff --git a/src/generators/nodes/Spread.ts b/src/generators/nodes/Spread.ts deleted file mode 100644 index 732a9380ad..0000000000 --- a/src/generators/nodes/Spread.ts +++ /dev/null @@ -1,200 +0,0 @@ -import deindent from '../../utils/deindent'; -import { DomGenerator } from '../dom/index'; -import Node from './shared/Node'; -import Element from './Element'; -import Block from '../dom/Block'; - -export default class Spread { - type: 'Spread'; - start: number; - end: number; - - generator: DomGenerator; - parent: Element; - expression: Node; - - metadata: { - dependencies: string[]; - snippet: string; - }; - - constructor({ - generator, - expression, - parent - }: { - generator: DomGenerator, - expression: Node, - parent: Element - }) { - this.type = 'Spread'; - this.generator = generator; - this.parent = parent; - - this.expression = expression; - } - - renderForElement(block: Block) { - const node = this.parent; - - const { expression } = this; - const { indexes } = block.contextualise(expression); - const { dependencies, snippet } = this.metadata; - - const value = snippet; - - const hasChangeableIndex = Array.from(indexes).some(index => block.changeableIndexes.get(index)); - - const shouldCache = ( - expression.type !== 'Identifier' || - block.contexts.has(expression.name) || - hasChangeableIndex - ); - - const last = shouldCache && block.getUniqueName(`${node.var}_spread_value`); - - if (shouldCache) block.addVariable(last); - - const init = shouldCache ? `${last} = ${value}` : value; - - const activeKeys = block.getUniqueName(`${node.var}_spread_keys`); - block.addVariable(activeKeys, '{}'); - - const changes = block.getUniqueName(`${node.var}_spread_changes`); - - const hasNamedAttributes = node.attributes.length; - const namedAttributes = block.getUniqueName(`${node.var}_attributes`); - - if (hasNamedAttributes) { - block.builders.init.addBlock(deindent` - var ${namedAttributes} = [${node.attributes.map(attr => `'${attr.name}'`).join(', ')}]; - `) - } - - block.builders.hydrate.addBlock(deindent` - var ${changes} = ${init}; - for (var key in ${changes}) { - ${hasNamedAttributes ? `if (${namedAttributes}.indexOf(key) !== -1) continue;` : ''} - - @setAttribute(${node.var}, key, ${changes}[key]); - ${activeKeys}[key] = true; - } - `); - - if (dependencies.length || hasChangeableIndex) { - const changedCheck = ( - ( block.hasOutroMethod ? `#outroing || ` : '' ) + - dependencies.map(dependency => `changed.${dependency}`).join(' || ') - ); - - const updateCachedValue = `${last} !== (${last} = ${value})`; - - const condition = shouldCache ? - ( dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue ) : - changedCheck; - - const oldKeys = block.getUniqueName(`${node.var}_spread_keys_old`); - - const updater = deindent` - var ${oldKeys} = ${activeKeys}; - ${activeKeys} = {}; - - var ${changes} = ${shouldCache ? last : value}; - for (var key in ${changes}) { - ${hasNamedAttributes ? `if (${namedAttributes}.indexOf(key) !== -1) continue;` : ''} - - @setAttribute(${node.var}, key, ${changes}[key]); - - ${activeKeys}[key] = true; - delete ${oldKeys}[key]; - } - - for (var key in ${oldKeys}) { - @removeAttribute(${node.var}, key); - } - `; - - block.builders.update.addConditional( - condition, - updater - ); - } - } - - renderForComponent(block: Block, updates: string[]) { - const node = this.parent; - - - const { expression } = this; - const { indexes } = block.contextualise(expression); - const { dependencies, snippet } = this.metadata; - - const value = snippet; - - const hasChangeableIndex = Array.from(indexes).some(index => block.changeableIndexes.get(index)); - - const shouldCache = ( - expression.type !== 'Identifier' || - block.contexts.has(expression.name) || - hasChangeableIndex - ); - - const last = shouldCache && block.getUniqueName(`${node.var}_spread_value`); - - if (shouldCache) block.addVariable(last); - - const init = shouldCache ? `${last} = ${value}` : value; - - const activeKeys = block.getUniqueName(`${node.var}_spread_keys`); - block.addVariable(activeKeys, '{}'); - - const changes = block.getUniqueName(`${node.var}_spread_changes`); - - const hasNamedAttributes = node.attributes.length; - const namedAttributes = block.getUniqueName(`${node.var}_attributes`); - - if (hasNamedAttributes) { - block.builders.init.addBlock(deindent` - var ${namedAttributes} = [${node.attributes.map(attr => `'${attr.name}'`).join(', ')}]; - `) - } - - if (dependencies.length || hasChangeableIndex) { - const changedCheck = ( - ( block.hasOutroMethod ? `#outroing || ` : '' ) + - dependencies.map(dependency => `changed.${dependency}`).join(' || ') - ); - - const updateCachedValue = `${last} !== (${last} = ${value})`; - - const condition = shouldCache ? - ( dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue ) : - changedCheck; - - const oldKeys = block.getUniqueName(`${node.var}_spread_keys_old`); - - updates.push(deindent` - if (${condition}) { - var ${oldKeys} = ${activeKeys}; - ${activeKeys} = {}; - - var ${changes} = ${shouldCache ? last : value}; - for (var key in ${changes}) { - ${hasNamedAttributes ? `if (${namedAttributes}.indexOf(key) !== -1) continue;` : ''} - - ${node.var}_changes[key] = ${changes}[key]; - - ${activeKeys}[key] = true; - delete ${oldKeys}[key]; - } - - for (var key in ${oldKeys}) { - ${node.var}_changes[key] = undefined; - } - } - `); - } - - return value; - } -} diff --git a/src/generators/nodes/index.ts b/src/generators/nodes/index.ts index 842d14777b..a7ba3c7252 100644 --- a/src/generators/nodes/index.ts +++ b/src/generators/nodes/index.ts @@ -18,7 +18,6 @@ import PendingBlock from './PendingBlock'; import RawMustacheTag from './RawMustacheTag'; import Ref from './Ref'; import Slot from './Slot'; -import Spread from './Spread'; import Text from './Text'; import ThenBlock from './ThenBlock'; import Title from './Title'; @@ -45,7 +44,6 @@ const nodes: Record = { RawMustacheTag, Ref, Slot, - Spread, Text, ThenBlock, Title, diff --git a/src/validate/html/a11y.ts b/src/validate/html/a11y.ts index e9953d6083..c52180cf63 100644 --- a/src/validate/html/a11y.ts +++ b/src/validate/html/a11y.ts @@ -27,6 +27,8 @@ export default function a11y( const attributeMap = new Map(); node.attributes.forEach((attribute: Node) => { + if (attribute.type === 'Spread') return; + const name = attribute.name.toLowerCase(); // aria-props