2017-01-10 20:18:22 +03:00
|
|
|
"use strict";
|
2017-04-12 23:41:51 +03:00
|
|
|
|
2017-03-25 18:10:17 +03:00
|
|
|
const fs = require("fs");
|
2017-05-08 15:57:19 +03:00
|
|
|
const extname = require("path").extname;
|
2017-01-12 18:09:13 +03:00
|
|
|
const prettier = require("../");
|
2017-05-01 19:25:49 +03:00
|
|
|
const types = require("../src/ast-types");
|
2017-03-25 18:10:17 +03:00
|
|
|
const parser = require("../src/parser");
|
2017-05-09 04:16:35 +03:00
|
|
|
const massageAST = require('../src/clean-ast.js').massageAST;
|
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;
|
2017-04-13 17:08:55 +03:00
|
|
|
const ALL_PARSERS = process.env["ALL_PARSERS"]
|
|
|
|
? JSON.parse(process.env["ALL_PARSERS"])
|
2017-03-25 18:10:17 +03:00
|
|
|
: ["flow", "babylon", "typescript"];
|
2016-12-30 08:01:44 +03:00
|
|
|
|
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-05-08 15:57:19 +03:00
|
|
|
const extension = extname(filename);
|
|
|
|
if (/^\.[jt]sx?$/.test(extension) && filename !== "jsfmt.spec.js") {
|
2017-03-25 18:10:17 +03:00
|
|
|
const path = dirname + "/" + filename;
|
2017-05-09 04:16:35 +03:00
|
|
|
const mergedOptions = mergeDefaultOptions(options || {});
|
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 output = prettyprint(source, path, mergedOptions);
|
|
|
|
test(`${mergedOptions.parser} - ${parser.parser}-verify`, () => {
|
2017-04-12 23:41:51 +03:00
|
|
|
expect(raw(source + "~".repeat(80) + "\n" + output)).toMatchSnapshot(
|
2017-03-25 18:10:17 +03:00
|
|
|
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);
|
2017-05-09 04:16:35 +03:00
|
|
|
const ast = parse(source, mergedOptions);
|
|
|
|
const astMassaged = massageAST(ast);
|
|
|
|
let ppastMassaged;
|
2016-12-27 21:28:04 +03:00
|
|
|
let pperr = null;
|
|
|
|
try {
|
2017-05-09 04:16:35 +03:00
|
|
|
const ppast = parse(prettyprint(source, path, mergedOptions), mergedOptions)
|
|
|
|
ppastMassaged = massageAST(ppast);
|
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);
|
2017-05-09 04:16:35 +03:00
|
|
|
expect(ppastMassaged).toBeDefined();
|
|
|
|
if (!ast.errors || ast.errors.length === 0) {
|
|
|
|
expect(astMassaged).toEqual(ppastMassaged);
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-09 04:16:35 +03:00
|
|
|
function parse(string, opts) {
|
|
|
|
return stripLocation(parser.parse(string, opts));
|
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");
|
|
|
|
}
|
|
|
|
|
2017-04-12 23:41:51 +03:00
|
|
|
/**
|
|
|
|
* Wraps a string in a marker object that is used by `./raw-serializer.js` to
|
|
|
|
* directly print that string in a snapshot without escaping all double quotes.
|
|
|
|
* Backticks will still be escaped.
|
|
|
|
*/
|
|
|
|
function raw(string) {
|
|
|
|
if (typeof string !== "string") {
|
|
|
|
throw new Error("Raw snapshots have to be strings.");
|
|
|
|
}
|
|
|
|
return { [Symbol.for("raw")]: string };
|
|
|
|
}
|
|
|
|
|
2017-03-25 18:10:17 +03:00
|
|
|
function mergeDefaultOptions(parserConfig) {
|
|
|
|
return Object.assign(
|
|
|
|
{
|
|
|
|
parser: "flow",
|
|
|
|
printWidth: 80
|
|
|
|
},
|
|
|
|
parserConfig
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getParsersToVerify(parser, additionalParsers) {
|
|
|
|
if (VERIFY_ALL_PARSERS) {
|
2017-05-09 03:27:39 +03:00
|
|
|
return ALL_PARSERS.splice(ALL_PARSERS.indexOf(parser), 1);
|
2017-03-25 18:10:17 +03:00
|
|
|
}
|
|
|
|
return additionalParsers;
|
2016-12-23 22:31:38 +03:00
|
|
|
}
|