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

Commit f6e56b3

Browse files
authored
Chore: Build out AST comparison tests and categorize issues (#358)
1 parent ab4e05e commit f6e56b3

File tree

9 files changed

+725
-785
lines changed

9 files changed

+725
-785
lines changed

tests/ast-alignment/known-issues.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* ==================================================
3+
* KNOWN/DIAGNOSED ISSUES
4+
* ==================================================
5+
*/
6+
7+
// const fixturePatternsToTest = [
8+
/**
9+
* "ExperimentalSpreadProperty" in espree/typescript-eslint-parser vs "SpreadElement" in Babylon
10+
* comes up a lot in this section
11+
*/
12+
// "ecma-features/experimentalObjectRestSpread/**/*.src.js", // mixture of babylon parse errors and AST diffs
13+
14+
/* ================================================== */
15+
16+
/**
17+
* Template strings seem to also be affected by the difference in opinion between different parsers in:
18+
* https://github.com/babel/babylon/issues/673
19+
*/
20+
// "comments/no-comment-template.src.js", // Purely AST diffs
21+
// "comments/template-string-block.src.js", // Purely AST diffs
22+
// "ecma-features/templateStrings/**/*.src.js", // mixture of babylon parse errors and AST diffs
23+
24+
/* ================================================== */
25+
26+
/**
27+
* TypeScript, espree and acorn parse this fine - esprima, flow and babylon do not...
28+
*/
29+
// "ecma-features/forOf/for-of-with-function-initializer.src.js", // babylon parse errors
30+
31+
/* ================================================== */
32+
33+
/**
34+
* TypeScript, flow and babylon parse this fine - esprima, espree and acorn do not...
35+
*/
36+
// "ecma-features/modules/invalid-export-default.src.js",, // typescript-eslint-parser parse errors
37+
38+
/* ================================================== */
39+
40+
/**
41+
* Babylon parse error because of more strict spec enforcement than other parsers.
42+
*/
43+
44+
/**
45+
* super() is being used outside of constructor. Other parsers (e.g. espree, acorn) do not error on this.
46+
*/
47+
// "ecma-features/classes/class-one-method-super.src.js", // babylon parse errors
48+
49+
/* ================================================== */
50+
51+
/**
52+
* Expected babylon parse errors - all of these files below produce parse errors in espree
53+
* as well, but the TypeScript compiler is so forgiving during parsing that typescript-eslint-parser
54+
* does not actually error on them and will produce an AST.
55+
*/
56+
// "ecma-features/arrowFunctions/error-dup-params.src.js", // babylon parse errors
57+
// "ecma-features/arrowFunctions/error-dup-params.src.js", // babylon parse errors
58+
// "ecma-features/arrowFunctions/error-strict-dup-params.src.js", // babylon parse errors
59+
// "ecma-features/arrowFunctions/error-strict-octal.src.js", // babylon parse errors
60+
// "ecma-features/arrowFunctions/error-two-lines.src.js", // babylon parse errors
61+
// "ecma-features/classes/invalid-class-declaration.src.js", // babylon parse errors
62+
// "ecma-features/classes/invalid-class-setter-declaration.src.js", // babylon parse errors
63+
// "ecma-features/destructuring/invalid-defaults-object-assign.src.js", // babylon parse errors
64+
// "ecma-features/destructuring-and-spread/error-complex-destructured-spread-first.src.js", // babylon parse errors
65+
// "ecma-features/objectLiteralDuplicateProperties/error-proto-property.src.js", // babylon parse errors
66+
// "ecma-features/objectLiteralDuplicateProperties/error-proto-string-property.src.js", // babylon parse errors
67+
// "ecma-features/modules/invalid-export-named-default.src.js", // babylon parse errors
68+
// "ecma-features/modules/invalid-import-default-module-specifier.src.js", // babylon parse errors
69+
// "ecma-features/modules/invalid-import-module-specifier.src.js", // babylon parse errors
70+
// "ecma-features/newTarget/invalid-new-target.src.js", // babylon parse errors
71+
// "ecma-features/newTarget/invalid-unknown-property.src.js", // babylon parse errors
72+
// "ecma-features/restParams/error-no-default.src.js", // babylon parse errors
73+
// "ecma-features/restParams/error-not-last.src.js", // babylon parse errors
74+
/**
75+
* Deleting local variable in strict mode
76+
*/
77+
// {
78+
// pattern: "ecma-features/modules/error-delete.src.js",
79+
// config: { babylonParserOptions: { sourceType: "module" } }
80+
// },
81+
/**
82+
* 'with' in strict mode
83+
*/
84+
// {
85+
// pattern: "ecma-features/modules/error-strict.src.js",
86+
// config: { babylonParserOptions: { sourceType: "module" } }
87+
// },
88+
// ];

tests/ast-alignment/parse.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ function createError(message, line, column) { // eslint-disable-line
1313
return error;
1414
}
1515

16-
function parseWithBabylonPluginTypescript(text) { // eslint-disable-line
16+
function parseWithBabylonPluginTypescript(text, parserOptions) { // eslint-disable-line
17+
parserOptions = parserOptions || {};
1718
const babylon = require("babylon");
18-
return babylon.parse(text, {
19+
return babylon.parse(text, Object.assign({
1920
sourceType: "script",
2021
allowImportExportEverywhere: true,
2122
allowReturnOutsideFunction: true,
@@ -35,13 +36,14 @@ function parseWithBabylonPluginTypescript(text) { // eslint-disable-line
3536
"numericSeparator",
3637
"estree"
3738
]
38-
});
39+
}, parserOptions));
3940
}
4041

41-
function parseWithTypeScriptESLintParser(text) { // eslint-disable-line
42+
function parseWithTypeScriptESLintParser(text, parserOptions) { // eslint-disable-line
43+
parserOptions = parserOptions || {};
4244
const parser = require("../../parser");
4345
try {
44-
return parser.parse(text, {
46+
return parser.parse(text, Object.assign({
4547
loc: true,
4648
range: true,
4749
tokens: false,
@@ -51,7 +53,7 @@ function parseWithTypeScriptESLintParser(text) { // eslint-disable-line
5153
ecmaFeatures: {
5254
jsx: true
5355
}
54-
});
56+
}, parserOptions));
5557
} catch (e) {
5658
throw createError(
5759
e.message,
@@ -63,21 +65,26 @@ function parseWithTypeScriptESLintParser(text) { // eslint-disable-line
6365

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

66-
let parseFunction;
67-
68-
switch (opts.parser) {
69-
case "typescript-eslint-parser":
70-
parseFunction = parseWithTypeScriptESLintParser;
71-
break;
72-
case "babylon-plugin-typescript":
73-
parseFunction = parseWithBabylonPluginTypescript;
74-
break;
75-
default:
76-
throw new Error("Please provide a valid parser: either \"typescript-eslint-parser\" or \"babylon-plugin-typescript\"");
77-
}
68+
/**
69+
* Always return a consistent interface, there will be times when we expect both
70+
* parsers to fail to parse the invalid source.
71+
*/
72+
const result = {
73+
parseError: null,
74+
ast: null
75+
};
7876

7977
try {
80-
return parseUtils.normalizeNodeTypes(parseFunction(text));
78+
switch (opts.parser) {
79+
case "typescript-eslint-parser":
80+
result.ast = parseUtils.normalizeNodeTypes(parseWithTypeScriptESLintParser(text, opts.typeScriptESLintParserOptions));
81+
break;
82+
case "babylon-plugin-typescript":
83+
result.ast = parseUtils.normalizeNodeTypes(parseWithBabylonPluginTypescript(text, opts.babylonParserOptions));
84+
break;
85+
default:
86+
throw new Error("Please provide a valid parser: either \"typescript-eslint-parser\" or \"babylon-plugin-typescript\"");
87+
}
8188
} catch (error) {
8289
const loc = error.loc;
8390
if (loc) {
@@ -86,7 +93,9 @@ module.exports = function parse(text, opts) {
8693
});
8794
error.message += `\n${error.codeFrame}`;
8895
}
89-
throw error;
96+
result.parseError = error;
9097
}
9198

99+
return result;
100+
92101
};

0 commit comments

Comments
 (0)