@@ -42,6 +42,19 @@ namespace ts.codefix {
4242
4343 return applyChange ( changeTracker , parent , sourceFile , fixedNodes ) ;
4444 }
45+
46+ const commaExpression = findAncestor ( token , node =>
47+ isExpressionStatement ( node . parent ) ? true :
48+ isPossiblyPartOfCommaSeperatedInitializer ( node ) ? false : "quit"
49+ ) ;
50+ if ( commaExpression ) {
51+ const checker = program . getTypeChecker ( ) ;
52+ if ( ! expressionCouldBeVariableDeclaration ( commaExpression , checker ) ) {
53+ return ;
54+ }
55+
56+ return applyChange ( changeTracker , commaExpression , sourceFile , fixedNodes ) ;
57+ }
4558 }
4659
4760 function applyChange ( changeTracker : textChanges . ChangeTracker , initializer : Node , sourceFile : SourceFile , fixedNodes ?: NodeSet < Node > ) {
@@ -63,11 +76,34 @@ namespace ts.codefix {
6376 }
6477 }
6578
66- function arrayElementCouldBeVariableDeclaration ( expression : Expression , checker : TypeChecker ) {
79+ function arrayElementCouldBeVariableDeclaration ( expression : Expression , checker : TypeChecker ) : boolean {
6780 const identifier =
6881 isIdentifier ( expression ) ? expression :
6982 isAssignmentExpression ( expression , /*excludeCompoundAssignment*/ true ) && isIdentifier ( expression . left ) ? expression . left :
7083 undefined ;
7184 return ! ! identifier && ! checker . getSymbolAtLocation ( identifier ) ;
7285 }
86+
87+ function isPossiblyPartOfCommaSeperatedInitializer ( node : Node ) : boolean {
88+ switch ( node . kind ) {
89+ case SyntaxKind . Identifier :
90+ case SyntaxKind . BinaryExpression :
91+ case SyntaxKind . CommaToken :
92+ return true ;
93+ default :
94+ return false ;
95+ }
96+ }
97+
98+ function expressionCouldBeVariableDeclaration ( expression : Node , checker : TypeChecker ) : boolean {
99+ if ( ! isBinaryExpression ( expression ) ) {
100+ return false ;
101+ }
102+
103+ if ( expression . operatorToken . kind === SyntaxKind . CommaToken ) {
104+ return every ( [ expression . left , expression . right ] , expression => expressionCouldBeVariableDeclaration ( expression , checker ) ) ;
105+ }
106+
107+ return isIdentifier ( expression . left ) && ! checker . getSymbolAtLocation ( expression . left ) ;
108+ }
73109}
0 commit comments