Merge pull request #3145 from babel/always-print

Always print strings for consistency
This commit is contained in:
Amjad Masad 2015-12-07 14:12:32 -05:00
commit b8f5ca4ff0
4 changed files with 35 additions and 8 deletions

View File

@ -221,7 +221,13 @@ export function MemberExpression(node: Object) {
this.push("]");
} else {
if (t.isLiteral(node.object) && !t.isTemplateLiteral(node.object)) {
let val = this.getPossibleRaw(node.object) || this._stringLiteral(node.object);
let val;
if (this.format.compact) {
val = this._stringLiteral(node.object);
} else {
val = this.getPossibleRaw(node.object) || this._stringLiteral(node.object);
}
if (isInteger(+val) && !SCIENTIFIC_NOTATION.test(val) && !ZERO_DECIMAL_INTEGER.test(val) && !this.endsWith(".")) {
this.push(".");
}

View File

@ -97,14 +97,21 @@ export default class Printer extends Buffer {
}
_print(node, parent) {
let extra = this.getPossibleRaw(node);
if (extra) {
this.push("");
this._push(extra);
} else {
let printMethod = this[node.type];
printMethod.call(this, node, parent);
// In compact mode we need to produce as little bytes as needed
// and need to make sure that string quoting is consistent.
// That means we have to always reprint as opposed to getting
// the raw value.
if (!this.format.compact) {
let extra = this.getPossibleRaw(node);
if (extra) {
this.push("");
this._push(extra);
return;
}
}
let printMethod = this[node.type];
printMethod.call(this, node, parent);
}
printJoin(nodes: ?Array, parent: Object, opts = {}) {

View File

@ -0,0 +1,13 @@
5;
5.0;
"foobar";
'\x20';
"\n\r";
/foobar/g;
null;
true;
false;
5.;
0b10;
0o70;
0X1F;

View File

@ -0,0 +1 @@
5;5;"foobar";" ";"\n\r";/foobar/g;null;true;false;5;2;56;31;