Only reprint in compact mode

This commit is contained in:
Amjad Masad 2015-12-07 10:59:50 -08:00
parent 89e6d5bc7e
commit 4b99d18ea7
4 changed files with 41 additions and 1 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._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

@ -89,7 +89,27 @@ export default class Printer extends Buffer {
}
}
getPossibleRaw(node) {
let extra = node.extra;
if (extra && extra.raw != null && extra.rawValue != null && node.value === extra.rawValue) {
return extra.raw;
}
}
_print(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);
}

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;