2016-12-23 22:31:38 +03:00
|
|
|
const fs = require('fs');
|
|
|
|
const child_process = require('child_process');
|
2016-12-24 05:12:18 +03:00
|
|
|
const jscodefmt = require("../");
|
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;
|
|
|
|
|
2016-12-24 05:12:18 +03:00
|
|
|
const RUN_AST_TESTS = /* true */ false;
|
2016-12-23 22:31:38 +03:00
|
|
|
|
|
|
|
if (!RUN_AST_TESTS) {
|
|
|
|
const source = read(path);
|
|
|
|
const output = prettyprint(path);
|
|
|
|
test(filename, () => {
|
|
|
|
expect(source + '~'.repeat(80) + '\n' + output).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RUN_AST_TESTS) {
|
|
|
|
test(path + ' parse', () => {
|
|
|
|
const file = read(dirname + '/' + filename);
|
|
|
|
const ast = parse(file);
|
|
|
|
const ppfile = pp(file);
|
|
|
|
const ppast = parse(ppfile);
|
|
|
|
expect(ast).toEqual(ppast);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
function prettyprint(path) {
|
2016-12-24 05:12:18 +03:00
|
|
|
const src = fs.readFileSync(path);
|
|
|
|
return jscodefmt.format(src);
|
2016-12-23 22:31:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function pp(string) {
|
|
|
|
const tmp = 'tmp' + Math.random() + '.js';
|
|
|
|
fs.writeFileSync(tmp, string);
|
|
|
|
const result = prettyprint(tmp);
|
|
|
|
fs.unlinkSync(tmp);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function read(filename) {
|
|
|
|
return fs.readFileSync(filename, 'utf8');
|
|
|
|
}
|