prevent compiler from generating ES2015+ code

pull/82/head
Rich-Harris 8 years ago
parent a59dc64880
commit 56b4fbcea2

@ -238,7 +238,7 @@ export default function generate ( parsed, source, options ) {
const topLevelStatements = []; const topLevelStatements = [];
const setStatements = [ deindent` const setStatements = [ deindent`
const oldState = state; var oldState = state;
state = Object.assign( {}, oldState, newState ); state = Object.assign( {}, oldState, newState );
` ]; ` ];
@ -392,19 +392,19 @@ export default function generate ( parsed, source, options ) {
var callbacks = Object.create( null ); var callbacks = Object.create( null );
function dispatchObservers ( group, newState, oldState ) { function dispatchObservers ( group, newState, oldState ) {
for ( const key in group ) { for ( var key in group ) {
if ( !( key in newState ) ) continue; if ( !( key in newState ) ) continue;
const newValue = newState[ key ]; var newValue = newState[ key ];
const oldValue = oldState[ key ]; var oldValue = oldState[ key ];
if ( newValue === oldValue && typeof newValue !== 'object' ) continue; if ( newValue === oldValue && typeof newValue !== 'object' ) continue;
const callbacks = group[ key ]; var callbacks = group[ key ];
if ( !callbacks ) continue; if ( !callbacks ) continue;
for ( let i = 0; i < callbacks.length; i += 1 ) { for ( var i = 0; i < callbacks.length; i += 1 ) {
const callback = callbacks[i]; var callback = callbacks[i];
if ( callback.__calling ) continue; if ( callback.__calling ) continue;
callback.__calling = true; callback.__calling = true;
@ -431,32 +431,32 @@ export default function generate ( parsed, source, options ) {
${setStatements.join( '\n\n' )} ${setStatements.join( '\n\n' )}
}; };
this.observe = function ( key, callback, options = {} ) { this.observe = function ( key, callback, options ) {
const group = options.defer ? observers.deferred : observers.immediate; var group = ( options && options.defer ) ? observers.deferred : observers.immediate;
( group[ key ] || ( group[ key ] = [] ) ).push( callback ); ( group[ key ] || ( group[ key ] = [] ) ).push( callback );
if ( options.init !== false ) { if ( !options || options.init !== false ) {
callback.__calling = true; callback.__calling = true;
callback.call( component, state[ key ] ); callback.call( component, state[ key ] );
callback.__calling = false; callback.__calling = false;
} }
return { return {
cancel () { cancel: function () {
const index = group[ key ].indexOf( callback ); var index = group[ key ].indexOf( callback );
if ( ~index ) group[ key ].splice( index, 1 ); if ( ~index ) group[ key ].splice( index, 1 );
} }
}; };
}; };
this.on = function on ( eventName, handler ) { this.on = function on ( eventName, handler ) {
const handlers = callbacks[ eventName ] || ( callbacks[ eventName ] = [] ); var handlers = callbacks[ eventName ] || ( callbacks[ eventName ] = [] );
handlers.push( handler ); handlers.push( handler );
return { return {
cancel: function () { cancel: function () {
const index = handlers.indexOf( handler ); var index = handlers.indexOf( handler );
if ( ~index ) handlers.splice( index, 1 ); if ( ~index ) handlers.splice( index, 1 );
} }
}; };

@ -48,7 +48,7 @@ export default {
` ); ` );
generator.current.teardownStatements.push( deindent` generator.current.teardownStatements.push( deindent`
for ( let i = 0; i < ${name}_iterations.length; i += 1 ) { for ( var i = 0; i < ${name}_iterations.length; i += 1 ) {
${name}_iterations[i].teardown( detach ); ${name}_iterations[i].teardown( detach );
} }

@ -143,7 +143,7 @@ export default function addElementAttributes ( generator, node, local ) {
if ( attribute.name in generator.events ) { if ( attribute.name in generator.events ) {
local.init.push( deindent` local.init.push( deindent`
const ${handlerName} = template.events.${attribute.name}.call( component, ${local.name}, function ( event ) { var ${handlerName} = template.events.${attribute.name}.call( component, ${local.name}, function ( event ) {
${handlerBody} ${handlerBody}
}); });
` ); ` );

@ -1,8 +1,10 @@
import deindent from '../compiler/generate/utils/deindent.js'; import deindent from '../compiler/generate/utils/deindent.js';
import spaces from '../compiler/utils/spaces.js';
import assert from 'assert'; import assert from 'assert';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import jsdom from 'jsdom'; import jsdom from 'jsdom';
import * as acorn from 'acorn';
import * as consoleGroup from 'console-group'; import * as consoleGroup from 'console-group';
consoleGroup.install(); consoleGroup.install();
@ -241,6 +243,16 @@ describe( 'svelte', () => {
return `${i}: ${line.replace( /^\t+/, match => match.split( '\t' ).join( ' ' ) )}`; return `${i}: ${line.replace( /^\t+/, match => match.split( '\t' ).join( ' ' ) )}`;
}).join( '\n' ); }).join( '\n' );
// check that no ES2015+ syntax slipped in
try {
const startIndex = code.indexOf( 'function renderMainFragment' ); // may change!
const es5 = spaces( startIndex ) + code.slice( startIndex ).replace( /export default .+/, '' );
acorn.parse( es5, { ecmaVersion: 5 });
} catch ( err ) {
console.log( withLineNumbers ); // eslint-disable-line no-console
throw err;
}
cache[ path.resolve( `test/compiler/${dir}/main.html` ) ] = code; cache[ path.resolve( `test/compiler/${dir}/main.html` ) ] = code;
let SvelteComponent; let SvelteComponent;

Loading…
Cancel
Save