Only reprint in compact mode
This commit is contained in:
parent
89e6d5bc7e
commit
4b99d18ea7
@ -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(".");
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
13
packages/babel-generator/test/fixtures/compact/literals/actual.js
vendored
Normal file
13
packages/babel-generator/test/fixtures/compact/literals/actual.js
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
5;
|
||||
5.0;
|
||||
"foobar";
|
||||
'\x20';
|
||||
"\n\r";
|
||||
/foobar/g;
|
||||
null;
|
||||
true;
|
||||
false;
|
||||
5.;
|
||||
0b10;
|
||||
0o70;
|
||||
0X1F;
|
||||
1
packages/babel-generator/test/fixtures/compact/literals/expected.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/compact/literals/expected.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
5;5;"foobar";" ";"\n\r";/foobar/g;null;true;false;5;2;56;31;
|
||||
Loading…
x
Reference in New Issue
Block a user