Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 773723b

Browse files
authored
refactor(ts-estree): add types to converter (typescript-eslint#156)
1 parent bf13399 commit 773723b

20 files changed

+3568
-3358
lines changed

packages/typescript-estree/src/ast-converter.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* @copyright jQuery Foundation and other contributors, https://jquery.org/
66
* MIT License
77
*/
8-
import convert, { getASTMaps, resetASTMaps, convertError } from './convert';
8+
import { convertError, Converter } from './convert';
99
import { convertComments } from './convert-comments';
1010
import { convertTokens } from './node-utils';
1111
import ts from 'typescript';
12-
import { Extra } from './temp-types-based-on-js-source';
12+
import { Extra } from './parser-options';
1313

1414
export default function astConverter(
1515
ast: ts.SourceFile,
@@ -27,17 +27,14 @@ export default function astConverter(
2727
/**
2828
* Recursively convert the TypeScript AST into an ESTree-compatible AST
2929
*/
30-
const estree: any = convert({
31-
node: ast,
32-
parent: null,
33-
ast,
34-
additionalOptions: {
35-
errorOnUnknownASTType: extra.errorOnUnknownASTType || false,
36-
useJSXTextNode: extra.useJSXTextNode || false,
37-
shouldProvideParserServices
38-
}
30+
const instance = new Converter(ast, {
31+
errorOnUnknownASTType: extra.errorOnUnknownASTType || false,
32+
useJSXTextNode: extra.useJSXTextNode || false,
33+
shouldProvideParserServices
3934
});
4035

36+
const estree = instance.convertProgram();
37+
4138
/**
4239
* Optionally convert and include all tokens in the AST
4340
*/
@@ -52,11 +49,9 @@ export default function astConverter(
5249
estree.comments = convertComments(ast, extra.code);
5350
}
5451

55-
let astMaps = undefined;
56-
if (shouldProvideParserServices) {
57-
astMaps = getASTMaps();
58-
resetASTMaps();
59-
}
52+
const astMaps = shouldProvideParserServices
53+
? instance.getASTMaps()
54+
: undefined;
6055

6156
return { estree, astMaps };
6257
}

packages/typescript-estree/src/convert-comments.ts

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,28 @@
77

88
import ts from 'typescript';
99
import { getLocFor, getNodeContainer } from './node-utils';
10-
import {
11-
ESTreeComment,
12-
LineAndColumnData
13-
} from './temp-types-based-on-js-source';
10+
import * as es from './typedefs';
1411

1512
/**
1613
* Converts a TypeScript comment to an Esprima comment.
17-
* @param {boolean} block True if it's a block comment, false if not.
18-
* @param {string} text The text of the comment.
19-
* @param {number} start The index at which the comment starts.
20-
* @param {number} end The index at which the comment ends.
21-
* @param {LineAndColumnData} startLoc The location at which the comment starts.
22-
* @param {LineAndColumnData} endLoc The location at which the comment ends.
23-
* @returns {Object} The comment object.
24-
* @private
14+
* @param block True if it's a block comment, false if not.
15+
* @param text The text of the comment.
16+
* @param start The index at which the comment starts.
17+
* @param end The index at which the comment ends.
18+
* @param startLoc The location at which the comment starts.
19+
* @param endLoc The location at which the comment ends.
20+
* @returns The comment object.
21+
* @internal
2522
*/
2623
function convertTypeScriptCommentToEsprimaComment(
2724
block: boolean,
2825
text: string,
2926
start: number,
3027
end: number,
31-
startLoc: LineAndColumnData,
32-
endLoc: LineAndColumnData
33-
): ESTreeComment {
34-
const comment: ESTreeComment = {
28+
startLoc: es.LineAndColumnData,
29+
endLoc: es.LineAndColumnData
30+
): es.Comment {
31+
const comment: es.OptionalRangeAndLoc<es.Comment> = {
3532
type: block ? 'Block' : 'Line',
3633
value: text
3734
};
@@ -47,22 +44,22 @@ function convertTypeScriptCommentToEsprimaComment(
4744
};
4845
}
4946

50-
return comment;
47+
return comment as es.Comment;
5148
}
5249

5350
/**
5451
* Convert comment from TypeScript Triva Scanner.
55-
* @param {ts.Scanner} triviaScanner TS Scanner
56-
* @param {ts.SourceFile} ast the AST object
57-
* @param {string} code TypeScript code
58-
* @returns {ESTreeComment} the converted ESTreeComment
52+
* @param triviaScanner TS Scanner
53+
* @param ast the AST object
54+
* @param code TypeScript code
55+
* @returns the converted Comment
5956
* @private
6057
*/
6158
function getCommentFromTriviaScanner(
6259
triviaScanner: ts.Scanner,
6360
ast: ts.SourceFile,
6461
code: string
65-
): ESTreeComment {
62+
): es.Comment {
6663
const kind = triviaScanner.getToken();
6764
const isBlock = kind === ts.SyntaxKind.MultiLineCommentTrivia;
6865
const range = {
@@ -77,30 +74,28 @@ function getCommentFromTriviaScanner(
7774
: comment.replace(/^\/\//, '');
7875
const loc = getLocFor(range.pos, range.end, ast);
7976

80-
const esprimaComment = convertTypeScriptCommentToEsprimaComment(
77+
return convertTypeScriptCommentToEsprimaComment(
8178
isBlock,
8279
text,
8380
range.pos,
8481
range.end,
8582
loc.start,
8683
loc.end
8784
);
88-
89-
return esprimaComment;
9085
}
9186

9287
/**
9388
* Convert all comments for the given AST.
94-
* @param {ts.SourceFile} ast the AST object
95-
* @param {string} code the TypeScript code
96-
* @returns {ESTreeComment[]} the converted ESTreeComment
89+
* @param ast the AST object
90+
* @param code the TypeScript code
91+
* @returns the converted ESTreeComment
9792
* @private
9893
*/
9994
export function convertComments(
10095
ast: ts.SourceFile,
10196
code: string
102-
): ESTreeComment[] {
103-
const comments: ESTreeComment[] = [];
97+
): es.Comment[] {
98+
const comments: es.Comment[] = [];
10499

105100
/**
106101
* Create a TypeScript Scanner, with skipTrivia set to false so that

0 commit comments

Comments
 (0)