Avoid looking for raw values for all nodes.

This commit is contained in:
Logan Smyth 2016-04-24 20:46:24 -07:00
parent 710f151d31
commit 528128f62d
4 changed files with 25 additions and 32 deletions

View File

@ -38,6 +38,4 @@ export function Directive(node: Object) {
this.semicolon();
}
export function DirectiveLiteral(node: Object) {
this.push(this._stringLiteral(node.value));
}
export { StringLiteral as DirectiveLiteral } from "./types";

View File

@ -140,16 +140,15 @@ export function NullableTypeAnnotation(node: Object) {
this.print(node.typeAnnotation, node);
}
export { NumericLiteral as NumericLiteralTypeAnnotation } from "./types";
export {
NumericLiteral as NumericLiteralTypeAnnotation,
StringLiteral as StringLiteralTypeAnnotation,
} from "./types";
export function NumberTypeAnnotation() {
this.push("number");
}
export function StringLiteralTypeAnnotation(node: Object) {
this.push(this._stringLiteral(node.value));
}
export function StringTypeAnnotation() {
this.push("string");
}

View File

@ -123,15 +123,27 @@ export function NullLiteral() {
}
export function NumericLiteral(node: Object) {
let raw = this.getPossibleRaw(node);
if (raw != null) {
// Write an empty string to add indentation on just this first time.
this.push("");
this.push(raw, true /* noIndent */);
return;
}
this.push(node.value + "");
}
export function StringLiteral(node: Object, parent: Object) {
this.push(this._stringLiteral(node.value, parent));
}
let raw = this.getPossibleRaw(node);
if (raw != null) {
// Write an empty string to add indentation on just this first time.
this.push("");
this.push(raw, true /* noIndent */);
return;
}
export function _stringLiteral(val: string, parent: Object): string {
val = JSON.stringify(val);
let val = JSON.stringify(node.value);
// escape illegal js but valid json unicode characters
val = val.replace(/[\u000A\u000D\u2028\u2029]/g, function (c) {
@ -152,5 +164,5 @@ export function _stringLiteral(val: string, parent: Object): string {
val = `'${val}'`;
}
return val;
return this.push(val);
}

View File

@ -53,7 +53,7 @@ export default class Printer extends Buffer {
let loc = (t.isProgram(node) || t.isFile(node)) ? null : node.loc;
this.withSource("start", loc, () => {
this._print(node, parent);
this[node.type](node, parent);
});
// Check again if any of our children may have left an aux comment on the stack
@ -96,30 +96,14 @@ export default class Printer extends Buffer {
}
getPossibleRaw(node) {
if (this.format.minified) return;
let extra = node.extra;
if (extra && extra.raw != null && extra.rawValue != null && node.value === extra.rawValue) {
return extra.raw;
}
}
_print(node, parent) {
// In minified 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.minified) {
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 = {}) {
if (!nodes || !nodes.length) return;