From 56b4fbcea2792cbe72d22dcf2e8b115056dabb05 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Thu, 1 Dec 2016 08:54:56 -0500 Subject: [PATCH] prevent compiler from generating ES2015+ code --- compiler/generate/index.js | 28 +++++++++---------- compiler/generate/visitors/EachBlock.js | 2 +- .../attributes/addElementAttributes.js | 2 +- test/test.js | 12 ++++++++ 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/compiler/generate/index.js b/compiler/generate/index.js index fa2ba7816a27..614668ae9ef9 100644 --- a/compiler/generate/index.js +++ b/compiler/generate/index.js @@ -238,7 +238,7 @@ export default function generate ( parsed, source, options ) { const topLevelStatements = []; const setStatements = [ deindent` - const oldState = state; + var oldState = state; state = Object.assign( {}, oldState, newState ); ` ]; @@ -392,19 +392,19 @@ export default function generate ( parsed, source, options ) { var callbacks = Object.create( null ); function dispatchObservers ( group, newState, oldState ) { - for ( const key in group ) { + for ( var key in group ) { if ( !( key in newState ) ) continue; - const newValue = newState[ key ]; - const oldValue = oldState[ key ]; + var newValue = newState[ key ]; + var oldValue = oldState[ key ]; if ( newValue === oldValue && typeof newValue !== 'object' ) continue; - const callbacks = group[ key ]; + var callbacks = group[ key ]; if ( !callbacks ) continue; - for ( let i = 0; i < callbacks.length; i += 1 ) { - const callback = callbacks[i]; + for ( var i = 0; i < callbacks.length; i += 1 ) { + var callback = callbacks[i]; if ( callback.__calling ) continue; callback.__calling = true; @@ -431,32 +431,32 @@ export default function generate ( parsed, source, options ) { ${setStatements.join( '\n\n' )} }; - this.observe = function ( key, callback, options = {} ) { - const group = options.defer ? observers.deferred : observers.immediate; + this.observe = function ( key, callback, options ) { + var group = ( options && options.defer ) ? observers.deferred : observers.immediate; ( group[ key ] || ( group[ key ] = [] ) ).push( callback ); - if ( options.init !== false ) { + if ( !options || options.init !== false ) { callback.__calling = true; callback.call( component, state[ key ] ); callback.__calling = false; } return { - cancel () { - const index = group[ key ].indexOf( callback ); + cancel: function () { + var index = group[ key ].indexOf( callback ); if ( ~index ) group[ key ].splice( index, 1 ); } }; }; this.on = function on ( eventName, handler ) { - const handlers = callbacks[ eventName ] || ( callbacks[ eventName ] = [] ); + var handlers = callbacks[ eventName ] || ( callbacks[ eventName ] = [] ); handlers.push( handler ); return { cancel: function () { - const index = handlers.indexOf( handler ); + var index = handlers.indexOf( handler ); if ( ~index ) handlers.splice( index, 1 ); } }; diff --git a/compiler/generate/visitors/EachBlock.js b/compiler/generate/visitors/EachBlock.js index 474eabce64e2..1cdce184e9f7 100644 --- a/compiler/generate/visitors/EachBlock.js +++ b/compiler/generate/visitors/EachBlock.js @@ -48,7 +48,7 @@ export default { ` ); 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 ); } diff --git a/compiler/generate/visitors/attributes/addElementAttributes.js b/compiler/generate/visitors/attributes/addElementAttributes.js index cb81a9a1f6c4..8637266ffdd5 100644 --- a/compiler/generate/visitors/attributes/addElementAttributes.js +++ b/compiler/generate/visitors/attributes/addElementAttributes.js @@ -143,7 +143,7 @@ export default function addElementAttributes ( generator, node, local ) { if ( attribute.name in generator.events ) { 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} }); ` ); diff --git a/test/test.js b/test/test.js index 481df8478f0f..9d132849e3c8 100644 --- a/test/test.js +++ b/test/test.js @@ -1,8 +1,10 @@ import deindent from '../compiler/generate/utils/deindent.js'; +import spaces from '../compiler/utils/spaces.js'; import assert from 'assert'; import * as path from 'path'; import * as fs from 'fs'; import jsdom from 'jsdom'; +import * as acorn from 'acorn'; import * as consoleGroup from 'console-group'; consoleGroup.install(); @@ -241,6 +243,16 @@ describe( 'svelte', () => { return `${i}: ${line.replace( /^\t+/, match => match.split( '\t' ).join( ' ' ) )}`; }).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; let SvelteComponent;