From 0e1d4c4bc71b90e5741026a60b980e040ae81429 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 1 Mar 2017 17:45:41 -0500 Subject: [PATCH] throw error in dev mode for missing bound properties (#249) --- src/generators/dom/index.js | 2 +- .../dom/visitors/attributes/binding/index.js | 22 ++++++++++++------- .../_config.js | 7 ++++++ .../main.html | 1 + 4 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 test/generator/dev-warning-missing-data-binding/_config.js create mode 100644 test/generator/dev-warning-missing-data-binding/main.html diff --git a/src/generators/dom/index.js b/src/generators/dom/index.js index 2ca76ba096..05aab72394 100644 --- a/src/generators/dom/index.js +++ b/src/generators/dom/index.js @@ -319,7 +319,7 @@ export default function dom ( parsed, source, options, names ) { if ( options.dev ) { Object.keys( generator.expectedProperties ).forEach( prop => { stateBlock.addLine( - `if ( !( '${prop}' in this._state ) ) throw new Error( "Component was created without expected data property 'foo'" );` + `if ( !( '${prop}' in this._state ) ) throw new Error( "Component was created without expected data property '${prop}'" );` ); }); } diff --git a/src/generators/dom/visitors/attributes/binding/index.js b/src/generators/dom/visitors/attributes/binding/index.js index 4123f3ad7a..ff3c6ee847 100644 --- a/src/generators/dom/visitors/attributes/binding/index.js +++ b/src/generators/dom/visitors/attributes/binding/index.js @@ -63,7 +63,7 @@ export default function createBinding ( generator, node, attribute, current, loc do { if ( fragment.expression && fragment.context === prop ) { - if ( !isReference( fragment.expression ) ) { + if ( !isReference( fragment.expression ) ) { // TODO this should happen in prior validation step throw new Error( `${prop} is read-only, it cannot be bound` ); } @@ -72,6 +72,8 @@ export default function createBinding ( generator, node, attribute, current, loc } } while ( fragment = fragment.parent ); + generator.expectedProperties[ prop ] = true; + const listName = current.listNames[ parts[0] ]; const indexName = current.indexNames[ parts[0] ]; @@ -84,14 +86,18 @@ export default function createBinding ( generator, node, attribute, current, loc component._set({ ${prop}: component.get( '${prop}' ) }); `; - } else if ( deep ) { - setter = deindent` - var ${parts[0]} = component.get( '${parts[0]}' ); - ${parts[0]}.${parts.slice( 1 ).join( '.' )} = ${value}; - component._set({ ${parts[0]}: ${parts[0]} }); - `; } else { - setter = `component._set({ ${attribute.value}: ${value} });`; + if ( deep ) { + setter = deindent` + var ${parts[0]} = component.get( '${parts[0]}' ); + ${parts[0]}.${parts.slice( 1 ).join( '.' )} = ${value}; + component._set({ ${parts[0]}: ${parts[0]} }); + `; + } else { + setter = `component._set({ ${attribute.value}: ${value} });`; + } + + generator.expectedProperties[ parts[0] ] = true; } // special case diff --git a/test/generator/dev-warning-missing-data-binding/_config.js b/test/generator/dev-warning-missing-data-binding/_config.js new file mode 100644 index 0000000000..7b3e61fcda --- /dev/null +++ b/test/generator/dev-warning-missing-data-binding/_config.js @@ -0,0 +1,7 @@ +export default { + dev: true, + + error ( assert, err ) { + assert.equal( err.message, `Component was created without expected data property 'value'` ); + } +}; \ No newline at end of file diff --git a/test/generator/dev-warning-missing-data-binding/main.html b/test/generator/dev-warning-missing-data-binding/main.html new file mode 100644 index 0000000000..f24d608cd5 --- /dev/null +++ b/test/generator/dev-warning-missing-data-binding/main.html @@ -0,0 +1 @@ + \ No newline at end of file