Skip to content

Commit ea1e196

Browse files
Alexey AndreevAlexey Andreev
authored andcommitted
JS: remove unused imports from generated JS. Fix KT-14748
1 parent cf89e24 commit ea1e196

File tree

6 files changed

+67
-1
lines changed

6 files changed

+67
-1
lines changed

js/js.dart-ast/src/com/google/dart/compiler/backend/js/ast/metadata/metadataProperties.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ var JsNameRef.coroutineController by MetadataProperty(default = false)
105105

106106
var JsFunction.continuationInterfaceRef: JsExpression? by MetadataProperty(default = null)
107107

108+
var JsName.imported by MetadataProperty(default = false)
109+
108110
enum class TypeCheck {
109111
TYPEOF,
110112
INSTANCEOF,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2010-2016 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.kotlin.js.inline.clean
18+
19+
import com.google.dart.compiler.backend.js.ast.*
20+
import com.google.dart.compiler.backend.js.ast.metadata.imported
21+
22+
fun removeUnusedImports(root: JsNode) {
23+
val collector = UsedImportsCollector()
24+
root.accept(collector)
25+
NodeRemover(JsVars::class.java) { statement ->
26+
if (statement.vars.size == 1) {
27+
val name = statement.vars[0].name
28+
name.imported && name !in collector.usedImports
29+
}
30+
else {
31+
false
32+
}
33+
}.accept(root)
34+
}
35+
36+
private class UsedImportsCollector : RecursiveJsVisitor() {
37+
val usedImports = mutableSetOf<JsName>()
38+
39+
override fun visitNameRef(nameRef: JsNameRef) {
40+
val name = nameRef.name
41+
if (name != null && name.imported) {
42+
usedImports += name
43+
}
44+
super.visitNameRef(nameRef)
45+
}
46+
}

js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4562,6 +4562,12 @@ public void testAllFilesPresentInInlineSizeReduction() throws Exception {
45624562
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("js/js.translator/testData/box/inlineSizeReduction"), Pattern.compile("^([^_](.+))\\.kt$"), TargetBackend.ANY, true);
45634563
}
45644564

4565+
@TestMetadata("inlineImportCleanup.kt")
4566+
public void testInlineImportCleanup() throws Exception {
4567+
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/inlineSizeReduction/inlineImportCleanup.kt");
4568+
doTest(fileName);
4569+
}
4570+
45654571
@TestMetadata("inlineLambdaCleanup.kt")
45664572
public void testInlineLambdaCleanup() throws Exception {
45674573
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/inlineSizeReduction/inlineLambdaCleanup.kt");

js/js.translator/src/org/jetbrains/kotlin/js/facade/K2JSTranslator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jetbrains.kotlin.js.coroutine.CoroutineTransformer;
2727
import org.jetbrains.kotlin.js.facade.exceptions.TranslationException;
2828
import org.jetbrains.kotlin.js.inline.JsInliner;
29+
import org.jetbrains.kotlin.js.inline.clean.RemoveUnusedImportsKt;
2930
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
3031
import org.jetbrains.kotlin.js.translate.general.Translation;
3132
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus;
@@ -88,6 +89,7 @@ public TranslationResult translate(
8889

8990
CoroutineTransformer coroutineTransformer = new CoroutineTransformer(program);
9091
coroutineTransformer.accept(program);
92+
RemoveUnusedImportsKt.removeUnusedImports(program);
9193
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
9294
if (hasError(diagnostics)) return new TranslationResult.Fail(diagnostics);
9395

js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import java.util.*;
4949

5050
import static org.jetbrains.kotlin.js.config.LibrarySourcesConfig.UNKNOWN_EXTERNAL_MODULE_NAME;
51-
import static org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils.getNameForAnnotatedObject;
5251
import static org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils.isLibraryObject;
5352
import static org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils.isNativeObject;
5453
import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.pureFqn;
@@ -403,6 +402,7 @@ public JsName importDeclaration(@NotNull String suggestedName, @NotNull JsExpres
403402
// TODO: remove prefix when problem with scopes is solved
404403

405404
JsName result = rootFunction.getScope().declareFreshName("imported$" + suggestedName);
405+
MetadataProperties.setImported(result, true);
406406
importStatements.add(JsAstUtils.newVar(result, declaration));
407407
return result;
408408
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MODULE: lib
2+
// FILE: lib.kt
3+
package lib
4+
5+
inline fun foo(f: () -> String) = f()
6+
7+
// MODULE: main(lib)
8+
// FILE: main.kt
9+
// PROPERTY_NOT_READ_FROM: foo_6r51u9$
10+
fun box() = lib.foo { "OK" }

0 commit comments

Comments
 (0)