I'm extremely stupid and didn't commit as I go. To anyone reading this
I'm extremely sorry. A lot of these changes are very broad and I plan on
releasing Babel 6.0.0 today live on stage at Ember Camp London so I'm
afraid I couldn't wait. If you're ever in London I'll buy you a beer
(or assorted beverage!) to make up for it, also I'll kiss your feet and
give you a back massage, maybe.
This commit is contained in:
Sebastian McKenzie
2015-10-29 17:51:24 +00:00
parent 3974dd762d
commit ae7d5367f1
1501 changed files with 16477 additions and 19786 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "babel-generator",
"version": "5.8.22",
"version": "5.10.32",
"description": "",
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"homepage": "https://babeljs.io/",
@@ -8,9 +8,9 @@
"repository": "babel/babel",
"main": "lib/index.js",
"dependencies": {
"babel-messages": "^5.8.22",
"babel-runtime": "^5.8.20",
"babel-types": "^5.8.22",
"babel-messages": "^5.10.32",
"babel-runtime": "^5.10.32",
"babel-types": "^5.10.32",
"detect-indent": "^3.0.1",
"is-integer": "^1.0.4",
"lodash": "^3.10.1",
@@ -21,4 +21,4 @@
"devDependencies": {
"babel-helper-fixtures": "^1.0.0"
}
}
}

View File

