Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Chore: AST alignment testing against Babylon #342

Merged
merged 2 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
},
"license": "BSD-2-Clause",
"devDependencies": {
"babel-code-frame": "^6.22.0",
"babylon": "^7.0.0-beta.16",
"eslint": "3.19.0",
"eslint-config-eslint": "4.0.0",
"eslint-plugin-node": "4.2.2",
"eslint-release": "0.10.3",
"glob": "^7.1.2",
"jest": "20.0.4",
"lodash.isplainobject": "^4.0.6",
"npm-license": "0.3.3",
"shelljs": "0.7.7",
"shelljs-nodecli": "0.1.1",
Expand All @@ -40,6 +44,7 @@
"scripts": {
"test": "node Makefile.js test",
"jest": "jest",
"ast-alignment-tests": "jest --config={} ./tests/ast-alignment/spec.js",
"lint": "node Makefile.js lint",
"release": "eslint-release",
"ci-release": "eslint-ci-release",
Expand Down
2 changes: 2 additions & 0 deletions tests/ast-alignment/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env:
jest: true
90 changes: 90 additions & 0 deletions tests/ast-alignment/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"use strict";

const codeFrame = require("babel-code-frame");
const parseUtils = require("./utils");

function createError(message, line, column) { // eslint-disable-line
// Construct an error similar to the ones thrown by Babylon.
const error = new SyntaxError(`${message} (${line}:${column})`);
error.loc = {
line,
column
};
return error;
}

function parseWithBabylonPluginTypescript(text) { // eslint-disable-line
const babylon = require("babylon");
return babylon.parse(text, {
sourceType: "script",
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
ranges: true,
plugins: [
"jsx",
"typescript",
"doExpressions",
"objectRestSpread",
"decorators",
"classProperties",
"exportExtensions",
"asyncGenerators",
"functionBind",
"functionSent",
"dynamicImport",
"numericSeparator",
"estree"
]
});
}

function parseWithTypeScriptESLintParser(text) { // eslint-disable-line
const parser = require("../../parser");
try {
return parser.parse(text, {
loc: true,
range: true,
tokens: false,
comment: false,
ecmaFeatures: {
jsx: true
}
});
} catch (e) {
throw createError(
e.message,
e.lineNumber,
e.column
);
}
}

module.exports = function parse(text, opts) {

let parseFunction;

switch (opts.parser) {
case "typescript-eslint-parser":
parseFunction = parseWithTypeScriptESLintParser;
break;
case "babylon-plugin-typescript":
parseFunction = parseWithBabylonPluginTypescript;
break;
default:
throw new Error("Please provide a valid parser: either \"typescript-eslint-parser\" or \"babylon-plugin-typescript\"");
}

try {
return parseUtils.normalizeNodeTypes(parseFunction(text));
} catch (error) {
const loc = error.loc;
if (loc) {
error.codeFrame = codeFrame(text, loc.line, loc.column + 1, {
highlightCode: true
});
error.message += `\n${error.codeFrame}`;
}
throw error;
}

};
106 changes: 106 additions & 0 deletions tests/ast-alignment/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"use strict";

const fs = require("fs");
const path = require("path");
const glob = require("glob");
const parse = require("./parse");
const parseUtils = require("./utils");

const fixturesDirPath = path.join(__dirname, "../fixtures");
const fixturePatternsToTest = [
"basics/instanceof.src.js",
"basics/update-expression.src.js",
"basics/new-without-parens.src.js",
"ecma-features/arrowFunctions/**/as*.src.js"
];

const fixturesToTest = [];

fixturePatternsToTest.forEach(fixturePattern => {
const matchingFixtures = glob.sync(`${fixturesDirPath}/${fixturePattern}`, {});
matchingFixtures.forEach(filename => fixturesToTest.push(filename));
});

/**
* - Babylon wraps the "Program" node in an extra "File" node, normalize this for simplicity for now...
* - Remove "start" and "end" values from Babylon nodes to reduce unimportant noise in diffs ("loc" data will still be in
* each final AST and compared).
*
* @param {Object} ast raw babylon AST
* @returns {Object} processed babylon AST
*/
function preprocessBabylonAST(ast) {
return parseUtils.omitDeep(ast.program, [
{
key: "start",
predicate(val) {
// only remove the "start" number (not the "start" object within loc)
return typeof val === "number";
}
},
{
key: "end",
predicate(val) {
// only remove the "end" number (not the "end" object within loc)
return typeof val === "number";
}
},
{
key: "identifierName",
predicate() {
return true;
}
},
{
key: "extra",
predicate() {
return true;
}
},
{
key: "directives",
predicate() {
return true;
}
},
{
key: "innerComments",
predicate() {
return true;
}
},
{
key: "leadingComments",
predicate() {
return true;
}
}
]);
}

fixturesToTest.forEach(filename => {

const source = fs.readFileSync(filename, "utf8").replace(/\r\n/g, "\n");

/**
* Parse with typescript-eslint-parser
*/
const typeScriptESLintParserAST = parse(source, {
parser: "typescript-eslint-parser"
});

/**
* Parse the source with babylon typescript-plugin, and perform some extra formatting steps
*/
const babylonTypeScriptPluginAST = preprocessBabylonAST(parse(source, {
parser: "babylon-plugin-typescript"
}));

/**
* Assert the two ASTs match
*/
test(`${filename}`, () => {
expect(babylonTypeScriptPluginAST).toEqual(typeScriptESLintParserAST);
});

});
66 changes: 66 additions & 0 deletions tests/ast-alignment/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"use strict";

const isPlainObject = require("lodash.isplainobject");

/**
* By default, pretty-format (within Jest matchers) retains the names/types of nodes from the babylon AST,
* quick and dirty way to avoid that is to JSON.stringify and then JSON.parser the
* ASTs before comparing them with pretty-format
*
* @param {Object} ast raw AST
* @returns {Object} normalized AST
*/
function normalizeNodeTypes(ast) {
return JSON.parse(JSON.stringify(ast));
}

/**
* Removes the given keys from the given AST object recursively
* @param {Object} obj A JavaScript object to remove keys from
* @param {Object[]} keysToOmit Names and predicate functions use to determine what keys to omit from the final object
* @returns {Object} formatted object
*/
function omitDeep(obj, keysToOmit) {
keysToOmit = keysToOmit || [];
function shouldOmit(keyName, val) { // eslint-disable-line
if (!keysToOmit || !keysToOmit.length) {
return false;
}
for (const keyConfig of keysToOmit) {
if (keyConfig.key !== keyName) {
continue;
}
return keyConfig.predicate(val);
}
return false;
}
for (const key in obj) {
if (!obj.hasOwnProperty(key)) {
continue;
}
const val = obj[key];
if (isPlainObject(val)) {
if (shouldOmit(key, val)) {
delete obj[key];
break;
}
omitDeep(val, keysToOmit);
} else if (Array.isArray(val)) {
if (shouldOmit(key, val)) {
delete obj[key];
break;
}
for (const i of val) {
omitDeep(i, keysToOmit);
}
} else if (shouldOmit(key, val)) {
delete obj[key];
}
}
return obj;
}

module.exports = {
normalizeNodeTypes,
omitDeep
};