From 33bce408390dd8360f34ca97d374c3c17af419af Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Wed, 26 Apr 2017 14:33:44 -0400 Subject: [PATCH] allow parameter-less transitions --- src/generators/dom/Block.js | 3 -- .../dom/visitors/Element/Transition.js | 16 ++++---- src/parse/read/directives.js | 22 ++++++----- src/parse/state/tag.js | 1 - .../transition-intro-no-params/input.html | 1 + .../transition-intro-no-params/output.json | 37 +++++++++++++++++++ 6 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 test/parser/samples/transition-intro-no-params/input.html create mode 100644 test/parser/samples/transition-intro-no-params/output.json diff --git a/src/generators/dom/Block.js b/src/generators/dom/Block.js index e79423c55a..8c6bbec667 100644 --- a/src/generators/dom/Block.js +++ b/src/generators/dom/Block.js @@ -33,9 +33,6 @@ export default class Block { this.variables = new Map(); this.getUniqueName = this.generator.getUniqueNameMaker( options.params ); - this.intros = []; - this.outros = []; - // unique names this.component = this.getUniqueName( 'component' ); this.target = this.getUniqueName( 'target' ); diff --git a/src/generators/dom/visitors/Element/Transition.js b/src/generators/dom/visitors/Element/Transition.js index 9b9957d4a6..50f7534979 100644 --- a/src/generators/dom/visitors/Element/Transition.js +++ b/src/generators/dom/visitors/Element/Transition.js @@ -5,12 +5,12 @@ export default function visitTransition ( generator, block, state, node, attribu block.addVariable( name ); + const snippet = attribute.expression ? block.contextualise( attribute.expression ).snippet : '{}'; + const fn = `${generator.alias( 'template' )}.transitions.${attribute.name}`; // TODO add built-in transitions? + if ( attribute.intro ) { generator.hasIntroTransitions = true; - const { snippet } = block.contextualise( attribute.expression ); - const fn = `${generator.alias( 'template' )}.transitions.${attribute.name}`; // TODO add built-in transitions? - block.builders.create.addBlock( deindent` ${block.component}._renderHooks.push({ fn: function () { @@ -22,9 +22,9 @@ export default function visitTransition ( generator, block, state, node, attribu ` ); } - ( attribute.intro ? block.intros : block.outros ).push({ - node: state.name, - transition: attribute.name, - params: block.contextualise( attribute.expression ).snippet - }); + if ( attribute.outro ) { + generator.hasOutroTransitions = true; + + throw new Error( 'TODO' ); + } } \ No newline at end of file diff --git a/src/parse/read/directives.js b/src/parse/read/directives.js index 9cc09392d7..32e86a0a4d 100644 --- a/src/parse/read/directives.js +++ b/src/parse/read/directives.js @@ -131,18 +131,22 @@ export function readBindingDirective ( parser, start, name ) { } export function readTransitionDirective ( parser, start, name, type ) { - const quoteMark = ( - parser.eat( `'` ) ? `'` : - parser.eat( `"` ) ? `"` : - null - ); + let expression = null; - const expressionStart = parser.index; + if ( parser.eat( '=' ) ) { + const quoteMark = ( + parser.eat( `'` ) ? `'` : + parser.eat( `"` ) ? `"` : + null + ); - const expression = readExpression( parser, expressionStart, quoteMark ); + const expressionStart = parser.index; + + expression = readExpression( parser, expressionStart, quoteMark ); - if ( expression.type !== 'ObjectExpression' ) { - parser.error( `Expected object expression`, expressionStart ); + if ( expression.type !== 'ObjectExpression' ) { + parser.error( `Expected object expression`, expressionStart ); + } } return { diff --git a/src/parse/state/tag.js b/src/parse/state/tag.js index 901b250833..475043f311 100644 --- a/src/parse/state/tag.js +++ b/src/parse/state/tag.js @@ -255,7 +255,6 @@ function readAttribute ( parser, uniqueNames ) { const match = /^(in|out|transition):/.exec( name ); if ( match ) { - parser.eat( '=', true ); return readTransitionDirective( parser, start, name.slice( match[0].length ), match[1] ); } diff --git a/test/parser/samples/transition-intro-no-params/input.html b/test/parser/samples/transition-intro-no-params/input.html new file mode 100644 index 0000000000..e5e4171434 --- /dev/null +++ b/test/parser/samples/transition-intro-no-params/input.html @@ -0,0 +1 @@ +
fades in
\ No newline at end of file diff --git a/test/parser/samples/transition-intro-no-params/output.json b/test/parser/samples/transition-intro-no-params/output.json new file mode 100644 index 0000000000..ae1f76a3bc --- /dev/null +++ b/test/parser/samples/transition-intro-no-params/output.json @@ -0,0 +1,37 @@ +{ + "hash": 1535528483, + "html": { + "start": 0, + "end": 27, + "type": "Fragment", + "children": [ + { + "start": 0, + "end": 27, + "type": "Element", + "name": "div", + "attributes": [ + { + "start": 5, + "end": 12, + "type": "Transition", + "name": "fade", + "intro": true, + "outro": false, + "expression": null + } + ], + "children": [ + { + "start": 13, + "end": 21, + "type": "Text", + "data": "fades in" + } + ] + } + ] + }, + "css": null, + "js": null +} \ No newline at end of file