2017-01-10 20:18:22 +03:00
|
|
|
"use strict";
|
2017-03-25 18:10:17 +03:00
|
|
|
const fs = require("fs");
|
2017-01-12 18:09:13 +03:00
|
|
|
const prettier = require("../");
|
2016-12-30 22:44:57 +03:00
|
|
|
const types = require("ast-types");
|
2017-03-25 18:10:17 +03:00
|
|
|
const parser = require("../src/parser");
|
2016-12-23 22:31:38 +03:00
|
|
|
|
2016-12-30 08:01:44 +03:00
|
|
|
const RUN_AST_TESTS = process.env["AST_COMPARE"];
|
2017-03-25 18:10:17 +03:00
|
|
|
const VERIFY_ALL_PARSERS = process.env["VERIFY_ALL_PARSERS"] || false;
|
|
|
|
const ALL_PASERS = process.env["ALL_PASERS"]
|
|
|
|
? JSON.parse(process.env["ALL_PASERS"])
|
|
|
|
: ["flow", "babylon", "typescript"];
|
2016-12-30 08:01:44 +03:00
|
|
|
|
2016-12-30 22:44:57 +03:00
|
|
|
// Ignoring empty statements that are added into the output removes a
|
|
|
|
// lot of noise from test failures and let's us focus on the real
|
|
|
|
// failures when comparing asts
|
|
|
|
function removeEmptyStatements(ast) {
|
|
|
|
return types.visit(ast, {
|
|
|
|
visitEmptyStatement: function(path) {
|
|
|
|
path.prune();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-25 18:10:17 +03:00
|
|
|
function run_spec(dirname, options, additionalParsers) {
|
2016-12-23 22:31:38 +03:00
|
|
|
fs.readdirSync(dirname).forEach(filename => {
|
2017-03-25 18:10:17 +03:00
|
|
|
if (
|
|
|
|
(filename.endsWith(".js") || filename.endsWith(".ts")) &&
|
|
|
|
filename !== "jsfmt.spec.js"
|
|
|
|
) {
|
|
|
|
const path = dirname + "/" + filename;
|
2016-12-23 22:31:38 +03:00
|
|
|
|
|
|
|
if (!RUN_AST_TESTS) {
|
2017-03-25 18:10:17 +03:00
|
|
|
const source = read(path).replace(/\r\n/g, "\n");
|
|
|
|
const mergedOptions = mergeDefaultOptions(options || {});
|
|
|
|
const output = prettyprint(source, path, mergedOptions);
|
|
|
|
test(`${mergedOptions.parser} - ${parser.parser}-verify`, () => {
|
|
|
|
expect(source + "~".repeat(80) + "\n" + output).toMatchSnapshot(
|
|
|
|
filename
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
getParsersToVerify(
|
|
|
|
mergedOptions.parser,
|
|
|
|
additionalParsers || []
|
|
|
|
).forEach(parserName => {
|
|
|
|
test(`${filename} - ${parserName}-verify`, () => {
|
|
|
|
const verifyOptions = Object.assign(mergedOptions, {
|
|
|
|
parser: parserName
|
|
|
|
});
|
|
|
|
const verifyOutput = prettyprint(source, path, verifyOptions);
|
|
|
|
expect(output).toEqual(verifyOutput);
|
|
|
|
});
|
2016-12-23 22:31:38 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RUN_AST_TESTS) {
|
2017-03-25 18:10:17 +03:00
|
|
|
const source = read(dirname + "/" + filename);
|
2016-12-30 23:07:35 +03:00
|
|
|
const ast = removeEmptyStatements(parse(source));
|
2016-12-27 21:28:04 +03:00
|
|
|
let prettyprinted = false;
|
|
|
|
let ppast;
|
|
|
|
let pperr = null;
|
|
|
|
try {
|
2016-12-30 23:07:35 +03:00
|
|
|
ppast = removeEmptyStatements(parse(prettyprint(source, path)));
|
2017-03-25 18:10:17 +03:00
|
|
|
} catch (e) {
|
2016-12-27 21:28:04 +03:00
|
|
|
pperr = e.stack;
|
|
|
|
}
|
|
|
|
|
2017-03-25 18:10:17 +03:00
|
|
|
test(path + " parse", () => {
|
2016-12-27 21:28:04 +03:00
|
|
|
expect(pperr).toBe(null);
|
|
|
|
expect(ppast).toBeDefined();
|
2017-03-25 18:10:17 +03:00
|
|
|
if (ast.errors.length === 0) {
|
2016-12-30 23:07:35 +03:00
|
|
|
expect(ast).toEqual(ppast);
|
2016-12-30 08:01:44 +03:00
|
|
|
}
|
2016-12-23 22:31:38 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
global.run_spec = run_spec;
|
|
|
|
|
|
|
|
function stripLocation(ast) {
|
|
|
|
if (Array.isArray(ast)) {
|
|
|
|
return ast.map(e => stripLocation(e));
|
|
|
|
}
|
2017-03-25 18:10:17 +03:00
|
|
|
if (typeof ast === "object") {
|
2016-12-23 22:31:38 +03:00
|
|
|
const newObj = {};
|
|
|
|
for (var key in ast) {
|
2017-03-25 18:10:17 +03:00
|
|
|
if (
|
|
|
|
key === "loc" || key === "range" || key === "raw" || key === "comments"
|
|
|
|
) {
|
2016-12-23 22:31:38 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
newObj[key] = stripLocation(ast[key]);
|
|
|
|
}
|
|
|
|
return newObj;
|
|
|
|
}
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parse(string) {
|
2017-01-20 00:23:04 +03:00
|
|
|
return stripLocation(parser.parseWithFlow(string));
|
2016-12-23 22:31:38 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 09:29:59 +03:00
|
|
|
function prettyprint(src, filename, options) {
|
2017-03-25 18:10:17 +03:00
|
|
|
return prettier.format(
|
|
|
|
src,
|
|
|
|
Object.assign(
|
|
|
|
{
|
|
|
|
filename
|
|
|
|
},
|
|
|
|
options
|
|
|
|
)
|
|
|
|
);
|
2016-12-23 22:31:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function read(filename) {
|
2017-03-25 18:10:17 +03:00
|
|
|
return fs.readFileSync(filename, "utf8");
|
|
|
|
}
|
|
|
|
|
|
|
|
function mergeDefaultOptions(parserConfig) {
|
|
|
|
return Object.assign(
|
|
|
|
{
|
|
|
|
parser: "flow",
|
|
|
|
printWidth: 80
|
|
|
|
},
|
|
|
|
parserConfig
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getParsersToVerify(parser, additionalParsers) {
|
|
|
|
if (VERIFY_ALL_PARSERS) {
|
|
|
|
return ALL_PASERS.splice(ALL_PASERS.indexOf(parent), 1);
|
|
|
|
}
|
|
|
|
return additionalParsers;
|
2016-12-23 22:31:38 +03:00
|
|
|
}
|