5
5
* MIT License
6
6
*/
7
7
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' ;
20
11
21
12
/**
22
13
* Converts a TypeScript comment to an Esprima comment.
23
14
* @param {boolean } block True if it's a block comment, false if not.
24
15
* @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.
29
20
* @returns {Object } The comment object.
30
21
* @private
31
22
*/
32
23
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 > = {
41
32
type : block ? 'Block' : 'Line' ,
42
33
value : text
43
34
} ;
@@ -53,18 +44,22 @@ function convertTypeScriptCommentToEsprimaComment(
53
44
} ;
54
45
}
55
46
56
- return comment ;
47
+ return comment as ESTreeComment ;
57
48
}
58
49
59
50
/**
60
51
* 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
63
54
* @param {string } code TypeScript code
64
55
* @returns {ESTreeComment } the converted ESTreeComment
65
56
* @private
66
57
*/
67
- function getCommentFromTriviaScanner ( triviaScanner , ast , code ) {
58
+ function getCommentFromTriviaScanner (
59
+ triviaScanner : ts . Scanner ,
60
+ ast : ts . SourceFile ,
61
+ code : string
62
+ ) : ESTreeComment {
68
63
const kind = triviaScanner . getToken ( ) ;
69
64
const isBlock = kind === ts . SyntaxKind . MultiLineCommentTrivia ;
70
65
const range = {
@@ -91,23 +86,18 @@ function getCommentFromTriviaScanner(triviaScanner, ast, code) {
91
86
return esprimaComment ;
92
87
}
93
88
94
- //------------------------------------------------------------------------------
95
- // Public
96
- //------------------------------------------------------------------------------
97
-
98
- module . exports = {
99
- convertComments
100
- } ;
101
-
102
89
/**
103
90
* Convert all comments for the given AST.
104
- * @param {Object } ast the AST object
91
+ * @param {ts.SourceFile } ast the AST object
105
92
* @param {string } code the TypeScript code
106
93
* @returns {ESTreeComment[] } the converted ESTreeComment
107
94
* @private
108
95
*/
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 [ ] = [ ] ;
111
101
112
102
/**
113
103
* Create a TypeScript Scanner, with skipTrivia set to false so that
@@ -120,7 +110,7 @@ function convertComments(ast, code) {
120
110
const start = triviaScanner . getTokenPos ( ) ;
121
111
const end = triviaScanner . getTextPos ( ) ;
122
112
123
- let container = null ;
113
+ let container : ts . Token < any > | null = null ;
124
114
switch ( kind ) {
125
115
case ts . SyntaxKind . SingleLineCommentTrivia :
126
116
case ts . SyntaxKind . MultiLineCommentTrivia : {
@@ -130,7 +120,7 @@ function convertComments(ast, code) {
130
120
break ;
131
121
}
132
122
case ts . SyntaxKind . CloseBraceToken :
133
- container = nodeUtils . getNodeContainer ( ast , start , end ) ;
123
+ container = nodeUtils . getNodeContainer ( ast , start , end ) as ts . Node ;
134
124
135
125
if (
136
126
container . kind === ts . SyntaxKind . TemplateMiddle ||
@@ -142,7 +132,7 @@ function convertComments(ast, code) {
142
132
break ;
143
133
case ts . SyntaxKind . SlashToken :
144
134
case ts . SyntaxKind . SlashEqualsToken :
145
- container = nodeUtils . getNodeContainer ( ast , start , end ) ;
135
+ container = nodeUtils . getNodeContainer ( ast , start , end ) as ts . Node ;
146
136
147
137
if ( container . kind === ts . SyntaxKind . RegularExpressionLiteral ) {
148
138
kind = triviaScanner . reScanSlashToken ( ) ;
0 commit comments