@@ -10,6 +10,7 @@ import trimRight from "trim-right";
export default class Buffer {
constructor(position: Position, format: Object) {
this.printedCommentStarts = {};
this.parenPushNewlineState = null;
this.position = position;
this._indent = format.indent.base;
@@ -102,6 +103,7 @@ export default class Buffer {
rightBrace() {
this.newline(true);
//if (this.format.compact) this._removeLast(";");
this.push("}");
}
@@ -132,8 +134,11 @@ export default class Buffer {
removeLast(cha: string) {
if (this.format.compact) return;
if (!this.isLast(cha)) return;
return this._removeLast(cha);
}
_removeLast(cha: string) {
if (!this._isLast(cha)) return;
this.buf = this.buf.substr(0, this.buf.length - 1);
this.position.unshift(cha);
}
@@ -316,7 +321,10 @@ export default class Buffer {
isLast(cha: string) {
if (this.format.compact) return false;
return this._isLast(cha);
}
_isLast(cha: string) {
let buf = this.buf;
let last = buf[buf.length - 1];

View File

@@ -6,7 +6,10 @@ export function File(node: Object) {
export function Program(node: Object) {
this.printInnerComments(node, false);
this.printSequence(node.directives, node);
if (node.directives && node.directives.length) this.newline();
this.printSequence(node.body, node);
}
@@ -15,7 +18,10 @@ export function BlockStatement(node: Object) {
this.printInnerComments(node);
if (node.body.length) {
this.newline();
this.printSequence(node.directives, node, { indent: true });
if (node.directives && node.directives.length) this.newline();
this.printSequence(node.body, node, { indent: true });
if (!this.format.retainLines) this.removeLast("\n");
this.rightBrace();

View File

@@ -59,12 +59,16 @@ export function ClassProperty(node: Object) {
this.semicolon();
}
export function MethodDefinition(node: Object) {
export function ClassMethod(node: Object) {
this.printJoin(node.decorators, node, { separator: "" });
if (node.static) {
this.push("static ");
}
if (node.kind === "constructorCall") {
this.push("call ");
}
this._method(node);
}

View File

@@ -3,6 +3,7 @@
import isInteger from "is-integer";
import isNumber from "lodash/lang/isNumber";
import * as t from "babel-types";
import n from "../node";
const SCIENTIFIC_NOTATION = /e/i;
@@ -107,7 +108,7 @@ export function CallExpression(node: Object) {
this.push(")");
}
function buildYieldAwait(keyword) {
function buildYieldAwait(keyword: string) {
return function (node: Object) {
this.push(keyword);
@@ -142,7 +143,16 @@ export function AssignmentPattern(node: Object) {
this.print(node.right, node);
}
export function AssignmentExpression(node: Object) {
export function AssignmentExpression(node: Object, parent: Object) {
// Somewhere inside a for statement `init` node but doesn't usually
// needs a paren except for `in` expressions: `for (a in b ? a : b;;)`
let parens = this._inForStatementInit && node.operator === "in" &&
!n.needsParens(node, parent);
if (parens) {
this.push("(");
}
this.print(node.left, node);
let spaces = !this.format.compact || node.operator === "in" || node.operator === "instanceof";
@@ -162,6 +172,10 @@ export function AssignmentExpression(node: Object) {
if (spaces) this.push(" ");
this.print(node.right, node);
if (parens) {
this.push(")");
}
}
export function BindExpression(node: Object) {
@@ -193,7 +207,7 @@ export function MemberExpression(node: Object) {
this.push("]");
} else {
if (t.isLiteral(node.object)) {
let val = this._stringLiteral(node.object);
let val = this.getPossibleRaw(node.object) || this._stringLiteral(node.object);
if (isInteger(+val) && !SCIENTIFIC_NOTATION.test(val) && !this.endsWith(".")) {
this.push(".");
}

View File

@@ -19,12 +19,11 @@ export function _params(node: Object) {
}
export function _method(node: Object) {
let value = node.value;
let kind = node.kind;
let key = node.key;
let kind = node.kind;
let key = node.key;
if (kind === "method" || kind === "init") {
if (value.generator) {
if (node.generator) {
this.push("*");
}
}
@@ -33,7 +32,7 @@ export function _method(node: Object) {
this.push(kind + " ");
}
if (value.async) this.push("async ");
if (node.async) this.push("async ");
if (node.computed) {
this.push("[");
@@ -43,9 +42,9 @@ export function _method(node: Object) {
this.print(key, node);
}
this._params(value);
this._params(node);
this.space();
this.print(value.body, value);
this.print(node.body, node);
}
export function FunctionExpression(node: Object) {

View File

@@ -64,13 +64,18 @@ function ExportDeclaration(node: Object) {
let specifiers = node.specifiers.slice(0);
let first = specifiers[0];
// print "special" specifiers first
let hasSpecial = false;
if (t.isExportDefaultSpecifier(first) || t.isExportNamespaceSpecifier(first)) {
hasSpecial = true;
this.print(specifiers.shift(), node);
if (specifiers.length) {
this.push(", ");
while (true) {
let first = specifiers[0];
if (t.isExportDefaultSpecifier(first) || t.isExportNamespaceSpecifier(first)) {
hasSpecial = true;
this.print(specifiers.shift(), node);
if (specifiers.length) {
this.push(", ");
}
} else {
break;
}
}
@@ -102,11 +107,16 @@ export function ImportDeclaration(node: Object) {
let specifiers = node.specifiers.slice(0);
if (specifiers && specifiers.length) {
let first = specifiers[0];
if (t.isImportDefaultSpecifier(first) || t.isImportNamespaceSpecifier(first)) {
this.print(specifiers.shift(), node);
if (specifiers.length) {
this.push(", ");
// print "special" specifiers first
while (true) {
let first = specifiers[0];
if (t.isImportDefaultSpecifier(first) || t.isImportNamespaceSpecifier(first)) {
this.print(specifiers.shift(), node);
if (specifiers.length) {
this.push(", ");
}
} else {
break;
}
}

View File

@@ -31,7 +31,9 @@ export function ForStatement(node: Object) {
this.keyword("for");
this.push("(");
this._inForStatementInit = true;
this.print(node.init, node);
this._inForStatementInit = false;
this.push(";");
if (node.test) {

View File

@@ -36,38 +36,39 @@ export function ObjectExpression(node: Object) {
export { ObjectExpression as ObjectPattern };
export function Property(node: Object) {
export function ObjectMethod(node: Object) {
this.printJoin(node.decorators, node, { separator: "" });
this._method(node);
}
export function ObjectProperty(node: Object) {
this.printJoin(node.decorators, node, { separator: "" });
if (node.method || node.kind === "get" || node.kind === "set") {
this._method(node);
if (node.computed) {
this.push("[");
this.print(node.key, node);
this.push("]");
} else {
if (node.computed) {
this.push("[");
this.print(node.key, node);
this.push("]");
} else {
// print `({ foo: foo = 5 } = {})` as `({ foo = 5 } = {});`
if (t.isAssignmentPattern(node.value) && t.isIdentifier(node.key) && node.key.name === node.value.left.name) {
this.print(node.value, node);
return;
}
this.print(node.key, node);
// shorthand!
if (node.shorthand &&
(t.isIdentifier(node.key) &&
t.isIdentifier(node.value) &&
node.key.name === node.value.name)) {
return;
}
// print `({ foo: foo = 5 } = {})` as `({ foo = 5 } = {});`
if (t.isAssignmentPattern(node.value) && t.isIdentifier(node.key) && node.key.name === node.value.left.name) {
this.print(node.value, node);
return;
}
this.push(":");
this.space();
this.print(node.value, node);
this.print(node.key, node);
// shorthand!
if (node.shorthand &&
(t.isIdentifier(node.key) &&
t.isIdentifier(node.value) &&
node.key.name === node.value.name)) {
return;
}
}
this.push(":");
this.space();
this.print(node.value, node);
}
export function ArrayExpression(node: Object) {

View File

@@ -39,6 +39,7 @@ export class CodeGenerator extends Printer {
shouldPrintComment: boolean;
retainLines: boolean;
comments: boolean;
auxiliaryComment: string;
compact: boolean | "auto";
quotes: "single" | "double";
concise: boolean;
@@ -49,6 +50,7 @@ export class CodeGenerator extends Printer {
}
};
auxiliaryComment: string;
whitespace: Whitespace;
position: Position;
map: SourceMap;
@@ -72,6 +74,7 @@ export class CodeGenerator extends Printer {
}
let format = {
auxiliaryComment: opts.auxiliaryComment,
shouldPrintComment: opts.shouldPrintComment,
retainLines: opts.retainLines,
comments: opts.comments == null || opts.comments,

View File

@@ -82,7 +82,8 @@ export function Binary(node: Object, parent: Object): boolean {
return true;
}
if (parentPos === nodePos && parent.right === node) {
// Logical expressions with the same precedence don't need parens.
if (parentPos === nodePos && parent.right === node && !t.isLogicalExpression(parent)) {
return true;
}
}
@@ -119,6 +120,10 @@ export function SequenceExpression(node: Object, parent: Object): boolean {
return false;
}
if (t.isReturnStatement(parent)) {
return false;
}
// Otherwise err on the side of overparenthesization, adding
// explicit exceptions above if this proves overzealous.
return true;

View File

@@ -163,7 +163,8 @@ exports.nodes = {
* Test if Property or SpreadProperty needs whitespace.
*/
exports.nodes.Property =
exports.nodes.ObjectProperty =
exports.nodes.ObjectMethod =
exports.nodes.SpreadProperty = function (node, parent) {
if (parent.properties[0] === node) {
return {

View File

@@ -6,6 +6,11 @@ import n from "./node";
import * as t from "babel-types";
export default class Printer extends Buffer {
constructor(...args) {
super(...args);
this.insideAux = false;
}
print(node, parent, opts = {}) {
if (!node) return;
@@ -13,6 +18,9 @@ export default class Printer extends Buffer {
node._compact = true;
}
let oldInAux = this.insideAux;
this.insideAux = !node.loc;
let oldConcise = this.format.concise;
if (node._compact) {
this.format.concise = true;
@@ -23,6 +31,8 @@ export default class Printer extends Buffer {
throw new ReferenceError(`unknown node of type ${JSON.stringify(node.type)} with constructor ${JSON.stringify(node && node.constructor.name)}`);
}
this.printAuxComment(oldInAux);
let needsParens = n.needsParens(node, parent);
if (needsParens) this.push("(");
@@ -33,28 +43,47 @@ export default class Printer extends Buffer {
this._printNewline(true, node, parent, opts);
if (opts.before) opts.before();
this.map.mark(node, "start");
//
this._print(node, parent);
this.printTrailingComments(node, parent);
if (needsParens) this.push(")");
// end
this.map.mark(node, "end");
if (opts.after) opts.after();
this.format.concise = oldConcise;
this.insideAux = oldInAux;
this._printNewline(false, node, parent, opts);
}
this.printTrailingComments(node, parent);
printAuxComment(wasInAux) {
let comment = this.format.auxiliaryComment;
if (comment && !wasInAux && this.insideAux) {
this.printComment({
type: "CommentBlock",
value: comment
});
}
}
getPossibleRaw(node) {
let extra = node.extra;
if (extra && extra.raw != null && extra.rawValue != null && node.value === extra.rawValue) {
return extra.raw;
}
}
_print(node, parent) {
let extra = node.extra;
if (extra && extra.raw != null && extra.rawValue != null && node.value === extra.rawValue) {
let extra = this.getPossibleRaw(node);
if (extra) {
this.push("");
this._push(extra.raw);
this._push(extra);
} else {
let printMethod = this[node.type];
printMethod.call(this, node, parent);
@@ -119,17 +148,17 @@ export default class Printer extends Buffer {
}
printTrailingComments(node, parent) {
this._printComments(this.getComments("trailingComments", node, parent));
this.printComments(this.getComments("trailingComments", node, parent));
}
printLeadingComments(node, parent) {
this._printComments(this.getComments("leadingComments", node, parent));
this.printComments(this.getComments("leadingComments", node, parent));
}
printInnerComments(node, indent = true) {
if (!node.innerComments) return;
if (indent) this.indent();
this._printComments(node.innerComments);
this.printComments(node.innerComments);
if (indent) this.dedent();
}
@@ -177,26 +206,7 @@ export default class Printer extends Buffer {
this.newline(lines);
}
getComments(key, node, parent) {
if (t.isExpressionStatement(parent)) {
return [];
}
let comments = [];
let nodes: Array<Object> = [node];
if (t.isExpressionStatement(node)) {
nodes.push(node.argument);
}
for (let node of nodes) {
comments = comments.concat(this._getComments(key, node));
}
return comments;
}
_getComments(key, node) {
getComments(key, node) {
return (node && node[key]) || [];
}
@@ -212,54 +222,64 @@ export default class Printer extends Buffer {
}
}
_printComments(comments) {
printComment(comment) {
if (!this.shouldPrintComment(comment)) return;
if (comment.ignore) return;
comment.ignore = true;
if (comment.start != null) {
if (this.printedCommentStarts[comment.start]) return;
this.printedCommentStarts[comment.start] = true;
}
this.catchUp(comment);
// whitespace before
this.newline(this.whitespace.getNewlinesBefore(comment));
let column = this.position.column;
let val = this.generateComment(comment);
if (column && !this.isLast(["\n", " ", "[", "{"])) {
this._push(" ");
column++;
}
//
if (comment.type === "CommentBlock" && this.format.indent.adjustMultilineComment) {
let offset = comment.loc && comment.loc.start.column;
if (offset) {
let newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
val = val.replace(newlineRegex, "\n");
}
let indent = Math.max(this.indentSize(), column);
val = val.replace(/\n/g, `\n${repeating(" ", indent)}`);
}
if (column === 0) {
val = this.getIndent() + val;
}
// force a newline for line comments when retainLines is set in case the next printed node
// doesn't catch up
if ((this.format.compact || this.format.retainLines) && comment.type === "CommentLine") {
val += "\n";
}
//
this._push(val);
// whitespace after
this.newline(this.whitespace.getNewlinesAfter(comment));
}
printComments(comments?: Array<Object>) {
if (!comments || !comments.length) return;
for (let comment of (comments: Array)) {
if (!this.shouldPrintComment(comment)) continue;
if (comment._displayed) continue;
comment._displayed = true;
this.catchUp(comment);
// whitespace before
this.newline(this.whitespace.getNewlinesBefore(comment));
let column = this.position.column;
let val = this.generateComment(comment);
if (column && !this.isLast(["\n", " ", "[", "{"])) {
this._push(" ");
column++;
}
//
if (comment.type === "CommentBlock" && this.format.indent.adjustMultilineComment) {
let offset = comment.loc && comment.loc.start.column;
if (offset) {
let newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
val = val.replace(newlineRegex, "\n");
}
let indent = Math.max(this.indentSize(), column);
val = val.replace(/\n/g, `\n${repeating(" ", indent)}`);
}
if (column === 0) {
val = this.getIndent() + val;
}
// force a newline for line comments when retainLines is set in case the next printed node
// doesn't catch up
if ((this.format.compact || this.format.retainLines) && comment.type === "CommentLine") {
val += "\n";
}
//
this._push(val);
// whitespace after
this.newline(this.whitespace.getNewlinesAfter(comment));
for (let comment of comments) {
this.printComment(comment);
}
}
}

View File

@@ -1,6 +1,6 @@
!function () {}, //
42;
!{ get 42() {}, //
foo: 42 };
(function () {});
//
!function () {} //
, 42;
!{ get 42() {} //
, foo: 42 };
(function () {} //
);

View File

@@ -0,0 +1,6 @@
function foo() {
if (bar) {
baz();
}
return;
}

View File

@@ -0,0 +1 @@
function foo(){if(bar){baz();}return;}

View File

@@ -1,9 +1,9 @@
for (var i = (1 in []) in []);
for (var i = 1 in [] in []);
for (var i = (10 * 10 in []) in []);
for (var i = (10 + 10 in []) in []);
for (var i = 10 + (10 in []) in []);
for (var i = 10 + 10 in [] in []);
//for (var i = (1 in []) in []);
//for (var i = 1 in [] in []);
//for (var i = (10 * 10 in []) in []);
//for (var i = (10 + 10 in []) in []);
//for (var i = 10 + (10 in []) in []);
//for (var i = 10 + 10 in [] in []);
for (var i = (1 in []);;);
for ((1 in []);;);
for (1 * (1 in []);;);

View File

@@ -1,9 +1,9 @@
for (var i = (1 in []) in []);
for (var i = 1 in ([] in []));
for (var i = (10 * 10 in []) in []);
for (var i = (10 + 10 in []) in []);
for (var i = 10 + (10 in []) in []);
for (var i = 10 + 10 in ([] in []));
//for (var i = (1 in []) in []);
//for (var i = 1 in [] in []);
//for (var i = (10 * 10 in []) in []);
//for (var i = (10 + 10 in []) in []);
//for (var i = 10 + (10 in []) in []);
//for (var i = 10 + 10 in [] in []);
for (var i = (1 in []);;);
for ((1 in []);;);
for (1 * (1 in []);;);

View File

@@ -0,0 +1 @@
for ((a in b) ? a : b; i;);

View File

@@ -0,0 +1 @@
for ((a in b) ? a : b; i;);

View File

@@ -0,0 +1 @@
a && (a.b && a.b.c());

View File

@@ -0,0 +1 @@
a && a.b && a.b.c();

View File

@@ -5,3 +5,7 @@ function foo() {
function bar() {
return "foo";
}
function foo() {
return 1, "foo";
}

View File

@@ -5,3 +5,7 @@ function foo() {
function bar() {
return "foo";
}
function foo() {
return 1, "foo";
}

View File

@@ -36,6 +36,7 @@ suites.forEach(function (testSuite) {
"asyncFunctions",
"exportExtensions",
"functionBind",
"classConstructorCall",
],
strictMode: false,
sourceType: "module",