Skip to content

Commit

Permalink
Merge pull request #324 from sveltejs/gh-177
Browse files Browse the repository at this point in the history
throw in dev mode if options.target is absent
  • Loading branch information
Rich-Harris authored Mar 1, 2017
2 parents d076f21 + ee5e8e8 commit e27df1b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}`};`
);

if ( templateProperties.computed ) {
stateBlock.addLine(
constructorBlock.addLine(
`applyComputations( this._state, this._state, {}, true );`
);
}

if ( options.dev ) {
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}'" );`
);
});
}

builders.main.addBlock( deindent`
function ${name} ( options ) {
options = options || {};
${generator.usesRefs ? `\nthis.refs = {}` : ``}
constructorBlock.addBlock(
`if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );`
);
}

${stateBlock}
constructorBlock.addBlock( deindent`
this._observers = {
pre: Object.create( null ),
post: Object.create( null )
};
this._observers = {
pre: Object.create( null ),
post: Object.create( null )
};
this._handlers = Object.create( null );
this._handlers = Object.create( null );
this._root = options._root;
this._yield = options._yield;
this._root = options._root;
this._yield = options._yield;
${builders.init}
` );

${builders.init}
builders.main.addBlock( deindent`
function ${name} ( options ) {
${constructorBlock}
}
` );

Expand Down
14 changes: 14 additions & 0 deletions test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,18 @@ describe( 'generate', () => {
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/ );
});
});

0 comments on commit e27df1b

Please sign in to comment.