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

Commit 231f6d8

Browse files
committed
refactor(*): brute force initial conversion to TS
1 parent d28abcf commit 231f6d8

37 files changed

+2134
-943
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

+32-42
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,30 @@
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 { ESTreeComment, ESTreeToken, LineAndColumnData } from './temp-types-based-on-js-source';
2011

2112
/**
2213
* Converts a TypeScript comment to an Esprima comment.
2314
* @param {boolean} block True if it's a block comment, false if not.
2415
* @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.
16+
* @param {number} start The index at which the comment starts.
17+
* @param {number} end The index at which the comment ends.
18+
* @param {LineAndColumnData} startLoc The location at which the comment starts.
19+
* @param {LineAndColumnData} endLoc The location at which the comment ends.
2920
* @returns {Object} The comment object.
3021
* @private
3122
*/
3223
function convertTypeScriptCommentToEsprimaComment(
33-
block,
34-
text,
35-
start,
36-
end,
37-
startLoc,
38-
endLoc
39-
) {
40-
const comment = {
24+
block: boolean,
25+
text: string,
26+
start: number,
27+
end: number,
28+
startLoc: LineAndColumnData,
29+
endLoc: LineAndColumnData
30+
): ESTreeComment {
31+
const comment: Partial<ESTreeToken> = {
4132
type: block ? 'Block' : 'Line',
4233
value: text
4334
};
@@ -53,18 +44,22 @@ function convertTypeScriptCommentToEsprimaComment(
5344
};
5445
}
5546

56-
return comment;
47+
return comment as ESTreeComment;
5748
}
5849

5950
/**
6051
* Convert comment from TypeScript Triva Scanner.
61-
* @param {Object} triviaScanner TS Scanner
62-
* @param {Object} ast the AST object
52+
* @param {ts.Scanner} triviaScanner TS Scanner
53+
* @param {ts.SourceFile} ast the AST object
6354
* @param {string} code TypeScript code
6455
* @returns {ESTreeComment} the converted ESTreeComment
6556
* @private
6657
*/
67-
function getCommentFromTriviaScanner(triviaScanner, ast, code) {
58+
function getCommentFromTriviaScanner(
59+
triviaScanner: ts.Scanner,
60+
ast: ts.SourceFile,
61+
code: string
62+
): ESTreeComment {
6863
const kind = triviaScanner.getToken();
6964
const isBlock = kind === ts.SyntaxKind.MultiLineCommentTrivia;
7065
const range = {
@@ -91,23 +86,18 @@ function getCommentFromTriviaScanner(triviaScanner, ast, code) {
9186
return esprimaComment;
9287
}
9388

94-
//------------------------------------------------------------------------------
95-
// Public
96-
//------------------------------------------------------------------------------
97-
98-
module.exports = {
99-
convertComments
100-
};
101-
10289
/**
10390
* Convert all comments for the given AST.
104-
* @param {Object} ast the AST object
91+
* @param {ts.SourceFile} ast the AST object
10592
* @param {string} code the TypeScript code
10693
* @returns {ESTreeComment[]} the converted ESTreeComment
10794
* @private
10895
*/
109-
function convertComments(ast, code) {
110-
const comments = [];
96+
export function convertComments(
97+
ast: ts.SourceFile,
98+
code: string
99+
): ESTreeComment[] {
100+
const comments: ESTreeComment[] = [];
111101

112102
/**
113103
* Create a TypeScript Scanner, with skipTrivia set to false so that
@@ -120,7 +110,7 @@ function convertComments(ast, code) {
120110
const start = triviaScanner.getTokenPos();
121111
const end = triviaScanner.getTextPos();
122112

123-
let container = null;
113+
let container: ts.Token<any> | null = null;
124114
switch (kind) {
125115
case ts.SyntaxKind.SingleLineCommentTrivia:
126116
case ts.SyntaxKind.MultiLineCommentTrivia: {
@@ -130,7 +120,7 @@ function convertComments(ast, code) {
130120
break;
131121
}
132122
case ts.SyntaxKind.CloseBraceToken:
133-
container = nodeUtils.getNodeContainer(ast, start, end);
123+
container = nodeUtils.getNodeContainer(ast, start, end) as ts.Node;
134124

135125
if (
136126
container.kind === ts.SyntaxKind.TemplateMiddle ||
@@ -142,7 +132,7 @@ function convertComments(ast, code) {
142132
break;
143133
case ts.SyntaxKind.SlashToken:
144134
case ts.SyntaxKind.SlashEqualsToken:
145-
container = nodeUtils.getNodeContainer(ast, start, end);
135+
container = nodeUtils.getNodeContainer(ast, start, end) as ts.Node;
146136

147137
if (container.kind === ts.SyntaxKind.RegularExpressionLiteral) {
148138
kind = triviaScanner.reScanSlashToken();

0 commit comments

Comments
 (0)