Add attachComment parser option to disable comment attachment (#13229)
This commit is contained in:
@@ -22,6 +22,7 @@ export type Options = {
|
||||
tokens: boolean,
|
||||
createParenthesizedExpressions: boolean,
|
||||
errorRecovery: boolean,
|
||||
attachComment: boolean,
|
||||
};
|
||||
|
||||
export const defaultOptions: Options = {
|
||||
@@ -66,6 +67,11 @@ export const defaultOptions: Options = {
|
||||
// When enabled, errors are attached to the AST instead of being directly thrown.
|
||||
// Some errors will still throw, because @babel/parser can't always recover.
|
||||
errorRecovery: false,
|
||||
// When enabled, comments will be attached to adjacent AST nodes as one of
|
||||
// `leadingComments`, `trailingComments` and `innerComments`. The comment attachment
|
||||
// is vital to preserve comments after transform. If you don't print AST back,
|
||||
// consider set this option to `false` for performance
|
||||
attachComment: true,
|
||||
};
|
||||
|
||||
// Interpret and default an options object
|
||||
|
||||
@@ -9,7 +9,6 @@ import type { Comment, Node as NodeType, NodeBase } from "../types";
|
||||
|
||||
class Node implements NodeBase {
|
||||
constructor(parser: Parser, pos: number, loc: Position) {
|
||||
this.type = "";
|
||||
this.start = pos;
|
||||
this.end = 0;
|
||||
this.loc = new SourceLocation(loc);
|
||||
@@ -17,15 +16,15 @@ class Node implements NodeBase {
|
||||
if (parser?.filename) this.loc.filename = parser.filename;
|
||||
}
|
||||
|
||||
type: string;
|
||||
start: number;
|
||||
end: number;
|
||||
loc: SourceLocation;
|
||||
range: [number, number];
|
||||
leadingComments: Array<Comment>;
|
||||
trailingComments: Array<Comment>;
|
||||
innerComments: Array<Comment>;
|
||||
extra: { [key: string]: any };
|
||||
type: string = "";
|
||||
declare start: number;
|
||||
declare end: number;
|
||||
declare loc: SourceLocation;
|
||||
declare range: [number, number];
|
||||
declare leadingComments: Array<Comment>;
|
||||
declare trailingComments: Array<Comment>;
|
||||
declare innerComments: Array<Comment>;
|
||||
declare extra: { [key: string]: any };
|
||||
}
|
||||
const NodePrototype = Node.prototype;
|
||||
|
||||
@@ -135,7 +134,7 @@ export class NodeUtils extends UtilParser {
|
||||
node.end = pos;
|
||||
node.loc.end = loc;
|
||||
if (this.options.ranges) node.range[1] = pos;
|
||||
this.processComment(node);
|
||||
if (this.options.attachComment) this.processComment(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
@@ -403,7 +403,7 @@ export default class Tokenizer extends ParserErrors {
|
||||
const comment = this.skipBlockComment();
|
||||
if (comment !== undefined) {
|
||||
this.addComment(comment);
|
||||
comments.push(comment);
|
||||
if (this.options.attachComment) comments.push(comment);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -412,7 +412,7 @@ export default class Tokenizer extends ParserErrors {
|
||||
const comment = this.skipLineComment(2);
|
||||
if (comment !== undefined) {
|
||||
this.addComment(comment);
|
||||
comments.push(comment);
|
||||
if (this.options.attachComment) comments.push(comment);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -436,7 +436,7 @@ export default class Tokenizer extends ParserErrors {
|
||||
const comment = this.skipLineComment(3);
|
||||
if (comment !== undefined) {
|
||||
this.addComment(comment);
|
||||
comments.push(comment);
|
||||
if (this.options.attachComment) comments.push(comment);
|
||||
}
|
||||
} else {
|
||||
break loop;
|
||||
@@ -452,7 +452,7 @@ export default class Tokenizer extends ParserErrors {
|
||||
const comment = this.skipLineComment(4);
|
||||
if (comment !== undefined) {
|
||||
this.addComment(comment);
|
||||
comments.push(comment);
|
||||
if (this.options.attachComment) comments.push(comment);
|
||||
}
|
||||
} else {
|
||||
break loop;
|
||||
|
||||
Reference in New Issue
Block a user