Skip to content

Commit

Permalink
Merge pull request #82 from sveltejs/gh-75
Browse files Browse the repository at this point in the history
prevent compiler from generating ES2015+ code
  • Loading branch information
Rich-Harris authored Dec 1, 2016
2 parents b3acc6d + 56b4fbc commit f9b596c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
28 changes: 14 additions & 14 deletions compiler/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
` ];

Expand Down Expand Up @@ -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;
Expand All @@ -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 );
}
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/generate/visitors/EachBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
});
` );
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f9b596c

Please sign in to comment.