From 3eef5f271e6d502a142068f67dae9f5defb6fbd5 Mon Sep 17 00:00:00 2001 From: traceyyoshima Date: Thu, 16 Nov 2023 17:41:02 -0700 Subject: [PATCH] Removed unnecessary calls to v8. --- .../TypeScriptSignatureBuilder.java | 13 +- .../javascript/TypeScriptTypeMapping.java | 21 +- .../internal/TypeScriptParserVisitor.java | 181 +++++++++++------- 3 files changed, 127 insertions(+), 88 deletions(-) diff --git a/src/main/java/org/openrewrite/javascript/TypeScriptSignatureBuilder.java b/src/main/java/org/openrewrite/javascript/TypeScriptSignatureBuilder.java index 08e5702b..1613bcaf 100644 --- a/src/main/java/org/openrewrite/javascript/TypeScriptSignatureBuilder.java +++ b/src/main/java/org/openrewrite/javascript/TypeScriptSignatureBuilder.java @@ -20,6 +20,7 @@ import org.openrewrite.java.JavaTypeSignatureBuilder; import org.openrewrite.java.tree.JavaType; import org.openrewrite.javascript.internal.tsc.TSCNode; +import org.openrewrite.javascript.internal.tsc.TSCNodeList; import org.openrewrite.javascript.internal.tsc.TSCSymbol; import org.openrewrite.javascript.internal.tsc.TSCType; import org.openrewrite.javascript.internal.tsc.generated.TSCObjectFlag; @@ -58,7 +59,8 @@ public String signature(@Nullable Object object) { case ClassDeclaration: case EnumDeclaration: case InterfaceDeclaration: - return node.hasProperty("typeParameters") && !node.getNodeListProperty("typeParameters").isEmpty() ? + TSCNodeList typeParameters = node.getOptionalNodeListProperty("typeParameters"); + return typeParameters != null && !typeParameters.isEmpty() ? parameterizedSignature(node) : classSignature(node); case ArrayType: cached = arraySignature(node); @@ -131,8 +133,8 @@ public String genericSignature(Object object) { StringBuilder s = new StringBuilder("Generic{").append(name); StringJoiner boundSigs = new StringJoiner(" & "); - if (node.hasProperty("constraint")) { - TSCNode constraint = node.getNodeProperty("constraint"); + TSCNode constraint = node.getOptionalNodeProperty("constraint"); + if (constraint != null) { if (constraint.syntaxKind() == TSCSyntaxKind.IntersectionType) { for (TSCNode type : constraint.getNodeListProperty("types")) { boundSigs.add(signature(type)); @@ -232,8 +234,9 @@ public String variableSignature(TSCNode node) { String name = node.getNodeProperty("name").getText(); String typeSig; - if (node.hasProperty("type")) { - typeSig = signature(node.getNodeProperty("type")); + TSCNode type = node.getOptionalNodeProperty("type"); + if (type != null) { + typeSig = signature(type); } else if (node.syntaxKind() == TSCSyntaxKind.EnumMember) { typeSig = owner; } else { diff --git a/src/main/java/org/openrewrite/javascript/TypeScriptTypeMapping.java b/src/main/java/org/openrewrite/javascript/TypeScriptTypeMapping.java index 14a5deba..39d83fb9 100644 --- a/src/main/java/org/openrewrite/javascript/TypeScriptTypeMapping.java +++ b/src/main/java/org/openrewrite/javascript/TypeScriptTypeMapping.java @@ -23,6 +23,7 @@ import org.openrewrite.java.tree.JavaType; import org.openrewrite.java.tree.TypeUtils; import org.openrewrite.javascript.internal.tsc.TSCNode; +import org.openrewrite.javascript.internal.tsc.TSCNodeList; import org.openrewrite.javascript.internal.tsc.TSCSymbol; import org.openrewrite.javascript.internal.tsc.TSCType; import org.openrewrite.javascript.internal.tsc.generated.TSCObjectFlag; @@ -171,8 +172,9 @@ private JavaType.FullyQualified classType(@Nullable TSCNode node, String signatu List propertyNodes = null; List methodNodes = null; List enumNodes = null; - if (node.hasProperty("members")) { - for (TSCNode member : node.getNodeListProperty("members")) { + TSCNodeList memberNodes = node.getOptionalNodeListProperty("members"); + if (memberNodes != null) { + for (TSCNode member : memberNodes) { if (member.syntaxKind() == TSCSyntaxKind.CallSignature || member.syntaxKind() == TSCSyntaxKind.Constructor || member.syntaxKind() == TSCSyntaxKind.ConstructSignature || @@ -228,15 +230,15 @@ private JavaType.FullyQualified classType(@Nullable TSCNode node, String signatu clazz.unsafeSet(null, supertype, owner, mapAnnotations(modifiers), interfaces, members, methods); } - if (node.hasProperty("typeParameters")) { + TSCNodeList typeParamNodes = node.getOptionalNodeListProperty("typeParameters"); + if (typeParamNodes != null) { JavaType jt = typeCache.get(signature); if (jt == null) { JavaType.Parameterized pt = new JavaType.Parameterized(null, null, null); typeCache.put(signature, pt); - List paramNodes = node.getNodeListProperty("typeParameters"); - List typeParams = new ArrayList<>(paramNodes.size()); - for (TSCNode paramNode : paramNodes) { + List typeParams = new ArrayList<>(typeParamNodes.size()); + for (TSCNode paramNode : typeParamNodes) { typeParams.add(type(paramNode)); } pt.unsafeSet(clazz, typeParams); @@ -255,8 +257,8 @@ public JavaType.GenericTypeVariable generic(TSCNode node, String signature) { List bounds = null; JavaType.GenericTypeVariable.Variance variance = INVARIANT; - if (node.hasProperty("constraint")) { - TSCNode constraint = node.getNodeProperty("constraint"); + TSCNode constraint = node.getOptionalNodeProperty("constraint"); + if (constraint != null) { if (constraint.syntaxKind() == TSCSyntaxKind.IntersectionType) { List types = constraint.getNodeListProperty("types"); bounds = new ArrayList<>(types.size()); @@ -297,11 +299,12 @@ public JavaType.Method methodDeclarationType(TSCNode node, @Nullable JavaType.Fu boolean isConstructor = node.syntaxKind() == TSCSyntaxKind.Constructor; List modifiers = node.getOptionalNodeListProperty("modifiers"); + TSCNode nodeName = node.getOptionalNodeProperty("name"); JavaType.Method method = new JavaType.Method( null, mapModifiers(modifiers), null, - isConstructor ? "" : node.hasProperty("name") ? node.getNodeProperty("name").getText() : "{anonymous}", + isConstructor ? "" : nodeName != null ? nodeName.getText() : "{anonymous}", null, paramNames, null, null, null, diff --git a/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java b/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java index e65a5aa4..872aed37 100644 --- a/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java +++ b/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java @@ -488,11 +488,12 @@ private J.Block visitBlock(@Nullable TSCNode node) { } private J.Break visitBreakStatement(TSCNode node) { + TSCNode label = node.getOptionalNodeProperty("label"); return new J.Break( randomId(), sourceBefore(TSCSyntaxKind.BreakKeyword), Markers.EMPTY, - node.hasProperty("label") ? (J.Identifier) visitNode(node.getNodeProperty("label")) : null + label != null ? (J.Identifier) visitNode(label) : null ); } @@ -508,12 +509,13 @@ private J.MethodInvocation visitCallExpression(TSCNode node) { JRightPadded select = null; TSCNode expression = node.getNodeProperty("expression"); - if (expression.hasProperty("expression")) { + TSCNode expr = expression.getOptionalNodeProperty("expression"); + if (expr != null) { // Adjust padding. implementMe(expression, "questionDotToken"); if (expression.syntaxKind() == TSCSyntaxKind.PropertyAccessExpression) { - select = padRight((Expression) visitNode(expression.getNodeProperty("expression")), sourceBefore(TSCSyntaxKind.DotToken)); + select = padRight((Expression) visitNode(expr), sourceBefore(TSCSyntaxKind.DotToken)); } else if (expression.syntaxKind() == TSCSyntaxKind.ParenthesizedExpression) { markers = markers.addIfAbsent(new OmitDot(randomId())); select = padRight((Expression) visitNode(expression), whitespace()); @@ -524,8 +526,9 @@ private J.MethodInvocation visitCallExpression(TSCNode node) { JavaType.Method type = typeMapping.methodInvocationType(node); J.Identifier name = null; - if (expression.hasProperty("name")) { - name = visitIdentifier(expression.getNodeProperty("name"), type); + TSCNode nameNode = expression.getOptionalNodeProperty("name"); + if (nameNode != null) { + name = visitIdentifier(nameNode, type); } else if (expression.syntaxKind() == TSCSyntaxKind.Identifier) { name = visitIdentifier(expression, type); } else if (expression.syntaxKind() == TSCSyntaxKind.SuperKeyword) { @@ -565,10 +568,11 @@ private J.MethodInvocation visitCallExpression(TSCNode node) { } JContainer arguments = null; - if (node.hasProperty("arguments")) { + TSCNodeList argNodes = node.getOptionalNodeListProperty("arguments"); + if (argNodes != null) { JContainer jContainer = mapContainer( TSCSyntaxKind.OpenParenToken, - node.getNodeListProperty("arguments"), + argNodes, TSCSyntaxKind.CommaToken, TSCSyntaxKind.CloseParenToken, this::visitNode, @@ -631,15 +635,12 @@ private J.ClassDeclaration visitClassDeclaration(TSCNode node) { J.ClassDeclaration.Kind kind = new J.ClassDeclaration.Kind(randomId(), kindPrefix, Markers.EMPTY, kindAnnotations, type); J.Identifier name; - if (node.hasProperty("name")) { - name = visitIdentifier(node.getNodeProperty("name")); - } else { - name = convertToIdentifier(EMPTY, ""); - } - - JContainer typeParams = !node.hasProperty("typeParameters") ? null : mapContainer( + TSCNode nameNode = node.getOptionalNodeProperty("name"); + name = nameNode != null ? visitIdentifier(nameNode) : convertToIdentifier(EMPTY, ""); + TSCNodeList typeParameterNodes = node.getOptionalNodeListProperty("typeParameters"); + JContainer typeParams = typeParameterNodes == null ? null : mapContainer( TSCSyntaxKind.LessThanToken, - node.getNodeListProperty("typeParameters"), + typeParameterNodes, TSCSyntaxKind.CommaToken, TSCSyntaxKind.GreaterThanToken, t -> (J.TypeParameter) visitNode(t) @@ -647,8 +648,9 @@ private J.ClassDeclaration visitClassDeclaration(TSCNode node) { JLeftPadded extendings = null; JContainer implementings = null; - if (node.hasProperty("heritageClauses")) { - for (TSCNode tscNode : node.getNodeListProperty("heritageClauses")) { + TSCNodeList heritageClausesNodes = node.getOptionalNodeListProperty("heritageClauses"); + if (heritageClausesNodes != null) { + for (TSCNode tscNode : heritageClausesNodes) { if (TSCSyntaxKind.fromCode(tscNode.getIntProperty("token")) == TSCSyntaxKind.ExtendsKeyword) { List types = tscNode.getNodeListProperty("types"); assert types.size() == 1; @@ -661,10 +663,10 @@ private J.ClassDeclaration visitClassDeclaration(TSCNode node) { J.Block body; List> members; - if (node.hasProperty("members")) { + TSCNodeList memberNodes = node.getOptionalNodeListProperty("members"); + if (memberNodes != null) { Space bodyPrefix = sourceBefore(TSCSyntaxKind.OpenBraceToken); - TSCNodeList memberNodes = node.getNodeListProperty("members"); if (kind.getType() == J.ClassDeclaration.Kind.Type.Enum) { Space enumPrefix = whitespace(); @@ -774,11 +776,12 @@ private J.Ternary visitConditionalExpression(TSCNode node) { } private J.Continue visitContinueStatement(TSCNode node) { + TSCNode label = node.getOptionalNodeProperty("label"); return new J.Continue( randomId(), sourceBefore(TSCSyntaxKind.ContinueKeyword), Markers.EMPTY, - node.hasProperty("label") ? (J.Identifier) visitNode(node.getNodeProperty("label")) : null + label != null ? (J.Identifier) visitNode(node.getNodeProperty("label")) : null ); } @@ -789,8 +792,9 @@ private J.MethodDeclaration visitConstructor(TSCNode node) { Space prefix = whitespace(); List modifiers; List leadingAnnotations = new ArrayList<>(); - if (node.hasProperty("modifiers")) { - modifiers = mapModifiers(node.getNodeListProperty("modifiers"), leadingAnnotations); + TSCNodeList modifierNodes = node.getOptionalNodeListProperty("modifiers"); + if (modifierNodes != null) { + modifiers = mapModifiers(modifierNodes, leadingAnnotations); } else { modifiers = emptyList(); } @@ -832,10 +836,11 @@ private J.Annotation visitDecorator(TSCNode node) { TSCNode callExpression = node.getNodeProperty("expression"); NameTree name = (NameTree) visitNameExpression(callExpression.getNodeProperty("expression")); JContainer arguments = null; - if (callExpression.hasProperty("arguments")) { + TSCNodeList args = callExpression.getOptionalNodeListProperty("arguments"); + if (args != null) { JContainer jContainer = mapContainer( TSCSyntaxKind.OpenParenToken, - callExpression.getNodeListProperty("arguments"), + args, TSCSyntaxKind.CommaToken, TSCSyntaxKind.CloseParenToken, this::visitNode, @@ -1155,8 +1160,9 @@ private J.MethodDeclaration visitFunctionDeclaration(TSCNode node) { J.Identifier name; JavaType.Method method = typeMapping.methodDeclarationType(node); - if (node.hasProperty("name")) { - name = visitIdentifier(node.getNodeProperty("name")); + TSCNode nameNode = node.getOptionalNodeProperty("name"); + if (nameNode != null) { + name = visitIdentifier(nameNode); } else { // FIXME: get input, we can add an anonymous name and prevent printing with a marker. // Function expressions do not require a name `function (..)` @@ -1384,13 +1390,14 @@ private J.If visitIfStatement(TSCNode node) { JRightPadded thenPart = visitStatement(node.getNodeProperty("thenStatement")); J.If.Else elsePart = null; - if (node.hasProperty("elseStatement")) { + TSCNode elseNode = node.getOptionalNodeProperty("elseStatement"); + if (elseNode != null) { Space elsePartPrefix = sourceBefore(TSCSyntaxKind.ElseKeyword); elsePart = new J.If.Else( randomId(), elsePartPrefix, Markers.EMPTY, - visitStatement(node.getNodeProperty("elseStatement")) + visitStatement(elseNode) ); } return new J.If( @@ -1529,9 +1536,10 @@ private J.MethodDeclaration visitMethodDeclaration(TSCNode node) { JContainer throw_ = null; TypeTree returnTypeExpression = null; - if (node.hasProperty("type")) { + TSCNode typeNode = node.getOptionalNodeProperty("type"); + if (typeNode != null) { markers = markers.addIfAbsent(new TypeReferencePrefix(randomId(), sourceBefore(TSCSyntaxKind.ColonToken))); - returnTypeExpression = (TypeTree) visitNode(node.getNodeProperty("type")); + returnTypeExpression = (TypeTree) visitNode(typeNode); } J.Block body = visitBlock(node.getOptionalNodeProperty("body")); @@ -1555,9 +1563,10 @@ private J.MethodDeclaration visitMethodDeclaration(TSCNode node) { } private Expression visitNameExpression(TSCNode expression) { - if (expression.hasProperty("expression")) { + TSCNode expr = expression.getOptionalNodeProperty("expression"); + if (expr != null) { Space prefix = whitespace(); - Expression select = visitNameExpression(expression.getNodeProperty("expression")); + Expression select = visitNameExpression(expr); // Adjust left padding from sourceBefore. implementMe(expression, "questionDotToken"); @@ -1574,11 +1583,16 @@ private Expression visitNameExpression(TSCNode expression) { ); } else { Expression identifier = null; - if (expression.hasProperty("name")) { - identifier = (Expression) visitNode(expression.getNodeProperty("name")); - } else if (expression.hasProperty("escapedText") || expression.syntaxKind() == TSCSyntaxKind.ThisKeyword) { + TSCNode name = expression.getOptionalNodeProperty("name"); + if (name != null) { + identifier = (Expression) visitNode(name); + } + + if (identifier == null && expression.hasProperty("escapedText") || expression.syntaxKind() == TSCSyntaxKind.ThisKeyword) { identifier = (Expression) visitNode(expression); - } else { + } + + if (identifier == null) { implementMe(expression); } return identifier; @@ -1632,8 +1646,9 @@ private J visitMetaProperty(TSCNode node) { private J.NewClass visitNewExpression(TSCNode node) { Space prefix = sourceBefore(TSCSyntaxKind.NewKeyword); TypeTree typeTree = null; - if (node.hasProperty("expression")) { - typeTree = (TypeTree) visitNameExpression(node.getNodeProperty("expression")); + TSCNode expr = node.getOptionalNodeProperty("expression"); + if (expr != null) { + typeTree = (TypeTree) visitNameExpression(expr); } implementMe(node, "typeArguments"); JContainer jContainer = mapContainer( @@ -1829,6 +1844,7 @@ private JS.ObjectBindingDeclarations mapObjectBindingDeclaration(TSCNode node) { bindings.add(padRight(b, after).withMarkers(bindingMarkers)); } + TSCNode init = bindingNode.getOptionalNodeProperty("initializer"); return new JS.ObjectBindingDeclarations( randomId(), prefix, @@ -1837,7 +1853,7 @@ private JS.ObjectBindingDeclarations mapObjectBindingDeclaration(TSCNode node) { modifiers, typeTree, JContainer.build(beforeBraces, bindings, Markers.EMPTY), - bindingNode.hasProperty("initializer") ? padLeft(sourceBefore(TSCSyntaxKind.EqualsToken), (Expression) visitNode(bindingNode.getNodeProperty("initializer"))) : null + init != null ? padLeft(sourceBefore(TSCSyntaxKind.EqualsToken), (Expression) visitNode(init)) : null ); } @@ -1890,7 +1906,8 @@ private J.VariableDeclarations visitPropertyDeclaration(TSCNode node) { } TypeTree typeTree = null; - if (node.hasProperty("type")) { + TSCNode typeNode = node.getOptionalNodeProperty("type"); + if (typeNode != null) { TSCNode questionToken = node.getOptionalNodeProperty("questionToken"); TSCNode exclamationToken = node.getOptionalNodeProperty("exclamationToken"); if (questionToken != null) { @@ -1902,15 +1919,15 @@ private J.VariableDeclarations visitPropertyDeclaration(TSCNode node) { Space beforeColon = sourceBefore(TSCSyntaxKind.ColonToken); markers = markers.addIfAbsent(new TypeReferencePrefix(randomId(), beforeColon)); - TSCNode type = node.getNodeProperty("type"); - typeTree = (TypeTree) visitNode(type); - name = name.withType(typeMapping.type(type)); + typeTree = (TypeTree) visitNode(typeNode); + name = name.withType(typeMapping.type(typeNode)); } JLeftPadded initializer; - if (node.hasProperty("initializer")) { + TSCNode initNode = node.getOptionalNodeProperty("initializer"); + if (initNode != null) { Space beforeEquals = sourceBefore(TSCSyntaxKind.EqualsToken); - J init = visitNode(node.getNodeProperty("initializer")); + J init = visitNode(initNode); if (init != null && !(init instanceof Expression)) { init = new JS.StatementExpression(randomId(), (Statement) init); } @@ -1969,7 +1986,8 @@ private J visitPropertyAssignment(TSCNode node) { // The initializer on a property declaration is the type reference. // { x : 1 } JLeftPadded initializer; - if (node.hasProperty("initializer")) { + TSCNode initNode = node.getOptionalNodeProperty("initializer"); + if (initNode != null) { TSCNode questionToken = node.getOptionalNodeProperty("questionToken"); TSCNode exclamationToken = node.getOptionalNodeProperty("exclamationToken"); if (questionToken != null) { @@ -1979,7 +1997,7 @@ private J visitPropertyAssignment(TSCNode node) { } Space beforeEquals = sourceBefore(TSCSyntaxKind.ColonToken); - J init = visitNode(node.getNodeProperty("initializer")); + J init = visitNode(initNode); if (init != null && !(init instanceof Expression)) { init = new JS.StatementExpression(randomId(), (Statement) init); } @@ -2221,8 +2239,9 @@ private J.Try visitTryStatement(TSCNode node) { ); JLeftPadded finallyBlock = null; - if (node.hasProperty("finallyBlock")) { - finallyBlock = padLeft(sourceBefore(TSCSyntaxKind.FinallyKeyword), (J.Block) visitNode(node.getNodeProperty("finallyBlock"))); + TSCNode finallyBlockNode = node.getOptionalNodeProperty("finallyBlock"); + if (finallyBlockNode != null) { + finallyBlock = padLeft(sourceBefore(TSCSyntaxKind.FinallyKeyword), (J.Block) visitNode(finallyBlockNode)); } return new J.Try( @@ -2342,13 +2361,21 @@ private J.TypeParameter visitTypeParameter(TSCNode node) { ); } - JContainer bounds = !node.hasProperty("constraint") ? null : - JContainer.build( - sourceBefore(TSCSyntaxKind.ExtendsKeyword), - convertAll(node.getNodeProperty("constraint").syntaxKind() == TSCSyntaxKind.IntersectionType ? - node.getNodeProperty("constraint").getNodeListProperty("types") : - singletonList(node.getNodeProperty("constraint")), t -> sourceBefore(TSCSyntaxKind.AmpersandToken), noDelim, true), - Markers.EMPTY); + TSCNode constraint = node.getOptionalNodeProperty("constraint"); + JContainer bounds; + if (constraint == null) { + bounds = null; + } else if (constraint.syntaxKind() == TSCSyntaxKind.IntersectionType) { + bounds = JContainer.build( + sourceBefore(TSCSyntaxKind.ExtendsKeyword), + convertAll(constraint.getNodeListProperty("types"), t -> sourceBefore(TSCSyntaxKind.AmpersandToken), noDelim, true), + Markers.EMPTY); + } else { + bounds = JContainer.build( + sourceBefore(TSCSyntaxKind.ExtendsKeyword), + convertAll(singletonList(constraint), t -> sourceBefore(TSCSyntaxKind.AmpersandToken), noDelim, true), + Markers.EMPTY); + } return new J.TypeParameter( randomId(), @@ -2376,15 +2403,16 @@ private J.ParameterizedType visitTypeQuery(TSCNode node) { typeMapping.type(node) ); + TSCNodeList typeArguments = node.getOptionalNodeListProperty("typeArguments"); return new J.ParameterizedType( randomId(), prefix, Markers.EMPTY, op, - !node.hasProperty("typeArguments") ? null : + typeArguments == null ? null : mapContainer( TSCSyntaxKind.LessThanToken, - node.getNodeListProperty("typeArguments"), + typeArguments, TSCSyntaxKind.CommaToken, TSCSyntaxKind.GreaterThanToken, t -> (Expression) visitNode(t), @@ -2395,15 +2423,16 @@ private J.ParameterizedType visitTypeQuery(TSCNode node) { } private J.ParameterizedType visitTypeReference(TSCNode node) { + TSCNodeList typeArguments = node.getOptionalNodeListProperty("typeArguments"); return new J.ParameterizedType( randomId(), whitespace(), Markers.EMPTY, (NameTree) visitNode(node.getNodeProperty("typeName")), - !node.hasProperty("typeArguments") ? null : + typeArguments == null ? null : mapContainer( TSCSyntaxKind.LessThanToken, - node.getNodeListProperty("typeArguments"), + typeArguments, TSCSyntaxKind.CommaToken, TSCSyntaxKind.GreaterThanToken, t -> (Expression) visitNode(t), @@ -2515,7 +2544,8 @@ private J.VariableDeclarations visitVariableDeclaration(TSCNode node) { } Markers variableMarker = Markers.EMPTY; - if (node.hasProperty("type")) { + TSCNode type = node.getOptionalNodeProperty("type"); + if (type != null) { TSCNode questionToken = node.getOptionalNodeProperty("questionToken"); TSCNode exclamationToken = node.getOptionalNodeProperty("exclamationToken"); if (questionToken != null) { @@ -2526,17 +2556,18 @@ private J.VariableDeclarations visitVariableDeclaration(TSCNode node) { Space beforeColon = sourceBefore(TSCSyntaxKind.ColonToken); markers = markers.addIfAbsent(new TypeReferencePrefix(randomId(), beforeColon)); - typeTree = (TypeTree) visitNode(node.getNodeProperty("type")); + typeTree = (TypeTree) visitNode(type); } + TSCNode init = node.getOptionalNodeProperty("initializer"); J.VariableDeclarations.NamedVariable variable = new J.VariableDeclarations.NamedVariable( randomId(), variablePrefix, variableMarker, name, emptyList(), - node.hasProperty("initializer") ? + init != null ? padLeft(sourceBefore(TSCSyntaxKind.EqualsToken), - (Expression) Objects.requireNonNull(visitNode(node.getNodeProperty("initializer")))) : null, + (Expression) Objects.requireNonNull(visitNode(init))) : null, typeMapping.variableType(node) ); @@ -2593,8 +2624,8 @@ private J.VariableDeclarations visitVariableDeclarationList(TSCNode node) { List> namedVariables = emptyList(); TypeTree typeTree = null; - if (node.hasProperty("declarations")) { - List declarations = node.getNodeListProperty("declarations"); + TSCNodeList declarations = node.getOptionalNodeListProperty("declarations"); + if (declarations != null) { Set types = new HashSet<>(declarations.size()); namedVariables = new ArrayList<>(declarations.size()); for (int i = 0; i < declarations.size(); i++) { @@ -2609,7 +2640,8 @@ private J.VariableDeclarations visitVariableDeclarationList(TSCNode node) { implementMe(declaration); } - if (declaration.hasProperty("type")) { + TSCNode type = declaration.getOptionalNodeProperty("type"); + if (type != null) { TSCNode questionToken = node.getOptionalNodeProperty("questionToken"); TSCNode exclamationToken = node.getOptionalNodeProperty("exclamationToken"); if (questionToken != null) { @@ -2620,7 +2652,6 @@ private J.VariableDeclarations visitVariableDeclarationList(TSCNode node) { Space beforeColon = sourceBefore(TSCSyntaxKind.ColonToken); markers = markers.addIfAbsent(new TypeReferencePrefix(randomId(), beforeColon)); - TSCNode type = declaration.getNodeProperty("type"); typeTree = (TypeTree) visitNode(type); if (typeTree.getType() != null) { types.add(typeTree.getType()); @@ -2629,15 +2660,16 @@ private J.VariableDeclarations visitVariableDeclarationList(TSCNode node) { } } } + TSCNode init = declaration.getOptionalNodeProperty("initializer"); J.VariableDeclarations.NamedVariable variable = new J.VariableDeclarations.NamedVariable( randomId(), variablePrefix, Markers.EMPTY, name, emptyList(), - declaration.hasProperty("initializer") ? + init != null ? padLeft(sourceBefore(TSCSyntaxKind.EqualsToken), - (Expression) Objects.requireNonNull(visitNode(declaration.getNodeProperty("initializer")))) : null, + (Expression) Objects.requireNonNull(visitNode(init))) : null, typeMapping.variableType(declaration) ); @@ -2724,10 +2756,10 @@ private J.VariableDeclarations visitVariableStatement(TSCNode node) { implementMe(declaration); } - if (declaration.hasProperty("type")) { + TSCNode type = declaration.getOptionalNodeProperty("type"); + if (type != null) { Space beforeColon = sourceBefore(TSCSyntaxKind.ColonToken); markers = markers.addIfAbsent(new TypeReferencePrefix(randomId(), beforeColon)); - TSCNode type = declaration.getNodeProperty("type"); typeTree = (TypeTree) visitNode(type); if (typeTree.getType() != null) { types.add(typeTree.getType()); @@ -3459,8 +3491,9 @@ private J mapVariableStatement(TSCNode node) { } private boolean sourceStartsWithAtCursor(String text) { - return source.hasProperty("text") && source.getStringProperty("text").length() > source.getText().length() ? - source.getStringProperty("text").startsWith(text, getCursor()) : source.getText().startsWith(text, getCursor()); + String propertyText = source.getOptionalStringProperty("text"); + return propertyText != null && propertyText.length() > source.getText().length() ? + propertyText.startsWith(text, getCursor()) : source.getText().startsWith(text, getCursor()); } private Space sourceBefore(TSCSyntaxKind syntaxKind) {