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

Chore: Build out AST comparison tests and categorize issues #358

Merged
merged 1 commit into from
Aug 18, 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
88 changes: 88 additions & 0 deletions tests/ast-alignment/known-issues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* ==================================================
* KNOWN/DIAGNOSED ISSUES
* ==================================================
*/

// const fixturePatternsToTest = [
/**
* "ExperimentalSpreadProperty" in espree/typescript-eslint-parser vs "SpreadElement" in Babylon
* comes up a lot in this section
*/
// "ecma-features/experimentalObjectRestSpread/**/*.src.js", // mixture of babylon parse errors and AST diffs

/* ================================================== */

/**
* Template strings seem to also be affected by the difference in opinion between different parsers in:
* https://github.com/babel/babylon/issues/673
*/
// "comments/no-comment-template.src.js", // Purely AST diffs
// "comments/template-string-block.src.js", // Purely AST diffs
// "ecma-features/templateStrings/**/*.src.js", // mixture of babylon parse errors and AST diffs

/* ================================================== */

/**
* TypeScript, espree and acorn parse this fine - esprima, flow and babylon do not...
*/
// "ecma-features/forOf/for-of-with-function-initializer.src.js", // babylon parse errors

/* ================================================== */

/**
* TypeScript, flow and babylon parse this fine - esprima, espree and acorn do not...
*/
// "ecma-features/modules/invalid-export-default.src.js",, // typescript-eslint-parser parse errors

/* ================================================== */

/**
* Babylon parse error because of more strict spec enforcement than other parsers.
*/

/**
* super() is being used outside of constructor. Other parsers (e.g. espree, acorn) do not error on this.
*/
// "ecma-features/classes/class-one-method-super.src.js", // babylon parse errors

/* ================================================== */

/**
* Expected babylon parse errors - all of these files below produce parse errors in espree
* as well, but the TypeScript compiler is so forgiving during parsing that typescript-eslint-parser
* does not actually error on them and will produce an AST.
*/
// "ecma-features/arrowFunctions/error-dup-params.src.js", // babylon parse errors
// "ecma-features/arrowFunctions/error-dup-params.src.js", // babylon parse errors
// "ecma-features/arrowFunctions/error-strict-dup-params.src.js", // babylon parse errors
// "ecma-features/arrowFunctions/error-strict-octal.src.js", // babylon parse errors
// "ecma-features/arrowFunctions/error-two-lines.src.js", // babylon parse errors
// "ecma-features/classes/invalid-class-declaration.src.js", // babylon parse errors
// "ecma-features/classes/invalid-class-setter-declaration.src.js", // babylon parse errors
// "ecma-features/destructuring/invalid-defaults-object-assign.src.js", // babylon parse errors
// "ecma-features/destructuring-and-spread/error-complex-destructured-spread-first.src.js", // babylon parse errors
// "ecma-features/objectLiteralDuplicateProperties/error-proto-property.src.js", // babylon parse errors
// "ecma-features/objectLiteralDuplicateProperties/error-proto-string-property.src.js", // babylon parse errors
// "ecma-features/modules/invalid-export-named-default.src.js", // babylon parse errors
// "ecma-features/modules/invalid-import-default-module-specifier.src.js", // babylon parse errors
// "ecma-features/modules/invalid-import-module-specifier.src.js", // babylon parse errors
// "ecma-features/newTarget/invalid-new-target.src.js", // babylon parse errors
// "ecma-features/newTarget/invalid-unknown-property.src.js", // babylon parse errors
// "ecma-features/restParams/error-no-default.src.js", // babylon parse errors
// "ecma-features/restParams/error-not-last.src.js", // babylon parse errors
/**
* Deleting local variable in strict mode
*/
// {
// pattern: "ecma-features/modules/error-delete.src.js",
// config: { babylonParserOptions: { sourceType: "module" } }
// },
/**
* 'with' in strict mode
*/
// {
// pattern: "ecma-features/modules/error-strict.src.js",
// config: { babylonParserOptions: { sourceType: "module" } }
// },
// ];
49 changes: 29 additions & 20 deletions tests/ast-alignment/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ function createError(message, line, column) { // eslint-disable-line
return error;
}

function parseWithBabylonPluginTypescript(text) { // eslint-disable-line
function parseWithBabylonPluginTypescript(text, parserOptions) { // eslint-disable-line
parserOptions = parserOptions || {};
const babylon = require("babylon");
return babylon.parse(text, {
return babylon.parse(text, Object.assign({
sourceType: "script",
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
Expand All @@ -35,13 +36,14 @@ function parseWithBabylonPluginTypescript(text) { // eslint-disable-line
"numericSeparator",
"estree"
]
});
}, parserOptions));
}

function parseWithTypeScriptESLintParser(text) { // eslint-disable-line
function parseWithTypeScriptESLintParser(text, parserOptions) { // eslint-disable-line
parserOptions = parserOptions || {};
const parser = require("../../parser");
try {
return parser.parse(text, {
return parser.parse(text, Object.assign({
loc: true,
range: true,
tokens: false,
Expand All @@ -51,7 +53,7 @@ function parseWithTypeScriptESLintParser(text) { // eslint-disable-line
ecmaFeatures: {
jsx: true
}
});
}, parserOptions));
} catch (e) {
throw createError(
e.message,
Expand All @@ -63,21 +65,26 @@ function parseWithTypeScriptESLintParser(text) { // eslint-disable-line

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\"");
}
/**
* Always return a consistent interface, there will be times when we expect both
* parsers to fail to parse the invalid source.
*/
const result = {
parseError: null,
ast: null
};

try {
return parseUtils.normalizeNodeTypes(parseFunction(text));
switch (opts.parser) {
case "typescript-eslint-parser":
result.ast = parseUtils.normalizeNodeTypes(parseWithTypeScriptESLintParser(text, opts.typeScriptESLintParserOptions));
break;
case "babylon-plugin-typescript":
result.ast = parseUtils.normalizeNodeTypes(parseWithBabylonPluginTypescript(text, opts.babylonParserOptions));
break;
default:
throw new Error("Please provide a valid parser: either \"typescript-eslint-parser\" or \"babylon-plugin-typescript\"");
}
} catch (error) {
const loc = error.loc;
if (loc) {
Expand All @@ -86,7 +93,9 @@ module.exports = function parse(text, opts) {
});
error.message += `\n${error.codeFrame}`;
}
throw error;
result.parseError = error;
}

return result;

};
Loading