Merge pull request #446 from sveltejs/better-prototype-assignment

better prototype assignment
pull/448/head
Rich Harris 8 years ago committed by GitHub
commit d818e42e56

@ -393,18 +393,17 @@ export default function dom ( parsed, source, options ) {
const sharedPath = options.shared === true ? 'svelte/shared.js' : options.shared; const sharedPath = options.shared === true ? 'svelte/shared.js' : options.shared;
if ( sharedPath ) { const prototypeBase = `${name}.prototype` + ( templateProperties.methods ? `, ${generator.alias( 'template' )}.methods` : '' );
const base = templateProperties.methods ? `{}, ${generator.alias( 'template' )}.methods` : `{}`; const proto = sharedPath ? `${generator.helper( 'proto' )} ` : deindent`
builders.main.addBlock( `${name}.prototype = ${generator.helper( 'assign' )}( ${base}, ${generator.helper( 'proto' )} );` ); {
} else { ${
if ( templateProperties.methods ) { [ 'get', 'fire', 'observe', 'on', 'set', '_flush' ]
builders.main.addBlock( `${name}.prototype = ${generator.alias( 'template' )}.methods;` ); .map( n => `${n}: ${generator.helper( n )}` )
} .join( ',\n' )
}
}`;
[ 'get', 'fire', 'observe', 'on', 'set', '_flush' ].forEach( methodName => { builders.main.addBlock( `${generator.helper( 'assign' )}( ${prototypeBase}, ${proto});` );
builders.main.addLine( `${name}.prototype.${methodName} = ${generator.helper( methodName )};` );
});
}
// TODO deprecate component.teardown() // TODO deprecate component.teardown()
builders.main.addBlock( deindent` builders.main.addBlock( deindent`

@ -2,10 +2,11 @@ import parse from './parse/index.js';
import validate from './validate/index.js'; import validate from './validate/index.js';
import generate from './generators/dom/index.js'; import generate from './generators/dom/index.js';
import generateSSR from './generators/server-side-rendering/index.js'; import generateSSR from './generators/server-side-rendering/index.js';
import { assign } from './shared/index.js';
import { version } from '../package.json'; import { version } from '../package.json';
function normalizeOptions ( options ) { function normalizeOptions ( options ) {
return Object.assign( { return assign( {
generate: 'dom', generate: 'dom',
// a filename is necessary for sourcemap generation // a filename is necessary for sourcemap generation

@ -141,8 +141,12 @@ describe( 'generate', () => {
component.destroy(); component.destroy();
assert.equal( target.innerHTML, '' ); assert.equal( target.innerHTML, '' );
} }
Object.assign = Object_assign;
}) })
.catch( err => { .catch( err => {
Object.assign = Object_assign;
if ( config.error && !unintendedError ) { if ( config.error && !unintendedError ) {
config.error( assert, err ); config.error( assert, err );
} }
@ -151,9 +155,6 @@ describe( 'generate', () => {
if ( !config.show ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console if ( !config.show ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console
throw err; throw err;
} }
})
.then( () => {
Object.assign = Object_assign;
}); });
}); });
} }

Loading…
Cancel
Save