Comparing version 0.1.0 to 0.2.0
# buble changelog | ||
## 0.2.0 | ||
* Support for a bunch more ES2015 features | ||
## 0.1.0 | ||
* First (experimental) release |
{ | ||
"name": "buble", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Common sense JavaScript transpilation", | ||
@@ -5,0 +5,0 @@ "main": "dist/buble.umd.js", |
@@ -7,3 +7,4 @@ import { parse } from 'acorn'; | ||
ecmaVersion: 6, | ||
preserveParens: true | ||
preserveParens: true, | ||
sourceType: 'module' | ||
}); | ||
@@ -10,0 +11,0 @@ |
@@ -7,2 +7,6 @@ import wrap from './wrap.js'; | ||
initialise () { | ||
this.thisAlias = null; | ||
this.argumentsAlias = null; | ||
this.defaultParameters = []; | ||
this.isFunctionBlock = this.parent.type === 'Root' || /Function/.test( this.parent.type ); | ||
@@ -52,16 +56,120 @@ this.scope = new Scope({ | ||
transpile () { | ||
let insert = ''; | ||
const magicString = this.program.magicString; | ||
const start = this.body[0] ? this.body[0].start : this.start + 1; | ||
let addedStuff = false; | ||
if ( this.argumentsAlias ) { | ||
const assignment = `var ${this.argumentsAlias} = arguments;`; | ||
insert += this.indentation ? `${assignment}\n${this.indentation}` : ` ${assignment} `; | ||
magicString.insert( start, assignment ); | ||
addedStuff = true; | ||
} | ||
if ( this.thisAlias ) { | ||
if ( addedStuff ) magicString.insert( start, `\n${this.indentation}` ); | ||
const assignment = `var ${this.thisAlias} = this;`; | ||
insert += this.indentation ? `${assignment}\n${this.indentation}` : ` ${assignment} `; | ||
magicString.insert( start, assignment ); | ||
addedStuff = true; | ||
} | ||
if ( insert ) this.program.magicString.insert( this.body[0] ? this.body[0].start : this.start + 1, insert ); | ||
if ( /Function/.test( this.parent.type ) ) { | ||
// default parameters | ||
this.parent.params.filter( param => param.type === 'AssignmentPattern' ).forEach( param => { | ||
if ( addedStuff ) magicString.insert( start, `\n${this.indentation}` ); | ||
const lhs = `if ( ${param.left.name} === void 0 ) ${param.left.name}`; | ||
magicString | ||
.insert( start, `${lhs}` ) | ||
.move( param.left.end, param.right.end, start ) | ||
.insert( start, `;` ); | ||
addedStuff = true; | ||
}); | ||
// object pattern | ||
this.parent.params.filter( param => param.type === 'ObjectPattern' ).forEach( param => { | ||
const ref = this.scope.createIdentifier( 'ref' ); | ||
magicString.insert( param.start, ref ); | ||
let lastIndex = param.start; | ||
param.properties.forEach( prop => { | ||
magicString.remove( lastIndex, prop.value.start ); | ||
if ( addedStuff ) magicString.insert( start, `\n${this.indentation}` ); | ||
const key = prop.key.name; | ||
if ( prop.value.type === 'Identifier' ) { | ||
magicString.remove( prop.value.start, prop.value.end ); | ||
lastIndex = prop.value.end; | ||
const value = prop.value.name; | ||
magicString.insert( start, `var ${value} = ${ref}.${key};` ); | ||
} else if ( prop.value.type === 'AssignmentPattern' ) { | ||
magicString.remove( prop.value.start, prop.value.right.start ); | ||
lastIndex = prop.value.right.end; | ||
const value = prop.value.left.name; | ||
magicString | ||
.insert( start, `var ${ref}_${key} = ref.${key}, ${value} = ref_${key} === void 0 ? ` ) | ||
.move( prop.value.right.start, prop.value.right.end, start ) | ||
.insert( start, ` : ref_${key};` ); | ||
} | ||
else { | ||
throw new Error( `${prop.type} not currently supported` ); // TODO... | ||
} | ||
addedStuff = true; | ||
lastIndex = prop.end; | ||
}); | ||
magicString.remove( lastIndex, param.end ); | ||
}); | ||
// array pattern. TODO dry this out | ||
this.parent.params.filter( param => param.type === 'ArrayPattern' ).forEach( param => { | ||
const ref = this.scope.createIdentifier( 'ref' ); | ||
magicString.insert( param.start, ref ); | ||
let lastIndex = param.start; | ||
param.elements.forEach( ( element, i ) => { | ||
magicString.remove( lastIndex, element.start ); | ||
if ( addedStuff ) magicString.insert( start, `\n${this.indentation}` ); | ||
if ( element.type === 'Identifier' ) { | ||
magicString.remove( element.start, element.end ); | ||
lastIndex = element.end; | ||
magicString.insert( start, `var ${element.name} = ${ref}[${i}];` ); | ||
} else if ( element.type === 'AssignmentPattern' ) { | ||
magicString.remove( element.start, element.right.start ); | ||
lastIndex = element.right.end; | ||
const name = element.left.name; | ||
magicString | ||
.insert( start, `var ${ref}_${i} = ref[${i}], ${name} = ref_${i} === void 0 ? ` ) | ||
.move( element.right.start, element.right.end, start ) | ||
.insert( start, ` : ref_${i};` ); | ||
} | ||
else { | ||
throw new Error( `${element.type} not currently supported` ); // TODO... | ||
} | ||
addedStuff = true; | ||
lastIndex = element.end; | ||
}); | ||
magicString.remove( lastIndex, param.end ); | ||
}); | ||
} | ||
if ( addedStuff ) { | ||
magicString.insert( start, `\n\n${this.indentation}` ); | ||
} | ||
if ( this.isFunctionBlock ) { | ||
@@ -68,0 +176,0 @@ Object.keys( this.scope.allDeclarations ).forEach( name => { |
import AssignmentExpression from './AssignmentExpression.js'; | ||
import ArrowFunctionExpression from './ArrowFunctionExpression.js'; | ||
import ClassDeclaration from './ClassDeclaration.js'; | ||
import ForStatement from './ForStatement.js'; | ||
import FunctionDeclaration from './FunctionDeclaration.js'; | ||
import FunctionExpression from './FunctionExpression.js'; | ||
import Literal from './Literal.js'; | ||
import Identifier from './Identifier.js'; | ||
@@ -18,2 +22,6 @@ import Property from './Property.js'; | ||
ClassDeclaration, | ||
ForStatement, | ||
FunctionDeclaration, | ||
FunctionExpression, | ||
Literal, | ||
Identifier, | ||
@@ -20,0 +28,0 @@ Property, |
@@ -6,6 +6,8 @@ import Node from '../Node.js'; | ||
initialise () { | ||
this.isObjectPattern = this.id.type === 'ObjectPattern'; | ||
// disallow compound destructuring, for now at least | ||
if ( /Pattern/.test( this.id.type ) ) { | ||
this.id[ this.id.type === 'ObjectPattern' ? 'properties' : 'elements' ].forEach( property => { | ||
if ( /Pattern/.test( property.value.type ) ) { | ||
this.id[ this.isObjectPattern ? 'properties' : 'elements' ].forEach( node => { | ||
if ( /Pattern/.test( this.isObjectPattern ? node.value.type : node.type ) ) { | ||
unsupported( this, 'Compound destructuring is not supported' ); | ||
@@ -35,19 +37,15 @@ } | ||
if ( this.id.type === 'ObjectPattern' ) { | ||
const props = this.id.properties; | ||
const props = this.isObjectPattern ? this.id.properties : this.id.elements; | ||
this.program.magicString.remove( this.start, props[0].start ); | ||
this.program.magicString.remove( this.start, props[0].start ); | ||
props.forEach( property => { | ||
props.forEach( this.isObjectPattern ? | ||
property => { | ||
this.program.magicString.overwrite( property.start, property.end, `${property.value.name} = ${name}.${property.key.name}` ); | ||
} : | ||
( property, i ) => { | ||
this.program.magicString.overwrite( property.start, property.end, `${property.name} = ${name}[${i}]` ); | ||
}); | ||
this.program.magicString.remove( props[ props.length - 1 ].end, this.init.start ); | ||
} else if ( this.id.type === 'ArrayPattern' ) { | ||
} | ||
else { | ||
throw new Error( 'Well, this is unexpected.' ); | ||
} | ||
this.program.magicString.remove( props[ props.length - 1 ].end, this.init.start ); | ||
} | ||
@@ -54,0 +52,0 @@ |
126
test/test.js
@@ -52,2 +52,3 @@ var assert = require( 'assert' ); | ||
var this$1 = this; | ||
this.foo = 'bar'; | ||
@@ -69,2 +70,3 @@ var lexicallyScoped = function () { return this$1.foo; };` ); | ||
var arguments$1 = arguments; | ||
return function () { return arguments$1[0]; }; | ||
@@ -90,2 +92,3 @@ } | ||
var this$1 = this; | ||
return function () { | ||
@@ -232,4 +235,4 @@ return function () { return this$1 * arguments$1[0]; }; | ||
assert.equal( result, ` | ||
function log(square) { | ||
console.log(square); | ||
function log ( square ) { | ||
console.log( square ); | ||
} | ||
@@ -244,5 +247,3 @@ | ||
for ( var i = 0; i < 10; i += 1 ) { | ||
forLoop( i ); | ||
}` ); | ||
for ( var i = 0; i < 10; i += 1 ) forLoop( i );` ); | ||
}); | ||
@@ -293,3 +294,3 @@ | ||
describe.only( 'classes', () => { | ||
describe( 'classes', () => { | ||
it( 'transpiles a class declaration', () => { | ||
@@ -398,3 +399,3 @@ var source = ` | ||
it.only( 'transpiles a subclass with super calls', function () { | ||
it( 'transpiles a subclass with super calls', function () { | ||
var source = ` | ||
@@ -433,16 +434,62 @@ class Foo extends Bar { | ||
describe( 'destructuring', () => { | ||
it( 'destructures an identifier', () => { | ||
var source = `var { width, height } = point;`; | ||
it( 'destructures an identifier with an object pattern', () => { | ||
var source = `var { x, y } = point;`; | ||
var result = buble.transform( source ).code; | ||
assert.equal( result, `var width = point.width, height = point.height;` ); | ||
assert.equal( result, `var x = point.x, y = point.y;` ); | ||
}); | ||
it( 'destructures a non-identifier', () => { | ||
var source = `var { width, height } = getPoint();`; | ||
it( 'destructures a non-identifier with an object pattern', () => { | ||
var source = `var { x, y } = getPoint();`; | ||
var result = buble.transform( source ).code; | ||
assert.equal( result, `var ref = getPoint(), width = ref.width, height = ref.height;` ); | ||
assert.equal( result, `var ref = getPoint(), x = ref.x, y = ref.y;` ); | ||
}); | ||
it( 'destructures a parameter with an object pattern', () => { | ||
var source = ` | ||
function pythag ( { x, y: z = 1 } ) { | ||
return Math.sqrt( x * x + z * z ); | ||
}` | ||
var result = buble.transform( source ).code; | ||
assert.equal( result, ` | ||
function pythag ( ref ) { | ||
var x = ref.x; | ||
var ref_y = ref.y, z = ref_y === void 0 ? 1 : ref_y; | ||
return Math.sqrt( x * x + z * z ); | ||
}` ); | ||
}); | ||
it( 'destructures an identifier with an array pattern', () => { | ||
var source = `var [ x, y ] = point;`; | ||
var result = buble.transform( source ).code; | ||
assert.equal( result, `var x = point[0], y = point[1];` ); | ||
}); | ||
it( 'destructures a non-identifier with an array pattern', () => { | ||
var source = `var [ x, y ] = getPoint();`; | ||
var result = buble.transform( source ).code; | ||
assert.equal( result, `var ref = getPoint(), x = ref[0], y = ref[1];` ); | ||
}); | ||
it( 'destructures a parameter with an object pattern', () => { | ||
var source = ` | ||
function pythag ( [ x, z = 1 ] ) { | ||
return Math.sqrt( x * x + z * z ); | ||
}` | ||
var result = buble.transform( source ).code; | ||
assert.equal( result, ` | ||
function pythag ( ref ) { | ||
var x = ref[0]; | ||
var ref_1 = ref[1], z = ref_1 === void 0 ? 1 : ref_1; | ||
return Math.sqrt( x * x + z * z ); | ||
}` ); | ||
}); | ||
it( 'disallows compound destructuring', () => { | ||
@@ -454,2 +501,55 @@ assert.throws( () => { | ||
}); | ||
describe( 'default parameters', () => { | ||
it( 'transpiles default parameters', () => { | ||
var source = ` | ||
function foo ( a = 1, b = 2 ) { | ||
console.log( a, b ); | ||
} | ||
var bar = function ( a = 1, b = 2 ) { | ||
console.log( a, b ); | ||
};`; | ||
var result = buble.transform( source ).code; | ||
assert.equal( result, ` | ||
function foo ( a, b ) { | ||
if ( a === void 0 ) a = 1; | ||
if ( b === void 0 ) b = 2; | ||
console.log( a, b ); | ||
} | ||
var bar = function ( a, b ) { | ||
if ( a === void 0 ) a = 1; | ||
if ( b === void 0 ) b = 2; | ||
console.log( a, b ); | ||
};` ); | ||
}); | ||
}); | ||
describe( 'binary and octal', () => { | ||
it( 'transpiles binary numbers', () => { | ||
var source = ` | ||
var num = 0b111110111; | ||
var str = '0b111110111';`; | ||
var result = buble.transform( source ).code; | ||
assert.equal( result, ` | ||
var num = 503; | ||
var str = '0b111110111';` ); | ||
}); | ||
it( 'transpiles octal numbers', () => { | ||
var source = ` | ||
var num = 0o767; | ||
var str = '0o767';`; | ||
var result = buble.transform( source ).code; | ||
assert.equal( result, ` | ||
var num = 503; | ||
var str = '0o767';` ); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43123
35
1238