diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 438e2165..52ab1f41 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -1256,22 +1256,7 @@ export class JavaScriptParserVisitor { } visitVariableStatement(node: ts.VariableStatement) { - if (node.declarationList.declarations.length > 1) { - return this.visitVariableDeclarationList(node.declarationList); - } - return new J.VariableDeclarations( - randomId(), - this.prefix(node), - Markers.EMPTY, - [], - this.mapModifiers(node), - node.declarationList.declarations[0].type ? this.visit(node.declarationList.declarations[0].type) : null, - null, - [], - this.rightPaddedList([...node.declarationList.declarations], n => { - return Space.EMPTY; - }) - ); + return this.visitVariableDeclarationList(node.declarationList); } visitExpressionStatement(node: ts.ExpressionStatement): JS.ExpressionStatement { @@ -1397,9 +1382,10 @@ export class JavaScriptParserVisitor { visitVariableDeclaration(node: ts.VariableDeclaration) { return new J.VariableDeclarations.NamedVariable( randomId(), - Space.EMPTY, + this.prefix(node), Markers.EMPTY, - this.visit(node.name), + // NOTE: the name prefix will be used as a suffix by the printer + this.visit(node.name).withPrefix(node.type ? this.suffix(node.name) : Space.EMPTY), [], node.initializer ? this.leftPadded(this.prefix(node.getChildAt(node.getChildCount(this.sourceFile) - 2)), this.visit(node.initializer)) : null, this.mapVariableType(node) diff --git a/openrewrite/test/javascript/parser/object.test.ts b/openrewrite/test/javascript/parser/object.test.ts index e552d392..9ee40a93 100644 --- a/openrewrite/test/javascript/parser/object.test.ts +++ b/openrewrite/test/javascript/parser/object.test.ts @@ -50,7 +50,7 @@ describe('object literal mapping', () => { typeScript( 'const c = { [ 1 + 1 ] : 1 }', cu => { - const literal = ((cu.statements[0]).variables[0].initializer); + const literal = (((cu.statements[0]).variables[0]).variables[0].initializer); expect(literal.body).toBeDefined(); const computedName = ((literal.body?.statements[0]).name); expect(computedName).toBeDefined(); diff --git a/openrewrite/test/javascript/parser/variableDeclarations.test.ts b/openrewrite/test/javascript/parser/variableDeclarations.test.ts index 812b2556..6f9fec5e 100644 --- a/openrewrite/test/javascript/parser/variableDeclarations.test.ts +++ b/openrewrite/test/javascript/parser/variableDeclarations.test.ts @@ -1,4 +1,5 @@ import * as J from "../../../dist/src/java"; +import * as JS from "../../../dist/src/javascript"; import {connect, disconnect, rewriteRun, typeScript} from '../testHarness'; describe('variable declaration mapping', () => { @@ -17,7 +18,7 @@ describe('variable declaration mapping', () => { expect(cu).toBeDefined(); expect(cu.statements).toHaveLength(2); cu.statements.forEach(statement => { - expect(statement).toBeInstanceOf(J.VariableDeclarations); + expect(statement).toBeInstanceOf(JS.ScopedVariableDeclarations); }); cu.padding.statements.forEach(statement => { expect(statement.after.comments).toHaveLength(0); @@ -38,7 +39,7 @@ describe('variable declaration mapping', () => { expect(cu).toBeDefined(); expect(cu.statements).toHaveLength(2); cu.statements.forEach(statement => { - expect(statement).toBeInstanceOf(J.VariableDeclarations); + expect(statement).toBeInstanceOf(JS.ScopedVariableDeclarations); }); cu.padding.statements.forEach(statement => { expect(statement.after.comments).toHaveLength(0); @@ -47,6 +48,12 @@ describe('variable declaration mapping', () => { }) ); }); + test('typed', () => { + rewriteRun( + //language=typescript + typeScript('let a : number =2') + ); + }); test('multi', () => { rewriteRun( //language=typescript diff --git a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index 1d6d4cde..f11ffde4 100644 --- a/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java +++ b/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java @@ -745,7 +745,8 @@ public J visitVariableDeclarations(J.VariableDeclarations multiVariable, PrintOu if (multiVariable.getVarargs() != null) { p.append("..."); } - visit(variable.getElement().getName(), p); + p.append(variable.getElement().getName().getSimpleName()); + visitSpace(variable.getElement().getName().getPrefix(), Space.Location.LANGUAGE_EXTENSION, p); PostFixOperator postFixOperator = multiVariable.getMarkers().findFirst(PostFixOperator.class).orElse(null); if (postFixOperator != null) { visitSpace(postFixOperator.getPrefix(), Space.Location.LANGUAGE_EXTENSION, p);