Comparing version 4.3.0 to 4.4.1
@@ -18,2 +18,3 @@ #!/usr/bin/env node | ||
commander.option("-c, --compact [mode]", "When set to \"auto\" compact is `true` when the input size exceeds 100KB. (auto|true|false)", "auto"); | ||
commander.option("-m, --modules [modules]", "Module formatter type to use [common]", "common"); | ||
@@ -117,18 +118,14 @@ commander.option("-l, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list); | ||
modules: commander.modules, | ||
compact: commander.compact, | ||
loose: commander.loose | ||
}; | ||
setTimeout(function () { | ||
// this is just a hack to give `babel-minify` and other files including this | ||
// time to modify `exports.opts` | ||
var fn; | ||
var fn; | ||
if (commander.outDir) { | ||
fn = require("./dir"); | ||
} else { | ||
fn = require("./file"); | ||
} | ||
if (commander.outDir) { | ||
fn = require("./dir"); | ||
} else { | ||
fn = require("./file"); | ||
} | ||
fn(commander, filenames, exports.opts); | ||
}, 0); | ||
fn(commander, filenames, exports.opts); |
@@ -16,2 +16,16 @@ # Changelog | ||
## 4.4.0 | ||
* **New Feature** | ||
* `/*** @jsx NAMESPACE **/` comments are now honored by the `react` transformer. | ||
* `getModuleName` option. | ||
* Infer function expression names. Thanks [@RReverser](https://github.com/RReverser)! | ||
* **Bug Fix** | ||
* Add proper control flow for tail recursion optimisation. | ||
* **Internal** | ||
* Remove useless `format` options and move the `format.compact` option to `format`. | ||
* **Polish** | ||
* Newline handling of the code generator has been heavily improved. | ||
* Code generator now deopts whitespace if the input size is >100KB. | ||
## 4.3.0 | ||
@@ -18,0 +32,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function(global){var babelHelpers=global.babelHelpers={};babelHelpers.inherits=function(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass)}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)subClass.__proto__=superClass};babelHelpers.defaults=function(obj,defaults){var keys=Object.getOwnPropertyNames(defaults);for(var i=0;i<keys.length;i++){var key=keys[i];var value=Object.getOwnPropertyDescriptor(defaults,key);if(value&&value.configurable&&obj[key]===undefined){Object.defineProperty(obj,key,value)}}return obj};babelHelpers.prototypeProperties=function(child,staticProps,instanceProps){if(staticProps)Object.defineProperties(child,staticProps);if(instanceProps)Object.defineProperties(child.prototype,instanceProps)};babelHelpers.applyConstructor=function(Constructor,args){var instance=Object.create(Constructor.prototype);var result=Constructor.apply(instance,args);return result!=null&&(typeof result=="object"||typeof result=="function")?result:instance};babelHelpers.taggedTemplateLiteral=function(strings,raw){return Object.freeze(Object.defineProperties(strings,{raw:{value:Object.freeze(raw)}}))};babelHelpers.taggedTemplateLiteralLoose=function(strings,raw){strings.raw=raw;return strings};babelHelpers.interopRequire=function(obj){return obj&&obj.__esModule?obj.default:obj};babelHelpers.toArray=function(arr){return Array.isArray(arr)?arr:Array.from(arr)};babelHelpers.toConsumableArray=function(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++)arr2[i]=arr[i];return arr2}else{return Array.from(arr)}};babelHelpers.slicedToArray=function(arr,i){if(Array.isArray(arr)){return arr}else if(Symbol.iterator in Object(arr)){var _arr=[];for(var _iterator=arr[Symbol.iterator](),_step;!(_step=_iterator.next()).done;){_arr.push(_step.value);if(i&&_arr.length===i)break}return _arr}else{throw new TypeError("Invalid attempt to destructure non-iterable instance")}};babelHelpers.objectWithoutProperties=function(obj,keys){var target={};for(var i in obj){if(keys.indexOf(i)>=0)continue;if(!Object.prototype.hasOwnProperty.call(obj,i))continue;target[i]=obj[i]}return target};babelHelpers.hasOwn=Object.prototype.hasOwnProperty;babelHelpers.slice=Array.prototype.slice;babelHelpers.bind=Function.prototype.bind;babelHelpers.defineProperty=function(obj,key,value){return Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})};babelHelpers.asyncToGenerator=function(fn){return function(){var gen=fn.apply(this,arguments);return new Promise(function(resolve,reject){var callNext=step.bind(null,"next");var callThrow=step.bind(null,"throw");function step(key,arg){try{var info=gen[key](arg);var value=info.value}catch(error){reject(error);return}if(info.done){resolve(value)}else{Promise.resolve(value).then(callNext,callThrow)}}callNext()})}};babelHelpers.interopRequireWildcard=function(obj){return obj&&obj.__esModule?obj:{"default":obj}};babelHelpers._typeof=function(obj){return obj&&obj.constructor===Symbol?"symbol":typeof obj};babelHelpers._extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key]}}}return target};babelHelpers.get=function get(object,property,receiver){var desc=Object.getOwnPropertyDescriptor(object,property);if(desc===undefined){var parent=Object.getPrototypeOf(object);if(parent===null){return undefined}else{return get(parent,property,receiver)}}else if("value"in desc&&desc.writable){return desc.value}else{var getter=desc.get;if(getter===undefined){return undefined}return getter.call(receiver)}};babelHelpers.set=function set(object,property,value,receiver){var desc=Object.getOwnPropertyDescriptor(object,property);if(desc===undefined){var parent=Object.getPrototypeOf(object);if(parent!==null){return set(parent,property,value,receiver)}}else if("value"in desc&&desc.writable){return desc.value=value}else{var setter=desc.set;if(setter!==undefined){return setter.call(receiver,value)}}};babelHelpers.classCallCheck=function(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}};babelHelpers.objectDestructuringEmpty=function(obj){if(obj==null)throw new TypeError("Cannot destructure undefined")};babelHelpers.temporalUndefined={};babelHelpers.temporalAssertDefined=function(val,name,undef){if(val===undef){throw new ReferenceError(name+" is not defined - temporal dead zone")}return true};babelHelpers.tailCall=function(){function Tail(func,args,context){this.func=func;this.args=args;this.context=context}Tail.prototype._isTailDescriptor=true;var isRunning=false;return function(func,args,context){var result=new Tail(func,args,context);if(!isRunning){isRunning=true;do{result=result.func.apply(result.context,result.args)}while(result instanceof Tail||result&&result._isTailDescriptor);isRunning=false}return result}}();babelHelpers.selfGlobal=typeof global==="undefined"?self:global})(typeof global==="undefined"?self:global); | ||
(function(global){var babelHelpers=global.babelHelpers={};babelHelpers.inherits=function(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass)}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)subClass.__proto__=superClass};babelHelpers.defaults=function(obj,defaults){var keys=Object.getOwnPropertyNames(defaults);for(var i=0;i<keys.length;i++){var key=keys[i];var value=Object.getOwnPropertyDescriptor(defaults,key);if(value&&value.configurable&&obj[key]===undefined){Object.defineProperty(obj,key,value)}}return obj};babelHelpers.prototypeProperties=function(child,staticProps,instanceProps){if(staticProps)Object.defineProperties(child,staticProps);if(instanceProps)Object.defineProperties(child.prototype,instanceProps)};babelHelpers.applyConstructor=function(Constructor,args){var instance=Object.create(Constructor.prototype);var result=Constructor.apply(instance,args);return result!=null&&(typeof result=="object"||typeof result=="function")?result:instance};babelHelpers.taggedTemplateLiteral=function(strings,raw){return Object.freeze(Object.defineProperties(strings,{raw:{value:Object.freeze(raw)}}))};babelHelpers.taggedTemplateLiteralLoose=function(strings,raw){strings.raw=raw;return strings};babelHelpers.interopRequire=function(obj){return obj&&obj.__esModule?obj.default:obj};babelHelpers.toArray=function(arr){return Array.isArray(arr)?arr:Array.from(arr)};babelHelpers.toConsumableArray=function(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++)arr2[i]=arr[i];return arr2}else{return Array.from(arr)}};babelHelpers.slicedToArray=function(arr,i){if(Array.isArray(arr)){return arr}else if(Symbol.iterator in Object(arr)){var _arr=[];for(var _iterator=arr[Symbol.iterator](),_step;!(_step=_iterator.next()).done;){_arr.push(_step.value);if(i&&_arr.length===i)break}return _arr}else{throw new TypeError("Invalid attempt to destructure non-iterable instance")}};babelHelpers.objectWithoutProperties=function(obj,keys){var target={};for(var i in obj){if(keys.indexOf(i)>=0)continue;if(!Object.prototype.hasOwnProperty.call(obj,i))continue;target[i]=obj[i]}return target};babelHelpers.hasOwn=Object.prototype.hasOwnProperty;babelHelpers.slice=Array.prototype.slice;babelHelpers.bind=Function.prototype.bind;babelHelpers.defineProperty=function(obj,key,value){return Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})};babelHelpers.asyncToGenerator=function(fn){return function(){var gen=fn.apply(this,arguments);return new Promise(function(resolve,reject){var callNext=step.bind(null,"next");var callThrow=step.bind(null,"throw");function step(key,arg){try{var info=gen[key](arg);var value=info.value}catch(error){reject(error);return}if(info.done){resolve(value)}else{Promise.resolve(value).then(callNext,callThrow)}}callNext()})}};babelHelpers.interopRequireWildcard=function(obj){return obj&&obj.__esModule?obj:{"default":obj}};babelHelpers._typeof=function(obj){return obj&&obj.constructor===Symbol?"symbol":typeof obj};babelHelpers._extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key]}}}return target};babelHelpers.get=function get(object,property,receiver){var desc=Object.getOwnPropertyDescriptor(object,property);if(desc===undefined){var parent=Object.getPrototypeOf(object);if(parent===null){return undefined}else{return get(parent,property,receiver)}}else if("value"in desc&&desc.writable){return desc.value}else{var getter=desc.get;if(getter===undefined){return undefined}return getter.call(receiver)}};babelHelpers.set=function set(object,property,value,receiver){var desc=Object.getOwnPropertyDescriptor(object,property);if(desc===undefined){var parent=Object.getPrototypeOf(object);if(parent!==null){return set(parent,property,value,receiver)}}else if("value"in desc&&desc.writable){return desc.value=value}else{var setter=desc.set;if(setter!==undefined){return setter.call(receiver,value)}}};babelHelpers.classCallCheck=function(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}};babelHelpers.objectDestructuringEmpty=function(obj){if(obj==null)throw new TypeError("Cannot destructure undefined")};babelHelpers.temporalUndefined={};babelHelpers.temporalAssertDefined=function(val,name,undef){if(val===undef){throw new ReferenceError(name+" is not defined - temporal dead zone")}return true};babelHelpers.selfGlobal=typeof global==="undefined"?self:global})(typeof global==="undefined"?self:global); |
@@ -62,4 +62,3 @@ "use strict"; | ||
if (this.format.compact) return; | ||
if (this.buf && !this.isLast([" ", "\n"])) { | ||
if (this.buf && !this.isLast(" ") && !this.isLast("\n")) { | ||
this.push(" "); | ||
@@ -70,2 +69,3 @@ } | ||
Buffer.prototype.removeLast = function (cha) { | ||
if (this.format.compact) return; | ||
if (!this.isLast(cha)) return; | ||
@@ -78,3 +78,5 @@ | ||
Buffer.prototype.newline = function (i, removeLast) { | ||
if (this.format.compact || this.format.concise) { | ||
if (this.format.compact) return; | ||
if (this.format.concise) { | ||
this.space(); | ||
@@ -87,7 +89,10 @@ return; | ||
if (isNumber(i)) { | ||
if (this.endsWith("{\n")) i--; | ||
if (this.endsWith(repeating("\n", i > 0 ? i : 0))) return; | ||
i = Math.min(2, i); | ||
while (i--) { | ||
if (this.endsWith("{\n") || this.endsWith(":\n")) i--; | ||
if (i <= 0) return; | ||
while (i > 0) { | ||
this._newline(removeLast); | ||
i--; | ||
} | ||
@@ -105,7 +110,9 @@ return; | ||
Buffer.prototype._newline = function (removeLast) { | ||
// never allow more than two lines | ||
if (this.endsWith("\n\n")) return; | ||
// remove the last newline | ||
if (removeLast && this.isLast("\n")) this.removeLast("\n"); | ||
this.removeLast(" "); | ||
// remove whitespace if last character was a newline | ||
this._removeSpacesAfterLastNewline(); | ||
@@ -118,2 +125,3 @@ this._push("\n"); | ||
*/ | ||
Buffer.prototype._removeSpacesAfterLastNewline = function () { | ||
@@ -139,3 +147,3 @@ var lastNewlineIndex = this.buf.lastIndexOf("\n"); | ||
Buffer.prototype.push = function (str, noIndent) { | ||
if (this._indent && !noIndent && str !== "\n") { | ||
if (!this.format.compact && this._indent && !noIndent && str !== "\n") { | ||
// we have an indent level and we aren't pushing a newline | ||
@@ -148,3 +156,3 @@ var indent = this.getIndent(); | ||
// we've got a newline before us so prepend on the indentation | ||
if (this.isLast("\n")) str = indent + str; | ||
if (this.isLast("\n")) this._push(indent); | ||
} | ||
@@ -161,9 +169,9 @@ | ||
Buffer.prototype.endsWith = function (str) { | ||
var d = this.buf.length - str.length; | ||
return d >= 0 && this.buf.lastIndexOf(str) === d; | ||
return this.buf.slice(-str.length) === str; | ||
}; | ||
Buffer.prototype.isLast = function (cha, shouldTrimRight) { | ||
Buffer.prototype.isLast = function (cha) { | ||
if (this.format.compact) return false; | ||
var buf = this.buf; | ||
if (shouldTrimRight) buf = trimRight(buf); | ||
var last = buf[buf.length - 1]; | ||
@@ -170,0 +178,0 @@ |
@@ -49,7 +49,5 @@ "use strict"; | ||
print(node.callee); | ||
if (node.arguments.length || this.format.parentheses) { | ||
this.push("("); | ||
print.list(node.arguments); | ||
this.push(")"); | ||
} | ||
this.push("("); | ||
print.list(node.arguments); | ||
this.push(")"); | ||
}; | ||
@@ -56,0 +54,0 @@ |
@@ -65,2 +65,6 @@ "use strict"; | ||
if (node.isType) { | ||
this.push("type "); | ||
} | ||
var specfiers = node.specifiers; | ||
@@ -67,0 +71,0 @@ if (specfiers && specfiers.length) { |
@@ -25,8 +25,3 @@ "use strict"; | ||
if (this.isLast("}")) this.space(); | ||
this.keyword("else"); | ||
if (this.format.format && !t.isBlockStatement(node.alternate)) { | ||
this.push(" "); | ||
} | ||
this.push("else "); | ||
print.indentOnComments(node.alternate); | ||
@@ -157,3 +152,10 @@ } | ||
this.push("{"); | ||
print.sequence(node.cases, { indent: true }); | ||
print.sequence(node.cases, { | ||
indent: true, | ||
addNewlines: function (leading, cas) { | ||
if (!leading && node.cases[node.cases.length - 1] === cas) return -1; | ||
} | ||
}); | ||
this.push("}"); | ||
@@ -171,4 +173,6 @@ }; | ||
this.newline(); | ||
print.sequence(node.consequent, { indent: true }); | ||
if (node.consequent.length) { | ||
this.newline(); | ||
print.sequence(node.consequent, { indent: true }); | ||
} | ||
}; | ||
@@ -195,3 +199,3 @@ | ||
var sep = ","; | ||
if (hasInits) { | ||
if (!this.format.compact && hasInits) { | ||
sep += "\n" + repeating(" ", node.kind.length + 1); | ||
@@ -198,0 +202,0 @@ } else { |
@@ -75,3 +75,3 @@ "use strict"; | ||
} else { | ||
if (i > 0 && !self.format.compact) self.push(" "); | ||
if (i > 0) self.push(" "); | ||
print(elem); | ||
@@ -78,0 +78,0 @@ if (i < len - 1) self.push(","); |
@@ -15,5 +15,5 @@ "use strict"; | ||
var Position = require("./position"); | ||
var messages = require("../messages"); | ||
var Buffer = require("./buffer"); | ||
var extend = require("lodash/object/extend"); | ||
var merge = require("lodash/object/merge"); | ||
var each = require("lodash/collection/each"); | ||
@@ -32,3 +32,3 @@ var n = require("./node"); | ||
this.whitespace = new Whitespace(this.tokens, this.comments); | ||
this.whitespace = new Whitespace(this.tokens, this.comments, this.format); | ||
this.position = new Position; | ||
@@ -52,7 +52,5 @@ this.map = new SourceMap(this.position, opts, code); | ||
return merge({ | ||
parentheses: true, | ||
var format = { | ||
comments: opts.comments == null || opts.comments, | ||
compact: false, | ||
concise: false, | ||
compact: opts.compact, | ||
indent: { | ||
@@ -63,3 +61,13 @@ adjustMultilineComment: true, | ||
} | ||
}, opts.format || {}); | ||
}; | ||
if (format.compact === "auto") { | ||
format.compact = code.length > 100000; // 100KB | ||
if (format.compact) { | ||
console.error(messages.get("codeGeneratorDeopt", opts.filename, "100KB")); | ||
} | ||
} | ||
return format; | ||
}; | ||
@@ -122,7 +130,3 @@ | ||
opts = opts || {}; | ||
var sep = opts.separator || ", "; | ||
if (self.format.compact) sep = ","; | ||
opts.separator = sep; | ||
opts.separator = opts.separator || ", "; | ||
print.join(items, opts); | ||
@@ -175,9 +179,10 @@ }; | ||
if (!leading) lines++; // always include at least a single line after | ||
if (opts.addNewlines) lines += opts.addNewlines(leading, node) || 0; | ||
var needs = n.needsWhitespaceAfter; | ||
if (leading) needs = n.needsWhitespaceBefore; | ||
lines += needs(node, parent); | ||
if (needs(node, parent)) lines++; | ||
// generated nodes can't add starting file whitespace | ||
if (!self.buffer.get()) lines = 0; | ||
if (!self.buffer.buf) lines = 0; | ||
} | ||
@@ -236,2 +241,3 @@ | ||
statement: opts.statement, | ||
addNewlines: opts.addNewlines, | ||
after: function () { | ||
@@ -312,2 +318,3 @@ if (opts.iterator) { | ||
if (this.format.compact) return; | ||
if (!this.format.comments) return; | ||
@@ -314,0 +321,0 @@ if (!comments || !comments.length) return; |
@@ -7,5 +7,5 @@ "use strict"; | ||
var parens = require("./parentheses"); | ||
var t = require("../../types"); | ||
var each = require("lodash/collection/each"); | ||
var some = require("lodash/collection/some"); | ||
var t = require("../../types"); | ||
@@ -46,10 +46,15 @@ var find = function (obj, node, parent) { | ||
var lines = find(whitespace[type].nodes, node, parent); | ||
if (lines) return lines; | ||
var linesInfo = find(whitespace.nodes, node, parent); | ||
each(find(whitespace[type].list, node, parent), function (expr) { | ||
lines = Node.needsWhitespace(expr, node, type); | ||
if (lines) return false; | ||
}); | ||
return lines || 0; | ||
if (!linesInfo) { | ||
var items = find(whitespace.list, node, parent); | ||
if (items) { | ||
for (var i = 0; i < items.length; i++) { | ||
linesInfo = Node.needsWhitespace(items[i], node, type); | ||
if (linesInfo) break; | ||
} | ||
} | ||
} | ||
return (linesInfo && linesInfo[type]) || 0; | ||
}; | ||
@@ -56,0 +61,0 @@ |
"use strict"; | ||
var t = require("../../types"); | ||
var each = require("lodash/collection/each"); | ||
var map = require("lodash/collection/map"); | ||
var isNumber = require("lodash/lang/isNumber"); | ||
var isBoolean = require("lodash/lang/isBoolean"); | ||
var each = require("lodash/collection/each"); | ||
var map = require("lodash/collection/map"); | ||
var t = require("../../types"); | ||
exports.before = { | ||
nodes: { | ||
Property: function (node, parent) { | ||
if (parent.properties[0] === node) { | ||
return 1; | ||
} | ||
}, | ||
var crawl = function (node, state) { | ||
state = state || {}; | ||
SpreadProperty: function (node, parent) { | ||
return exports.before.nodes.Property(node, parent); | ||
}, | ||
if (t.isMemberExpression(node)) { | ||
crawl(node.object, state); | ||
if (node.computed) crawl(node.property, state); | ||
} else if (t.isBinary(node) || t.isAssignmentExpression(node)) { | ||
crawl(node.left, state); | ||
crawl(node.right, state); | ||
} else if (t.isCallExpression(node)) { | ||
state.hasCall = true; | ||
crawl(node.callee, state); | ||
} else if (t.isFunction(node)) { | ||
state.hasFunction = true; | ||
} else if (t.isIdentifier(node)) { | ||
state.hasHelper = state.hasHelper || isHelper(node.callee); | ||
} | ||
SwitchCase: function (node, parent) { | ||
if (parent.cases[0] === node) { | ||
return 1; | ||
} | ||
}, | ||
return state; | ||
}; | ||
CallExpression: function (node) { | ||
if (t.isFunction(node.callee)) { | ||
return 1; | ||
} | ||
} | ||
var isHelper = function (node) { | ||
if (t.isMemberExpression(node)) { | ||
return isHelper(node.object) || isHelper(node.property); | ||
} else if (t.isIdentifier(node)) { | ||
return node.name === "require" || node.name[0] === "_"; | ||
} else if (t.isCallExpression(node)) { | ||
return isHelper(node.callee); | ||
} else if (t.isBinary(node) || t.isAssignmentExpression(node)) { | ||
return (t.isIdentifier(node.left) && isHelper(node.left)) || isHelper(node.right); | ||
} else { | ||
return false; | ||
} | ||
}; | ||
exports.after = { | ||
nodes: { | ||
LogicalExpression: function (node) { | ||
return t.isFunction(node.left) || t.isFunction(node.right); | ||
}, | ||
var isType = function (node) { | ||
return t.isLiteral(node) || t.isObjectExpression(node) || t.isArrayExpression(node) || | ||
t.isIdentifier(node) || t.isMemberExpression(node); | ||
}; | ||
AssignmentExpression: function (node) { | ||
if (t.isFunction(node.right)) { | ||
return 1; | ||
} | ||
exports.nodes = { | ||
AssignmentExpression: function (node) { | ||
var state = crawl(node.right); | ||
if ((state.hasCall && state.hasHelper) || state.hasFunction) { | ||
return { | ||
before: state.hasFunction, | ||
after: true | ||
}; | ||
} | ||
}, | ||
list: { | ||
VariableDeclaration: function (node) { | ||
return map(node.declarations, "init"); | ||
}, | ||
SwitchCase: function (node, parent) { | ||
return { | ||
before: node.consequent.length || parent.cases[0] === node | ||
}; | ||
}, | ||
ArrayExpression: function (node) { | ||
return node.elements; | ||
}, | ||
LogicalExpression: function (node) { | ||
if (t.isFunction(node.left) || t.isFunction(node.right)) { | ||
return { | ||
after: true | ||
}; | ||
} | ||
}, | ||
ObjectExpression: function (node) { | ||
return node.properties; | ||
Literal: function (node) { | ||
if (node.value === "use strict") { | ||
return { | ||
after: true | ||
}; | ||
} | ||
}, | ||
CallExpression: function (node) { | ||
if (t.isFunction(node.callee) || isHelper(node)) { | ||
return { | ||
before: true, | ||
after: true | ||
}; | ||
} | ||
}, | ||
VariableDeclaration: function (node) { | ||
for (var i = 0; i < node.declarations.length; i++) { | ||
var declar = node.declarations[i]; | ||
var enabled = isHelper(declar.id) && !isType(declar.init); | ||
if (!enabled) { | ||
var state = crawl(declar.init); | ||
enabled = (isHelper(declar.init) && state.hasCall) || state.hasFunction; | ||
} | ||
if (enabled) { | ||
return { | ||
before: true, | ||
after: true | ||
}; | ||
} | ||
} | ||
}, | ||
IfStatement: function (node) { | ||
if (t.isBlockStatement(node.consequent)) { | ||
return { | ||
before: true, | ||
after: true | ||
}; | ||
} | ||
} | ||
}; | ||
exports.nodes.Property = | ||
exports.nodes.SpreadProperty = function (node, parent) { | ||
if (parent.properties[0] === node) { | ||
return { | ||
before: true | ||
}; | ||
} | ||
}; | ||
exports.list = { | ||
VariableDeclaration: function (node) { | ||
return map(node.declarations, "init"); | ||
}, | ||
ArrayExpression: function (node) { | ||
return node.elements; | ||
}, | ||
ObjectExpression: function (node) { | ||
return node.properties; | ||
} | ||
}; | ||
each({ | ||
Function: 1, | ||
Class: 1, | ||
For: 1, | ||
ArrayExpression: { after: 1 }, | ||
ObjectExpression: { after: 1 }, | ||
SwitchStatement: 1, | ||
IfStatement: { before: 1 }, | ||
CallExpression: { after: 1 }, | ||
Literal: { after: 1 } | ||
Function: true, | ||
Class: true, | ||
Loop: true, | ||
LabeledStatement: true, | ||
SwitchStatement: true, | ||
TryStatement: true | ||
}, function (amounts, type) { | ||
if (isNumber(amounts)) { | ||
if (isBoolean(amounts)) { | ||
amounts = { after: amounts, before: amounts }; | ||
@@ -78,8 +155,6 @@ } | ||
each([type].concat(t.FLIPPED_ALIAS_KEYS[type] || []), function (type) { | ||
each(amounts, function (amount, key) { | ||
exports[key].nodes[type] = function () { | ||
return amount; | ||
}; | ||
}); | ||
exports.nodes[type] = function () { | ||
return amounts; | ||
}; | ||
}); | ||
}); |
@@ -16,7 +16,9 @@ "use strict"; | ||
*/ | ||
function getLookupIndex(i, base, max) { | ||
i += base; | ||
if (i >= max) | ||
if (i >= max) { | ||
i -= max; | ||
} | ||
@@ -23,0 +25,0 @@ return i; |
@@ -20,3 +20,4 @@ var util = require("util"); | ||
didYouMean: "Did you mean $1?", | ||
evalInStrictMode: "eval is not allowed in strict mode" | ||
evalInStrictMode: "eval is not allowed in strict mode", | ||
codeGeneratorDeopt: "Note: The code generator has deoptimised the styling of $1 as it exceeds the max of $2." | ||
}; | ||
@@ -23,0 +24,0 @@ |
@@ -63,3 +63,2 @@ "use strict"; | ||
"temporal-assert-defined", | ||
"tail-call", | ||
"self-global" | ||
@@ -87,9 +86,14 @@ ]; | ||
"ast", | ||
"format", | ||
"playground", | ||
"experimental", | ||
"resolveModuleSource", | ||
"externalHelpers", | ||
"auxiliaryComment", | ||
"compact", | ||
"resolveModuleSource", | ||
"getModuleName", | ||
// legacy | ||
"format", | ||
// these are used by plugins | ||
@@ -119,3 +123,2 @@ "ignore", | ||
playground: false, | ||
whitespace: true, | ||
moduleIds: false, | ||
@@ -129,2 +132,3 @@ blacklist: [], | ||
modules: "common", | ||
compact: "auto", | ||
loose: [], | ||
@@ -146,2 +150,3 @@ code: true, | ||
opts.optional = util.arrayify(opts.optional); | ||
opts.compact = util.booleanify(opts.compact); | ||
opts.loose = util.arrayify(opts.loose); | ||
@@ -185,4 +190,3 @@ | ||
opts.optional.push("reactCompat"); | ||
console.error("The reactCompat option has been moved into the optional transformer " + | ||
"`reactCompat` - backwards compatibility will be removed in v4.0.0"); | ||
console.error("The reactCompat option has been moved into the optional transformer `reactCompat`"); | ||
} | ||
@@ -189,0 +193,0 @@ |
@@ -76,3 +76,3 @@ "use strict"; | ||
if (opts.pre) { | ||
opts.pre(state); | ||
opts.pre(state, file); | ||
} | ||
@@ -90,3 +90,3 @@ | ||
if (opts.post) { | ||
opts.post(state); | ||
opts.post(state, file); | ||
} | ||
@@ -93,0 +93,0 @@ |
@@ -149,2 +149,4 @@ "use strict"; | ||
var opts = this.file.opts; | ||
if (opts.getModuleName) return opts.getModuleName(opts.filename); | ||
var filenameRelative = opts.filenameRelative; | ||
@@ -151,0 +153,0 @@ var moduleName = ""; |
@@ -7,8 +7,7 @@ "use strict"; | ||
var AMDFormatter = require("./amd"); | ||
var useStrict = require("../helpers/use-strict"); | ||
var util = require("../../util"); | ||
var t = require("../../types"); | ||
var last = require("lodash/array/last"); | ||
var each = require("lodash/collection/each"); | ||
var map = require("lodash/collection/map"); | ||
var t = require("../../types"); | ||
@@ -197,7 +196,3 @@ function SystemFormatter(file) { | ||
if (useStrict.has(block)) { | ||
handlerBody.unshift(block.body.shift()); | ||
} | ||
program.body = [runner]; | ||
}; |
@@ -162,3 +162,3 @@ "use strict"; | ||
if (node.key.name === "constructor") { | ||
if ((!node.computed && t.isIdentifier(node.key, { name: "constructor" })) || t.isLiteral(node.key, { value: "constructor" })) { | ||
this.pushConstructor(node); | ||
@@ -165,0 +165,0 @@ } else { |
@@ -44,19 +44,26 @@ "use strict"; | ||
DestructuringTransformer.prototype.push = function (elem, parentId) { | ||
if (t.isObjectPattern(elem)) { | ||
this.pushObjectPattern(elem, parentId); | ||
} else if (t.isArrayPattern(elem)) { | ||
this.pushArrayPattern(elem, parentId); | ||
} else if (t.isAssignmentPattern(elem)) { | ||
this.pushAssignmentPattern(elem, parentId); | ||
DestructuringTransformer.prototype.push = function (id, init) { | ||
if (t.isObjectPattern(id)) { | ||
this.pushObjectPattern(id, init); | ||
} else if (t.isArrayPattern(id)) { | ||
this.pushArrayPattern(id, init); | ||
} else if (t.isAssignmentPattern(id)) { | ||
this.pushAssignmentPattern(id, init); | ||
} else { | ||
this.nodes.push(this.buildVariableAssignment(elem, parentId)); | ||
this.nodes.push(this.buildVariableAssignment(id, init)); | ||
} | ||
}; | ||
DestructuringTransformer.prototype.pushAssignmentPattern = function (pattern, parentId) { | ||
var tempParentId = this.scope.generateUidBasedOnNode(parentId); | ||
DestructuringTransformer.prototype.get = function () { | ||
}; | ||
DestructuringTransformer.prototype.pushAssignmentPattern = function (pattern, valueRef) { | ||
// we need to assign the current value of the assignment to avoid evaluating | ||
// it more than once | ||
var tempValueRef = this.scope.generateUidBasedOnNode(valueRef); | ||
var declar = t.variableDeclaration("var", [ | ||
t.variableDeclarator(tempParentId, parentId) | ||
t.variableDeclarator(tempValueRef, valueRef) | ||
]); | ||
@@ -66,8 +73,10 @@ declar._blockHoist = this.blockHoist; | ||
// | ||
this.nodes.push(this.buildVariableAssignment( | ||
pattern.left, | ||
t.conditionalExpression( | ||
t.binaryExpression("===", tempParentId, t.identifier("undefined")), | ||
t.binaryExpression("===", tempValueRef, t.identifier("undefined")), | ||
pattern.right, | ||
tempParentId | ||
tempValueRef | ||
) | ||
@@ -77,55 +86,70 @@ )); | ||
DestructuringTransformer.prototype.pushObjectSpread = function (pattern, parentId, prop, i) { | ||
DestructuringTransformer.prototype.pushObjectSpread = function (pattern, objRef, spreadProp, spreadPropIndex) { | ||
// get all the keys that appear in this object before the current spread | ||
var keys = []; | ||
for (var i2 = 0; i2 < pattern.properties.length; i2++) { | ||
var prop2 = pattern.properties[i2]; | ||
if (i2 >= i) break; | ||
if (t.isSpreadProperty(prop2)) continue; | ||
for (var i = 0; i < pattern.properties.length; i++) { | ||
var prop = pattern.properties[i]; | ||
var key = prop2.key; | ||
if (t.isIdentifier(key)) { | ||
key = t.literal(prop2.key.name); | ||
} | ||
// we've exceeded the index of the spread property to all properties to the | ||
// right need to be ignored | ||
if (i >= spreadPropIndex) break; | ||
// ignore other spread properties | ||
if (t.isSpreadProperty(prop)) continue; | ||
var key = prop.key; | ||
if (t.isIdentifier(key)) key = t.literal(prop.key.name); | ||
keys.push(key); | ||
} | ||
keys = t.arrayExpression(keys); | ||
var value = t.callExpression(this.file.addHelper("object-without-properties"), [parentId, keys]); | ||
this.nodes.push(this.buildVariableAssignment(prop.argument, value)); | ||
// | ||
var value = t.callExpression(this.file.addHelper("object-without-properties"), [objRef, keys]); | ||
this.nodes.push(this.buildVariableAssignment(spreadProp.argument, value)); | ||
}; | ||
DestructuringTransformer.prototype.pushObjectProperty = function (prop, parentId) { | ||
DestructuringTransformer.prototype.pushObjectProperty = function (prop, propRef) { | ||
if (t.isLiteral(prop.key)) prop.computed = true; | ||
var pattern2 = prop.value; | ||
var patternId2 = t.memberExpression(parentId, prop.key, prop.computed); | ||
var pattern = prop.value; | ||
var objRef = t.memberExpression(propRef, prop.key, prop.computed); | ||
if (t.isPattern(pattern2)) { | ||
this.push(pattern2, patternId2); | ||
if (t.isPattern(pattern)) { | ||
this.push(pattern, objRef); | ||
} else { | ||
this.nodes.push(this.buildVariableAssignment(pattern2, patternId2)); | ||
this.nodes.push(this.buildVariableAssignment(pattern, objRef)); | ||
} | ||
}; | ||
DestructuringTransformer.prototype.pushObjectPattern = function (pattern, parentId) { | ||
DestructuringTransformer.prototype.pushObjectPattern = function (pattern, objRef) { | ||
// https://github.com/babel/babel/issues/681 | ||
if (!pattern.properties.length) { | ||
this.nodes.push(t.expressionStatement( | ||
t.callExpression(this.file.addHelper("object-destructuring-empty"), [parentId]) | ||
t.callExpression(this.file.addHelper("object-destructuring-empty"), [objRef]) | ||
)); | ||
} | ||
if (pattern.properties.length > 1 && t.isMemberExpression(parentId)) { | ||
var temp = this.scope.generateUidBasedOnNode(parentId, this.file); | ||
this.nodes.push(this.buildVariableDeclaration(temp, parentId)); | ||
parentId = temp; | ||
// if we have more than one properties in this pattern and the objectRef is a | ||
// member expression then we need to assign it to a temporary variable so it's | ||
// only evaluated once | ||
if (pattern.properties.length > 1 && t.isMemberExpression(objRef)) { | ||
var temp = this.scope.generateUidBasedOnNode(objRef, this.file); | ||
this.nodes.push(this.buildVariableDeclaration(temp, objRef)); | ||
objRef = temp; | ||
} | ||
// | ||
for (var i = 0; i < pattern.properties.length; i++) { | ||
var prop = pattern.properties[i]; | ||
if (t.isSpreadProperty(prop)) { | ||
this.pushObjectSpread(pattern, parentId, prop, i); | ||
this.pushObjectSpread(pattern, objRef, prop, i); | ||
} else { | ||
this.pushObjectProperty(prop, parentId); | ||
this.pushObjectProperty(prop, objRef); | ||
} | ||
@@ -144,16 +168,66 @@ } | ||
DestructuringTransformer.prototype.pushArrayPattern = function (pattern, parentId) { | ||
DestructuringTransformer.prototype.canUnpackArrayPattern = function (pattern, arr) { | ||
// not an array so there's no way we can deal with this | ||
if (!t.isArrayExpression(arr)) return false; | ||
// pattern has less elements than the array and doesn't have a rest so some | ||
// elements wont be evaluated | ||
if (pattern.elements.length < arr.elements.length && !hasRest(pattern)) return false; | ||
// deopt on holes | ||
for (var i = 0; i < pattern.elements.length; i++) { | ||
if (!pattern.elements[i]) return false; | ||
} | ||
return true; | ||
}; | ||
DestructuringTransformer.prototype.pushUnpackedArrayPattern = function (pattern, arr) { | ||
for (var i = 0; i < pattern.elements.length; i++) { | ||
var elem = pattern.elements[i]; | ||
if (t.isRestElement(elem)) { | ||
this.push(elem.argument, t.arrayExpression(arr.elements.slice(i))); | ||
} else { | ||
this.push(elem, arr.elements[i]); | ||
} | ||
} | ||
}; | ||
DestructuringTransformer.prototype.pushArrayPattern = function (pattern, arrayRef) { | ||
if (!pattern.elements) return; | ||
// if we have a rest then we need all the elements | ||
// optimise basic array destructuring of an array expression | ||
// | ||
// we can't do this to a pattern of unequal size to it's right hand | ||
// array expression as then there will be values that wont be evaluated | ||
// | ||
// eg: var [a, b] = [1, 2]; | ||
if (this.canUnpackArrayPattern(pattern, arrayRef)) { | ||
return this.pushUnpackedArrayPattern(pattern, arrayRef); | ||
} | ||
// if we have a rest then we need all the elements so don't tell | ||
// `scope.toArray` to only get a certain amount | ||
var count = !hasRest(pattern) && pattern.elements.length; | ||
var toArray = this.scope.toArray(parentId, count); | ||
// so we need to ensure that the `arrayRef` is an array, `scope.toArray` will | ||
// return a locally bound identifier if it's been inferred to be an array, | ||
// otherwise it'll be a call to a helper that will ensure it's one | ||
var _parentId = this.scope.generateUidBasedOnNode(parentId); | ||
this.nodes.push(this.buildVariableDeclaration(_parentId, toArray)); | ||
parentId = _parentId; | ||
var toArray = this.scope.toArray(arrayRef, count); | ||
this.scope.assignTypeGeneric(parentId.name, "Array"); | ||
if (t.isIdentifier(toArray)) { | ||
// we've been given an identifier so it must have been inferred to be an | ||
// array | ||
arrayRef = toArray; | ||
} else { | ||
arrayRef = this.scope.generateUidBasedOnNode(arrayRef); | ||
this.nodes.push(this.buildVariableDeclaration(arrayRef, toArray)); | ||
this.scope.assignTypeGeneric(arrayRef.name, "Array"); | ||
} | ||
// | ||
for (var i = 0; i < pattern.elements.length; i++) { | ||
@@ -165,28 +239,35 @@ var elem = pattern.elements[i]; | ||
var newPatternId; | ||
var elemRef; | ||
if (t.isRestElement(elem)) { | ||
newPatternId = this.scope.toArray(parentId); | ||
elemRef = this.scope.toArray(arrayRef); | ||
if (i > 0) { | ||
newPatternId = t.callExpression(t.memberExpression(newPatternId, t.identifier("slice")), [t.literal(i)]); | ||
elemRef = t.callExpression(t.memberExpression(elemRef, t.identifier("slice")), [t.literal(i)]); | ||
} | ||
// set the element to the rest element argument since we've dealt with it | ||
// being a rest already | ||
elem = elem.argument; | ||
} else { | ||
newPatternId = t.memberExpression(parentId, t.literal(i), true); | ||
elemRef = t.memberExpression(arrayRef, t.literal(i), true); | ||
} | ||
this.push(elem, newPatternId); | ||
this.push(elem, elemRef); | ||
} | ||
}; | ||
DestructuringTransformer.prototype.init = function (pattern, parentId) { | ||
if (!t.isArrayExpression(parentId) && !t.isMemberExpression(parentId) && !t.isIdentifier(parentId)) { | ||
var key = this.scope.generateUidBasedOnNode(parentId); | ||
this.nodes.push(this.buildVariableDeclaration(key, parentId)); | ||
parentId = key; | ||
DestructuringTransformer.prototype.init = function (pattern, ref) { | ||
// trying to destructure a value that we can't evaluate more than once so we | ||
// need to save it to a variable | ||
if (!t.isArrayExpression(ref) && !t.isMemberExpression(ref) && !t.isIdentifier(ref)) { | ||
var key = this.scope.generateUidBasedOnNode(ref); | ||
this.nodes.push(this.buildVariableDeclaration(key, ref)); | ||
ref = key; | ||
} | ||
this.push(pattern, parentId); | ||
// | ||
this.push(pattern, ref); | ||
}; | ||
@@ -252,3 +333,3 @@ | ||
hasDestructuringTransformer = true; | ||
var parentId = scope.generateUidIdentifier("ref"); | ||
var ref = scope.generateUidIdentifier("ref"); | ||
@@ -262,5 +343,5 @@ var destructuring = new DestructuringTransformer({ | ||
}); | ||
destructuring.init(pattern, parentId); | ||
destructuring.init(pattern, ref); | ||
return parentId; | ||
return ref; | ||
}); | ||
@@ -267,0 +348,0 @@ |
@@ -40,2 +40,25 @@ "use strict"; | ||
var breakVisitor = { | ||
enter: function (node, parent, scope, state) { | ||
if (t.isLoop(node)) { | ||
state.ignoreLabeless = true; | ||
scope.traverse(node, breakVisitor, state); | ||
state.ignoreLabeless = false; | ||
return this.skip(); | ||
} | ||
if (t.isBreakStatement(node)) { | ||
if (!node.label && state.ignoreLabeless) return; | ||
if (node.label && node.label.name !== state.label) return; | ||
var ret = t.expressionStatement( | ||
t.callExpression(t.memberExpression(state.iteratorKey, t.identifier("return")), []) | ||
); | ||
if (state.wrapReturn) ret = state.wrapReturn(ret); | ||
return [ret, node]; | ||
} | ||
} | ||
}; | ||
var loose = function (node, parent, scope, file) { | ||
@@ -58,5 +81,8 @@ var left = node.left; | ||
var iteratorKey = scope.generateUidIdentifier("iterator"); | ||
var isArrayKey = scope.generateUidIdentifier("isArray"); | ||
var loop = util.template("for-of-loose", { | ||
LOOP_OBJECT: scope.generateUidIdentifier("iterator"), | ||
IS_ARRAY: scope.generateUidIdentifier("isArray"), | ||
LOOP_OBJECT: iteratorKey, | ||
IS_ARRAY: isArrayKey, | ||
OBJECT: node.right, | ||
@@ -73,2 +99,14 @@ INDEX: scope.generateUidIdentifier("i"), | ||
// | ||
scope.traverse(node, breakVisitor, { | ||
iteratorKey: iteratorKey, | ||
wrapReturn: function (node) { | ||
return t.ifStatement(t.unaryExpression("!", isArrayKey, true), node); | ||
}, | ||
label: t.isLabeledStatement(parent) && parent.label.name | ||
}); | ||
// | ||
return { | ||
@@ -99,4 +137,8 @@ declar: declar, | ||
// | ||
var iteratorKey = scope.generateUidIdentifier("iterator"); | ||
var loop = util.template("for-of", { | ||
ITERATOR_KEY: scope.generateUidIdentifier("iterator"), | ||
ITERATOR_KEY: iteratorKey, | ||
STEP_KEY: stepKey, | ||
@@ -106,2 +148,11 @@ OBJECT: node.right | ||
// | ||
scope.traverse(node, breakVisitor, { | ||
iteratorKey: iteratorKey, | ||
label: t.isLabeledStatement(parent) && parent.label.name | ||
}); | ||
// | ||
return { | ||
@@ -108,0 +159,0 @@ declar: declar, |
"use strict"; | ||
var nameMethod = require("../../helpers/name-method"); | ||
var t = require("../../../types"); | ||
var clone = require("lodash/lang/clone"); | ||
var clone = require("lodash/lang/clone"); | ||
var t = require("../../../types"); | ||
@@ -11,6 +10,5 @@ exports.check = function (node) { | ||
exports.Property = function (node, parent, scope, file) { | ||
exports.Property = function (node) { | ||
if (node.method) { | ||
node.method = false; | ||
nameMethod.property(node, file, scope); | ||
} | ||
@@ -17,0 +15,0 @@ |
@@ -43,2 +43,6 @@ "use strict"; | ||
TailCallTransformer.prototype.getAgainId = function () { | ||
return this.againId = this.againId || this.scope.generateUidIdentifier("again"); | ||
}; | ||
TailCallTransformer.prototype.getParams = function () { | ||
@@ -116,3 +120,8 @@ var params = this.params; | ||
body.unshift(t.expressionStatement( | ||
t.assignmentExpression("=", this.getAgainId(), t.literal(false))) | ||
); | ||
node.body = util.template("tail-call-body", { | ||
AGAIN_ID: this.getAgainId(), | ||
THIS_ID: this.thisId, | ||
@@ -291,2 +300,5 @@ ARGUMENTS_ID: this.argumentsId, | ||
body.push(t.expressionStatement( | ||
t.assignmentExpression("=", this.getAgainId(), t.literal(true)) | ||
)); | ||
body.push(t.continueStatement(this.getFunctionId())); | ||
@@ -293,0 +305,0 @@ |
module.exports = { | ||
useStrict: require("./other/use-strict"), | ||
// this goes at the start so we only transform the original user code | ||
"spec.functionName": require("./spec/function-name"), | ||
"validation.undeclaredVariableCheck": require("./validation/undeclared-variable-check"), | ||
@@ -94,3 +97,2 @@ "validation.noForInOfAssignment": require("./validation/no-for-in-of-assignment"), | ||
"spec.undefinedToVoid": require("./spec/undefined-to-void"), | ||
"spec.functionName": require("./spec/function-name"), | ||
@@ -97,0 +99,0 @@ _moduleFormatter: require("./internal/module-formatter"), |
"use strict"; | ||
var useStrict = require("../../helpers/use-strict"); | ||
var groupBy = require("lodash/collection/groupBy"); | ||
var flatten = require("lodash/array/flatten"); | ||
var values = require("lodash/object/values"); | ||
var groupBy = require("lodash/collection/groupBy"); | ||
var flatten = require("lodash/array/flatten"); | ||
var values = require("lodash/object/values"); | ||
@@ -25,13 +24,11 @@ // Priority: | ||
useStrict.wrap(node, function () { | ||
var nodePriorities = groupBy(node.body, function (bodyNode) { | ||
var priority = bodyNode._blockHoist; | ||
if (priority == null) priority = 1; | ||
if (priority === true) priority = 2; | ||
return priority; | ||
}); | ||
var nodePriorities = groupBy(node.body, function (bodyNode) { | ||
var priority = bodyNode._blockHoist; | ||
if (priority == null) priority = 1; | ||
if (priority === true) priority = 2; | ||
return priority; | ||
}); | ||
node.body = flatten(values(nodePriorities).reverse()); | ||
}); | ||
node.body = flatten(values(nodePriorities).reverse()); | ||
} | ||
}; |
"use strict"; | ||
var useStrict = require("../../helpers/use-strict"); | ||
var t = require("../../../types"); | ||
var t = require("../../../types"); | ||
@@ -15,23 +14,21 @@ exports.secondPass = true; | ||
useStrict.wrap(node, function () { | ||
for (var i in node._declarations) { | ||
var declar = node._declarations[i]; | ||
for (var i in node._declarations) { | ||
var declar = node._declarations[i]; | ||
kind = declar.kind || "var"; | ||
var declarNode = t.variableDeclarator(declar.id, declar.init); | ||
kind = declar.kind || "var"; | ||
var declarNode = t.variableDeclarator(declar.id, declar.init); | ||
if (declar.init) { | ||
node.body.unshift(file.attachAuxiliaryComment(t.variableDeclaration(kind, [declarNode]))); | ||
} else { | ||
kinds[kind] = kinds[kind] || []; | ||
kinds[kind].push(declarNode); | ||
} | ||
if (declar.init) { | ||
node.body.unshift(file.attachAuxiliaryComment(t.variableDeclaration(kind, [declarNode]))); | ||
} else { | ||
kinds[kind] = kinds[kind] || []; | ||
kinds[kind].push(declarNode); | ||
} | ||
} | ||
for (kind in kinds) { | ||
node.body.unshift(file.attachAuxiliaryComment(t.variableDeclaration(kind, kinds[kind]))); | ||
} | ||
}); | ||
for (kind in kinds) { | ||
node.body.unshift(file.attachAuxiliaryComment(t.variableDeclaration(kind, kinds[kind]))); | ||
} | ||
node._declarations = null; | ||
}; |
"use strict"; | ||
var useStrict = require("../../helpers/use-strict"); | ||
exports.Program = function (program, parent, scope, file) { | ||
if (!file.transformers["es6.modules"].canRun()) return; | ||
useStrict.wrap(program, function () { | ||
program.body = file.dynamicImports.concat(program.body); | ||
}); | ||
program.body = file.dynamicImports.concat(program.body); | ||
@@ -12,0 +8,0 @@ if (file.moduleFormatter.transform) { |
@@ -6,2 +6,18 @@ "use strict"; | ||
var JSX_ANNOTATION_REGEX = /^\*\s*@jsx\s+([^\s]+)/; | ||
exports.Program = function (node, parent, scope, file) { | ||
var id = "React.createElement"; | ||
var comment = file.ast.comments[0]; | ||
if (comment) { | ||
var matches = JSX_ANNOTATION_REGEX.exec(comment.value); | ||
if (matches) id = matches[1]; | ||
} | ||
file.set("jsxIdentifier", id.split(".").map(t.identifier).reduce(function (object, property) { | ||
return t.memberExpression(object, property); | ||
})); | ||
}; | ||
require("../../helpers/build-react-transformer")(exports, { | ||
@@ -18,5 +34,5 @@ pre: function (state) { | ||
post: function (state) { | ||
state.callee = t.memberExpression(t.identifier("React"), t.identifier("createElement")); | ||
post: function (state, file) { | ||
state.callee = file.get("jsxIdentifier"); | ||
} | ||
}); |
"use strict"; | ||
var useStrict = require("../../helpers/use-strict"); | ||
var messages = require("../../../messages"); | ||
var t = require("../../../types"); | ||
var messages = require("../../../messages"); | ||
var t = require("../../../types"); | ||
exports.Program = function (program) { | ||
if (!useStrict.has(program)) { | ||
program.body.unshift(t.expressionStatement(t.literal("use strict"))); | ||
var first = program.body[0]; | ||
if (t.isExpressionStatement(first) && t.isLiteral(first.expression, { value: "use strict" })) { | ||
program.body.shift(); | ||
} | ||
}; | ||
exports.post = function (file) { | ||
file.ast.program.body.unshift(t.expressionStatement(t.literal("use strict"))); | ||
}; | ||
exports.FunctionDeclaration = | ||
@@ -14,0 +18,0 @@ exports.FunctionExpression = function () { |
"use strict"; | ||
var util = require("../../../util"); | ||
var t = require("../../../types"); | ||
var t = require("../../../types"); | ||
var util = require("../../../util"); | ||
@@ -14,8 +14,13 @@ var propertyFunctionVisitor = { | ||
exports.FunctionExpression = function (node, parent, scope, file) { | ||
//exports.ArrowFunctionExpression = | ||
exports.FunctionExpression = function (node, parent, scope) { | ||
// has an `id` so we don't need to infer one | ||
if (node.id) return; | ||
var id; | ||
if (t.isProperty(parent)) { | ||
if (t.isProperty(parent) && parent.kind === "init" && !parent.computed) { | ||
// { foo: function () {} }; | ||
id = parent.key; | ||
} else if (t.isVariableDeclarator(parent)) { | ||
// var foo = function () {}; | ||
id = parent.id; | ||
@@ -25,15 +30,52 @@ } else { | ||
} | ||
var binding = scope.getBindingIdentifier(id.name); | ||
var outerId, selfGlobalId; | ||
if (!t.isIdentifier(id)) return; | ||
var name = t.toIdentifier(id.name); | ||
id = t.identifier(name); | ||
// check to see if we have a local binding of the id we're setting inside of | ||
// the function, this is important as there are caveats associated | ||
var bindingInfo = scope.getOwnBindingInfo(name); | ||
if (bindingInfo) { | ||
if (bindingInfo.type === "param") { | ||
// safari will blow up in strict mode with code like: | ||
// | ||
// var t = function t(t) {}; | ||
// | ||
// with the error: | ||
// | ||
// Cannot declare a parameter named 't' as it shadows the name of a | ||
// strict mode function. | ||
// | ||
// this isn't to the spec and they've invented this behaviour which is | ||
// **extremely** annoying so we avoid setting the name if it has a param | ||
// with the same id | ||
} else { | ||
// otherwise it's defined somewhere in scope like: | ||
// | ||
// var t = function () { | ||
// var t = 2; | ||
// }; | ||
// | ||
// so we can safely just set the id and move along as it shadows the | ||
// bound function id | ||
node.id = id; | ||
} | ||
return; | ||
} | ||
// | ||
var binding = scope.getBindingIdentifier(name); | ||
var outerId; | ||
scope.traverse(node, propertyFunctionVisitor, { | ||
name: id.name, | ||
name: name, | ||
binding: binding, | ||
getOuter: function () { | ||
if (!binding) { | ||
return t.memberExpression( | ||
selfGlobalId || (selfGlobalId = file.addHelper("self-global")), | ||
id | ||
); | ||
} | ||
return t.callExpression( | ||
@@ -45,12 +87,12 @@ outerId || (outerId = scope.generateUidIdentifier("getOuter")), | ||
}); | ||
node.id = id; | ||
if (outerId) { | ||
return util.template("named-func", { | ||
GET_OUTER_ID: outerId, | ||
ID: id, | ||
FUNCTION: node | ||
FUNCTION: node, | ||
ID: id | ||
}); | ||
} | ||
}; | ||
exports.optional = true; |
@@ -22,3 +22,3 @@ { | ||
"ArrowFunctionExpression": ["Scopable", "Function", "Expression"], | ||
"FunctionDeclaration": ["Statement", "Declaration", "Scopable", "Function"], | ||
"FunctionDeclaration": ["Scopable", "Function", "Statement", "Declaration"], | ||
"FunctionExpression": ["Scopable", "Function", "Expression"], | ||
@@ -43,5 +43,5 @@ | ||
"ForOfStatement": ["Statement", "For", "Scopable", "Loop"], | ||
"ForInStatement": ["Statement", "For", "Scopable", "Loop"], | ||
"ForStatement": ["Statement", "For", "Scopable", "Loop"], | ||
"ForOfStatement": ["Scopable", "Statement", "For", "Loop"], | ||
"ForInStatement": ["Scopable", "Statement", "For", "Loop"], | ||
"ForStatement": ["Scopable", "Statement", "For", "Loop"], | ||
@@ -48,0 +48,0 @@ "ObjectPattern": ["Pattern"], |
@@ -59,2 +59,8 @@ "use strict"; | ||
exports.booleanify = function (val) { | ||
if (val === "true") return true; | ||
if (val === "false") return false; | ||
return val; | ||
}; | ||
var templateVisitor = { | ||
@@ -61,0 +67,0 @@ enter: function (node, parent, scope, nodes) { |
{ | ||
"name": "babel", | ||
"description": "Turn ES6 code into readable vanilla ES5 with source maps", | ||
"version": "4.3.0", | ||
"version": "4.4.1", | ||
"author": "Sebastian McKenzie <sebmck@gmail.com>", | ||
@@ -15,7 +15,5 @@ "homepage": "https://babeljs.io/", | ||
"6to5": "./bin/deprecated/6to5", | ||
"6to5-minify": "./bin/deprecated/6to5-minify", | ||
"6to5-node": "./bin/deprecated/6to5-node", | ||
"6to5-runtime": "./bin/deprecated/6to5-runtime", | ||
"babel": "./bin/babel/index.js", | ||
"babel-minify": "./bin/babel-minify", | ||
"babel-node": "./bin/babel-node", | ||
@@ -22,0 +20,0 @@ "babel-external-helpers": "./bin/babel-external-helpers" |
<p align="center"> | ||
<img alt="babel" src="https://raw.githubusercontent.com/babel/logo/master/logo.png" width="546"> | ||
<a href="https://babeljs.io/"> | ||
<img alt="babel" src="https://raw.githubusercontent.com/babel/logo/master/logo.png" width="546"> | ||
</a> | ||
</p> | ||
@@ -13,1 +15,5 @@ | ||
</p> | ||
<p align="center"> | ||
For documentation and website issues please visit the <a href="https://github.com/babel/babel.github.io">babel.github.io</a> repo. | ||
</p> |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
13459
19
1590406
157