Skip to content

Commit

Permalink
Merge pull request #322 from sveltejs/gh-249
Browse files Browse the repository at this point in the history
throw error in dev mode for missing bound properties (#249)
  • Loading branch information
Rich-Harris authored Mar 1, 2017
2 parents 0da2a99 + 0e1d4c4 commit d076f21
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}'" );`
);
});
}
Expand Down
22 changes: 14 additions & 8 deletions src/generators/dom/visitors/attributes/binding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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` );
}
Expand All @@ -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] ];

Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/generator/dev-warning-missing-data-binding/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
dev: true,

error ( assert, err ) {
assert.equal( err.message, `Component was created without expected data property 'value'` );
}
};
1 change: 1 addition & 0 deletions test/generator/dev-warning-missing-data-binding/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input bind:value>

0 comments on commit d076f21

Please sign in to comment.