From 11e613e7d04c74cf54df098bf485644a7d287e2a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 1 Feb 2017 17:16:15 -0500 Subject: [PATCH] handle default paramters in computed values (fixes #274) --- src/generators/Generator.js | 2 +- src/generators/dom/index.js | 8 ++++---- test/generator/computed-values-default/_config.js | 9 +++++++++ test/generator/computed-values-default/main.html | 9 +++++++++ test/generator/computed-values/_config.js | 1 + .../errors.json | 0 .../input.html | 0 7 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 test/generator/computed-values-default/_config.js create mode 100644 test/generator/computed-values-default/main.html rename test/validator/{computed-values => properties-computed-values-needs-arguments}/errors.json (100%) rename test/validator/{computed-values => properties-computed-values-needs-arguments}/input.html (100%) diff --git a/src/generators/Generator.js b/src/generators/Generator.js index 84bd2be36758..8568a515e7e8 100644 --- a/src/generators/Generator.js +++ b/src/generators/Generator.js @@ -246,7 +246,7 @@ export default class Generator { const key = prop.key.name; const value = prop.value; - const deps = value.params.map( param => param.name ); + const deps = value.params.map( param => param.type === 'AssignmentPattern' ? param.left.name : param.name ); dependencies.set( key, deps ); }); diff --git a/src/generators/dom/index.js b/src/generators/dom/index.js index 649252557d05..41d95bb58421 100644 --- a/src/generators/dom/index.js +++ b/src/generators/dom/index.js @@ -185,19 +185,19 @@ export default function dom ( parsed, source, options, names ) { computations.forEach( ({ key, deps }) => { builder.addBlock( deindent` - if ( ${deps.map( dep => `( '${dep}' in newState && typeof state.${dep} === 'object' || state.${dep} !== oldState.${dep} )` ).join( ' || ' )} ) { + if ( isInitial || ${deps.map( dep => `( '${dep}' in newState && typeof state.${dep} === 'object' || state.${dep} !== oldState.${dep} )` ).join( ' || ' )} ) { state.${key} = newState.${key} = template.computed.${key}( ${deps.map( dep => `state.${dep}` ).join( ', ' )} ); } ` ); }); builders.main.addBlock( deindent` - function applyComputations ( state, newState, oldState ) { + function applyComputations ( state, newState, oldState, isInitial ) { ${builder} } ` ); - builders._set.addLine( `applyComputations( this._state, newState, oldState )` ); + builders._set.addLine( `applyComputations( this._state, newState, oldState, false )` ); } // TODO is the `if` necessary? @@ -277,7 +277,7 @@ export default function dom ( parsed, source, options, names ) { function ${name} ( options ) { options = options || {}; ${generator.usesRefs ? `\nthis.refs = {}` : ``} - this._state = ${initialState};${templateProperties.computed ? `\napplyComputations( this._state, this._state, {} );` : ``} + this._state = ${initialState};${templateProperties.computed ? `\napplyComputations( this._state, this._state, {}, true );` : ``} this._observers = { pre: Object.create( null ), diff --git a/test/generator/computed-values-default/_config.js b/test/generator/computed-values-default/_config.js new file mode 100644 index 000000000000..9d761d891577 --- /dev/null +++ b/test/generator/computed-values-default/_config.js @@ -0,0 +1,9 @@ +export default { + html: '

2

', + + test ( assert, component, target ) { + component.set({ a: 2 }); + assert.equal( target.innerHTML, '

4

' ); + component.teardown(); + } +}; diff --git a/test/generator/computed-values-default/main.html b/test/generator/computed-values-default/main.html new file mode 100644 index 000000000000..74292a3a2e14 --- /dev/null +++ b/test/generator/computed-values-default/main.html @@ -0,0 +1,9 @@ +

{{foo}}

+ + diff --git a/test/generator/computed-values/_config.js b/test/generator/computed-values/_config.js index 179ebc2c9e47..b5f7112fa8e9 100644 --- a/test/generator/computed-values/_config.js +++ b/test/generator/computed-values/_config.js @@ -5,5 +5,6 @@ export default { assert.equal( component.get( 'c' ), 5 ); assert.equal( component.get( 'cSquared' ), 25 ); assert.equal( target.innerHTML, '

3 + 2 = 5

\n

5 * 5 = 25

' ); + component.teardown(); } }; diff --git a/test/validator/computed-values/errors.json b/test/validator/properties-computed-values-needs-arguments/errors.json similarity index 100% rename from test/validator/computed-values/errors.json rename to test/validator/properties-computed-values-needs-arguments/errors.json diff --git a/test/validator/computed-values/input.html b/test/validator/properties-computed-values-needs-arguments/input.html similarity index 100% rename from test/validator/computed-values/input.html rename to test/validator/properties-computed-values-needs-arguments/input.html