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