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

Commit f083824

Browse files
armano2JamesHenry
authored andcommitted
test: replace babylon(beta) with babel/parser (#40)
1 parent 9b7e13c commit f083824

File tree

6 files changed

+41
-34
lines changed

6 files changed

+41
-34
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ npm-debug.log
55
_test.js
66
.DS_Store
77
.vscode
8-
dist
8+
.idea
9+
dist

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"license": "BSD-2-Clause",
2020
"devDependencies": {
21+
"@babel/code-frame": "7.0.0",
2122
"@babel/parser": "7.1.6",
2223
"@commitlint/cli": "^7.1.2",
2324
"@commitlint/config-conventional": "^7.1.2",
@@ -29,7 +30,6 @@
2930
"@types/node": "^10.12.2",
3031
"@types/semver": "^5.5.0",
3132
"@types/shelljs": "^0.8.0",
32-
"babel-code-frame": "6.26.0",
3333
"cz-conventional-changelog": "2.1.0",
3434
"glob": "7.1.2",
3535
"husky": "0.14.3",

tests/ast-alignment/fixtures-to-test.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import glob from 'glob';
22
import path from 'path';
33
import jsxKnownIssues from '../jsx-known-issues';
4+
import { ParserOptions as BabelParserOptions } from '@babel/parser';
5+
import { ParserOptions } from '../../src/temp-types-based-on-js-source';
46

57
interface Fixture {
68
filename: string;
@@ -10,8 +12,8 @@ interface Fixture {
1012
interface FixturePatternConfig {
1113
pattern: string;
1214
config?: {
13-
babylonParserOptions?: any;
14-
typeScriptESTreeOptions?: any;
15+
babelParserOptions?: BabelParserOptions;
16+
typeScriptESTreeOptions?: ParserOptions;
1517
};
1618
}
1719

@@ -77,16 +79,17 @@ function createFixturePatternConfigFor(
7779
config.ignore,
7880
config.parseWithSourceTypeModule
7981
);
80-
fixturesRequiringSourceTypeModule = ([] as FixturePatternConfig[]).concat(
81-
fixturesRequiringSourceTypeModule,
82-
config.parseWithSourceTypeModule.map(fixture => ({
82+
for (const fixture of config.parseWithSourceTypeModule) {
83+
fixturesRequiringSourceTypeModule.push({
8384
// It needs to be the full path from within fixtures/ for the pattern
84-
pattern: `${fixturesSubPath}/${fixture}.src.${
85-
(config as CreateFixturePatternConfig).fileType
86-
}`,
87-
config: { babylonParserOptions: { sourceType: 'module' } }
88-
}))
89-
);
85+
pattern: `${fixturesSubPath}/${fixture}.src.${config.fileType}`,
86+
config: {
87+
babelParserOptions: {
88+
sourceType: 'module'
89+
}
90+
}
91+
});
92+
}
9093
}
9194
return {
9295
pattern: `${fixturesSubPath}/!(${config.ignore.join('|')}).src.${
@@ -370,8 +373,6 @@ let fixturePatternConfigsToTest = [
370373
'interface-with-all-property-types', // babylon parse errors
371374
'interface-with-construct-signature-with-parameter-accessibility', // babylon parse errors
372375
'class-with-implements-and-extends', // babylon parse errors
373-
'var-with-definite-assignment', // babylon parse errors
374-
'class-with-definite-assignment', // babylon parse errors
375376
/**
376377
* typescript-estree erroring, but babylon not.
377378
*/

tests/ast-alignment/parse.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import codeFrame from 'babel-code-frame';
22
import * as parser from '../../src/parser';
3-
import { ParserOptions } from '../../src/temp-types-based-on-js-source';
43
import * as parseUtils from './utils';
4+
import { ParserOptions as BabelParserOptions } from '@babel/parser';
5+
import { ParserOptions } from '../../src/temp-types-based-on-js-source';
56

67
function createError(message: string, line: number, column: number) {
78
// Construct an error similar to the ones thrown by Babylon.
@@ -13,7 +14,10 @@ function createError(message: string, line: number, column: number) {
1314
return error;
1415
}
1516

16-
function parseWithBabylonPluginTypescript(text: string, parserOptions?: any) {
17+
function parseWithBabelParser(
18+
text: string,
19+
parserOptions?: BabelParserOptions
20+
) {
1721
parserOptions = parserOptions || {};
1822
const babylon = require('@babel/parser');
1923
return babylon.parse(
@@ -67,7 +71,7 @@ function parseWithTypeScriptESTree(
6771
interface ASTComparisonParseOptions {
6872
parser: string;
6973
typeScriptESTreeOptions?: ParserOptions;
70-
babylonParserOptions?: any;
74+
babelParserOptions?: BabelParserOptions;
7175
}
7276

7377
export function parse(text: string, opts: ASTComparisonParseOptions) {
@@ -87,14 +91,14 @@ export function parse(text: string, opts: ASTComparisonParseOptions) {
8791
parseWithTypeScriptESTree(text, opts.typeScriptESTreeOptions)
8892
);
8993
break;
90-
case 'babylon-plugin-typescript':
94+
case '@babel/parser':
9195
result.ast = parseUtils.normalizeNodeTypes(
92-
parseWithBabylonPluginTypescript(text, opts.babylonParserOptions)
96+
parseWithBabelParser(text, opts.babelParserOptions)
9397
);
9498
break;
9599
default:
96100
throw new Error(
97-
'Please provide a valid parser: either "typescript-estree" or "babylon-plugin-typescript"'
101+
'Please provide a valid parser: either "typescript-estree" or "@babel/parser"'
98102
);
99103
}
100104
} catch (error) {

tests/ast-alignment/spec.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ fixturesToTest.forEach(fixture => {
2121
/**
2222
* Parse the source with babylon typescript-plugin
2323
*/
24-
const babylonTypeScriptPluginResult = parse(source, {
25-
parser: 'babylon-plugin-typescript',
26-
babylonParserOptions:
27-
fixture.config && fixture.config.babylonParserOptions
28-
? fixture.config.babylonParserOptions
24+
const babelParserResult = parse(source, {
25+
parser: '@babel/parser',
26+
babelParserOptions:
27+
fixture.config && fixture.config.babelParserOptions
28+
? fixture.config.babelParserOptions
2929
: null
3030
});
3131

3232
/**
3333
* If babylon fails to parse the source, ensure that typescript-estree has the same fundamental issue
3434
*/
35-
if (babylonTypeScriptPluginResult.parseError) {
35+
if (babelParserResult.parseError) {
3636
/**
3737
* FAIL: babylon errored but typescript-estree did not
3838
*/
3939
if (!typeScriptESTreeResult.parseError) {
4040
it(`TEST FAIL [BABYLON ERRORED, BUT TSEP DID NOT] - ${filename}`, () => {
4141
expect(typeScriptESTreeResult.parseError).toEqual(
42-
babylonTypeScriptPluginResult.parseError
42+
babelParserResult.parseError
4343
);
4444
});
4545
return;
@@ -48,7 +48,7 @@ fixturesToTest.forEach(fixture => {
4848
* Both parsers errored - this is OK as long as the errors are of the same "type"
4949
*/
5050
it(`[Both parsers error as expected] - ${filename}`, () => {
51-
expect(babylonTypeScriptPluginResult.parseError.name).toEqual(
51+
expect(babelParserResult.parseError.name).toEqual(
5252
typeScriptESTreeResult.parseError.name
5353
);
5454
});
@@ -60,7 +60,7 @@ fixturesToTest.forEach(fixture => {
6060
*/
6161
if (typeScriptESTreeResult.parseError) {
6262
it(`TEST FAIL [TSEP ERRORED, BUT BABYLON DID NOT] - ${filename}`, () => {
63-
expect(babylonTypeScriptPluginResult.parseError).toEqual(
63+
expect(babelParserResult.parseError).toEqual(
6464
typeScriptESTreeResult.parseError
6565
);
6666
});
@@ -71,14 +71,14 @@ fixturesToTest.forEach(fixture => {
7171
* No errors, assert the two ASTs match
7272
*/
7373
it(`${filename}`, () => {
74-
expect(babylonTypeScriptPluginResult.ast).toBeTruthy();
74+
expect(babelParserResult.ast).toBeTruthy();
7575
expect(typeScriptESTreeResult.ast).toBeTruthy();
7676
/**
7777
* Perform some extra formatting steps on the babylon AST before comparing
7878
*/
7979
expect(
8080
parseUtils.removeLocationDataFromProgramNode(
81-
parseUtils.preprocessBabylonAST(babylonTypeScriptPluginResult.ast)
81+
parseUtils.preprocessBabylonAST(babelParserResult.ast)
8282
)
8383
).toEqual(
8484
parseUtils.removeLocationDataFromProgramNode(typeScriptESTreeResult.ast)

yarn.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
# yarn lockfile v1
33

44

5-
"@babel/code-frame@^7.0.0":
5+
"@babel/code-frame@7.0.0", "@babel/code-frame@^7.0.0":
66
version "7.0.0"
77
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0.tgz#06e2ab19bdb535385559aabb5ba59729482800f8"
8+
integrity sha512-OfC2uemaknXr87bdLUkWog7nYuliM9Ij5HUcajsVcMCpQrcLmtxRbVFTIqmcSkSeYRBFBRxs2FiUqFJDLdiebA==
89
dependencies:
910
"@babel/highlight" "^7.0.0"
1011

@@ -1125,7 +1126,7 @@ aws4@^1.6.0, aws4@^1.8.0:
11251126
version "1.8.0"
11261127
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"
11271128

1128-
babel-code-frame@6.26.0, babel-code-frame@^6.26.0:
1129+
babel-code-frame@^6.26.0:
11291130
version "6.26.0"
11301131
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
11311132
dependencies:

0 commit comments

Comments
 (0)