diff --git a/src/language-js/clean.js b/src/language-js/clean.js index 4f5bfbf20..25d67a044 100644 --- a/src/language-js/clean.js +++ b/src/language-js/clean.js @@ -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; diff --git a/src/main/massage-ast.js b/src/main/massage-ast.js index fbede504a..f2ce7beb7 100644 --- a/src/main/massage-ast.js +++ b/src/main/massage-ast.js @@ -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; }