Skip to content

Commit

Permalink
Improve performance of type attribution
Browse files Browse the repository at this point in the history
  • Loading branch information
ammachado authored and knutwannheden committed Oct 22, 2023
1 parent 7d4d532 commit 0f42b2f
Show file tree
Hide file tree
Showing 3 changed files with 2,499 additions and 17 deletions.
56 changes: 39 additions & 17 deletions src/main/java/org/openrewrite/TypeScriptSignatureBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
import org.openrewrite.javascript.internal.tsc.generated.TSCTypeFlag;
import org.openrewrite.javascript.tree.TsType;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
import java.util.*;

import static org.openrewrite.javascript.internal.tsc.TSCProgramContext.CompilerBridgeSourceKind.ApplicationCode;

Expand All @@ -39,47 +36,72 @@ public class TypeScriptSignatureBuilder implements JavaTypeSignatureBuilder {
@Nullable
Set<String> typeVariableNameStack;

Map<TSCNode, String> signatures = new IdentityHashMap<>();

@Override
public String signature(@Nullable Object object) {
if (object == null) {
return "{undefined}";
}

TSCNode node = (TSCNode) object;
String cached = signatures.get(node);
if (cached != null) {
return cached;
}

switch (node.syntaxKind()) {
case SourceFile:
return mapSourceFileFqn((TSCNode.SourceFile) node);
cached = mapSourceFileFqn((TSCNode.SourceFile) node);
break;
case ClassDeclaration:
case EnumDeclaration:
case InterfaceDeclaration:
return node.hasProperty("typeParameters") && !node.getNodeListProperty("typeParameters").isEmpty() ?
cached = node.hasProperty("typeParameters") && !node.getNodeListProperty("typeParameters").isEmpty() ?
parameterizedSignature(node) : classSignature(node);
break;
case ArrayType:
return arraySignature(node);
cached = arraySignature(node);
break;
case EnumMember:
return mapEnumMember(node);
cached = mapEnumMember(node);
break;
case Identifier:
return mapIdentifier(node);
cached = mapIdentifier(node);
break;
case Parameter:
return mapParameter(node);
cached = mapParameter(node);
break;
case QualifiedName:
return mapQualifiedName(node);
cached = mapQualifiedName(node);
break;
case ThisKeyword:
return mapThis(node);
cached = mapThis(node);
break;
case TypeOperator:
return mapTypeOperator(node);
cached = mapTypeOperator(node);
break;
case TypeParameter:
return genericSignature(node);
cached = genericSignature(node);
break;
case ExpressionWithTypeArguments:
case TypeReference:
case TypeQuery:
return mapTypeReference(node);
cached = mapTypeReference(node);
break;
case UnionType:
return TsType.Union.getFullyQualifiedName();
cached = TsType.Union.getFullyQualifiedName();
break;
case PropertyDeclaration:
case VariableDeclaration:
return variableSignature(node);
cached = variableSignature(node);
break;
}
if (cached != null) {
signatures.put(node, cached);
return cached;
}

TSCType type = node.getTypeChecker().getTypeAtLocation(node);
return mapType(type);
}
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/openrewrite/javascript/tree/ParserPerfTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.javascript.tree;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.test.RewriteTest;

class ParserPerfTest implements RewriteTest {

// from https://github.com/apache/camel/blob/main/components/camel-cometd/src/test/resources/webapp/dojo/org/cometd.js
@Language("typescript")
private static final String cometd = StringUtils.readFully(ParserPerfTest.class.getResourceAsStream("/perf/cometd.js"));

@Test
void tryParseCometd() {
rewriteRun(ParserTest.javaScript(cometd));
}
}
Loading

0 comments on commit 0f42b2f

Please sign in to comment.