Files
test-repo/src/clean-ast.js
T
Joseph Frazier 18fd014985 Run AST comparison tests on Travis (#1553)
* Run AST comparison tests on Travis

It looks like some of these currently fail, so we should probably also
sort that out.

Inspired by https://github.com/prettier/prettier/issues/1552

* tests: Use specified parser when AST_COMPARE=1

This fixes some of the tests with AST_COMPARE=1

* Move cleanAST() into prettier.__debug

This makes it available for tests to use.

* AST_COMPARE=1 uses cleanAst() instead of removeEmptyStatements()

Ths fixes some of the tests with AST_COMPARE=1

* Export parse() from src/parser.js

This makes it available for tests to use.

* tests: Use specified parser more when AST_COMPARE=1

This is a continuation of commit 86437a66d326919897fe89891a25824870f5bb79

This fixes some of the tests with AST_COMPARE=1

* massageAST: remove leadingComments/trailingComments

This fixes some of the tests with AST_COMPARE=1

* massageAST: remove `extra`

This fixes some of the tests with AST_COMPARE=1

* tests_config/run_spec.js: Rename variables for clarity

* AST_COMPARE=1 tests compare unstringified objects

This makes the test error output shorter.

* fixup! Export parse() from src/parser.js

* Revert "Run AST comparison tests on Travis"

See https://github.com/prettier/prettier/pull/1553#issuecomment-300027747

This reverts commit 49873a956c532f23fd216551a35ae35c1a18407e.

* fixup! fixup! Export parse() from src/parser.js

* parser: Require babel-code-frame only when needed

This addresses:
* https://github.com/prettier/prettier/pull/1553#discussion_r115386253
* https://github.com/prettier/prettier/pull/1553#discussion_r115386250

* parser: Don't export now-unused parseWith* functions

Addresses https://github.com/prettier/prettier/pull/1553#discussion_r115386964

* Move cleanAST/massageAST into own file, don't export

This addresses:
* https://github.com/prettier/prettier/pull/1553#discussion_r115386993
* https://github.com/prettier/prettier/pull/1553#discussion_r115386611

* Don't destructure require() result (Node v4 compat.)

* Fix copy/paste error
2017-05-08 18:16:35 -07:00

76 lines
1.6 KiB
JavaScript

"use strict";
function cleanAST(ast) {
return JSON.stringify(massageAST(ast), null, 2);
}
function massageAST(ast) {
if (Array.isArray(ast)) {
return ast.map(e => massageAST(e)).filter(e => e);
}
if (ast && typeof ast === "object") {
// We remove extra `;` and add them when needed
if (ast.type === "EmptyStatement") {
return undefined;
}
// We move text around, including whitespaces and add {" "}
if (ast.type === "JSXText") {
return undefined;
}
if (
ast.type === "JSXExpressionContainer" &&
ast.expression.type === "Literal" &&
ast.expression.value === " "
) {
return undefined;
}
const newObj = {};
for (var key in ast) {
newObj[key] = massageAST(ast[key]);
}
[
"loc",
"range",
"raw",
"comments",
"leadingComments",
"trailingComments",
"extra",
"start",
"end",
"tokens",
"flags"
].forEach(name => {
delete newObj[name];
});
// We convert <div></div> to <div />
if (ast.type === "JSXOpeningElement") {
delete newObj.selfClosing;
}
if (ast.type === "JSXElement") {
delete newObj.closingElement;
}
// We change {'key': value} into {key: value}
if (
(ast.type === "Property" ||
ast.type === "MethodDefinition" ||
ast.type === "ClassProperty") &&
typeof ast.key === "object" &&
ast.key &&
(ast.key.type === "Literal" || ast.key.type === "Identifier")
) {
delete newObj.key;
}
return newObj;
}
return ast;
}
module.exports = { cleanAST, massageAST };