Reimplemented non-trivial logic of ModuleConfiguration.
This commit is contained in:
@@ -18,6 +18,7 @@ package org.jetbrains.k2js.test.config;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.k2js.config.Config;
|
||||
@@ -33,8 +34,9 @@ public class TestConfig extends Config {
|
||||
public TestConfig create(@NotNull Project project,
|
||||
@NotNull EcmaVersion version,
|
||||
@NotNull List<JetFile> files,
|
||||
@NotNull BindingContext context) {
|
||||
return new TestConfig(project, version, files, context, false);
|
||||
@NotNull BindingContext libraryContext,
|
||||
@NotNull ModuleDescriptor module) {
|
||||
return new TestConfig(project, version, files, libraryContext, module, false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -43,8 +45,9 @@ public class TestConfig extends Config {
|
||||
public TestConfig create(@NotNull Project project,
|
||||
@NotNull EcmaVersion version,
|
||||
@NotNull List<JetFile> files,
|
||||
@NotNull BindingContext context) {
|
||||
return new TestConfig(project, version, files, context, true);
|
||||
@NotNull BindingContext libraryContext,
|
||||
@NotNull ModuleDescriptor module) {
|
||||
return new TestConfig(project, version, files, libraryContext, module, true);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -52,19 +55,29 @@ public class TestConfig extends Config {
|
||||
private final List<JetFile> jsLibFiles;
|
||||
@NotNull
|
||||
private final BindingContext libraryContext;
|
||||
@NotNull
|
||||
private final ModuleDescriptor libraryModule;
|
||||
|
||||
public TestConfig(@NotNull Project project, @NotNull EcmaVersion version,
|
||||
@NotNull List<JetFile> files, @NotNull BindingContext context, boolean sourcemap) {
|
||||
@NotNull List<JetFile> files, @NotNull BindingContext libraryContext, @NotNull ModuleDescriptor module, boolean sourcemap) {
|
||||
super(project, REWRITABLE_MODULE_NAME, version, sourcemap);
|
||||
jsLibFiles = files;
|
||||
libraryContext = context;
|
||||
this.libraryContext = libraryContext;
|
||||
libraryModule = module;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public BindingContext getLibraryBindingContext() {
|
||||
public BindingContext getLibraryContext() {
|
||||
return libraryContext;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ModuleDescriptor getLibraryModule() {
|
||||
return libraryModule;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<JetFile> generateLibFiles() {
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.k2js.test.config;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
@@ -26,5 +27,5 @@ import java.util.List;
|
||||
|
||||
public interface TestConfigFactory {
|
||||
TestConfig create(@NotNull Project project, @NotNull EcmaVersion version,
|
||||
@NotNull List<JetFile> files, @NotNull BindingContext context);
|
||||
@NotNull List<JetFile> files, @NotNull BindingContext libraryContext, @NotNull ModuleDescriptor module);
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.k2js.test.config;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
@@ -34,8 +35,9 @@ public class TestConfigWithUnitTests extends TestConfig {
|
||||
public TestConfig create(@NotNull Project project,
|
||||
@NotNull EcmaVersion version,
|
||||
@NotNull List<JetFile> files,
|
||||
@NotNull BindingContext context) {
|
||||
return new TestConfigWithUnitTests(project, version, files, context);
|
||||
@NotNull BindingContext libraryContext,
|
||||
@NotNull ModuleDescriptor module) {
|
||||
return new TestConfigWithUnitTests(project, version, files, libraryContext, module);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -47,7 +49,8 @@ public class TestConfigWithUnitTests extends TestConfig {
|
||||
public TestConfigWithUnitTests(@NotNull Project project,
|
||||
@NotNull EcmaVersion version,
|
||||
@NotNull List<JetFile> files,
|
||||
@NotNull BindingContext context) {
|
||||
super(project, version, files, context, false);
|
||||
@NotNull BindingContext libraryContext,
|
||||
@NotNull ModuleDescriptor module) {
|
||||
super(project, version, files, libraryContext, module, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ import org.jetbrains.jet.OutputFileCollection;
|
||||
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
|
||||
import org.jetbrains.jet.cli.common.output.outputUtils.OutputUtilsPackage;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.k2js.analyze.AnalyzerFacadeForJS;
|
||||
import org.jetbrains.k2js.config.Config;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
@@ -50,7 +49,7 @@ public final class TranslationUtils {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static SoftReference<BindingContext> cachedLibraryContext = new SoftReference<BindingContext>(null);
|
||||
private static SoftReference<AnalyzeExhaust> cachedLibraryExhaust = new SoftReference<AnalyzeExhaust>(null);
|
||||
|
||||
@Nullable
|
||||
private static List<JetFile> libFiles = null;
|
||||
@@ -64,24 +63,23 @@ public final class TranslationUtils {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static BindingContext getLibraryContext(@NotNull Project project) {
|
||||
BindingContext context = cachedLibraryContext.get();
|
||||
if (context == null) {
|
||||
List<JetFile> allLibFiles = getAllLibFiles(project);
|
||||
Predicate<PsiFile> filesWithCode = new Predicate<PsiFile>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PsiFile file) {
|
||||
assert file != null;
|
||||
return isFileWithCode((JetFile) file);
|
||||
}
|
||||
};
|
||||
AnalyzeExhaust exhaust = AnalyzerFacadeForJS
|
||||
.analyzeFiles(allLibFiles, filesWithCode, Config.getEmptyConfig(project));
|
||||
context = exhaust.getBindingContext();
|
||||
AnalyzerFacadeForJS.checkForErrors(allLibFiles, context);
|
||||
cachedLibraryContext = new SoftReference<BindingContext>(context);
|
||||
public static AnalyzeExhaust getLibraryAnalyzeExhaust(@NotNull Project project) {
|
||||
AnalyzeExhaust cachedModule = cachedLibraryExhaust.get();
|
||||
if (cachedModule != null) {
|
||||
return cachedModule;
|
||||
}
|
||||
return context;
|
||||
List<JetFile> allLibFiles = getAllLibFiles(project);
|
||||
Predicate<PsiFile> filesWithCode = new Predicate<PsiFile>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PsiFile file) {
|
||||
assert file != null;
|
||||
return isFileWithCode((JetFile) file);
|
||||
}
|
||||
};
|
||||
AnalyzeExhaust exhaust = AnalyzerFacadeForJS.analyzeFiles(allLibFiles, filesWithCode, Config.getEmptyConfig(project));
|
||||
AnalyzerFacadeForJS.checkForErrors(allLibFiles, exhaust.getBindingContext());
|
||||
cachedLibraryExhaust = new SoftReference<AnalyzeExhaust>(exhaust);
|
||||
return exhaust;
|
||||
}
|
||||
|
||||
private static boolean isFileWithCode(@NotNull JetFile file) {
|
||||
@@ -95,8 +93,10 @@ public final class TranslationUtils {
|
||||
|
||||
@NotNull
|
||||
public static Config getConfig(@NotNull Project project, @NotNull EcmaVersion version, @NotNull TestConfigFactory configFactory) {
|
||||
BindingContext preanalyzedContext = getLibraryContext(project);
|
||||
return configFactory.create(project, version, getLibFilesWithCode(getAllLibFiles(project)), preanalyzedContext);
|
||||
AnalyzeExhaust libraryAnalyzeExhaust = getLibraryAnalyzeExhaust(project);
|
||||
return configFactory.create(
|
||||
project, version, getLibFilesWithCode(getAllLibFiles(project)),
|
||||
libraryAnalyzeExhaust.getBindingContext(), libraryAnalyzeExhaust.getModuleDescriptor());
|
||||
}
|
||||
|
||||
public static void translateFiles(
|
||||
|
||||
@@ -84,14 +84,19 @@ public final class AnalyzerFacadeForJS {
|
||||
TopDownAnalysisParameters topDownAnalysisParameters = new TopDownAnalysisParameters(
|
||||
completely, false, false, Collections.<AnalyzerScriptParameter>emptyList());
|
||||
|
||||
BindingContext libraryBindingContext = config.getLibraryBindingContext();
|
||||
BindingTrace trace = libraryBindingContext == null ?
|
||||
new ObservableBindingTrace(new BindingTraceContext()) :
|
||||
new DelegatingBindingTrace(libraryBindingContext, "trace for analyzing library in js");
|
||||
owner.setModuleConfiguration(new JsConfiguration(libraryBindingContext));
|
||||
ModuleDescriptor libraryModule = config.getLibraryModule();
|
||||
if (libraryModule != null) {
|
||||
owner.addFragmentProvider(libraryModule.getPackageFragmentProvider()); // "import" analyzed library module
|
||||
}
|
||||
|
||||
BindingContext libraryContext = config.getLibraryContext();
|
||||
BindingTrace trace = libraryContext == null
|
||||
? new BindingTraceContext()
|
||||
: new DelegatingBindingTrace(libraryContext, "trace with preanalyzed library");
|
||||
owner.setModuleConfiguration(new JsConfiguration());
|
||||
InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs(project, topDownAnalysisParameters, trace, owner);
|
||||
try {
|
||||
Collection<JetFile> allFiles = libraryBindingContext != null ?
|
||||
Collection<JetFile> allFiles = libraryModule != null ?
|
||||
files :
|
||||
Config.withJsLibAdded(files, config);
|
||||
injector.getTopDownAnalyzer().analyzeFiles(allFiles, Collections.<AnalyzerScriptParameter>emptyList());
|
||||
@@ -144,7 +149,7 @@ public final class AnalyzerFacadeForJS {
|
||||
FileBasedDeclarationProviderFactory declarationProviderFactory = new FileBasedDeclarationProviderFactory(
|
||||
storageManager, Config.withJsLibAdded(files, config), Predicates.<FqName>alwaysFalse());
|
||||
ModuleDescriptorImpl lazyModule = createJsModule("<lazy module>");
|
||||
lazyModule.setModuleConfiguration(new JsConfiguration(null));
|
||||
lazyModule.setModuleConfiguration(new JsConfiguration());
|
||||
return new ResolveSession(config.getProject(), storageManager, lazyModule, declarationProviderFactory);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,22 +18,14 @@ package org.jetbrains.k2js.analyze;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.DefaultModuleConfiguration;
|
||||
import org.jetbrains.jet.lang.ModuleConfiguration;
|
||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||
import org.jetbrains.jet.lang.resolve.ImportPath;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isRootNamespace;
|
||||
|
||||
public class JsConfiguration implements ModuleConfiguration {
|
||||
|
||||
@NotNull
|
||||
@@ -43,51 +35,10 @@ public class JsConfiguration implements ModuleConfiguration {
|
||||
new ImportPath(KotlinBuiltIns.getInstance().getBuiltInsPackageFqName(), true),
|
||||
new ImportPath("kotlin.*"));
|
||||
|
||||
/*
|
||||
* Adds a possibility to inject some preanalyzed files to speed up tests.
|
||||
*/
|
||||
@Nullable
|
||||
private final BindingContext preanalyzedContext;
|
||||
|
||||
JsConfiguration(@Nullable BindingContext preanalyzedContext) {
|
||||
this.preanalyzedContext = preanalyzedContext;
|
||||
JsConfiguration() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void extendNamespaceScope(@NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) {
|
||||
DefaultModuleConfiguration.INSTANCE.extendNamespaceScope(namespaceDescriptor, namespaceMemberScope);
|
||||
|
||||
// Extend root namespace with standard classes
|
||||
if (namespaceDescriptor.getFqName().shortNameOrSpecial().equals(FqNameUnsafe.ROOT_NAME)) {
|
||||
namespaceMemberScope.importScope(KotlinBuiltIns.getInstance().getBuiltInsScope());
|
||||
}
|
||||
|
||||
if (hasPreanalyzedContextForTests()) {
|
||||
extendScopeWithPreAnalyzedContextForTests(namespaceDescriptor, namespaceMemberScope);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasPreanalyzedContextForTests() {
|
||||
return preanalyzedContext != null;
|
||||
}
|
||||
|
||||
/*NOTE: this code is wrong. Check it if you have tests failing for frontend reasons*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
private void extendScopeWithPreAnalyzedContextForTests(@NotNull NamespaceDescriptor namespaceDescriptor,
|
||||
@NotNull WritableScope namespaceMemberScope) {
|
||||
if (isNamespaceImportedByDefault(namespaceDescriptor) || isRootNamespace(namespaceDescriptor)) {
|
||||
FqName descriptorName = DescriptorUtils.getFQName(namespaceDescriptor).toSafe();
|
||||
NamespaceDescriptor alreadyAnalyzedNamespace = preanalyzedContext.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, descriptorName);
|
||||
namespaceMemberScope.importScope(alreadyAnalyzedNamespace.getMemberScope());
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isNamespaceImportedByDefault(@NotNull NamespaceDescriptor namespaceDescriptor) {
|
||||
for (ImportPath path : DEFAULT_IMPORT_PATHS) {
|
||||
if (path.fqnPart().equals(DescriptorUtils.getFQName(namespaceDescriptor).toSafe())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.google.common.collect.Lists;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.k2js.translate.test.JSTester;
|
||||
@@ -190,7 +191,12 @@ public abstract class Config {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BindingContext getLibraryBindingContext() {
|
||||
public BindingContext getLibraryContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ModuleDescriptor getLibraryModule() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user