buble
Advanced tools
Comparing version 0.7.1 to 0.8.0
# buble changelog | ||
## 0.8.0 | ||
* Subclasses inherit static methods ([#33](https://gitlab.com/Rich-Harris/buble/issues/33)) | ||
* Performance enhancements ([!21](https://gitlab.com/Rich-Harris/buble/merge_requests/21)) | ||
## 0.7.1 | ||
@@ -4,0 +9,0 @@ |
{ | ||
"name": "buble", | ||
"version": "0.7.1", | ||
"version": "0.8.0", | ||
"description": "The blazing fast, batteries-included ES2015 compiler", | ||
@@ -62,5 +62,5 @@ "main": "dist/buble.umd.js", | ||
"chalk": "^1.1.3", | ||
"magic-string": "^0.12.1", | ||
"magic-string": "^0.13.1", | ||
"minimist": "^1.2.0" | ||
} | ||
} |
@@ -65,3 +65,3 @@ import wrap from './wrap.js'; | ||
transpile ( code, transforms ) { | ||
const start = this.body[0] ? this.body[0].start : this.start + 1; | ||
const start = this.parent.type === 'Root' || this.synthetic ? this.start : this.start + 1; | ||
@@ -72,33 +72,22 @@ const indentation = this.synthetic ? | ||
let addedStuff = false; | ||
let introStatementGenerators = []; | ||
if ( this.argumentsAlias ) { | ||
const assignment = `var ${this.argumentsAlias} = arguments;`; | ||
code.insert( start, assignment ); | ||
addedStuff = true; | ||
introStatementGenerators.push( ( start, prefix, suffix ) => { | ||
const assignment = `${prefix}var ${this.argumentsAlias} = arguments;${suffix}`; | ||
code.insertLeft( start, assignment ); | ||
}); | ||
} | ||
if ( this.thisAlias ) { | ||
if ( addedStuff ) code.insert( start, `\n${indentation}` ); | ||
const assignment = `var ${this.thisAlias} = this;`; | ||
code.insert( start, assignment ); | ||
addedStuff = true; | ||
introStatementGenerators.push( ( start, prefix, suffix ) => { | ||
const assignment = `${prefix}var ${this.thisAlias} = this;${suffix}`; | ||
code.insertLeft( start, assignment ); | ||
}); | ||
} | ||
if ( /Function/.test( this.parent.type ) ) { | ||
const params = this.parent.params; | ||
// default parameters | ||
if ( transforms.defaultParameter ) { | ||
params.filter( param => param.type === 'AssignmentPattern' ).forEach( param => { | ||
if ( addedStuff ) code.insert( start, `\n${indentation}` ); | ||
const lhs = `if ( ${param.left.name} === void 0 ) ${param.left.name}`; | ||
code | ||
.insert( start, `${lhs}` ) | ||
.move( param.left.end, param.right.end, start ) | ||
.insert( start, `;` ); | ||
addedStuff = true; | ||
}); | ||
this.transpileDefaultParameters( code, introStatementGenerators ); | ||
} | ||
@@ -108,178 +97,235 @@ | ||
if ( transforms.parameterDestructuring ) { | ||
params.filter( param => param.type === 'ObjectPattern' ).forEach( param => { | ||
const ref = this.scope.createIdentifier( 'ref' ); | ||
code.insert( param.start, ref ); | ||
this.transpileObjectPattern( code, introStatementGenerators ); | ||
this.transpileArrayPattern( code, introStatementGenerators ); | ||
} | ||
param.properties.forEach( prop => { | ||
const key = prop.key.name; | ||
// rest parameter | ||
if ( transforms.spreadRest ) { | ||
this.transpileRestElement( code, introStatementGenerators, indentation ); | ||
} | ||
} | ||
if ( prop.value.type === 'Identifier' ) { | ||
code.remove( prop.value.start, prop.value.end ); | ||
if ( transforms.letConst && this.isFunctionBlock ) { | ||
this.transpileBlockScopedIdentifiers( code ); | ||
} | ||
const value = prop.value.name; | ||
const declaration = this.scope.findDeclaration( value ); | ||
super.transpile( code, transforms ); | ||
if ( declaration.instances.length === 1 ) { | ||
const instance = declaration.instances[0]; | ||
code.overwrite( instance.start, instance.end, `${ref}.${key}` ); | ||
} else { | ||
if ( addedStuff ) code.insert( start, `\n${indentation}` ); | ||
code.insert( start, `var ${value} = ${ref}.${key};` ); | ||
addedStuff = true; | ||
} | ||
} | ||
if ( this.synthetic ) { | ||
if ( this.parent.type === 'ArrowFunctionExpression' ) { | ||
const expr = this.body[0]; | ||
else if ( prop.value.type === 'AssignmentPattern' ) { | ||
code.remove( prop.value.start, prop.value.right.start ); | ||
if ( introStatementGenerators.length ) { | ||
code.insertLeft( this.start, `{` ).insertRight( this.end, `${this.parent.getIndentation()}}` ); | ||
if ( addedStuff ) code.insert( start, `\n${indentation}` ); | ||
code.insertRight( expr.start, `\n${indentation}return ` ); | ||
code.insertLeft( expr.end, `;\n` ); | ||
} else if ( transforms.arrow ) { | ||
code.insertRight( expr.start, `{ return ` ); | ||
code.insertLeft( expr.end, `; }` ); | ||
} | ||
} | ||
const value = prop.value.left.name; | ||
code | ||
.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 if ( introStatementGenerators.length ) { | ||
code.insertLeft( this.start, `{` ).insertRight( this.end, `}` ); | ||
} | ||
} | ||
addedStuff = true; | ||
} | ||
let prefix = `\n${indentation}`; | ||
let suffix = ''; | ||
introStatementGenerators.forEach( ( fn, i ) => { | ||
if ( i === introStatementGenerators.length - 1 ) suffix = ( this.parent.type === 'Root' ? `\n` : `\n` ); | ||
fn( start, prefix, suffix ); | ||
}); | ||
} | ||
else { | ||
throw new CompileError( prop, `Compound destructuring is not supported` ); | ||
} | ||
}); | ||
transpileBlockScopedIdentifiers ( code ) { | ||
Object.keys( this.scope.blockScopedDeclarations ).forEach( name => { | ||
const declarations = this.scope.blockScopedDeclarations[ name ]; | ||
code.remove( param.start, param.end ); | ||
}); | ||
for ( let i = 0; i < declarations.length; i += 1 ) { | ||
const declaration = declarations[i]; | ||
let cont = false; // TODO implement proper continue... | ||
// array pattern. TODO dry this out | ||
params.filter( param => param.type === 'ArrayPattern' ).forEach( param => { | ||
const ref = this.scope.createIdentifier( 'ref' ); | ||
code.insert( param.start, ref ); | ||
if ( declaration.kind === 'for.let' ) { | ||
// special case | ||
const forStatement = declaration.node.findNearest( 'ForStatement' ); | ||
param.elements.forEach( ( element, i ) => { | ||
if ( addedStuff ) code.insert( start, `\n${indentation}` ); | ||
if ( forStatement.shouldRewriteAsFunction ) { | ||
const outerAlias = this.scope.createIdentifier( name ); | ||
const innerAlias = forStatement.reassigned[ name ] ? | ||
this.scope.createIdentifier( name ) : | ||
name; | ||
if ( element.type === 'Identifier' ) { | ||
code.remove( element.start, element.end ); | ||
declaration.name = outerAlias; | ||
code.insert( start, `var ${element.name} = ${ref}[${i}];` ); | ||
} else if ( element.type === 'AssignmentPattern' ) { | ||
code.remove( element.start, element.right.start ); | ||
forStatement.aliases[ name ] = { | ||
outer: outerAlias, | ||
inner: innerAlias | ||
}; | ||
const name = element.left.name; | ||
code | ||
.insert( start, `var ${ref}_${i} = ref[${i}], ${name} = ref_${i} === void 0 ? ` ) | ||
.move( element.right.start, element.right.end, start ) | ||
.insert( start, ` : ref_${i};` ); | ||
} | ||
for ( const identifier of declaration.instances ) { | ||
const alias = forStatement.body.contains( identifier ) ? | ||
innerAlias : | ||
outerAlias; | ||
else { | ||
throw new CompileError( element, `Compound destructuring is not supported` ); | ||
if ( name !== alias ) { | ||
code.overwrite( identifier.start, identifier.end, alias, true ); | ||
} | ||
} | ||
addedStuff = true; | ||
}); | ||
cont = true; | ||
} | ||
} | ||
code.remove( param.start, param.end ); | ||
}); | ||
if ( !cont ) { | ||
const alias = this.scope.createIdentifier( name ); | ||
if ( name !== alias ) { | ||
declaration.name = alias; | ||
for ( const identifier of declaration.instances ) { | ||
identifier.rewritten = true; | ||
code.overwrite( identifier.start, identifier.end, alias, true ); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
// rest parameter | ||
if ( transforms.spreadRest ) { | ||
const lastParam = params[ params.length - 1 ]; | ||
if ( lastParam && lastParam.type === 'RestElement' ) { | ||
const penultimateParam = params[ params.length - 2 ]; | ||
transpileDefaultParameters ( code, introStatementGenerators ) { | ||
this.parent.params.filter( param => param.type === 'AssignmentPattern' ).forEach( param => { | ||
introStatementGenerators.push( ( start, prefix, suffix ) => { | ||
const lhs = `${prefix}if ( ${param.left.name} === void 0 ) ${param.left.name}`; | ||
code | ||
.insertRight( param.left.end, `${lhs}` ) | ||
.move( param.left.end, param.right.end, start ) | ||
.insertLeft( param.right.end, `;${suffix}` ); | ||
}); | ||
}); | ||
} | ||
if ( penultimateParam ) { | ||
code.remove( penultimateParam ? penultimateParam.end : lastParam.start, lastParam.end ); | ||
} else { | ||
let start = lastParam.start, end = lastParam.end; // TODO https://gitlab.com/Rich-Harris/buble/issues/8 | ||
transpileObjectPattern ( code, introStatementGenerators ) { | ||
this.parent.params.filter( param => param.type === 'ObjectPattern' ).forEach( param => { | ||
const ref = this.scope.createIdentifier( 'ref' ); | ||
code.insertRight( param.start, ref ); | ||
while ( /\s/.test( code.original[ start - 1 ] ) ) start -= 1; | ||
while ( /\s/.test( code.original[ end ] ) ) end += 1; | ||
let c = param.start; | ||
code.remove( start, end ); | ||
} | ||
param.properties.forEach( prop => { | ||
code.remove( c, prop.value.start ); | ||
const key = prop.key.name; | ||
if ( addedStuff ) code.insert( start, `\n${indentation}` ); | ||
if ( prop.value.type === 'Identifier' ) { | ||
code.remove( prop.value.start, prop.value.end ); | ||
const name = lastParam.argument.name; | ||
const len = this.scope.createIdentifier( 'len' ); | ||
const count = params.length - 1; | ||
const value = prop.value.name; | ||
const declaration = this.scope.findDeclaration( value ); | ||
if ( count ) { | ||
code.insert( start, `var ${name} = [], ${len} = arguments.length - ${count};\n${indentation}while ( ${len}-- > 0 ) ${name}[ ${len} ] = arguments[ ${len} + ${count} ];` ); | ||
if ( declaration.instances.length === 1 ) { | ||
const instance = declaration.instances[0]; | ||
code.overwrite( instance.start, instance.end, `${ref}.${key}` ); | ||
} else { | ||
code.insert( start, `var ${name} = [], ${len} = arguments.length;\n${indentation}while ( ${len}-- ) ${name}[ ${len} ] = arguments[ ${len} ];` ); | ||
introStatementGenerators.push( ( start, prefix, suffix ) => { | ||
code.insertLeft( start, `${prefix}var ${value} = ${ref}.${key};${suffix}` ); | ||
}); | ||
} | ||
} | ||
addedStuff = true; | ||
else if ( prop.value.type === 'AssignmentPattern' ) { | ||
introStatementGenerators.push( ( start, prefix, suffix ) => { | ||
code.remove( prop.value.start, prop.value.right.start ); | ||
const value = prop.value.left.name; | ||
code | ||
.insertRight( prop.value.right.start, `${prefix}var ${ref}_${key} = ${ref}.${key}, ${value} = ${ref}_${key} === void 0 ? ` ) | ||
.insertLeft( prop.value.right.end, ` : ${ref}_${key};${suffix}` ) | ||
.move( prop.value.right.start, prop.value.right.end, start ); | ||
}); | ||
} | ||
} | ||
} | ||
if ( addedStuff ) { | ||
code.insert( start, `\n\n${indentation}` ); | ||
} | ||
else { | ||
throw new CompileError( prop, `Compound destructuring is not supported` ); | ||
} | ||
if ( transforms.letConst && this.isFunctionBlock ) { | ||
Object.keys( this.scope.blockScopedDeclarations ).forEach( name => { | ||
const declarations = this.scope.blockScopedDeclarations[ name ]; | ||
c = prop.value.end; | ||
}); | ||
for ( let i = 0; i < declarations.length; i += 1 ) { | ||
const declaration = declarations[i]; | ||
let cont = false; // TODO implement proper continue... | ||
code.remove( c, param.end ); | ||
}); | ||
} | ||
if ( declaration.kind === 'for.let' ) { | ||
// special case | ||
const forStatement = declaration.node.findNearest( 'ForStatement' ); | ||
transpileArrayPattern ( code, introStatementGenerators ) { | ||
// array pattern. TODO dry this out | ||
this.parent.params.filter( param => param.type === 'ArrayPattern' ).forEach( param => { | ||
const ref = this.scope.createIdentifier( 'ref' ); | ||
code.insertRight( param.start, ref ); | ||
if ( forStatement.shouldRewriteAsFunction ) { | ||
const outerAlias = this.scope.createIdentifier( name ); | ||
const innerAlias = forStatement.reassigned[ name ] ? | ||
this.scope.createIdentifier( name ) : | ||
name; | ||
let c = param.start; | ||
declaration.name = outerAlias; | ||
param.elements.forEach( ( element, i ) => { | ||
code.remove( c, element.start ); | ||
forStatement.aliases[ name ] = { | ||
outer: outerAlias, | ||
inner: innerAlias | ||
}; | ||
if ( element.type === 'Identifier' ) { | ||
code.remove( element.start, element.end ); | ||
for ( const identifier of declaration.instances ) { | ||
const alias = forStatement.body.contains( identifier ) ? | ||
innerAlias : | ||
outerAlias; | ||
introStatementGenerators.push( ( start, prefix, suffix ) => { | ||
code.insertLeft( start, `${prefix}var ${element.name} = ${ref}[${i}];${suffix}` ); | ||
}); | ||
} else if ( element.type === 'AssignmentPattern' ) { | ||
introStatementGenerators.push( ( start, prefix, suffix ) => { | ||
code.remove( element.start, element.right.start ); | ||
if ( name !== alias ) { | ||
code.overwrite( identifier.start, identifier.end, alias, true ); | ||
} | ||
} | ||
const name = element.left.name; | ||
code | ||
.insertRight( element.right.start, `${prefix}var ${ref}_${i} = ref[${i}], ${name} = ref_${i} === void 0 ? ` ) | ||
.insertLeft( element.right.end, ` : ref_${i};${suffix}` ) | ||
.move( element.right.start, element.right.end, start ); | ||
}); | ||
} | ||
cont = true; | ||
} | ||
} | ||
else { | ||
throw new CompileError( element, `Compound destructuring is not supported` ); | ||
} | ||
if ( !cont ) { | ||
const alias = this.scope.createIdentifier( name ); | ||
c = element.end; | ||
}); | ||
if ( name !== alias ) { | ||
declaration.name = alias; | ||
code.remove( c, param.end ); | ||
}); | ||
} | ||
for ( const identifier of declaration.instances ) { | ||
code.overwrite( identifier.start, identifier.end, alias, true ); | ||
} | ||
} | ||
} | ||
transpileRestElement ( code, introStatementGenerators, indentation ) { | ||
const params = this.parent.params; | ||
const lastParam = params[ params.length - 1 ]; | ||
if ( lastParam && lastParam.type === 'RestElement' ) { | ||
introStatementGenerators.push( ( start, prefix, suffix ) => { | ||
const penultimateParam = params[ params.length - 2 ]; | ||
if ( penultimateParam ) { | ||
code.remove( penultimateParam ? penultimateParam.end : lastParam.start, lastParam.end ); | ||
} else { | ||
let start = lastParam.start, end = lastParam.end; // TODO https://gitlab.com/Rich-Harris/buble/issues/8 | ||
while ( /\s/.test( code.original[ start - 1 ] ) ) start -= 1; | ||
while ( /\s/.test( code.original[ end ] ) ) end += 1; | ||
code.remove( start, end ); | ||
} | ||
const name = lastParam.argument.name; | ||
const len = this.scope.createIdentifier( 'len' ); | ||
const count = params.length - 1; | ||
if ( count ) { | ||
code.insertLeft( start, `${prefix}var ${name} = [], ${len} = arguments.length - ${count};\n${indentation}while ( ${len}-- > 0 ) ${name}[ ${len} ] = arguments[ ${len} + ${count} ];${suffix}` ); | ||
} else { | ||
code.insertLeft( start, `${prefix}var ${name} = [], ${len} = arguments.length;\n${indentation}while ( ${len}-- ) ${name}[ ${len} ] = arguments[ ${len} ];${suffix}` ); | ||
} | ||
}); | ||
} | ||
if ( this.synthetic && this.parent.type === 'ArrowFunctionExpression' ) { | ||
code.insert( this.body[0].start, 'return ' ); | ||
} | ||
super.transpile( code, transforms ); | ||
} | ||
} |
@@ -13,3 +13,3 @@ import Node from '../Node.js'; | ||
} else { | ||
code.insert( this.start + 1, `].concat(` ); | ||
code.insertLeft( this.start + 1, `].concat(` ); | ||
} | ||
@@ -16,0 +16,0 @@ |
import Node from '../Node.js'; | ||
import { find } from '../../utils/array.js'; | ||
@@ -15,4 +14,2 @@ export default class ArrowFunctionExpression extends Node { | ||
} else { | ||
code.insert( this.start, 'function ' ); | ||
let parenthesised = false; | ||
@@ -27,6 +24,7 @@ let charIndex = this.start; | ||
if ( !parenthesised ) { | ||
code.insert( this.start, '( ' ); | ||
} | ||
let start = 'function '; | ||
if ( !parenthesised ) start += '( '; | ||
code.insertRight( this.start, start ); | ||
charIndex = this.params[ this.params.length -1 ].end; | ||
@@ -40,21 +38,4 @@ while ( code.original[ charIndex ] !== '=' ) charIndex += 1; | ||
if ( this.body.synthetic ) { | ||
if ( find( this.params, param => param.type === 'RestElement' || /Pattern/.test( param.type ) ) ) { | ||
const indentation = this.getIndentation(); | ||
code.insert( this.body.start, `{\n${indentation}${code.getIndentString()}` ); | ||
super.transpile( code, transforms ); | ||
code.insert( this.body.end, `;\n${indentation}}` ); | ||
} | ||
else if ( transforms.arrow ) { | ||
code.insert( this.body.start, `{ ` ); | ||
super.transpile( code, transforms ); | ||
code.insertAfter( this.body.end, `; }` ); | ||
} | ||
} | ||
else { | ||
super.transpile( code, transforms ); | ||
} | ||
super.transpile( code, transforms ); | ||
} | ||
} |
@@ -74,3 +74,3 @@ import Node from '../Node.js'; | ||
if ( needsObjectVar && needsPropertyVar ) { | ||
code.insert( statement.start, `var ${object} = ` ); | ||
code.insertRight( statement.start, `var ${object} = ` ); | ||
code.overwrite( left.object.end, left.property.start, `;\n${i0}var ${property} = ` ); | ||
@@ -81,13 +81,14 @@ code.overwrite( left.property.end, left.end, `;\n${i0}${object}[${property}]` ); | ||
else if ( needsObjectVar ) { | ||
code.insert( statement.start, `var ${object} = ` ); | ||
code.insert( left.object.end, `;\n${i0}` ); | ||
code.insert( left.object.end, object ); | ||
code.insertRight( statement.start, `var ${object} = ` ); | ||
code.insertLeft( left.object.end, `;\n${i0}` ); | ||
code.insertLeft( left.object.end, object ); | ||
} | ||
else if ( needsPropertyVar ) { | ||
code.insert( statement.start, `var ${property} = ` ); | ||
code.move( left.property.start, left.property.end, statement.start ); | ||
code.insert( statement.start, `;\n${i0}` ); | ||
code.insertRight( left.property.start, `var ${property} = ` ); | ||
code.insertLeft( left.property.end, `;\n${i0}` ); | ||
code.move( left.property.start, left.property.end, this.start ); | ||
code.overwrite( left.object.end, left.property.start, `[${property}]` ); | ||
code.insertLeft( left.object.end, `[${property}]` ); | ||
code.remove( left.object.end, left.property.start ); | ||
code.remove( left.property.end, left.end ); | ||
@@ -101,8 +102,6 @@ } | ||
if ( needsPropertyVar ) declarators.push( property ); | ||
code.insert( statement.start, `var ${declarators.join( ', ' )};\n${i0}` ); | ||
code.insertRight( statement.start, `var ${declarators.join( ', ' )};\n${i0}` ); | ||
code.insert( left.start, `( ` ); | ||
if ( needsObjectVar && needsPropertyVar ) { | ||
code.insert( left.start, `${object} = ` ); | ||
code.insertRight( left.start, `( ${object} = ` ); | ||
code.overwrite( left.object.end, left.property.start, `, ${property} = ` ); | ||
@@ -113,10 +112,11 @@ code.overwrite( left.property.end, left.end, `, ${object}[${property}]` ); | ||
else if ( needsObjectVar ) { | ||
code.insert( left.start, `${object} = ` ); | ||
code.insert( left.object.end, `, ${object}` ); | ||
code.insertRight( left.start, `( ${object} = ` ); | ||
code.insertLeft( left.object.end, `, ${object}` ); | ||
} | ||
else if ( needsPropertyVar ) { | ||
code.insert( left.start, `${property} = ` ); | ||
code.insertRight( left.property.start, `( ${property} = ` ); | ||
code.insertLeft( left.property.end, `, ` ); | ||
code.move( left.property.start, left.property.end, left.start ); | ||
code.insert( left.start, `, ` ); | ||
code.overwrite( left.object.end, left.property.start, `[${property}]` ); | ||
@@ -126,3 +126,3 @@ code.remove( left.property.end, left.end ); | ||
code.insert( this.end, ` )` ); | ||
code.insertLeft( this.end, ` )` ); | ||
} | ||
@@ -133,4 +133,4 @@ | ||
code.insert( this.right.start, `Math.pow( ${base}, ` ); | ||
code.insert( this.right.end, ` )` ); | ||
code.insertRight( this.right.start, `Math.pow( ${base}, ` ); | ||
code.insertLeft( this.right.end, ` )` ); | ||
} | ||
@@ -137,0 +137,0 @@ |
@@ -6,5 +6,5 @@ import Node from '../Node.js'; | ||
if ( this.operator === '**' && transforms.exponentiation ) { | ||
code.insert ( this.start, `Math.pow( ` ); | ||
code.insertRight( this.start, `Math.pow( ` ); | ||
code.overwrite( this.left.end, this.right.start, `, ` ); | ||
code.insert ( this.end, ` )` ); | ||
code.insertLeft( this.end, ` )` ); | ||
} | ||
@@ -11,0 +11,0 @@ super.transpile( code, transforms ); |
import Node from '../Node.js'; | ||
import CompileError from '../../utils/CompileError.js'; | ||
@@ -18,4 +17,4 @@ export default class CallExpression extends Node { | ||
context = this.findScope( true ).createIdentifier( 'ref' ); | ||
code.insert( statement.start, `var ${context} = ` ); | ||
code.insert( statement.end, `;\n${i0}${context}` ); | ||
code.insertRight( statement.start, `var ${context} = ` ); | ||
code.insertLeft( statement.end, `;\n${i0}${context}` ); | ||
} | ||
@@ -26,3 +25,3 @@ } else { | ||
code.insert( this.callee.end, '.apply' ); | ||
code.insertLeft( this.callee.end, '.apply' ); | ||
@@ -32,7 +31,7 @@ const penultimateArgument = this.arguments[ this.arguments.length - 2 ]; | ||
if ( penultimateArgument ) { | ||
code.insert( this.arguments[0].start, `${context}, [ ` ); | ||
code.insertRight( this.arguments[0].start, `${context}, [ ` ); | ||
code.overwrite( penultimateArgument.end, lastArgument.start, ` ].concat( ` ); | ||
code.insert( lastArgument.end, ` )` ); | ||
code.insertLeft( lastArgument.end, ` )` ); | ||
} else { | ||
code.insert( lastArgument.start, `${context}, ` ); | ||
code.insertRight( lastArgument.start, `${context}, ` ); | ||
} | ||
@@ -39,0 +38,0 @@ |
@@ -11,3 +11,4 @@ import Node from '../Node.js'; | ||
const indentStr = code.getIndentString(); | ||
let indentation = this.getIndentation() + ( inFunctionExpression ? indentStr : '' ); | ||
const i0 = this.getIndentation() + ( inFunctionExpression ? indentStr : '' ); | ||
const i1 = i0 + indentStr; | ||
@@ -34,26 +35,24 @@ const constructorIndex = findIndex( this.body, node => node.kind === 'constructor' ); | ||
if ( !inFunctionExpression ) code.insert( constructor.end, ';' ); | ||
if ( constructorIndex > 0 ) { | ||
if ( nextMethod ) { | ||
code.insert( nextMethod.start, `\n\n${indentation}` ); | ||
} else { | ||
code.insert( constructor.end, `\n\n${indentation}` ); | ||
} | ||
} | ||
} else { | ||
const fn = `function ${name} () {` + ( superName ? | ||
`\n${indentation}${indentStr}${superName}.apply(this, arguments);\n${indentation}}` : | ||
`}` ) + ( inFunctionExpression ? '' : ';' ) + ( this.body.length ? `\n\n${indentation}` : '' ); | ||
code.insert( this.start, fn ); | ||
if ( !inFunctionExpression ) code.insertLeft( constructor.end, ';' ); | ||
} | ||
if ( this.parent.superClass ) { | ||
let inheritanceBlock = `${name}.prototype = Object.create( ${superName} && ${superName}.prototype );\n${indentation}${name}.prototype.constructor = ${name};`; | ||
let inheritanceBlock = `if ( ${superName} ) ${name}.__proto__ = ${superName};\n${i0}${name}.prototype = Object.create( ${superName} && ${superName}.prototype );\n${i0}${name}.prototype.constructor = ${name};`; | ||
if ( constructor ) { | ||
code.insert( constructor.end, `\n\n${indentation}` + inheritanceBlock ); | ||
code.insertLeft( constructor.end, `\n\n${i0}` + inheritanceBlock ); | ||
} else { | ||
code.insert( this.start, inheritanceBlock + `\n\n${indentation}` ); | ||
const fn = `function ${name} () {` + ( superName ? | ||
`\n${i1}${superName}.apply(this, arguments);\n${i0}}` : | ||
`}` ) + ( inFunctionExpression ? '' : ';' ) + ( this.body.length ? `\n\n${i0}` : '' ); | ||
inheritanceBlock = fn + inheritanceBlock; | ||
code.insertRight( this.start, inheritanceBlock + `\n\n${i0}` ); | ||
} | ||
} else if ( !constructor ) { | ||
let fn = `function ${name} () {}`; | ||
if ( this.parent.type === 'ClassDeclaration' ) fn += ';'; | ||
if ( this.body.length ) fn += `\n\n${i0}`; | ||
code.insertRight( this.start, fn ); | ||
} | ||
@@ -68,3 +67,3 @@ | ||
this.body.forEach( method => { | ||
this.body.forEach( ( method, i ) => { | ||
if ( method.kind === 'constructor' ) { | ||
@@ -100,5 +99,10 @@ code.overwrite( method.key.start, method.key.end, `function ${name}` ); | ||
code.insert( method.start, `${lhs} = function` + ( method.value.generator ? '*' : '' ) + ( isAccessor ? '' : ' ' ) ); | ||
code.insert( method.end, ';' ); | ||
const insertNewlines = ( constructorIndex > 0 && i === constructorIndex + 1 ) || | ||
( i === 0 && constructorIndex === this.body.length - 1 ); | ||
if ( insertNewlines ) lhs = `\n\n${i0}${lhs}`; | ||
code.insertRight( method.start, `${lhs} = function` + ( method.value.generator ? '*' : '' ) + ( isAccessor ? '' : ' ' ) ); | ||
code.insertLeft( method.end, ';' ); | ||
if ( method.value.generator ) code.remove( method.start, method.key.start ); | ||
@@ -127,8 +131,8 @@ | ||
if ( constructor ) { | ||
code.insert( constructor.end, `\n\n${indentation}${intro.join( `\n${indentation}` )}` ); | ||
code.insertLeft( constructor.end, `\n\n${i0}${intro.join( `\n${i0}` )}` ); | ||
} else { | ||
code.insert( this.start, `${intro.join( `\n${indentation}` )}\n\n${indentation}` ); | ||
code.insertRight( this.start, `${intro.join( `\n${i0}` )}\n\n${i0}` ); | ||
} | ||
code.insert( this.end, `\n\n${indentation}${outro.join( `\n${indentation}` )}` ); | ||
code.insertLeft( this.end, `\n\n${i0}${outro.join( `\n${i0}` )}` ); | ||
} | ||
@@ -135,0 +139,0 @@ } |
@@ -33,5 +33,5 @@ import Node from '../Node.js'; | ||
if ( this.superClass ) { | ||
code.insert( this.end, `\n\n${indentation}${indentStr}return ${this.name};\n${indentation}}(` ); | ||
code.insertLeft( this.end, `\n\n${indentation}${indentStr}return ${this.name};\n${indentation}}(` ); | ||
code.move( this.superClass.start, this.superClass.end, this.end ); | ||
code.insert( this.end, '));' ); | ||
code.insertRight( this.end, '));' ); | ||
} | ||
@@ -38,0 +38,0 @@ } |
@@ -23,3 +23,3 @@ import Node from '../Node.js'; | ||
code.remove( this.superClass.end, this.body.start ); | ||
code.insert( this.start, `(function (${superName}) {\n${i1}` ); | ||
code.insertLeft( this.start, `(function (${superName}) {\n${i1}` ); | ||
} else { | ||
@@ -34,7 +34,7 @@ code.overwrite( this.start, this.body.start, `(function () {\n${i1}` ); | ||
if ( this.superClass ) { | ||
code.insert( this.end, outro ); | ||
code.insertLeft( this.end, outro ); | ||
code.move( this.superClass.start, this.superClass.end, this.end ); | ||
code.insert( this.end, '))' ); | ||
code.insertRight( this.end, '))' ); | ||
} else { | ||
code.insert( this.end, `\n\n${i1}return ${this.name};\n${i0}}())` ); | ||
code.insertLeft( this.end, `\n\n${i1}return ${this.name};\n${i0}}())` ); | ||
} | ||
@@ -41,0 +41,0 @@ } |
@@ -14,7 +14,7 @@ import Node from '../Node.js'; | ||
if ( this.declaration.type === 'ClassDeclaration' ) { | ||
code.insert( this.end, `\n\n${this.getIndentation()}` ); | ||
code.insertLeft( this.end, `\n\n${this.getIndentation()}` ); | ||
code.move( this.start, this.declaration.start, this.end ); | ||
code.insert( this.end, `${this.declaration.id.name};` ); | ||
code.insertRight( this.end, `${this.declaration.id.name};` ); | ||
} | ||
} | ||
} |
@@ -24,4 +24,4 @@ import LoopStatement from './shared/LoopStatement.js'; | ||
if ( this.body.synthetic ) { | ||
code.insert( this.body.body[0].start, `{\n${i1}` ); | ||
code.insert( this.body.body[0].end, `\n${i0}}` ); | ||
code.insertRight( this.left.start, `{\n${i1}` ); | ||
code.insertLeft( this.body.body[0].end, `\n${i0}}` ); | ||
} | ||
@@ -33,6 +33,6 @@ | ||
code.move( this.left.start, this.left.end, bodyStart ); | ||
code.insert( bodyStart, ` = ${list}[${key}];\n\n${i1}` ); | ||
code.insertLeft( this.left.end, ` = ${list}[${key}];\n\n${i1}` ); | ||
code.insert( this.right.start, `var ${key} = 0, ${list} = ` ); | ||
code.insert( this.right.end, `; ${key} < ${list}.length; ${key} += 1` ); | ||
code.insertRight( this.right.start, `var ${key} = 0, ${list} = ` ); | ||
code.insertLeft( this.right.end, `; ${key} < ${list}.length; ${key} += 1` ); | ||
@@ -39,0 +39,0 @@ super.transpile( code, transforms ); |
@@ -28,6 +28,6 @@ import LoopStatement from './shared/LoopStatement.js'; | ||
if ( this.body.synthetic ) { | ||
code.insert( this.body.body[0].end, `; ${updates.join(` `)}` ); | ||
code.insertLeft( this.body.body[0].end, `; ${updates.join(` `)}` ); | ||
} else { | ||
const lastStatement = this.body.body[ this.body.body.length - 1 ]; | ||
code.insert( lastStatement.end, `\n\n${i1}${updates.join(`\n${i1}`)}` ); | ||
code.insertLeft( lastStatement.end, `\n\n${i1}${updates.join(`\n${i1}`)}` ); | ||
} | ||
@@ -34,0 +34,0 @@ } |
@@ -8,3 +8,3 @@ import Node from '../Node.js'; | ||
code.overwrite( this.object.end, this.property.start, `['` ); | ||
code.insert( this.property.end, `']` ); | ||
code.insertLeft( this.property.end, `']` ); | ||
} | ||
@@ -11,0 +11,0 @@ |
@@ -17,7 +17,7 @@ import Node from '../Node.js'; | ||
if ( this.shorthand ) { | ||
code.insert( this.start, `${this.key.name}: ` ); | ||
code.insertRight( this.start, `${this.key.name}: ` ); | ||
} else if ( this.method ) { | ||
const name = this.findScope( true ).createIdentifier( this.key.name ); | ||
if ( this.value.generator ) code.remove( this.start, this.key.start ); | ||
code.insert( this.key.end, `: function${this.value.generator ? '*' : ''} ${name}` ); | ||
code.insertLeft( this.key.end, `: function${this.value.generator ? '*' : ''} ${name}` ); | ||
} | ||
@@ -27,4 +27,4 @@ } | ||
if ( transforms.reservedProperties && reserved[ this.key.name ] ) { | ||
code.insert( this.key.start, `'` ); | ||
code.insert( this.key.end, `'` ); | ||
code.insertRight( this.key.start, `'` ); | ||
code.insertLeft( this.key.end, `'` ); | ||
} | ||
@@ -31,0 +31,0 @@ |
@@ -19,9 +19,9 @@ import Node from '../Node.js'; | ||
const shouldWrap = this.shouldWrap && this.loop && this.loop.shouldRewriteAsFunction; | ||
if ( shouldWrap ) code.insert( this.argument.start, `{ v: ` ); | ||
if ( shouldWrap ) code.insertRight( this.argument.start, `{ v: ` ); | ||
if ( this.argument ) this.argument.transpile( code, transforms ); | ||
if ( shouldWrap ) code.insert( this.argument.end, ` }` ); | ||
if ( shouldWrap ) code.insertLeft( this.argument.end, ` }` ); | ||
} | ||
} | ||
} |
@@ -59,10 +59,6 @@ import Node from '../../Node.js'; | ||
code.insert( this.start, before ); | ||
code.move( this.body.start, this.body.end, this.start ); | ||
code.insert( this.start, after ); | ||
code.insertRight( this.body.start, before ); | ||
code.insertLeft( this.body.end, after ); | ||
code.move( this.start, this.body.start, this.body.end ); | ||
const index = this.type === 'DoWhileStatement' ? | ||
this.start + 2 : | ||
this.end; | ||
if ( this.canBreak || this.canReturn ) { | ||
@@ -76,3 +72,3 @@ const returned = functionScope.createIdentifier( 'returned' ); | ||
code.insert( index, insert ); | ||
code.insertRight( this.body.end, insert ); | ||
} else { | ||
@@ -84,3 +80,3 @@ const callExpression = `${loop}(${argString});`; | ||
} else { | ||
code.insert( index, callExpression ); | ||
code.insertRight( this.body.end, callExpression ); | ||
} | ||
@@ -87,0 +83,0 @@ } |
@@ -31,3 +31,6 @@ import Node from '../Node.js'; | ||
if ( transforms.classes ) { | ||
const expression = this.isCalled ? this.superClassName : `${this.superClassName}.prototype`; | ||
const expression = ( this.isCalled || this.method.static ) ? | ||
this.superClassName : | ||
`${this.superClassName}.prototype`; | ||
code.overwrite( this.start, this.end, expression, true ); | ||
@@ -38,8 +41,8 @@ | ||
if ( callExpression && callExpression.type === 'CallExpression' ) { | ||
code.insert( callExpression.callee.end, '.call' ); | ||
code.insertLeft( callExpression.callee.end, '.call' ); | ||
if ( callExpression.arguments.length ) { | ||
code.insert( callExpression.arguments[0].start, `this, ` ); | ||
code.insertLeft( callExpression.arguments[0].start, `this, ` ); | ||
} else { | ||
code.insert( callExpression.end - 1, `this` ); | ||
code.insertLeft( callExpression.end - 1, `this` ); | ||
} | ||
@@ -46,0 +49,0 @@ } |
@@ -35,3 +35,3 @@ import Node from '../Node.js'; | ||
if ( parenthesise ) code.insert( this.start, '(' ); | ||
if ( parenthesise ) code.insertRight( this.start, '(' ); | ||
@@ -56,3 +56,3 @@ let lastIndex = this.start; | ||
if ( parenthesise ) code.insert( node.end, ')' ); | ||
if ( parenthesise ) code.insertLeft( node.end, ')' ); | ||
} | ||
@@ -59,0 +59,0 @@ |
@@ -28,8 +28,18 @@ import Node from '../Node.js'; | ||
if ( transforms.destructuring && this.id.type !== 'Identifier' ) { | ||
const simple = this.init.type === 'Identifier'; | ||
const simple = this.init.type === 'Identifier' && !this.init.rewritten; | ||
const props = this.isObjectPattern ? this.id.properties : this.id.elements; | ||
const i0 = this.getIndentation(); | ||
const name = simple ? this.init.name : this.findScope( true ).createIdentifier( 'ref' ); | ||
const indentation = this.getIndentation(); | ||
const props = this.isObjectPattern ? this.id.properties : this.id.elements; | ||
let c = this.start; | ||
let first = simple; | ||
if ( simple ) { | ||
code.remove( this.id.end, this.end ); | ||
} else { | ||
code.insertRight( this.id.end, `${name}` ); | ||
code.move( this.id.end, this.end, c ); | ||
} | ||
props.forEach( ( property, i ) => { | ||
@@ -40,17 +50,17 @@ if ( property ) { | ||
if (!simple || i !== 0) { | ||
code.insert( this.parent.end, `\n${indentation}` ); | ||
} | ||
let start = first ? '' : 'var '; | ||
if ( !first ) start = `;\n${i0}${start}`; | ||
code.insert( this.parent.end, 'var ' ); | ||
code.move( id.start, id.end, this.parent.end ); | ||
code.insert( this.parent.end, ` = ${rhs};` ); | ||
code.insertRight( id.start, start ); | ||
code.move( id.start, id.end, this.start ); | ||
code.insertLeft( id.end, ` = ${rhs}` ); | ||
code.remove( c, id.start ); | ||
c = property.end; | ||
first = false; | ||
} | ||
}); | ||
if ( !simple ) { | ||
code.overwrite( this.id.start, this.id.end, name ); | ||
} else { | ||
code.remove( this.parent.start, this.parent.end ); | ||
} | ||
code.remove( c, this.id.end ); | ||
} | ||
@@ -57,0 +67,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
1238412
11204
+ Addedmagic-string@0.13.1(transitive)
- Removedmagic-string@0.12.1(transitive)
Updatedmagic-string@^0.13.1