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

Commit ab6cb20

Browse files
authored
fix(*): brute force initial conversion to TS (#25)
1 parent d28abcf commit ab6cb20

37 files changed

+2244
-946
lines changed

.gitignore

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

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ script:
1616
- commitlint-travis
1717
- yarn check-format
1818
- yarn test
19+
- yarn build
1920
after_success:
2021
- npm run travis-deploy-once "npm run semantic-release"
2122
branches:

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
## About
1717

18-
This parser is somewhat generic and robust, and could be used to power any use-case which requires taking TypeScript source code and producing an ESTree-compatiable AST.
18+
This parser is somewhat generic and robust, and could be used to power any use-case which requires taking TypeScript source code and producing an ESTree-compatible AST.
1919

2020
In fact, it is already used within these hyper-popular open-source projects to power their TypeScript support:
2121

package.json

+24-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"name": "typescript-estree",
33
"description": "A parser that converts TypeScript source code into an ESTree compatible form",
44
"homepage": "https://github.com/JamesHenry/typescript-estree",
5-
"main": "parser.js",
5+
"main": "dist/parser.js",
66
"version": "0.0.0-development",
77
"files": [
8-
"lib",
9-
"parser.js"
8+
"dist",
9+
"README.md",
10+
"LICENSE"
1011
],
1112
"engines": {
1213
"node": ">=6.14.0"
@@ -20,6 +21,12 @@
2021
"@commitlint/cli": "^7.1.2",
2122
"@commitlint/config-conventional": "^7.1.2",
2223
"@commitlint/travis-cli": "^7.1.2",
24+
"@types/babel-code-frame": "^6.20.1",
25+
"@types/jest": "^23.3.9",
26+
"@types/lodash.isplainobject": "^4.0.4",
27+
"@types/lodash.unescape": "^4.0.4",
28+
"@types/semver": "^5.5.0",
29+
"@types/shelljs": "^0.8.0",
2330
"babel-code-frame": "6.26.0",
2431
"babylon": "7.0.0-beta.39",
2532
"cz-conventional-changelog": "2.1.0",
@@ -32,6 +39,7 @@
3239
"semantic-release": "^15.9.16",
3340
"shelljs": "0.8.2",
3441
"travis-deploy-once": "^5.0.8",
42+
"ts-jest": "^23.10.4",
3543
"typescript": "~3.1.1"
3644
},
3745
"keywords": [
@@ -44,6 +52,7 @@
4452
"syntax"
4553
],
4654
"scripts": {
55+
"build": "tsc",
4756
"test": "npm run unit-tests && npm run ast-alignment-tests",
4857
"unit-tests": "jest",
4958
"ast-alignment-tests": "jest --config=./tests/ast-alignment/jest.config.js",
@@ -79,8 +88,18 @@
7988
},
8089
"jest": {
8190
"testEnvironment": "node",
82-
"testRegex": "tests/lib/.+\\.js$",
83-
"testPathIgnorePatterns": [],
91+
"transform": {
92+
"^.+\\.tsx?$": "ts-jest"
93+
},
94+
"testRegex": "(/tests/lib/.*)\\.(jsx?|tsx?)$",
95+
"moduleFileExtensions": [
96+
"ts",
97+
"tsx",
98+
"js",
99+
"jsx",
100+
"json",
101+
"node"
102+
],
84103
"collectCoverage": true,
85104
"coverageReporters": [
86105
"text-summary"

lib/ast-converter.js renamed to src/ast-converter.ts

+7-21
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,25 @@
55
* @copyright jQuery Foundation and other contributors, https://jquery.org/
66
* MIT License
77
*/
8-
9-
'use strict';
10-
11-
//------------------------------------------------------------------------------
12-
// Requirements
13-
//------------------------------------------------------------------------------
14-
15-
const convert = require('./convert'),
16-
convertComments = require('./convert-comments').convertComments,
17-
nodeUtils = require('./node-utils');
18-
19-
//------------------------------------------------------------------------------
20-
// Private
21-
//------------------------------------------------------------------------------
8+
import { convert } from './convert';
9+
import { convertComments } from './convert-comments';
10+
import nodeUtils from './node-utils';
11+
import { Extra } from './temp-types-based-on-js-source';
2212

2313
/**
2414
* Extends and formats a given error object
2515
* @param {Object} error the error object
2616
* @returns {Object} converted error object
2717
*/
28-
function convertError(error) {
18+
function convertError(error: any) {
2919
return nodeUtils.createError(
3020
error.file,
3121
error.start,
3222
error.message || error.messageText
3323
);
3424
}
3525

36-
//------------------------------------------------------------------------------
37-
// Public
38-
//------------------------------------------------------------------------------
39-
40-
module.exports = (ast, extra) => {
26+
export default (ast: any, extra: Extra) => {
4127
/**
4228
* The TypeScript compiler produced fundamental parse errors when parsing the
4329
* source.
@@ -49,7 +35,7 @@ module.exports = (ast, extra) => {
4935
/**
5036
* Recursively convert the TypeScript AST into an ESTree-compatible AST
5137
*/
52-
const estree = convert({
38+
const estree: any = convert({
5339
node: ast,
5440
parent: null,
5541
ast,

lib/ast-node-types.js renamed to src/ast-node-types.ts

+1-14
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,7 @@
55
* @copyright jQuery Foundation and other contributors, https://jquery.org/
66
* MIT License
77
*/
8-
9-
'use strict';
10-
11-
//------------------------------------------------------------------------------
12-
// Requirements
13-
//------------------------------------------------------------------------------
14-
15-
// None!
16-
17-
//------------------------------------------------------------------------------
18-
// Public
19-
//------------------------------------------------------------------------------
20-
21-
module.exports = {
8+
export const AST_NODE_TYPES: { [key: string]: string } = {
229
ArrayExpression: 'ArrayExpression',
2310
ArrayPattern: 'ArrayPattern',
2411
ArrowFunctionExpression: 'ArrowFunctionExpression',

lib/convert-comments.js renamed to src/convert-comments.ts

+36-42
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,34 @@
55
* MIT License
66
*/
77

8-
'use strict';
9-
10-
//------------------------------------------------------------------------------
11-
// Requirements
12-
//------------------------------------------------------------------------------
13-
14-
const ts = require('typescript'),
15-
nodeUtils = require('./node-utils');
16-
17-
//------------------------------------------------------------------------------
18-
// Private
19-
//------------------------------------------------------------------------------
8+
import ts from 'typescript';
9+
import nodeUtils from './node-utils';
10+
import {
11+
ESTreeComment,
12+
ESTreeToken,
13+
LineAndColumnData
14+
} from './temp-types-based-on-js-source';
2015

2116
/**
2217
* Converts a TypeScript comment to an Esprima comment.
2318
* @param {boolean} block True if it's a block comment, false if not.
2419
* @param {string} text The text of the comment.
25-
* @param {int} start The index at which the comment starts.
26-
* @param {int} end The index at which the comment ends.
27-
* @param {Location} startLoc The location at which the comment starts.
28-
* @param {Location} endLoc The location at which the comment ends.
20+
* @param {number} start The index at which the comment starts.
21+
* @param {number} end The index at which the comment ends.
22+
* @param {LineAndColumnData} startLoc The location at which the comment starts.
23+
* @param {LineAndColumnData} endLoc The location at which the comment ends.
2924
* @returns {Object} The comment object.
3025
* @private
3126
*/
3227
function convertTypeScriptCommentToEsprimaComment(
33-
block,
34-
text,
35-
start,
36-
end,
37-
startLoc,
38-
endLoc
39-
) {
40-
const comment = {
28+
block: boolean,
29+
text: string,
30+
start: number,
31+
end: number,
32+
startLoc: LineAndColumnData,
33+
endLoc: LineAndColumnData
34+
): ESTreeComment {
35+
const comment: Partial<ESTreeToken> = {
4136
type: block ? 'Block' : 'Line',
4237
value: text
4338
};
@@ -53,18 +48,22 @@ function convertTypeScriptCommentToEsprimaComment(
5348
};
5449
}
5550

56-
return comment;
51+
return comment as ESTreeComment;
5752
}
5853

5954
/**
6055
* Convert comment from TypeScript Triva Scanner.
61-
* @param {Object} triviaScanner TS Scanner
62-
* @param {Object} ast the AST object
56+
* @param {ts.Scanner} triviaScanner TS Scanner
57+
* @param {ts.SourceFile} ast the AST object
6358
* @param {string} code TypeScript code
6459
* @returns {ESTreeComment} the converted ESTreeComment
6560
* @private
6661
*/
67-
function getCommentFromTriviaScanner(triviaScanner, ast, code) {
62+
function getCommentFromTriviaScanner(
63+
triviaScanner: ts.Scanner,
64+
ast: ts.SourceFile,
65+
code: string
66+
): ESTreeComment {
6867
const kind = triviaScanner.getToken();
6968
const isBlock = kind === ts.SyntaxKind.MultiLineCommentTrivia;
7069
const range = {
@@ -91,23 +90,18 @@ function getCommentFromTriviaScanner(triviaScanner, ast, code) {
9190
return esprimaComment;
9291
}
9392

94-
//------------------------------------------------------------------------------
95-
// Public
96-
//------------------------------------------------------------------------------
97-
98-
module.exports = {
99-
convertComments
100-
};
101-
10293
/**
10394
* Convert all comments for the given AST.
104-
* @param {Object} ast the AST object
95+
* @param {ts.SourceFile} ast the AST object
10596
* @param {string} code the TypeScript code
10697
* @returns {ESTreeComment[]} the converted ESTreeComment
10798
* @private
10899
*/
109-
function convertComments(ast, code) {
110-
const comments = [];
100+
export function convertComments(
101+
ast: ts.SourceFile,
102+
code: string
103+
): ESTreeComment[] {
104+
const comments: ESTreeComment[] = [];
111105

112106
/**
113107
* Create a TypeScript Scanner, with skipTrivia set to false so that
@@ -120,7 +114,7 @@ function convertComments(ast, code) {
120114
const start = triviaScanner.getTokenPos();
121115
const end = triviaScanner.getTextPos();
122116

123-
let container = null;
117+
let container: ts.Token<any> | null = null;
124118
switch (kind) {
125119
case ts.SyntaxKind.SingleLineCommentTrivia:
126120
case ts.SyntaxKind.MultiLineCommentTrivia: {
@@ -130,7 +124,7 @@ function convertComments(ast, code) {
130124
break;
131125
}
132126
case ts.SyntaxKind.CloseBraceToken:
133-
container = nodeUtils.getNodeContainer(ast, start, end);
127+
container = nodeUtils.getNodeContainer(ast, start, end) as ts.Node;
134128

135129
if (
136130
container.kind === ts.SyntaxKind.TemplateMiddle ||
@@ -142,7 +136,7 @@ function convertComments(ast, code) {
142136
break;
143137
case ts.SyntaxKind.SlashToken:
144138
case ts.SyntaxKind.SlashEqualsToken:
145-
container = nodeUtils.getNodeContainer(ast, start, end);
139+
container = nodeUtils.getNodeContainer(ast, start, end) as ts.Node;
146140

147141
if (container.kind === ts.SyntaxKind.RegularExpressionLiteral) {
148142
kind = triviaScanner.reScanSlashToken();

0 commit comments

Comments
 (0)