clean up auxiliaryComment

This commit is contained in:
Sebastian McKenzie 2015-11-03 02:46:00 +00:00
parent 56e9805657
commit a639bffcd6
4 changed files with 39 additions and 14 deletions

View File

@ -139,9 +139,14 @@
"default": "module" "default": "module"
}, },
"auxiliaryComment": { "auxiliaryCommentBefore": {
"description": "", "type": "string",
"type": "string" "description": "print a comment before any injected non-user code"
},
"auxiliaryCommentAfter": {
"type": "string",
"description": "print a comment after any injected non-user code"
}, },
"resolveModuleSource": { "resolveModuleSource": {

View File

@ -47,7 +47,8 @@ suite("api", function () {
test("auxiliaryComment option", function () { test("auxiliaryComment option", function () {
return transformAsync("class Foo {}", { return transformAsync("class Foo {}", {
auxiliaryComment: "yo bro", auxiliaryCommentBefore: "before",
auxiliaryCommentAfter: "after",
plugins: [function (babel) { plugins: [function (babel) {
var t = babel.types; var t = babel.types;
return { return {
@ -60,7 +61,7 @@ suite("api", function () {
}; };
}] }]
}).then(function (result) { }).then(function (result) {
assert.equal(result.code, "/*yo bro*/start;\nclass Foo {}\n/*yo bro*/end;"); assert.equal(result.code, "/*before*/start;\n/*after*/class Foo {}\n/*before*/end;\n/*after*/");
}); });
}); });

View File

@ -39,7 +39,8 @@ export class CodeGenerator extends Printer {
shouldPrintComment: boolean; shouldPrintComment: boolean;
retainLines: boolean; retainLines: boolean;
comments: boolean; comments: boolean;
auxiliaryComment: string; auxiliaryCommentBefore: string;
auxiliaryCommentAfter: string;
compact: boolean | "auto"; compact: boolean | "auto";
quotes: "single" | "double"; quotes: "single" | "double";
concise: boolean; concise: boolean;
@ -50,7 +51,8 @@ export class CodeGenerator extends Printer {
} }
}; };
auxiliaryComment: string; auxiliaryCommentBefore: string;
auxiliaryCommentAfter: string;
whitespace: Whitespace; whitespace: Whitespace;
position: Position; position: Position;
map: SourceMap; map: SourceMap;
@ -64,6 +66,7 @@ export class CodeGenerator extends Printer {
* *
* - Detects code indentation. * - Detects code indentation.
* - If `opts.compact = "auto"` and the code is over 100KB, `compact` will be set to `true`. * - If `opts.compact = "auto"` and the code is over 100KB, `compact` will be set to `true`.
*/ */
static normalizeOptions(code, opts, tokens) { static normalizeOptions(code, opts, tokens) {
@ -74,7 +77,8 @@ export class CodeGenerator extends Printer {
} }
let format = { let format = {
auxiliaryComment: opts.auxiliaryComment, auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
shouldPrintComment: opts.shouldPrintComment, shouldPrintComment: opts.shouldPrintComment,
retainLines: opts.retainLines, retainLines: opts.retainLines,
comments: opts.comments == null || opts.comments, comments: opts.comments == null || opts.comments,
@ -143,6 +147,7 @@ export class CodeGenerator extends Printer {
generate() { generate() {
this.print(this.ast); this.print(this.ast);
this.printAuxAfterComment();
return { return {
map: this.map.get(), map: this.map.get(),

View File

@ -9,6 +9,7 @@ export default class Printer extends Buffer {
constructor(...args) { constructor(...args) {
super(...args); super(...args);
this.insideAux = false; this.insideAux = false;
this.printAuxAfterOnNextUserNode = false;
} }
print(node, parent, opts = {}) { print(node, parent, opts = {}) {
@ -31,7 +32,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)}`); throw new ReferenceError(`unknown node of type ${JSON.stringify(node.type)} with constructor ${JSON.stringify(node && node.constructor.name)}`);
} }
this.printAuxComment(oldInAux); if (node.loc) this.printAuxAfterComment();
this.printAuxBeforeComment(oldInAux);
let needsParens = n.needsParens(node, parent); let needsParens = n.needsParens(node, parent);
if (needsParens) this.push("("); if (needsParens) this.push("(");
@ -62,10 +64,22 @@ export default class Printer extends Buffer {
this._printNewline(false, node, parent, opts); this._printNewline(false, node, parent, opts);
} }
printAuxComment(wasInAux) { printAuxBeforeComment(wasInAux) {
let comment = this.format.auxiliaryComment; let comment = this.format.auxiliaryCommentBefore;
if (comment && !wasInAux && this.insideAux) { if (!wasInAux && this.insideAux) {
this.printComment({ this.printAuxAfterOnNextUserNode = true;
if (comment) this.printComment({
type: "CommentBlock",
value: comment
});
}
}
printAuxAfterComment() {
if (this.printAuxAfterOnNextUserNode) {
this.printAuxAfterOnNextUserNode = false;
let comment = this.format.auxiliaryCommentAfter;
if (comment) this.printComment({
type: "CommentBlock", type: "CommentBlock",
value: comment value: comment
}); });