@@ -380,22 +380,42 @@ public WithoutSemanticRewriter(CSTOJSOptions options)
380380 }
381381 return node ;
382382 }
383- public override SyntaxNode ? VisitVariableDeclaration ( VariableDeclarationSyntax node )
384- {
385- if ( node . Parent is ForStatementSyntax ||
386- node . Parent is LocalDeclarationStatementSyntax )
383+
384+ public override SyntaxNode ? VisitLocalDeclarationStatement ( LocalDeclarationStatementSyntax node )
385+ {
386+ //first process local declaration
387+ node = ( LocalDeclarationStatementSyntax ) base . VisitLocalDeclarationStatement ( node ) ! ;
388+
389+ SyntaxToken ? constToken = TryGetModifier ( SyntaxKind . ConstKeyword , node . Modifiers ) ;
390+
391+ if ( constToken != null )
387392 {
388- if ( _Options . UseVarOverLet )
389- node = node . ReplaceNode ( node . Type , SyntaxFactory . IdentifierName ( "var" ) . WithLeadingTrivia ( node . Type . GetLeadingTrivia ( ) ) . WithTrailingTrivia ( node . Type . GetTrailingTrivia ( ) ) ) ;
390- else
391- node = node . ReplaceNode ( node . Type , SyntaxFactory . IdentifierName ( "let" ) . WithLeadingTrivia ( node . Type . GetLeadingTrivia ( ) ) . WithTrailingTrivia ( node . Type . GetTrailingTrivia ( ) ) ) ;
392-
393+ //Add type with const.
394+ node = node . WithDeclaration ( node . Declaration . WithType ( SyntaxFactory . IdentifierName ( "const" ) . WithLeadingTrivia ( node . Modifiers [ 0 ] . LeadingTrivia ) . WithTrailingTrivia ( node . Modifiers [ 0 ] . TrailingTrivia ) ) ) ;
395+ //Clear modifiers.
396+ node = node . ReplaceTokens ( node . Modifiers , ( o , r ) => SyntaxFactory . Token ( SyntaxKind . None ) ) ;
393397 }
394-
398+
399+ return node ;
400+ }
401+
402+ public override SyntaxNode ? VisitVariableDeclaration ( VariableDeclarationSyntax node )
403+ {
404+ node = node . ReplaceNode ( node . Type , GetTypeVarOrLet ( node . Type ) ) ;
405+
395406 node = ( VariableDeclarationSyntax ) base . VisitVariableDeclaration ( node ) ! ;
396407
397408 return node ;
398409 }
410+ //There is no VariableDeclaration in ForEachStatement, so do an explicit replacement of a type.
411+ public override SyntaxNode ? VisitForEachStatement ( ForEachStatementSyntax node )
412+ {
413+ node = node . ReplaceNode ( node . Type , GetTypeVarOrLet ( node . Type ) ) ;
414+
415+ node = ( ForEachStatementSyntax ) base . VisitForEachStatement ( node ) ! ;
416+
417+ return node ;
418+ }
399419 public override SyntaxNode ? VisitInvocationExpression ( InvocationExpressionSyntax node )
400420 {
401421 if ( node . Expression is IdentifierNameSyntax identifier )
@@ -671,6 +691,15 @@ public WithoutSemanticRewriter(CSTOJSOptions options)
671691 }
672692 return mod ;
673693 }
694+
695+ private TypeSyntax GetTypeVarOrLet ( TypeSyntax type )
696+ {
697+ if ( _Options . UseVarOverLet )
698+ return SyntaxFactory . IdentifierName ( "var" ) . WithLeadingTrivia ( type . GetLeadingTrivia ( ) ) . WithTrailingTrivia ( type . GetTrailingTrivia ( ) ) ;
699+ else
700+ return SyntaxFactory . IdentifierName ( "let" ) . WithLeadingTrivia ( type . GetLeadingTrivia ( ) ) . WithTrailingTrivia ( type . GetTrailingTrivia ( ) ) ;
701+
702+ }
674703}
675704
676705internal class KeepBraceOnTheSameLineRewriter : CSharpSyntaxRewriter
0 commit comments