Merge pull request #324 from sveltejs/gh-177

throw in dev mode if options.target is absent
pull/328/head
Rich Harris 8 years ago committed by GitHub
commit e27df1bde0

@ -304,44 +304,50 @@ export default function dom ( parsed, source, options, names ) {
` ); ` );
} }
const stateBlock = new CodeBuilder(); const constructorBlock = new CodeBuilder();
stateBlock.addLine( constructorBlock.addLine( `options = options || {};` );
if ( generator.usesRefs ) constructorBlock.addLine( `this.refs = {};` );
constructorBlock.addLine(
`this._state = ${templateProperties.data ? `Object.assign( template.data(), options.data )` : `options.data || {}`};` `this._state = ${templateProperties.data ? `Object.assign( template.data(), options.data )` : `options.data || {}`};`
); );
if ( templateProperties.computed ) { if ( templateProperties.computed ) {
stateBlock.addLine( constructorBlock.addLine(
`applyComputations( this._state, this._state, {}, true );` `applyComputations( this._state, this._state, {}, true );`
); );
} }
if ( options.dev ) { if ( options.dev ) {
Object.keys( generator.expectedProperties ).forEach( prop => { Object.keys( generator.expectedProperties ).forEach( prop => {
stateBlock.addLine( constructorBlock.addLine(
`if ( !( '${prop}' in this._state ) ) throw new Error( "Component was created without expected data property '${prop}'" );` `if ( !( '${prop}' in this._state ) ) throw new Error( "Component was created without expected data property '${prop}'" );`
); );
}); });
}
builders.main.addBlock( deindent` constructorBlock.addBlock(
function ${name} ( options ) { `if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );`
options = options || {}; );
${generator.usesRefs ? `\nthis.refs = {}` : ``} }
${stateBlock} constructorBlock.addBlock( deindent`
this._observers = {
pre: Object.create( null ),
post: Object.create( null )
};
this._observers = { this._handlers = Object.create( null );
pre: Object.create( null ),
post: Object.create( null )
};
this._handlers = Object.create( null ); this._root = options._root;
this._yield = options._yield;
this._root = options._root; ${builders.init}
this._yield = options._yield; ` );
${builders.init} builders.main.addBlock( deindent`
function ${name} ( options ) {
${constructorBlock}
} }
` ); ` );

@ -134,4 +134,18 @@ describe( 'generate', () => {
runTest( dir, path.resolve( 'shared.js' ) ); runTest( dir, path.resolve( 'shared.js' ) );
}); });
}); });
it( 'fails if options.target is missing in dev mode', () => {
const { code } = svelte.compile( `<div></div>`, {
format: 'iife',
name: 'SvelteComponent',
dev: true
});
const SvelteComponent = eval( `(function () { ${code}; return SvelteComponent; }())` );
assert.throws( () => {
new SvelteComponent();
}, /'target' is a required option/ );
});
}); });

Loading…
Cancel
Save