Skip to content

Commit a8d1983

Browse files
authored
Merge pull request #320 from sveltejs/gh-13
add development warnings for missing data properties
2 parents 9fc2108 + d61b192 commit a8d1983

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

src/generators/Generator.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export default class Generator {
1919
this.components = {};
2020
this.events = {};
2121

22+
// track which properties are needed, so we can provide useful info
23+
// in dev mode
24+
this.expectedProperties = {};
25+
2226
this.elementDepth = 0;
2327

2428
this.code = new MagicString( source );
@@ -99,6 +103,10 @@ export default class Generator {
99103
}
100104
});
101105

106+
dependencies.forEach( name => {
107+
this.expectedProperties[ name ] = true;
108+
});
109+
102110
return {
103111
dependencies,
104112
contexts: usedContexts,

src/generators/dom/index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,32 @@ export default function dom ( parsed, source, options, names ) {
304304
` );
305305
}
306306

307-
const initialState = templateProperties.data ? `Object.assign( template.data(), options.data )` : `options.data || {}`;
307+
const stateBlock = new CodeBuilder();
308+
309+
stateBlock.addLine(
310+
`this._state = ${templateProperties.data ? `Object.assign( template.data(), options.data )` : `options.data || {}`};`
311+
);
312+
313+
if ( templateProperties.computed ) {
314+
stateBlock.addLine(
315+
`applyComputations( this._state, this._state, {}, true );`
316+
);
317+
}
318+
319+
if ( options.dev ) {
320+
Object.keys( generator.expectedProperties ).forEach( prop => {
321+
stateBlock.addLine(
322+
`if ( !( '${prop}' in this._state ) ) throw new Error( "Component was created without expected data property 'foo'" );`
323+
);
324+
});
325+
}
308326

309327
builders.main.addBlock( deindent`
310328
function ${name} ( options ) {
311329
options = options || {};
312330
${generator.usesRefs ? `\nthis.refs = {}` : ``}
313-
this._state = ${initialState};${templateProperties.computed ? `\napplyComputations( this._state, this._state, {}, true );` : ``}
331+
332+
${stateBlock}
314333
315334
this._observers = {
316335
pre: Object.create( null ),

test/generate.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe( 'generate', () => {
4141
showCompiledCode = config.show;
4242
compileOptions = config.compileOptions || {};
4343
compileOptions.shared = shared;
44+
compileOptions.dev = config.dev;
4445

4546
try {
4647
const source = fs.readFileSync( `test/generator/${dir}/main.html`, 'utf-8' );
@@ -79,6 +80,8 @@ describe( 'generate', () => {
7980
throw err;
8081
}
8182

83+
let unintendedError = null;
84+
8285
return env()
8386
.then( window => {
8487
// Put the constructor on window for testing
@@ -91,6 +94,11 @@ describe( 'generate', () => {
9194
data: config.data
9295
});
9396

97+
if ( config.error ) {
98+
unintendedError = true;
99+
throw new Error( 'Expected a runtime error' );
100+
}
101+
94102
if ( config.html ) {
95103
assert.htmlEqual( target.innerHTML, config.html );
96104
}
@@ -103,8 +111,14 @@ describe( 'generate', () => {
103111
}
104112
})
105113
.catch( err => {
106-
if ( !config.show ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console
107-
throw err;
114+
if ( config.error && !unintendedError ) {
115+
config.error( assert, err );
116+
}
117+
118+
else {
119+
if ( !config.show ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console
120+
throw err;
121+
}
108122
});
109123
});
110124
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
dev: true,
3+
4+
error ( assert, err ) {
5+
assert.equal( err.message, `Component was created without expected data property 'foo'` );
6+
}
7+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>{{foo}}</p>

0 commit comments

Comments
 (0)