@@ -37,8 +37,8 @@ var ts = require("typescript"),
3737// Private
3838//------------------------------------------------------------------------------
3939
40- var SyntaxKind = ts . SyntaxKind ,
41- TokenClass = ts . TokenClass ;
40+ var SyntaxKind = ts . SyntaxKind ;
41+ // var TokenClass = ts.TokenClass;
4242
4343var ASSIGNMENT_OPERATORS = [
4444 SyntaxKind . EqualsToken ,
@@ -115,22 +115,47 @@ TOKEN_TO_TEXT[SyntaxKind.CaretEqualsToken] = "^=";
115115TOKEN_TO_TEXT [ SyntaxKind . AtToken ] = "@" ;
116116TOKEN_TO_TEXT [ SyntaxKind . InKeyword ] = "in" ;
117117
118+ /**
119+ * Returns true if the given TSNode is a valid ESTree class member
120+ * @param {TSNode } node TypeScript AST node
121+ * @returns {boolean } is valid ESTree class member
122+ */
118123function isESTreeClassMember ( node ) {
119124 return node . kind !== SyntaxKind . PropertyDeclaration && node . kind !== SyntaxKind . SemicolonClassElement ;
120125}
121126
127+ /**
128+ * Returns true if the given TSToken is a comma
129+ * @param {TSToken } token the TypeScript token
130+ * @returns {boolean } is comma
131+ */
122132function isComma ( token ) {
123133 return token . kind === SyntaxKind . CommaToken ;
124134}
125135
136+ /**
137+ * Returns true if the given TSToken is the assignment operator
138+ * @param {TSToken } operator the operator token
139+ * @returns {boolean } is assignment
140+ */
126141function isAssignmentOperator ( operator ) {
127142 return ASSIGNMENT_OPERATORS . indexOf ( operator . kind ) > - 1 ;
128143}
129144
145+ /**
146+ * Returns true if the given TSToken is a logical operator
147+ * @param {TSToken } operator the operator token
148+ * @returns {boolean } is a logical operator
149+ */
130150function isLogicalOperator ( operator ) {
131151 return LOGICAL_OPERATORS . indexOf ( operator . kind ) > - 1 ;
132152}
133153
154+ /**
155+ * Returns the binary expression type of the given TSToken
156+ * @param {TSToken } operator the operator token
157+ * @returns {string } the binary expression type
158+ */
134159function getBinaryExpressionType ( operator ) {
135160 if ( isAssignmentOperator ( operator ) ) {
136161 return "AssignmentExpression" ;
@@ -141,6 +166,14 @@ function getBinaryExpressionType(operator) {
141166 }
142167}
143168
169+ /**
170+ * Returns line and column data for the given start and end positions,
171+ * for the given AST
172+ * @param {Object } start start data
173+ * @param {Object } end end data
174+ * @param {Object } ast the AST object
175+ * @returns {Object } the loc data
176+ */
144177function getLocFor ( start , end , ast ) {
145178 var startLoc = ast . getLineAndCharacterOfPosition ( start ) ,
146179 endLoc = ast . getLineAndCharacterOfPosition ( end ) ;
@@ -157,6 +190,13 @@ function getLocFor(start, end, ast) {
157190 } ;
158191}
159192
193+ /**
194+ * Returns line and column data for the given ESTreeNode or ESTreeToken,
195+ * for the given AST
196+ * @param {ESTreeToken|ESTreeNode } nodeOrToken the ESTreeNode or ESTreeToken
197+ * @param {Object } ast the AST object
198+ * @returns {Object } the loc data
199+ */
160200function getLoc ( nodeOrToken , ast ) {
161201 return getLocFor ( nodeOrToken . getStart ( ) , nodeOrToken . end , ast ) ;
162202 // var start = nodeOrToken.getStart(),
@@ -175,6 +215,13 @@ function getLoc(nodeOrToken, ast) {
175215 // };
176216}
177217
218+ /**
219+ * Fixes the exports of the given TSNode
220+ * @param {TSNode } node the TSNode
221+ * @param {Object } result result
222+ * @param {Object } ast the AST
223+ * @returns {TSNode } the TSNode with fixed exports
224+ */
178225function fixExports ( node , result , ast ) {
179226 // check for exports
180227 if ( node . modifiers && node . modifiers [ 0 ] . kind === SyntaxKind . ExportKeyword ) {
@@ -206,6 +253,11 @@ function fixExports(node, result, ast) {
206253 return result ;
207254}
208255
256+ /**
257+ * Extends and formats a given error object
258+ * @param {Object } error the error object
259+ * @returns {Object } converted error object
260+ */
209261function convertError ( error ) {
210262
211263 var loc = error . file . getLineAndCharacterOfPosition ( error . start ) ;
@@ -218,9 +270,13 @@ function convertError(error) {
218270 } ;
219271}
220272
273+ /**
274+ * Returns the type of a given ESTreeToken
275+ * @param {ESTreeToken } token the ESTreeToken
276+ * @returns {string } the token type
277+ */
221278function getTokenType ( token ) {
222279
223-
224280 // Need two checks for keywords since some are also identifiers
225281 if ( token . originalKeywordKind ) {
226282
@@ -233,7 +289,7 @@ function getTokenType(token) {
233289 return "Identifier" ;
234290
235291 default :
236- return "Keyword"
292+ return "Keyword" ;
237293 }
238294 }
239295
@@ -264,19 +320,23 @@ function getTokenType(token) {
264320 case SyntaxKind . GetKeyword :
265321 case SyntaxKind . SetKeyword :
266322 // falls through
323+ default :
267324 }
268325
269-
270-
271-
272326 return "Identifier" ;
273327}
274328
329+ /**
330+ * Extends and formats a given ESTreeToken, for a given AST
331+ * @param {ESTreeToken } token the ESTreeToken
332+ * @param {Object } ast the AST object
333+ * @returns {ESTreeToken } the converted ESTreeToken
334+ */
275335function convertToken ( token , ast ) {
276336
277337 var start = token . getStart ( ) ,
278338 value = ast . text . slice ( start , token . end ) ,
279- newToken = {
339+ newToken = {
280340 type : getTokenType ( token ) ,
281341 value : value ,
282342 range : [ start , token . end ] ,
@@ -293,6 +353,11 @@ function convertToken(token, ast) {
293353 return newToken ;
294354}
295355
356+ /**
357+ * Converts all tokens for the given AST
358+ * @param {Object } ast the AST object
359+ * @returns {ESTreeToken[] } the converted ESTreeTokens
360+ */
296361function convertTokens ( ast ) {
297362 var token = ast . getFirstToken ( ) ,
298363 converted ,
@@ -320,6 +385,12 @@ module.exports = function(ast, extra) {
320385 throw convertError ( ast . parseDiagnostics [ 0 ] ) ;
321386 }
322387
388+ /**
389+ * Converts a TypeScript node into an ESTree node
390+ * @param {TSNode } node the TSNode
391+ * @param {TSNode } parent the parent TSNode
392+ * @returns {ESTreeNode } the converted ESTreeNode
393+ */
323394 function convert ( node , parent ) {
324395
325396 // exit early for null and undefined
@@ -333,12 +404,22 @@ module.exports = function(ast, extra) {
333404 loc : getLoc ( node , ast )
334405 } ;
335406
407+ /**
408+ * Copies the result object into an ESTree node with just a type property.
409+ * This is used only for leaf nodes that have no other properties.
410+ * @returns {void }
411+ */
336412 function simplyCopy ( ) {
337413 assign ( result , {
338414 type : SyntaxKind [ node . kind ]
339415 } ) ;
340416 }
341417
418+ /**
419+ * Converts a TypeScript node into an ESTree node.
420+ * @param {TSNode } child the child TSNode
421+ * @returns {ESTreeNode } the converted ESTree node
422+ */
342423 function convertChild ( child ) {
343424 return convert ( child , node ) ;
344425 }
@@ -348,7 +429,7 @@ module.exports = function(ast, extra) {
348429 assign ( result , {
349430 type : "Program" ,
350431 body : [ ] ,
351- sourceType : node . externalModuleIndicator ? "module" : "script"
432+ sourceType : node . externalModuleIndicator ? "module" : "script"
352433 } ) ;
353434
354435 // filter out unknown nodes for now
@@ -531,10 +612,18 @@ module.exports = function(ast, extra) {
531612
532613 case SyntaxKind . VariableStatement :
533614
615+ var varStatementKind ;
616+
617+ if ( node . declarationList . flags ) {
618+ varStatementKind = ( node . declarationList . flags === ts . NodeFlags . Let ) ? "let" : "const" ;
619+ } else {
620+ varStatementKind = "var" ;
621+ }
622+
534623 assign ( result , {
535624 type : "VariableDeclaration" ,
536625 declarations : node . declarationList . declarations . map ( convertChild ) ,
537- kind : ( node . declarationList . flags ? ( node . declarationList . flags === ts . NodeFlags . Let ? "let" : "const" ) : "var" )
626+ kind : varStatementKind
538627 } ) ;
539628
540629 // check for exports
@@ -543,10 +632,19 @@ module.exports = function(ast, extra) {
543632
544633 // mostly for for-of, for-in
545634 case SyntaxKind . VariableDeclarationList :
635+
636+ var varDeclarationListKind ;
637+
638+ if ( node . flags ) {
639+ varDeclarationListKind = ( node . flags === ts . NodeFlags . Let ) ? "let" : "const" ;
640+ } else {
641+ varDeclarationListKind = "var" ;
642+ }
643+
546644 assign ( result , {
547645 type : "VariableDeclaration" ,
548646 declarations : node . declarations . map ( convertChild ) ,
549- kind : ( node . flags ? ( node . flags === ts . NodeFlags . Let ? "let" : "const" ) : "var" )
647+ kind : varDeclarationListKind
550648 } ) ;
551649 break ;
552650
@@ -719,7 +817,7 @@ module.exports = function(ast, extra) {
719817 // TypeScript uses this even for static methods named "constructor"
720818 case SyntaxKind . Constructor :
721819
722- var constructorIsStatic = Boolean ( node . flags & ts . NodeFlags . Static ) ,
820+ var constructorIsStatic = Boolean ( node . flags & ts . NodeFlags . Static ) ,
723821 firstConstructorToken = constructorIsStatic ? ts . findNextToken ( node . getFirstToken ( ) , ast ) : node . getFirstToken ( ) ,
724822 constructorOffset = 11 ,
725823 constructorStartOffset = constructorOffset + firstConstructorToken . getStart ( ) - node . getFirstToken ( ) . getStart ( ) ,
@@ -807,13 +905,6 @@ module.exports = function(ast, extra) {
807905 } ) ;
808906 break ;
809907
810- case SyntaxKind . SpreadElementExpression :
811- assign ( result , {
812- type : "SpreadElement" ,
813- argument : convertChild ( node . expression )
814- } ) ;
815- break ;
816-
817908 case SyntaxKind . ArrayBindingPattern :
818909 assign ( result , {
819910 type : "ArrayPattern" ,
@@ -997,7 +1088,7 @@ module.exports = function(ast, extra) {
9971088 range : [ openBrace . getStart ( ) , result . range [ 1 ] ] ,
9981089 loc : getLocFor ( openBrace . getStart ( ) , node . end , ast )
9991090 } ,
1000- superClass : ( node . heritageClauses ? convertChild ( node . heritageClauses [ 0 ] . types [ 0 ] . expression ) : null ) ,
1091+ superClass : ( node . heritageClauses ? convertChild ( node . heritageClauses [ 0 ] . types [ 0 ] . expression ) : null )
10011092 } ) ;
10021093
10031094 var filteredMembers = node . members . filter ( isESTreeClassMember ) ;
0 commit comments