@@ -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 node is a valid ESTree class member
120+ * @param {object } node AST node
121+ * @returns {boolean } is valid 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 token is a comma
129+ * @param {object } token the syntax 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 operator is the assignment operator
138+ * @param {object } operator the operator
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 operator is a logical operator
147+ * @param {object } operator the operator
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 operator
156+ * @param {object } operator the operator
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 node or token,
195+ * for the given AST
196+ * @param {object } nodeOrToken the node or token
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 node
220+ * @param {object } node the node
221+ * @param {object } result result
222+ * @param {[type] } ast the AST
223+ * @returns {object } the node 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 token
275+ * @param {object } token the token
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 token, for a given AST
331+ * @param {object } token the token
332+ * @param {object } ast the AST object
333+ * @returns {object } the converted token
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 {array } the converted tokens
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 node
390+ * @param {object } node the node
391+ * @param {object } parent the parent node
392+ * @returns {object } the converted node
393+ */
323394 function convert ( node , parent ) {
324395
325396 // exit early for null and undefined
@@ -333,12 +404,21 @@ module.exports = function(ast, extra) {
333404 loc : getLoc ( node , ast )
334405 } ;
335406
407+ /**
408+ * Copies the result object
409+ * @returns {void }
410+ */
336411 function simplyCopy ( ) {
337412 assign ( result , {
338413 type : SyntaxKind [ node . kind ]
339414 } ) ;
340415 }
341416
417+ /**
418+ * Converts child node
419+ * @param {object } child the child node
420+ * @returns {object } the converted child node
421+ */
342422 function convertChild ( child ) {
343423 return convert ( child , node ) ;
344424 }
@@ -348,7 +428,7 @@ module.exports = function(ast, extra) {
348428 assign ( result , {
349429 type : "Program" ,
350430 body : [ ] ,
351- sourceType : node . externalModuleIndicator ? "module" : "script"
431+ sourceType : node . externalModuleIndicator ? "module" : "script"
352432 } ) ;
353433
354434 // filter out unknown nodes for now
@@ -531,10 +611,18 @@ module.exports = function(ast, extra) {
531611
532612 case SyntaxKind . VariableStatement :
533613
614+ var varStatementKind ;
615+
616+ if ( node . declarationList . flags ) {
617+ varStatementKind = ( node . declarationList . flags === ts . NodeFlags . Let ) ? "let" : "const" ;
618+ } else {
619+ varStatementKind = "var" ;
620+ }
621+
534622 assign ( result , {
535623 type : "VariableDeclaration" ,
536624 declarations : node . declarationList . declarations . map ( convertChild ) ,
537- kind : ( node . declarationList . flags ? ( node . declarationList . flags === ts . NodeFlags . Let ? "let" : "const" ) : "var" )
625+ kind : varStatementKind
538626 } ) ;
539627
540628 // check for exports
@@ -543,10 +631,19 @@ module.exports = function(ast, extra) {
543631
544632 // mostly for for-of, for-in
545633 case SyntaxKind . VariableDeclarationList :
634+
635+ var varDeclarationListKind ;
636+
637+ if ( node . flags ) {
638+ varDeclarationListKind = ( node . flags === ts . NodeFlags . Let ) ? "let" : "const" ;
639+ } else {
640+ varDeclarationListKind = "var" ;
641+ }
642+
546643 assign ( result , {
547644 type : "VariableDeclaration" ,
548645 declarations : node . declarations . map ( convertChild ) ,
549- kind : ( node . flags ? ( node . flags === ts . NodeFlags . Let ? "let" : "const" ) : "var" )
646+ kind : varDeclarationListKind
550647 } ) ;
551648 break ;
552649
@@ -719,7 +816,7 @@ module.exports = function(ast, extra) {
719816 // TypeScript uses this even for static methods named "constructor"
720817 case SyntaxKind . Constructor :
721818
722- var constructorIsStatic = Boolean ( node . flags & ts . NodeFlags . Static ) ,
819+ var constructorIsStatic = Boolean ( node . flags & ts . NodeFlags . Static ) ,
723820 firstConstructorToken = constructorIsStatic ? ts . findNextToken ( node . getFirstToken ( ) , ast ) : node . getFirstToken ( ) ,
724821 constructorOffset = 11 ,
725822 constructorStartOffset = constructorOffset + firstConstructorToken . getStart ( ) - node . getFirstToken ( ) . getStart ( ) ,
@@ -807,13 +904,6 @@ module.exports = function(ast, extra) {
807904 } ) ;
808905 break ;
809906
810- case SyntaxKind . SpreadElementExpression :
811- assign ( result , {
812- type : "SpreadElement" ,
813- argument : convertChild ( node . expression )
814- } ) ;
815- break ;
816-
817907 case SyntaxKind . ArrayBindingPattern :
818908 assign ( result , {
819909 type : "ArrayPattern" ,
@@ -997,7 +1087,7 @@ module.exports = function(ast, extra) {
9971087 range : [ openBrace . getStart ( ) , result . range [ 1 ] ] ,
9981088 loc : getLocFor ( openBrace . getStart ( ) , node . end , ast )
9991089 } ,
1000- superClass : ( node . heritageClauses ? convertChild ( node . heritageClauses [ 0 ] . types [ 0 ] . expression ) : null ) ,
1090+ superClass : ( node . heritageClauses ? convertChild ( node . heritageClauses [ 0 ] . types [ 0 ] . expression ) : null )
10011091 } ) ;
10021092
10031093 var filteredMembers = node . members . filter ( isESTreeClassMember ) ;
0 commit comments