Improve massageAST (#9519)
This commit is contained in:
1 parent
bdc8bf0de3
commit
51d6badb82
2 files changed
+29
-21
No files matched your search
+18
-18
@@ -1,24 +1,22 @@
|
||||
"use strict";
|
||||
|
||||
function clean(ast, newObj, parent) {
|
||||
[
|
||||
"range",
|
||||
"raw",
|
||||
"comments",
|
||||
"leadingComments",
|
||||
"trailingComments",
|
||||
"innerComments",
|
||||
"extra",
|
||||
"start",
|
||||
"end",
|
||||
"loc",
|
||||
"flags",
|
||||
"errors",
|
||||
"tokens",
|
||||
].forEach((name) => {
|
||||
delete newObj[name];
|
||||
});
|
||||
const ignoredProperties = new Set([
|
||||
"range",
|
||||
"raw",
|
||||
"comments",
|
||||
"leadingComments",
|
||||
"trailingComments",
|
||||
"innerComments",
|
||||
"extra",
|
||||
"start",
|
||||
"end",
|
||||
"loc",
|
||||
"flags",
|
||||
"errors",
|
||||
"tokens",
|
||||
]);
|
||||
|
||||
function clean(ast, newObj, parent) {
|
||||
if (ast.type === "Program") {
|
||||
delete newObj.sourceType;
|
||||
}
|
||||
@@ -210,4 +208,6 @@ function clean(ast, newObj, parent) {
|
||||
}
|
||||
}
|
||||
|
||||
clean.ignoredProperties = ignoredProperties;
|
||||
|
||||
module.exports = clean;
|
||||
+11
-3
@@ -9,15 +9,23 @@ function massageAST(ast, options, parent) {
|
||||
return ast;
|
||||
}
|
||||
|
||||
const cleanFunction = options.printer.massageAstNode;
|
||||
let ignoredProperties;
|
||||
if (cleanFunction && cleanFunction.ignoredProperties) {
|
||||
ignoredProperties = cleanFunction.ignoredProperties;
|
||||
} else {
|
||||
ignoredProperties = new Set();
|
||||
}
|
||||
|
||||
const newObj = {};
|
||||
for (const key of Object.keys(ast)) {
|
||||
if (typeof ast[key] !== "function") {
|
||||
if (!ignoredProperties.has(key) && typeof ast[key] !== "function") {
|
||||
newObj[key] = massageAST(ast[key], options, ast);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.printer.massageAstNode) {
|
||||
const result = options.printer.massageAstNode(ast, newObj, parent);
|
||||
if (cleanFunction) {
|
||||
const result = cleanFunction(ast, newObj, parent);
|
||||
if (result === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user