clean up auxiliaryComment
This commit is contained in:
parent
56e9805657
commit
a639bffcd6
@ -138,10 +138,15 @@
|
||||
"description": "",
|
||||
"default": "module"
|
||||
},
|
||||
|
||||
"auxiliaryCommentBefore": {
|
||||
"type": "string",
|
||||
"description": "print a comment before any injected non-user code"
|
||||
},
|
||||
|
||||
"auxiliaryComment": {
|
||||
"description": "",
|
||||
"type": "string"
|
||||
"auxiliaryCommentAfter": {
|
||||
"type": "string",
|
||||
"description": "print a comment after any injected non-user code"
|
||||
},
|
||||
|
||||
"resolveModuleSource": {
|
||||
|
||||
@ -47,7 +47,8 @@ suite("api", function () {
|
||||
|
||||
test("auxiliaryComment option", function () {
|
||||
return transformAsync("class Foo {}", {
|
||||
auxiliaryComment: "yo bro",
|
||||
auxiliaryCommentBefore: "before",
|
||||
auxiliaryCommentAfter: "after",
|
||||
plugins: [function (babel) {
|
||||
var t = babel.types;
|
||||
return {
|
||||
@ -60,7 +61,7 @@ suite("api", function () {
|
||||
};
|
||||
}]
|
||||
}).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*/");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -39,7 +39,8 @@ export class CodeGenerator extends Printer {
|
||||
shouldPrintComment: boolean;
|
||||
retainLines: boolean;
|
||||
comments: boolean;
|
||||
auxiliaryComment: string;
|
||||
auxiliaryCommentBefore: string;
|
||||
auxiliaryCommentAfter: string;
|
||||
compact: boolean | "auto";
|
||||
quotes: "single" | "double";
|
||||
concise: boolean;
|
||||
@ -50,7 +51,8 @@ export class CodeGenerator extends Printer {
|
||||
}
|
||||
};
|
||||
|
||||
auxiliaryComment: string;
|
||||
auxiliaryCommentBefore: string;
|
||||
auxiliaryCommentAfter: string;
|
||||
whitespace: Whitespace;
|
||||
position: Position;
|
||||
map: SourceMap;
|
||||
@ -64,6 +66,7 @@ export class CodeGenerator extends Printer {
|
||||
*
|
||||
* - Detects code indentation.
|
||||
* - If `opts.compact = "auto"` and the code is over 100KB, `compact` will be set to `true`.
|
||||
|
||||
*/
|
||||
|
||||
static normalizeOptions(code, opts, tokens) {
|
||||
@ -74,7 +77,8 @@ export class CodeGenerator extends Printer {
|
||||
}
|
||||
|
||||
let format = {
|
||||
auxiliaryComment: opts.auxiliaryComment,
|
||||
auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
|
||||
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
|
||||
shouldPrintComment: opts.shouldPrintComment,
|
||||
retainLines: opts.retainLines,
|
||||
comments: opts.comments == null || opts.comments,
|
||||
@ -143,6 +147,7 @@ export class CodeGenerator extends Printer {
|
||||
|
||||
generate() {
|
||||
this.print(this.ast);
|
||||
this.printAuxAfterComment();
|
||||
|
||||
return {
|
||||
map: this.map.get(),
|
||||
|
||||
@ -9,6 +9,7 @@ export default class Printer extends Buffer {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
this.insideAux = false;
|
||||
this.printAuxAfterOnNextUserNode = false;
|
||||
}
|
||||
|
||||
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)}`);
|
||||
}
|
||||
|
||||
this.printAuxComment(oldInAux);
|
||||
if (node.loc) this.printAuxAfterComment();
|
||||
this.printAuxBeforeComment(oldInAux);
|
||||
|
||||
let needsParens = n.needsParens(node, parent);
|
||||
if (needsParens) this.push("(");
|
||||
@ -62,10 +64,22 @@ export default class Printer extends Buffer {
|
||||
this._printNewline(false, node, parent, opts);
|
||||
}
|
||||
|
||||
printAuxComment(wasInAux) {
|
||||
let comment = this.format.auxiliaryComment;
|
||||
if (comment && !wasInAux && this.insideAux) {
|
||||
this.printComment({
|
||||
printAuxBeforeComment(wasInAux) {
|
||||
let comment = this.format.auxiliaryCommentBefore;
|
||||
if (!wasInAux && this.insideAux) {
|
||||
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",
|
||||
value: comment
|
||||
});
|
||||
@ -224,7 +238,7 @@ export default class Printer extends Buffer {
|
||||
|
||||
printComment(comment) {
|
||||
if (!this.shouldPrintComment(comment)) return;
|
||||
|
||||
|
||||
if (comment.ignore) return;
|
||||
comment.ignore = true;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user