js compiler: module dependency
This commit is contained in:
@@ -151,10 +151,10 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
|
|||||||
@NotNull
|
@NotNull
|
||||||
private static Config getConfig(@NotNull K2JSCompilerArguments arguments, @NotNull Project project) {
|
private static Config getConfig(@NotNull K2JSCompilerArguments arguments, @NotNull Project project) {
|
||||||
EcmaVersion ecmaVersion = EcmaVersion.fromString(arguments.target);
|
EcmaVersion ecmaVersion = EcmaVersion.fromString(arguments.target);
|
||||||
if (arguments.libzip == null) {
|
if (arguments.libraryFiles == null) {
|
||||||
// lets discover the JS library definitions on the classpath
|
// lets discover the JS library definitions on the classpath
|
||||||
return new ClassPathLibraryDefintionsConfig(project, ecmaVersion);
|
return new ClassPathLibraryDefintionsConfig(project, ecmaVersion);
|
||||||
}
|
}
|
||||||
return new ZippedLibrarySourcesConfig(project, arguments.libzip, ecmaVersion);
|
return new LibrarySourcesConfig(project, arguments.libraryFiles, ecmaVersion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,8 +36,8 @@ public class K2JSCompilerArguments extends CompilerArguments {
|
|||||||
public String outputFile;
|
public String outputFile;
|
||||||
|
|
||||||
//NOTE: may well be a subject to change soon
|
//NOTE: may well be a subject to change soon
|
||||||
@Argument(value = "libzip", description = "Path to zipped lib sources")
|
@Argument(value = "libraryFiles", description = "Path to zipped lib sources or kotlin files")
|
||||||
public String libzip;
|
public String[] libraryFiles;
|
||||||
|
|
||||||
@Argument(value = "srcdir", description = "Sources directory")
|
@Argument(value = "srcdir", description = "Sources directory")
|
||||||
public String srcdir;
|
public String srcdir;
|
||||||
|
|||||||
@@ -17,27 +17,29 @@
|
|||||||
package org.jetbrains.jet.plugin.compiler;
|
package org.jetbrains.jet.plugin.compiler;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import com.intellij.openapi.application.AccessToken;
|
||||||
import com.intellij.openapi.application.ApplicationManager;
|
import com.intellij.openapi.application.ApplicationManager;
|
||||||
import com.intellij.openapi.compiler.CompileContext;
|
import com.intellij.openapi.application.ReadAction;
|
||||||
import com.intellij.openapi.compiler.CompileScope;
|
import com.intellij.openapi.compiler.*;
|
||||||
import com.intellij.openapi.compiler.CompilerMessageCategory;
|
|
||||||
import com.intellij.openapi.compiler.TranslatingCompiler;
|
|
||||||
import com.intellij.openapi.module.Module;
|
import com.intellij.openapi.module.Module;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.roots.ModuleOrderEntry;
|
||||||
import com.intellij.openapi.roots.ModuleRootManager;
|
import com.intellij.openapi.roots.ModuleRootManager;
|
||||||
|
import com.intellij.openapi.roots.OrderEntry;
|
||||||
import com.intellij.openapi.util.Pair;
|
import com.intellij.openapi.util.Pair;
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
import com.intellij.util.ArrayUtil;
|
import com.intellij.util.ArrayUtil;
|
||||||
import com.intellij.util.Chunk;
|
import com.intellij.util.Chunk;
|
||||||
|
import com.intellij.util.StringBuilderSpinAllocator;
|
||||||
|
import gnu.trove.THashSet;
|
||||||
import jet.Function1;
|
import jet.Function1;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.plugin.JetFileType;
|
import org.jetbrains.jet.plugin.JetFileType;
|
||||||
import org.jetbrains.jet.plugin.k2jsrun.K2JSRunnerUtils;
|
|
||||||
import org.jetbrains.jet.plugin.project.JsModuleDetector;
|
import org.jetbrains.jet.plugin.project.JsModuleDetector;
|
||||||
|
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import static org.jetbrains.jet.plugin.compiler.CompilerUtils.invokeExecMethod;
|
import static org.jetbrains.jet.plugin.compiler.CompilerUtils.invokeExecMethod;
|
||||||
import static org.jetbrains.jet.plugin.compiler.CompilerUtils.outputCompilerMessagesAndHandleExitCode;
|
import static org.jetbrains.jet.plugin.compiler.CompilerUtils.outputCompilerMessagesAndHandleExitCode;
|
||||||
@@ -122,9 +124,9 @@ public final class K2JSCompiler implements TranslatingCompiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
VirtualFile outDir = context.getModuleOutputDirectory(module);
|
VirtualFile outDir = context.getModuleOutputDirectory(module);
|
||||||
String outFile = outDir == null ? null : K2JSRunnerUtils.constructPathToGeneratedFile(context.getProject(), outDir.getPath());
|
String outFile = outDir == null ? null : outDir.getPath() + "/" + module.getName() + ".js";
|
||||||
|
|
||||||
String[] commandLineArgs = constructArguments(context.getProject(), outFile, roots[0]);
|
String[] commandLineArgs = constructArguments(module, outFile, roots[0]);
|
||||||
Object rc = invokeExecMethod(environment, out, context, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler");
|
Object rc = invokeExecMethod(environment, out, context, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler");
|
||||||
|
|
||||||
if (outDir != null && !ApplicationManager.getApplication().isUnitTestMode()) {
|
if (outDir != null && !ApplicationManager.getApplication().isUnitTestMode()) {
|
||||||
@@ -134,20 +136,67 @@ public final class K2JSCompiler implements TranslatingCompiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static String[] constructArguments(@NotNull Project project, @Nullable String outFile, @NotNull VirtualFile srcDir) {
|
private static String[] constructArguments(@NotNull Module module, @Nullable String outFile, @NotNull VirtualFile srcDir) {
|
||||||
ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version");
|
ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version", "-mainCall", "mainWithArgs");
|
||||||
addPathToSourcesDir(args, srcDir);
|
addPathToSourcesDir(args, srcDir);
|
||||||
addOutputPath(outFile, args);
|
addOutputPath(outFile, args);
|
||||||
addLibLocationAndTarget(project, args);
|
addLibLocationAndTarget(module, args);
|
||||||
return ArrayUtil.toStringArray(args);
|
return ArrayUtil.toStringArray(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addLibLocationAndTarget(@NotNull Project project, @NotNull ArrayList<String> args) {
|
// we cannot use OrderEnumerator because it has critical bug — try https://gist.github.com/2953261, processor will never be called for module dependency
|
||||||
Pair<String, String> libLocationAndTarget = JsModuleDetector.getLibLocationAndTargetForProject(project);
|
private static void collectModuleDependencies(Module dependentModule, Set<Module> modules) {
|
||||||
if (libLocationAndTarget.first != null) {
|
for (OrderEntry entry : ModuleRootManager.getInstance(dependentModule).getOrderEntries()) {
|
||||||
args.add("-libzip");
|
if (entry instanceof ModuleOrderEntry) {
|
||||||
args.add(libLocationAndTarget.first);
|
ModuleOrderEntry moduleEntry = (ModuleOrderEntry) entry;
|
||||||
|
if (!moduleEntry.getScope().isForProductionCompile()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Module module = moduleEntry.getModule();
|
||||||
|
if (module == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (modules.add(module) && moduleEntry.isExported()) {
|
||||||
|
collectModuleDependencies(module, modules);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addLibLocationAndTarget(@NotNull Module module, @NotNull ArrayList<String> args) {
|
||||||
|
Pair<String[], String> libLocationAndTarget = JsModuleDetector.getLibLocationAndTargetForProject(module);
|
||||||
|
|
||||||
|
StringBuilder sb = StringBuilderSpinAllocator.alloc();
|
||||||
|
AccessToken token = ReadAction.start();
|
||||||
|
try {
|
||||||
|
THashSet<Module> modules = new THashSet<Module>();
|
||||||
|
collectModuleDependencies(module, modules);
|
||||||
|
if (!modules.isEmpty()) {
|
||||||
|
VirtualFile[] files = CompilerManager.getInstance(module.getProject()).createModulesCompileScope(
|
||||||
|
modules.toArray(new Module[modules.size()]), false).getFiles(JetFileType.INSTANCE, true);
|
||||||
|
for (VirtualFile file : files) {
|
||||||
|
sb.append(file.getPath()).append(',');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (libLocationAndTarget.first != null) {
|
||||||
|
for (String file : libLocationAndTarget.first) {
|
||||||
|
sb.append(file).append(',');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sb.length() > 0) {
|
||||||
|
args.add("-libraryFiles");
|
||||||
|
args.add(sb.substring(0, sb.length() - 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
token.finish();
|
||||||
|
StringBuilderSpinAllocator.dispose(sb);
|
||||||
|
}
|
||||||
|
|
||||||
if (libLocationAndTarget.second != null) {
|
if (libLocationAndTarget.second != null) {
|
||||||
args.add("-target");
|
args.add("-target");
|
||||||
args.add(libLocationAndTarget.second);
|
args.add(libLocationAndTarget.second);
|
||||||
|
|||||||
@@ -19,14 +19,14 @@ package org.jetbrains.jet.plugin.project;
|
|||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.k2js.config.EcmaVersion;
|
import org.jetbrains.k2js.config.EcmaVersion;
|
||||||
import org.jetbrains.k2js.config.ZippedLibrarySourcesConfig;
|
import org.jetbrains.k2js.config.LibrarySourcesConfig;
|
||||||
|
|
||||||
import static org.jetbrains.jet.plugin.project.JsModuleDetector.getLibLocationAndTargetForProject;
|
import static org.jetbrains.jet.plugin.project.JsModuleDetector.getLibLocationAndTargetForProject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
public final class IDEAConfig extends ZippedLibrarySourcesConfig {
|
public final class IDEAConfig extends LibrarySourcesConfig {
|
||||||
public IDEAConfig(@NotNull Project project) {
|
public IDEAConfig(@NotNull Project project) {
|
||||||
super(project, getLibLocationAndTargetForProject(project).first, EcmaVersion.defaultVersion());
|
super(project, getLibLocationAndTargetForProject(project).first, EcmaVersion.defaultVersion());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,15 +57,22 @@ public final class JsModuleDetector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static Pair<String, String> getLibLocationAndTargetForProject(@NotNull Project project) {
|
public static Pair<String[], String> getLibLocationAndTargetForProject(@NotNull Project project) {
|
||||||
Module module = getJSModule(project);
|
Module module = getJSModule(project);
|
||||||
if (module == null) {
|
if (module == null) {
|
||||||
return Pair.empty();
|
return Pair.empty();
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
return getLibLocationAndTargetForProject(module);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static Pair<String[], String> getLibLocationAndTargetForProject(@NotNull Module module) {
|
||||||
K2JSModuleComponent jsModuleComponent = K2JSModuleComponent.getInstance(module);
|
K2JSModuleComponent jsModuleComponent = K2JSModuleComponent.getInstance(module);
|
||||||
String pathToJavaScriptLibrary = jsModuleComponent.getPathToJavaScriptLibrary();
|
String pathToJavaScriptLibrary = jsModuleComponent.getPathToJavaScriptLibrary();
|
||||||
String basePath = ModuleRootManager.getInstance(module).getContentRoots()[0].getPath();
|
String basePath = ModuleRootManager.getInstance(module).getContentRoots()[0].getPath();
|
||||||
return Pair.create(basePath + pathToJavaScriptLibrary, jsModuleComponent.getEcmaVersion().toString());
|
return Pair.create(new String[] {basePath + pathToJavaScriptLibrary}, jsModuleComponent.getEcmaVersion().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|||||||
+37
-14
@@ -18,6 +18,7 @@ package org.jetbrains.k2js.config;
|
|||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
|
import com.intellij.openapi.util.Key;
|
||||||
import com.intellij.openapi.util.io.FileUtil;
|
import com.intellij.openapi.util.io.FileUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@@ -27,6 +28,7 @@ import org.jetbrains.k2js.utils.JetFileUtils;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -36,33 +38,53 @@ import java.util.zip.ZipFile;
|
|||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
public class ZippedLibrarySourcesConfig extends Config {
|
public class LibrarySourcesConfig extends Config {
|
||||||
@Nullable
|
public static final Key<Boolean> EXTERNAL_LIB = new Key<Boolean>("externalLib");
|
||||||
protected final String pathToLibZip;
|
|
||||||
|
|
||||||
public ZippedLibrarySourcesConfig(@NotNull Project project, @Nullable String pathToZip, @NotNull EcmaVersion ecmaVersion) {
|
@Nullable
|
||||||
|
private final String[] files;
|
||||||
|
|
||||||
|
public LibrarySourcesConfig(@NotNull Project project, @Nullable String[] files, @NotNull EcmaVersion ecmaVersion) {
|
||||||
super(project, ecmaVersion);
|
super(project, ecmaVersion);
|
||||||
pathToLibZip = pathToZip;
|
this.files = files;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public List<JetFile> generateLibFiles() {
|
public List<JetFile> generateLibFiles() {
|
||||||
if (pathToLibZip == null) {
|
if (files == null) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
File file = new File(pathToLibZip);
|
List<JetFile> jetFiles = new ArrayList<JetFile>();
|
||||||
ZipFile zipFile = new ZipFile(file);
|
for (String path : files) {
|
||||||
|
File file = new File(path);
|
||||||
try {
|
try {
|
||||||
return traverseArchive(zipFile);
|
String name = file.getName();
|
||||||
|
if (name.endsWith(".jar") || name.endsWith(".zip")) {
|
||||||
|
jetFiles.addAll(readZip(file));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
JetFile psiFile = JetFileUtils.createPsiFile(path, FileUtil.loadFile(file), getProject());
|
||||||
|
psiFile.putUserData(EXTERNAL_LIB, true);
|
||||||
|
jetFiles.add(psiFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
finally {
|
catch (IOException e) {
|
||||||
zipFile.close();
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
|
||||||
return Collections.emptyList();
|
return jetFiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<JetFile> readZip(File file) throws IOException {
|
||||||
|
ZipFile zipFile = new ZipFile(file);
|
||||||
|
try {
|
||||||
|
return traverseArchive(zipFile);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
zipFile.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,6 +98,7 @@ public class ZippedLibrarySourcesConfig extends Config {
|
|||||||
InputStream stream = file.getInputStream(entry);
|
InputStream stream = file.getInputStream(entry);
|
||||||
String text = FileUtil.loadTextAndClose(stream);
|
String text = FileUtil.loadTextAndClose(stream);
|
||||||
JetFile jetFile = JetFileUtils.createPsiFile(entry.getName(), text, getProject());
|
JetFile jetFile = JetFileUtils.createPsiFile(entry.getName(), text, getProject());
|
||||||
|
jetFile.putUserData(EXTERNAL_LIB, true);
|
||||||
result.add(jetFile);
|
result.add(jetFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,13 +96,11 @@ public final class K2JSTranslator {
|
|||||||
throws TranslationException {
|
throws TranslationException {
|
||||||
JetStandardLibrary.initialize(config.getProject());
|
JetStandardLibrary.initialize(config.getProject());
|
||||||
BindingContext bindingContext = AnalyzerFacadeForJS.analyzeFilesAndCheckErrors(filesToTranslate, config);
|
BindingContext bindingContext = AnalyzerFacadeForJS.analyzeFilesAndCheckErrors(filesToTranslate, config);
|
||||||
Collection<JetFile> files = AnalyzerFacadeForJS.withJsLibAdded(filesToTranslate, config);
|
return Translation.generateAst(bindingContext, filesToTranslate, mainCallParameters, config.getTarget(), rawStatements);
|
||||||
|
|
||||||
return Translation.generateAst(bindingContext, Lists.newArrayList(files), mainCallParameters, config.getTarget(), rawStatements);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Project getProject() {
|
private Project getProject() {
|
||||||
return config.getProject();
|
return config.getProject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -144,7 +144,7 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
|
|||||||
@NotNull
|
@NotNull
|
||||||
public List<JsPropertyInitializer> classDeclarationsForNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
|
public List<JsPropertyInitializer> classDeclarationsForNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
|
||||||
List<JsPropertyInitializer> result = Lists.newArrayList();
|
List<JsPropertyInitializer> result = Lists.newArrayList();
|
||||||
for (ClassDescriptor classDescriptor : getAllClassesDefinedInNamespace(namespaceDescriptor)) {
|
for (ClassDescriptor classDescriptor : getAllClassesDefinedInNamespace(namespaceDescriptor, bindingContext())) {
|
||||||
result.add(getClassNameToClassObject(classDescriptor));
|
result.add(getClassNameToClassObject(classDescriptor));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
+13
-17
@@ -31,6 +31,7 @@ import org.jetbrains.k2js.translate.utils.JsAstUtils;
|
|||||||
import org.jetbrains.k2js.translate.utils.JsDescriptorUtils;
|
import org.jetbrains.k2js.translate.utils.JsDescriptorUtils;
|
||||||
import org.jetbrains.k2js.translate.utils.TranslationUtils;
|
import org.jetbrains.k2js.translate.utils.TranslationUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@@ -64,33 +65,28 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
|||||||
private List<ClassDescriptor> getAllClasses() {
|
private List<ClassDescriptor> getAllClasses() {
|
||||||
List<ClassDescriptor> result = Lists.newArrayList();
|
List<ClassDescriptor> result = Lists.newArrayList();
|
||||||
for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
|
for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
|
||||||
result.addAll(getAllClassesDefinedInNamespace(namespaceDescriptor));
|
result.addAll(getAllClassesDefinedInNamespace(namespaceDescriptor, context().bindingContext()));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<JsStatement> translate() {
|
private List<JsStatement> translate() {
|
||||||
List<JsStatement> result = classesDeclarations();
|
List<JsStatement> result = new ArrayList<JsStatement>();
|
||||||
result.addAll(namespacesDeclarations());
|
classesDeclarations(result);
|
||||||
|
namespacesDeclarations(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
private void classesDeclarations(List<JsStatement> statements) {
|
||||||
private List<JsStatement> classesDeclarations() {
|
|
||||||
List<JsStatement> result = Lists.newArrayList();
|
|
||||||
classDeclarationTranslator.generateDeclarations();
|
classDeclarationTranslator.generateDeclarations();
|
||||||
result.add(classDeclarationTranslator.getDeclarationsStatement());
|
statements.add(classDeclarationTranslator.getDeclarationsStatement());
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
private void namespacesDeclarations(List<JsStatement> statements) {
|
||||||
private List<JsStatement> namespacesDeclarations() {
|
|
||||||
List<JsStatement> result = Lists.newArrayList();
|
|
||||||
List<NamespaceTranslator> namespaceTranslators = getTranslatorsForNonEmptyNamespaces();
|
List<NamespaceTranslator> namespaceTranslators = getTranslatorsForNonEmptyNamespaces();
|
||||||
result.addAll(declarationStatements(namespaceTranslators, context()));
|
statements.addAll(declarationStatements(namespaceTranslators, context()));
|
||||||
result.addAll(initializeStatements(namespaceTranslators));
|
statements.addAll(initializeStatements());
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -121,7 +117,7 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<JsStatement> initializeStatements(@NotNull List<NamespaceTranslator> namespaceTranslators) {
|
private List<JsStatement> initializeStatements() {
|
||||||
List<JsStatement> result = Lists.newArrayList();
|
List<JsStatement> result = Lists.newArrayList();
|
||||||
for (NamespaceDescriptor descriptor : filterNonEmptyNamespaces(namespaceDescriptors)) {
|
for (NamespaceDescriptor descriptor : filterNonEmptyNamespaces(namespaceDescriptors)) {
|
||||||
JsNameRef initializeMethodReference = Namer.initializeMethodReference();
|
JsNameRef initializeMethodReference = Namer.initializeMethodReference();
|
||||||
@@ -144,10 +140,10 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static List<NamespaceDescriptor> filterNonEmptyNamespaces(@NotNull List<NamespaceDescriptor> namespaceDescriptors) {
|
private List<NamespaceDescriptor> filterNonEmptyNamespaces(@NotNull List<NamespaceDescriptor> namespaceDescriptors) {
|
||||||
List<NamespaceDescriptor> result = Lists.newArrayList();
|
List<NamespaceDescriptor> result = Lists.newArrayList();
|
||||||
for (NamespaceDescriptor descriptor : namespaceDescriptors) {
|
for (NamespaceDescriptor descriptor : namespaceDescriptors) {
|
||||||
if (!JsDescriptorUtils.isNamespaceEmpty(descriptor)) {
|
if (!JsDescriptorUtils.isNamespaceEmpty(descriptor, context().bindingContext())) {
|
||||||
result.add(descriptor);
|
result.add(descriptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -128,7 +128,7 @@ public final class NamespaceTranslator extends AbstractTranslator {
|
|||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
List<JsPropertyInitializer> result = Lists.newArrayList();
|
List<JsPropertyInitializer> result = Lists.newArrayList();
|
||||||
List<NamespaceDescriptor> nestedNamespaces = JsDescriptorUtils.getNestedNamespaces(descriptor);
|
List<NamespaceDescriptor> nestedNamespaces = JsDescriptorUtils.getNestedNamespaces(descriptor, context().bindingContext());
|
||||||
for (NamespaceDescriptor nestedNamespace : nestedNamespaces) {
|
for (NamespaceDescriptor nestedNamespace : nestedNamespaces) {
|
||||||
NamespaceTranslator nestedNamespaceTranslator = new NamespaceTranslator(nestedNamespace, classDeclarationTranslator, context());
|
NamespaceTranslator nestedNamespaceTranslator = new NamespaceTranslator(nestedNamespace, classDeclarationTranslator, context());
|
||||||
result.add(nestedNamespaceTranslator.getDeclarationAsInitializer());
|
result.add(nestedNamespaceTranslator.getDeclarationAsInitializer());
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ public final class BindingUtils {
|
|||||||
public static List<JetDeclaration> getDeclarationsForNamespace(@NotNull BindingContext bindingContext,
|
public static List<JetDeclaration> getDeclarationsForNamespace(@NotNull BindingContext bindingContext,
|
||||||
@NotNull NamespaceDescriptor namespace) {
|
@NotNull NamespaceDescriptor namespace) {
|
||||||
List<JetDeclaration> declarations = new ArrayList<JetDeclaration>();
|
List<JetDeclaration> declarations = new ArrayList<JetDeclaration>();
|
||||||
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespace)) {
|
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespace, bindingContext)) {
|
||||||
if (descriptor instanceof NamespaceDescriptor) {
|
if (descriptor instanceof NamespaceDescriptor) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,14 +17,19 @@
|
|||||||
package org.jetbrains.k2js.translate.utils;
|
package org.jetbrains.k2js.translate.utils;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import com.intellij.psi.PsiElement;
|
||||||
|
import com.intellij.psi.PsiFile;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.descriptors.*;
|
import org.jetbrains.jet.lang.descriptors.*;
|
||||||
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
|
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
|
||||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||||
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
|
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
|
||||||
import org.jetbrains.jet.lang.types.JetType;
|
import org.jetbrains.jet.lang.types.JetType;
|
||||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
|
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
|
||||||
|
import org.jetbrains.k2js.config.LibrarySourcesConfig;
|
||||||
import org.jetbrains.k2js.translate.context.Namer;
|
import org.jetbrains.k2js.translate.context.Namer;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -193,22 +198,24 @@ public final class JsDescriptorUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static List<ClassDescriptor> getAllClassesDefinedInNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
|
public static List<ClassDescriptor> getAllClassesDefinedInNamespace(@NotNull NamespaceDescriptor namespaceDescriptor,
|
||||||
|
@NotNull BindingContext context) {
|
||||||
List<ClassDescriptor> classDescriptors = Lists.newArrayList();
|
List<ClassDescriptor> classDescriptors = Lists.newArrayList();
|
||||||
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor)) {
|
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor, context)) {
|
||||||
if (descriptor instanceof ClassDescriptor) {
|
if (descriptor instanceof ClassDescriptor) {
|
||||||
classDescriptors.add((ClassDescriptor)descriptor);
|
classDescriptors.add((ClassDescriptor) descriptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return classDescriptors;
|
return classDescriptors;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static List<NamespaceDescriptor> getNestedNamespaces(@NotNull NamespaceDescriptor namespaceDescriptor) {
|
public static List<NamespaceDescriptor> getNestedNamespaces(@NotNull NamespaceDescriptor namespaceDescriptor,
|
||||||
|
@NotNull BindingContext context) {
|
||||||
List<NamespaceDescriptor> result = Lists.newArrayList();
|
List<NamespaceDescriptor> result = Lists.newArrayList();
|
||||||
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor)) {
|
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor, context)) {
|
||||||
if (descriptor instanceof NamespaceDescriptor) {
|
if (descriptor instanceof NamespaceDescriptor) {
|
||||||
result.add((NamespaceDescriptor)descriptor);
|
result.add((NamespaceDescriptor) descriptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -227,22 +234,29 @@ public final class JsDescriptorUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static List<DeclarationDescriptor> getContainedDescriptorsWhichAreNotPredefined(@NotNull NamespaceDescriptor namespace) {
|
public static List<DeclarationDescriptor> getContainedDescriptorsWhichAreNotPredefined(@NotNull NamespaceDescriptor namespace,
|
||||||
|
@NotNull BindingContext context) {
|
||||||
List<DeclarationDescriptor> result = Lists.newArrayList();
|
List<DeclarationDescriptor> result = Lists.newArrayList();
|
||||||
for (DeclarationDescriptor descriptor : namespace.getMemberScope().getAllDescriptors()) {
|
for (DeclarationDescriptor descriptor : namespace.getMemberScope().getAllDescriptors()) {
|
||||||
if (!AnnotationsUtils.isPredefinedObject(descriptor)) {
|
if (!AnnotationsUtils.isPredefinedObject(descriptor)) {
|
||||||
result.add(descriptor);
|
PsiElement psiElement = BindingContextUtils.descriptorToDeclaration(context, descriptor);
|
||||||
|
if (psiElement != null) {
|
||||||
|
PsiFile file = psiElement.getContainingFile();
|
||||||
|
if (file.getUserData(LibrarySourcesConfig.EXTERNAL_LIB) == null) {
|
||||||
|
result.add(descriptor);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: at the moment this check is very ineffective
|
//TODO: at the moment this check is very ineffective
|
||||||
public static boolean isNamespaceEmpty(@NotNull NamespaceDescriptor namespace) {
|
public static boolean isNamespaceEmpty(@NotNull NamespaceDescriptor namespace, @NotNull BindingContext context) {
|
||||||
List<DeclarationDescriptor> containedDescriptors = getContainedDescriptorsWhichAreNotPredefined(namespace);
|
List<DeclarationDescriptor> containedDescriptors = getContainedDescriptorsWhichAreNotPredefined(namespace, context);
|
||||||
for (DeclarationDescriptor descriptor : containedDescriptors) {
|
for (DeclarationDescriptor descriptor : containedDescriptors) {
|
||||||
if (descriptor instanceof NamespaceDescriptor) {
|
if (descriptor instanceof NamespaceDescriptor) {
|
||||||
if (!isNamespaceEmpty((NamespaceDescriptor)descriptor)) {
|
if (!isNamespaceEmpty((NamespaceDescriptor) descriptor, context)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,11 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// todo org.jetbrains.k2js.test.semantics.WebDemoExamples2Test#testBuilder
|
||||||
|
var kotlin = {set:function (receiver, key, value) {
|
||||||
|
return receiver.put(key, value);
|
||||||
|
}};
|
||||||
|
|
||||||
(function () {
|
(function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user