2016-12-23 22:31:38 +03:00
|
|
|
const fs = require('fs');
|
2016-12-24 05:12:18 +03:00
|
|
|
const jscodefmt = require("../");
|
2016-12-30 22:44:57 +03:00
|
|
|
const recast = require("recast");
|
|
|
|
const types = require("ast-types");
|
2016-12-23 22:31:38 +03:00
|
|
|
|
2016-12-30 08:01:44 +03:00
|
|
|
const RUN_AST_TESTS = process.env["AST_COMPARE"];
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-23 22:31:38 +03:00
|
|
|
function run_spec(dirname) {
|
|
|
|
fs.readdirSync(dirname).forEach(filename => {
|
|
|
|
if (filename.endsWith('.js') && filename !== 'jsfmt.spec.js') {
|
|
|
|
const path = dirname + '/' + filename;
|
|
|
|
|
|
|
|
if (!RUN_AST_TESTS) {
|
|
|
|
const source = read(path);
|
2016-12-30 21:41:45 +03:00
|
|
|
const output = prettyprint(source, path);
|
2016-12-23 22:31:38 +03:00
|
|
|
test(filename, () => {
|
|
|
|
expect(source + '~'.repeat(80) + '\n' + output).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RUN_AST_TESTS) {
|
2016-12-27 21:28:04 +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)));
|
2016-12-27 21:28:04 +03:00
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
pperr = e.stack;
|
|
|
|
}
|
|
|
|
|
2016-12-23 22:31:38 +03:00
|
|
|
test(path + ' parse', () => {
|
2016-12-27 21:28:04 +03:00
|
|
|
expect(pperr).toBe(null);
|
|
|
|
expect(ppast).toBeDefined();
|
2016-12-30 23:07:35 +03:00
|
|
|
if(ast.errors.length === 0) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
if (typeof ast === 'object') {
|
|
|
|
const newObj = {};
|
|
|
|
for (var key in ast) {
|
|
|
|
if (key === 'loc' || key === 'range' || key === 'raw' || key === 'comments') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
newObj[key] = stripLocation(ast[key]);
|
|
|
|
}
|
|
|
|
return newObj;
|
|
|
|
}
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parse(string) {
|
|
|
|
const flowParser = require('flow-parser');
|
|
|
|
return stripLocation(flowParser.parse(string));
|
|
|
|
}
|
|
|
|
|
2016-12-30 21:41:45 +03:00
|
|
|
function prettyprint(src, filename) {
|
2017-01-05 01:23:07 +03:00
|
|
|
return jscodefmt.format(src, { filename, useFlowParser: true, printWidth: 80 });
|
2016-12-23 22:31:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function read(filename) {
|
|
|
|
return fs.readFileSync(filename, 'utf8');
|
|
|
|
}
|