warn on onrender/onteardown, replace with oncreate/ondestroy (#40)

pull/316/head
Rich Harris 8 years ago
parent 54445fa23d
commit 16e3574bfb

@ -243,7 +243,7 @@ export default class Generator {
} }
defaultExport.declaration.properties.forEach( prop => { defaultExport.declaration.properties.forEach( prop => {
templateProperties[ prop.key.name ] = prop.value; templateProperties[ prop.key.name ] = prop;
}); });
this.code.prependRight( js.content.start, 'var template = (function () {' ); this.code.prependRight( js.content.start, 'var template = (function () {' );
@ -255,7 +255,7 @@ export default class Generator {
[ 'helpers', 'events', 'components' ].forEach( key => { [ 'helpers', 'events', 'components' ].forEach( key => {
if ( templateProperties[ key ] ) { if ( templateProperties[ key ] ) {
templateProperties[ key ].properties.forEach( prop => { templateProperties[ key ].value.properties.forEach( prop => {
this[ key ][ prop.key.name ] = prop.value; this[ key ][ prop.key.name ] = prop.value;
}); });
} }
@ -264,7 +264,7 @@ export default class Generator {
if ( templateProperties.computed ) { if ( templateProperties.computed ) {
const dependencies = new Map(); const dependencies = new Map();
templateProperties.computed.properties.forEach( prop => { templateProperties.computed.value.properties.forEach( prop => {
const key = prop.key.name; const key = prop.key.name;
const value = prop.value; const value = prop.value;
@ -286,7 +286,7 @@ export default class Generator {
computations.push({ key, deps }); computations.push({ key, deps });
} }
templateProperties.computed.properties.forEach( prop => visit( prop.key.name ) ); templateProperties.computed.value.properties.forEach( prop => visit( prop.key.name ) );
} }
} }

@ -156,6 +156,19 @@ export default function dom ( parsed, source, options, names ) {
const { computations, templateProperties } = generator.parseJs(); const { computations, templateProperties } = generator.parseJs();
// Remove these after version 2
if ( templateProperties.onrender ) {
const { key } = templateProperties.onrender;
generator.code.overwrite( key.start, key.end, 'oncreate', true );
templateProperties.oncreate = templateProperties.onrender;
}
if ( templateProperties.onteardown ) {
const { key } = templateProperties.onteardown;
generator.code.overwrite( key.start, key.end, 'ondestroy', true );
templateProperties.ondestroy = templateProperties.onteardown;
}
generator.imports.forEach( node => { generator.imports.forEach( node => {
node.specifiers.forEach( specifier => { node.specifiers.forEach( specifier => {
generator.importedNames[ specifier.local.name ] = true; generator.importedNames[ specifier.local.name ] = true;
@ -164,7 +177,7 @@ export default function dom ( parsed, source, options, names ) {
let namespace = null; let namespace = null;
if ( templateProperties.namespace ) { if ( templateProperties.namespace ) {
const ns = templateProperties.namespace.value; const ns = templateProperties.namespace.value.value;
namespace = namespaces[ ns ] || ns; namespace = namespaces[ ns ] || ns;
// TODO remove the namespace property from the generated code, it's unused past this point // TODO remove the namespace property from the generated code, it's unused past this point
@ -281,12 +294,12 @@ export default function dom ( parsed, source, options, names ) {
builders._set.addBlock( statement ); builders._set.addBlock( statement );
} }
if ( templateProperties.onrender ) { if ( templateProperties.oncreate ) {
builders.init.addBlock( deindent` builders.init.addBlock( deindent`
if ( options._root ) { if ( options._root ) {
options._root._renderHooks.push({ fn: template.onrender, context: this }); options._root._renderHooks.push({ fn: template.oncreate, context: this });
} else { } else {
template.onrender.call( this ); template.oncreate.call( this );
} }
` ); ` );
} }
@ -348,7 +361,7 @@ export default function dom ( parsed, source, options, names ) {
}; };
${name}.prototype.teardown = function teardown ( detach ) { ${name}.prototype.teardown = function teardown ( detach ) {
this.fire( 'teardown' );${templateProperties.onteardown ? `\ntemplate.onteardown.call( this );` : ``} this.fire( 'teardown' );${templateProperties.ondestroy ? `\ntemplate.ondestroy.call( this );` : ``}
this._fragment.teardown( detach !== false ); this._fragment.teardown( detach !== false );
this._fragment = null; this._fragment = null;

@ -114,7 +114,7 @@ export default function ssr ( parsed, source, options, names ) {
} }
` ); ` );
templateProperties.components.properties.forEach( prop => { templateProperties.components.value.properties.forEach( prop => {
builders.renderCss.addLine( `addComponent( template.components.${prop.key.name} );` ); builders.renderCss.addLine( `addComponent( template.components.${prop.key.name} );` );
}); });
} }

@ -36,8 +36,6 @@ export default function validate ( parsed, source, { onerror, onwarn, name, file
}); });
}, },
templateProperties: {},
names: [], names: [],
namespace: null namespace: null

@ -23,10 +23,21 @@ export default function validateJs ( validator, js ) {
checkForComputedKeys( validator, node.declaration.properties ); checkForComputedKeys( validator, node.declaration.properties );
checkForDupes( validator, node.declaration.properties ); checkForDupes( validator, node.declaration.properties );
const templateProperties = {};
node.declaration.properties.forEach( prop => { node.declaration.properties.forEach( prop => {
validator.templateProperties[ prop.key.name ] = prop; templateProperties[ prop.key.name ] = prop;
}); });
// Remove these checks in version 2
if ( templateProperties.oncreate && templateProperties.onrender ) {
validator.error( 'Cannot have both oncreate and onrender', templateProperties.onrender.start );
}
if ( templateProperties.ondestroy && templateProperties.onteardown ) {
validator.error( 'Cannot have both ondestroy and onteardown', templateProperties.onteardown.start );
}
// ensure all exported props are valid // ensure all exported props are valid
node.declaration.properties.forEach( prop => { node.declaration.properties.forEach( prop => {
const propValidator = propValidators[ prop.key.name ]; const propValidator = propValidators[ prop.key.name ];
@ -45,8 +56,8 @@ export default function validateJs ( validator, js ) {
} }
}); });
if ( validator.templateProperties.namespace ) { if ( templateProperties.namespace ) {
const ns = validator.templateProperties.namespace.value.value; const ns = templateProperties.namespace.value.value;
validator.namespace = namespaces[ ns ] || ns; validator.namespace = namespaces[ ns ] || ns;
} }

@ -1,5 +1,7 @@
import data from './data.js'; import data from './data.js';
import computed from './computed.js'; import computed from './computed.js';
import oncreate from './oncreate.js';
import ondestroy from './ondestroy.js';
import onrender from './onrender.js'; import onrender from './onrender.js';
import onteardown from './onteardown.js'; import onteardown from './onteardown.js';
import helpers from './helpers.js'; import helpers from './helpers.js';
@ -11,6 +13,8 @@ import namespace from './namespace.js';
export default { export default {
data, data,
computed, computed,
oncreate,
ondestroy,
onrender, onrender,
onteardown, onteardown,
helpers, helpers,

@ -0,0 +1,9 @@
import usesThisOrArguments from '../utils/usesThisOrArguments.js';
export default function oncreate ( validator, prop ) {
if ( prop.value.type === 'ArrowFunctionExpression' ) {
if ( usesThisOrArguments( prop.value.body ) ) {
validator.error( `'oncreate' should be a function expression, not an arrow function expression`, prop.start );
}
}
}

@ -0,0 +1,9 @@
import usesThisOrArguments from '../utils/usesThisOrArguments.js';
export default function onteardown ( validator, prop ) {
if ( prop.value.type === 'ArrowFunctionExpression' ) {
if ( usesThisOrArguments( prop.value.body ) ) {
validator.error( `'onteardown' should be a function expression, not an arrow function expression`, prop.start );
}
}
}

@ -1,9 +1,6 @@
import usesThisOrArguments from '../utils/usesThisOrArguments.js'; import oncreate from './oncreate.js';
export default function onrender ( validator, prop ) { export default function onrender ( validator, prop ) {
if ( prop.value.type === 'ArrowFunctionExpression' ) { validator.warn( `'onrender' has been deprecated in favour of 'oncreate', and will cause an error in Svelte 2.x`, prop.start );
if ( usesThisOrArguments( prop.value.body ) ) { oncreate( validator, prop );
validator.error( `'onrender' should be a function expression, not an arrow function expression`, prop.start );
}
}
} }

@ -1,9 +1,6 @@
import usesThisOrArguments from '../utils/usesThisOrArguments.js'; import ondestroy from './ondestroy.js';
export default function onteardown ( validator, prop ) { export default function onteardown ( validator, prop ) {
if ( prop.value.type === 'ArrowFunctionExpression' ) { validator.warn( `'onteardown' has been deprecated in favour of 'ondestroy', and will cause an error in Svelte 2.x`, prop.start );
if ( usesThisOrArguments( prop.value.body ) ) { ondestroy( validator, prop );
validator.error( `'onteardown' should be a function expression, not an arrow function expression`, prop.start );
}
}
} }

Loading…
Cancel
Save