From 4072c7d1ae224356de07d5bb59c2ff934cfcd279 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Wed, 29 Jun 2022 23:56:16 +0200 Subject: [PATCH] Extracting typescript into separate module (#846) --- .github/CODEOWNERS | 25 ++--- .github/dependabot.yml | 2 +- cpg-console/build.gradle.kts | 3 - cpg-core/build.gradle.kts | 42 --------- .../aisec/cpg/graph/TypeManager.java | 13 ++- cpg-language-typescript/build.gradle.kts | 94 +++++++++++++++++++ .../typescript/DeclarationHandler.kt | 0 .../frontends/typescript/ExpressionHandler.kt | 0 .../frontends/typescript/StatementHandler.kt | 0 .../cpg/frontends/typescript/TypeHandler.kt | 0 .../typescript/TypeScriptLanguageFrontend.kt | 0 .../src/main/nodejs/.gitignore | 0 .../src/main/nodejs/package.json | 0 .../src/main/nodejs/src/parser.ts | 0 .../src/main/nodejs/tsconfig.json | 0 .../src/main/nodejs/webpack.config.js | 0 .../src/main/nodejs/yarn.lock | 0 .../TypescriptLanguageFrontendTest.kt | 2 +- .../test/resources/typescript/component.tsx | 0 .../test/resources/typescript/decorator.ts | 0 .../src/test/resources/typescript/fetch.ts | 0 .../typescript/function-component.tsx | 0 .../src/test/resources/typescript/function.js | 0 .../src/test/resources/typescript/function.ts | 0 .../src/test/resources/typescript/lambda.js | 0 .../src/test/resources/typescript/simple.jsx | 0 cpg-neo4j/build.gradle.kts | 1 + settings.gradle.kts | 1 + 28 files changed, 122 insertions(+), 61 deletions(-) create mode 100644 cpg-language-typescript/build.gradle.kts rename {cpg-core/src/main/java => cpg-language-typescript/src/main/kotlin}/de/fraunhofer/aisec/cpg/frontends/typescript/DeclarationHandler.kt (100%) rename {cpg-core/src/main/java => cpg-language-typescript/src/main/kotlin}/de/fraunhofer/aisec/cpg/frontends/typescript/ExpressionHandler.kt (100%) rename {cpg-core/src/main/java => cpg-language-typescript/src/main/kotlin}/de/fraunhofer/aisec/cpg/frontends/typescript/StatementHandler.kt (100%) rename {cpg-core/src/main/java => cpg-language-typescript/src/main/kotlin}/de/fraunhofer/aisec/cpg/frontends/typescript/TypeHandler.kt (100%) rename {cpg-core/src/main/java => cpg-language-typescript/src/main/kotlin}/de/fraunhofer/aisec/cpg/frontends/typescript/TypeScriptLanguageFrontend.kt (100%) rename {cpg-core => cpg-language-typescript}/src/main/nodejs/.gitignore (100%) rename {cpg-core => cpg-language-typescript}/src/main/nodejs/package.json (100%) rename {cpg-core => cpg-language-typescript}/src/main/nodejs/src/parser.ts (100%) rename {cpg-core => cpg-language-typescript}/src/main/nodejs/tsconfig.json (100%) rename {cpg-core => cpg-language-typescript}/src/main/nodejs/webpack.config.js (100%) rename {cpg-core => cpg-language-typescript}/src/main/nodejs/yarn.lock (100%) rename {cpg-core => cpg-language-typescript}/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/TypescriptLanguageFrontendTest.kt (99%) rename {cpg-core => cpg-language-typescript}/src/test/resources/typescript/component.tsx (100%) rename {cpg-core => cpg-language-typescript}/src/test/resources/typescript/decorator.ts (100%) rename {cpg-core => cpg-language-typescript}/src/test/resources/typescript/fetch.ts (100%) rename {cpg-core => cpg-language-typescript}/src/test/resources/typescript/function-component.tsx (100%) rename {cpg-core => cpg-language-typescript}/src/test/resources/typescript/function.js (100%) rename {cpg-core => cpg-language-typescript}/src/test/resources/typescript/function.ts (100%) rename {cpg-core => cpg-language-typescript}/src/test/resources/typescript/lambda.js (100%) rename {cpg-core => cpg-language-typescript}/src/test/resources/typescript/simple.jsx (100%) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 7579b0d485..0a6c6d4bba 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,16 +1,19 @@ -* @konradweiss @oxisto +* @konradweiss @oxisto -*.go @oxisto -cpg-language-go @oxisto +*.go @oxisto +cpg-language-go @oxisto -*.py @maximiliankaul -cpg-language-python @maximiliankaul +*.ts @oxisto +cpg-language-typescript @oxisto -*.ll @KuechA -cpg-language-llvm @KuechA -cpg-analysis @KuechA +*.py @maximiliankaul +cpg-language-python @maximiliankaul -cpg-neo4j @peckto +*.ll @KuechA +cpg-language-llvm @KuechA +cpg-analysis @KuechA -build.gradle.kts @oxisto -.github @oxisto +cpg-neo4j @peckto + +build.gradle.kts @oxisto +.github @oxisto diff --git a/.github/dependabot.yml b/.github/dependabot.yml index a86cf406b3..c09ba87573 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -17,7 +17,7 @@ updates: - dependency-name: "*" update-types: ["version-update:semver-patch"] - package-ecosystem: "npm" - directory: "/cpg-core/src/main/nodejs" + directory: "/cpg-language-typescript/src/main/nodejs" schedule: interval: "daily" labels: diff --git a/cpg-console/build.gradle.kts b/cpg-console/build.gradle.kts index 34623bdfb7..6be15b43b4 100644 --- a/cpg-console/build.gradle.kts +++ b/cpg-console/build.gradle.kts @@ -64,9 +64,6 @@ dependencies { // CPG api(project(":cpg-core")) api(project(":cpg-analysis")) - api(project(":cpg-language-llvm")) - api(project(":cpg-language-python")) - api(project(":cpg-language-go")) api(project(":cpg-neo4j")) implementation("org.apache.logging.log4j:log4j-slf4j18-impl:2.17.2") diff --git a/cpg-core/build.gradle.kts b/cpg-core/build.gradle.kts index 6d7ea9a4f1..a981f97826 100644 --- a/cpg-core/build.gradle.kts +++ b/cpg-core/build.gradle.kts @@ -23,14 +23,10 @@ * \______/ \__| \______/ * */ -import com.github.gradle.node.yarn.task.YarnTask - plugins { `java-library` `java-test-fixtures` signing - - id("com.github.node-gradle.node") version "3.4.0" } publishing { @@ -53,48 +49,10 @@ tasks.named("test") { if (!project.hasProperty("experimental")) { excludeTags("experimental") } - - if (!project.hasProperty("experimentalTypeScript")) { - excludeTags("experimentalTypeScript") - } } maxHeapSize = "4048m" } -node { - download.set(findProperty("nodeDownload")?.toString()?.toBoolean() ?: false) - version.set("16.4.2") -} - -val yarnInstall by tasks.registering(YarnTask::class) { - inputs.file("src/main/nodejs/package.json").withPathSensitivity(PathSensitivity.RELATIVE) - inputs.file("src/main/nodejs/yarn.lock").withPathSensitivity(PathSensitivity.RELATIVE) - outputs.dir("src/main/nodejs/node_modules") - outputs.cacheIf { true } - - workingDir.set(file("src/main/nodejs")) - yarnCommand.set(listOf("install", "--ignore-optional")) -} - -val yarnBuild by tasks.registering(YarnTask::class) { - inputs.file("src/main/nodejs/package.json").withPathSensitivity(PathSensitivity.RELATIVE) - inputs.file("src/main/nodejs/yarn.lock").withPathSensitivity(PathSensitivity.RELATIVE) - inputs.dir("src/main/nodejs/src").withPathSensitivity(PathSensitivity.RELATIVE) - outputs.dir("build/resources/main/nodejs") - outputs.cacheIf { true } - - workingDir.set(file("src/main/nodejs")) - yarnCommand.set(listOf("bundle")) - - dependsOn(yarnInstall) -} - -if (project.hasProperty("experimentalTypeScript")) { - tasks.processResources { - dependsOn(yarnBuild) - } -} - dependencies { api("org.apache.commons:commons-lang3:3.12.0") diff --git a/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/graph/TypeManager.java b/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/graph/TypeManager.java index d52eea98a6..9830dbc3e4 100644 --- a/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/graph/TypeManager.java +++ b/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/graph/TypeManager.java @@ -28,7 +28,6 @@ import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend; import de.fraunhofer.aisec.cpg.frontends.cpp.CXXLanguageFrontend; import de.fraunhofer.aisec.cpg.frontends.java.JavaLanguageFrontend; -import de.fraunhofer.aisec.cpg.frontends.typescript.TypeScriptLanguageFrontend; import de.fraunhofer.aisec.cpg.graph.declarations.Declaration; import de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration; import de.fraunhofer.aisec.cpg.graph.declarations.TemplateDeclaration; @@ -57,11 +56,11 @@ public class TypeManager { private static Class llvmClass = null; private static Class pythonClass = null; private static Class goClass = null; + private static Class typescriptClass = null; static { try { llvmClass = Class.forName("de.fraunhofer.aisec.cpg.frontends.llvm.LLVMIRLanguageFrontend"); - } catch (ClassNotFoundException | ExceptionInInitializerError ignored) { log.info("LLVM frontend not loaded."); } @@ -78,6 +77,12 @@ public class TypeManager { } catch (LinkageError ex) { log.error("Go frontend was found, but could not be loaded", ex); } + try { + typescriptClass = + Class.forName("de.fraunhofer.aisec.cpg.frontends.typescript.TypeScriptLanguageFrontend"); + } catch (ClassNotFoundException | ExceptionInInitializerError ignored) { + log.info("TypeScript frontend not loaded."); + } } private static final List primitiveTypeNames = @@ -600,7 +605,9 @@ public Language getLanguage() { && pythonClass != null && pythonClass.isAssignableFrom(frontend.getClass())) { return Language.PYTHON; - } else if (frontend instanceof TypeScriptLanguageFrontend) { + } else if (frontend != null + && typescriptClass != null + && typescriptClass.isAssignableFrom(frontend.getClass())) { return Language.TYPESCRIPT; } else if (frontend != null && llvmClass != null diff --git a/cpg-language-typescript/build.gradle.kts b/cpg-language-typescript/build.gradle.kts new file mode 100644 index 0000000000..5b239c2791 --- /dev/null +++ b/cpg-language-typescript/build.gradle.kts @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2022, Fraunhofer AISEC. All rights reserved. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * $$$$$$\ $$$$$$$\ $$$$$$\ + * $$ __$$\ $$ __$$\ $$ __$$\ + * $$ / \__|$$ | $$ |$$ / \__| + * $$ | $$$$$$$ |$$ |$$$$\ + * $$ | $$ ____/ $$ |\_$$ | + * $$ | $$\ $$ | $$ | $$ | + * \$$$$$ |$$ | \$$$$$ | + * \______/ \__| \______/ + * + */ +import com.github.gradle.node.yarn.task.YarnTask + +plugins { + `java-library` + + id("com.github.node-gradle.node") version "3.4.0" +} + +publishing { + publications { + named("cpg-language-typescript") { + pom { + artifactId = "cpg-language-typescript" + name.set("Code Property Graph - JavaScript/TypeScript Frontend") + description.set("A JavaScript/TypeScript language frontend for the CPG") + } + } + } +} + +dependencies { + api(project(":cpg-core")) + + testImplementation(testFixtures(project(":cpg-core"))) +} + + +node { + download.set(findProperty("nodeDownload")?.toString()?.toBoolean() ?: false) + version.set("16.4.2") +} + +val yarnInstall by tasks.registering(YarnTask::class) { + inputs.file("src/main/nodejs/package.json").withPathSensitivity(PathSensitivity.RELATIVE) + inputs.file("src/main/nodejs/yarn.lock").withPathSensitivity(PathSensitivity.RELATIVE) + outputs.dir("src/main/nodejs/node_modules") + outputs.cacheIf { true } + + workingDir.set(file("src/main/nodejs")) + yarnCommand.set(listOf("install", "--ignore-optional")) +} + +val yarnBuild by tasks.registering(YarnTask::class) { + inputs.file("src/main/nodejs/package.json").withPathSensitivity(PathSensitivity.RELATIVE) + inputs.file("src/main/nodejs/yarn.lock").withPathSensitivity(PathSensitivity.RELATIVE) + inputs.dir("src/main/nodejs/src").withPathSensitivity(PathSensitivity.RELATIVE) + outputs.dir("build/resources/main/nodejs") + outputs.cacheIf { true } + + workingDir.set(file("src/main/nodejs")) + yarnCommand.set(listOf("bundle")) + + dependsOn(yarnInstall) +} + +if (project.hasProperty("experimentalTypeScript")) { + tasks.processResources { + dependsOn(yarnBuild) + } +} + +tasks.named("test") { + useJUnitPlatform { + if (!project.hasProperty("experimentalTypeScript")) { + excludeTags("experimentalTypeScript") + } + } + maxHeapSize = "4048m" +} \ No newline at end of file diff --git a/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/typescript/DeclarationHandler.kt b/cpg-language-typescript/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/DeclarationHandler.kt similarity index 100% rename from cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/typescript/DeclarationHandler.kt rename to cpg-language-typescript/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/DeclarationHandler.kt diff --git a/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/typescript/ExpressionHandler.kt b/cpg-language-typescript/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/ExpressionHandler.kt similarity index 100% rename from cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/typescript/ExpressionHandler.kt rename to cpg-language-typescript/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/ExpressionHandler.kt diff --git a/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/typescript/StatementHandler.kt b/cpg-language-typescript/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/StatementHandler.kt similarity index 100% rename from cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/typescript/StatementHandler.kt rename to cpg-language-typescript/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/StatementHandler.kt diff --git a/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/typescript/TypeHandler.kt b/cpg-language-typescript/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/TypeHandler.kt similarity index 100% rename from cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/typescript/TypeHandler.kt rename to cpg-language-typescript/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/TypeHandler.kt diff --git a/cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/typescript/TypeScriptLanguageFrontend.kt b/cpg-language-typescript/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/TypeScriptLanguageFrontend.kt similarity index 100% rename from cpg-core/src/main/java/de/fraunhofer/aisec/cpg/frontends/typescript/TypeScriptLanguageFrontend.kt rename to cpg-language-typescript/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/TypeScriptLanguageFrontend.kt diff --git a/cpg-core/src/main/nodejs/.gitignore b/cpg-language-typescript/src/main/nodejs/.gitignore similarity index 100% rename from cpg-core/src/main/nodejs/.gitignore rename to cpg-language-typescript/src/main/nodejs/.gitignore diff --git a/cpg-core/src/main/nodejs/package.json b/cpg-language-typescript/src/main/nodejs/package.json similarity index 100% rename from cpg-core/src/main/nodejs/package.json rename to cpg-language-typescript/src/main/nodejs/package.json diff --git a/cpg-core/src/main/nodejs/src/parser.ts b/cpg-language-typescript/src/main/nodejs/src/parser.ts similarity index 100% rename from cpg-core/src/main/nodejs/src/parser.ts rename to cpg-language-typescript/src/main/nodejs/src/parser.ts diff --git a/cpg-core/src/main/nodejs/tsconfig.json b/cpg-language-typescript/src/main/nodejs/tsconfig.json similarity index 100% rename from cpg-core/src/main/nodejs/tsconfig.json rename to cpg-language-typescript/src/main/nodejs/tsconfig.json diff --git a/cpg-core/src/main/nodejs/webpack.config.js b/cpg-language-typescript/src/main/nodejs/webpack.config.js similarity index 100% rename from cpg-core/src/main/nodejs/webpack.config.js rename to cpg-language-typescript/src/main/nodejs/webpack.config.js diff --git a/cpg-core/src/main/nodejs/yarn.lock b/cpg-language-typescript/src/main/nodejs/yarn.lock similarity index 100% rename from cpg-core/src/main/nodejs/yarn.lock rename to cpg-language-typescript/src/main/nodejs/yarn.lock diff --git a/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/TypescriptLanguageFrontendTest.kt b/cpg-language-typescript/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/TypescriptLanguageFrontendTest.kt similarity index 99% rename from cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/TypescriptLanguageFrontendTest.kt rename to cpg-language-typescript/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/TypescriptLanguageFrontendTest.kt index 0f640d3a19..a6052ea11b 100644 --- a/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/TypescriptLanguageFrontendTest.kt +++ b/cpg-language-typescript/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/typescript/TypescriptLanguageFrontendTest.kt @@ -45,7 +45,7 @@ import org.junit.jupiter.api.Tag @Tag("experimentalTypeScript") @ExperimentalTypeScript -class TypescriptLanguageFrontendTest { +class TypeScriptLanguageFrontendTest { @Test fun testFunction() { diff --git a/cpg-core/src/test/resources/typescript/component.tsx b/cpg-language-typescript/src/test/resources/typescript/component.tsx similarity index 100% rename from cpg-core/src/test/resources/typescript/component.tsx rename to cpg-language-typescript/src/test/resources/typescript/component.tsx diff --git a/cpg-core/src/test/resources/typescript/decorator.ts b/cpg-language-typescript/src/test/resources/typescript/decorator.ts similarity index 100% rename from cpg-core/src/test/resources/typescript/decorator.ts rename to cpg-language-typescript/src/test/resources/typescript/decorator.ts diff --git a/cpg-core/src/test/resources/typescript/fetch.ts b/cpg-language-typescript/src/test/resources/typescript/fetch.ts similarity index 100% rename from cpg-core/src/test/resources/typescript/fetch.ts rename to cpg-language-typescript/src/test/resources/typescript/fetch.ts diff --git a/cpg-core/src/test/resources/typescript/function-component.tsx b/cpg-language-typescript/src/test/resources/typescript/function-component.tsx similarity index 100% rename from cpg-core/src/test/resources/typescript/function-component.tsx rename to cpg-language-typescript/src/test/resources/typescript/function-component.tsx diff --git a/cpg-core/src/test/resources/typescript/function.js b/cpg-language-typescript/src/test/resources/typescript/function.js similarity index 100% rename from cpg-core/src/test/resources/typescript/function.js rename to cpg-language-typescript/src/test/resources/typescript/function.js diff --git a/cpg-core/src/test/resources/typescript/function.ts b/cpg-language-typescript/src/test/resources/typescript/function.ts similarity index 100% rename from cpg-core/src/test/resources/typescript/function.ts rename to cpg-language-typescript/src/test/resources/typescript/function.ts diff --git a/cpg-core/src/test/resources/typescript/lambda.js b/cpg-language-typescript/src/test/resources/typescript/lambda.js similarity index 100% rename from cpg-core/src/test/resources/typescript/lambda.js rename to cpg-language-typescript/src/test/resources/typescript/lambda.js diff --git a/cpg-core/src/test/resources/typescript/simple.jsx b/cpg-language-typescript/src/test/resources/typescript/simple.jsx similarity index 100% rename from cpg-core/src/test/resources/typescript/simple.jsx rename to cpg-language-typescript/src/test/resources/typescript/simple.jsx diff --git a/cpg-neo4j/build.gradle.kts b/cpg-neo4j/build.gradle.kts index 49540f19ba..668a88d9c5 100644 --- a/cpg-neo4j/build.gradle.kts +++ b/cpg-neo4j/build.gradle.kts @@ -72,6 +72,7 @@ dependencies { api(project(":cpg-language-llvm")) api(project(":cpg-language-python")) api(project(":cpg-language-go")) + api(project(":cpg-language-typescript")) implementation("org.apache.logging.log4j:log4j-slf4j18-impl:2.17.2") diff --git a/settings.gradle.kts b/settings.gradle.kts index ddddaa5006..8873b355ed 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -9,6 +9,7 @@ include(":cpg-neo4j") include(":cpg-language-llvm") include(":cpg-language-python") include(":cpg-language-go") +include(":cpg-language-typescript") include(":cpg-console") gradleEnterprise {