JS backend: refactor Configs.

This commit is contained in:
Zalim Bashorov
2014-10-03 23:46:43 +04:00
parent dd1fffae40
commit c8278fd887
5 changed files with 125 additions and 297 deletions
@@ -0,0 +1,124 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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.
*/
package org.jetbrains.k2js.test.config;
import com.google.common.base.Predicates;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.utils.PathUtil;
import org.jetbrains.k2js.analyze.TopDownAnalyzerFacadeForJS;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.config.LibrarySourcesConfig;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class LibrarySourcesConfigWithCaching extends LibrarySourcesConfig {
public static final List<String> JS_STDLIB =
Arrays.asList("@" + "kotlin_lib_compiled", PathUtil.getKotlinPathsForDistDirectory().getJsLibJarPath().getAbsolutePath());
private static List<JetFile> jsLibFiles;
private static AnalyzeExhaust exhaust;
private BindingContext libraryContext;
private ModuleDescriptor libraryModule;
private final boolean isUnitTestConfig;
public LibrarySourcesConfigWithCaching(
@NotNull Project project,
@NotNull String moduleId,
@NotNull EcmaVersion ecmaVersion,
boolean sourcemap,
boolean inlineEnabled,
boolean isUnitTestConfig
) {
super(project, moduleId, JS_STDLIB, ecmaVersion, sourcemap, inlineEnabled);
this.isUnitTestConfig = isUnitTestConfig;
}
@NotNull
@Override
public List<JetFile> generateLibFiles() {
if (jsLibFiles == null) {
//noinspection AssignmentToStaticFieldFromInstanceMethod
jsLibFiles = super.generateLibFiles();
}
return jsLibFiles;
}
@Nullable
@Override
public ModuleDescriptor getLibraryModule() {
if (libraryModule == null) {
libraryModule = getExhaust().getModuleDescriptor();
}
return libraryModule;
}
@Nullable
@Override
public BindingContext getLibraryContext() {
if (libraryContext == null) {
//TODO check errors?
// TopDownAnalyzerFacadeForJS.checkForErrors(allLibFiles, exhaust.getBindingContext());
libraryContext = getExhaust().getBindingContext();
}
return libraryContext;
}
@Override
public boolean isTestConfig() {
return isUnitTestConfig;
}
private AnalyzeExhaust getExhaust() {
if (exhaust == null) {
//noinspection AssignmentToStaticFieldFromInstanceMethod
exhaust = TopDownAnalyzerFacadeForJS.analyzeFiles(
generateLibFiles(),
Predicates.<PsiFile>alwaysFalse(),
createConfigWithoutLibFiles(getProject(), getModuleId(), getTarget())
);
}
return exhaust;
}
@NotNull
private static Config createConfigWithoutLibFiles(@NotNull Project project, @NotNull String moduleId, @NotNull EcmaVersion ecmaVersion) {
return new Config(project, moduleId, ecmaVersion, /* generate sourcemaps = */ false, /* inlineEnabled = */ false) {
@NotNull
@Override
protected List<JetFile> generateLibFiles() {
return Collections.emptyList();
}
};
}
}
@@ -1,93 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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.
*/
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;
import org.jetbrains.k2js.config.EcmaVersion;
import java.util.List;
public class TestConfig extends Config {
@NotNull
public static TestConfigFactory FACTORY_WITHOUT_SOURCEMAP = new TestConfigFactory() {
@Override
public TestConfig create(@NotNull Project project,
@NotNull EcmaVersion version,
@NotNull List<JetFile> files,
@NotNull BindingContext libraryContext,
@NotNull ModuleDescriptor module) {
return new TestConfig(project, version, files, libraryContext, module, false, true);
}
};
public static TestConfigFactory FACTORY_WITH_SOURCEMAP = new TestConfigFactory() {
@Override
public TestConfig create(@NotNull Project project,
@NotNull EcmaVersion version,
@NotNull List<JetFile> files,
@NotNull BindingContext libraryContext,
@NotNull ModuleDescriptor module) {
return new TestConfig(project, version, files, libraryContext, module, true, true);
}
};
@NotNull
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 libraryContext,
@NotNull ModuleDescriptor module,
boolean sourcemap,
boolean inlineEnabled
) {
super(project, REWRITABLE_MODULE_NAME, version, sourcemap, inlineEnabled);
jsLibFiles = files;
this.libraryContext = libraryContext;
libraryModule = module;
}
@NotNull
@Override
public BindingContext getLibraryContext() {
return libraryContext;
}
@NotNull
@Override
public ModuleDescriptor getLibraryModule() {
return libraryModule;
}
@Override
@NotNull
public List<JetFile> generateLibFiles() {
return jsLibFiles;
}
}
@@ -1,31 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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.
*/
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;
import java.util.List;
public interface TestConfigFactory {
TestConfig create(@NotNull Project project, @NotNull EcmaVersion version,
@NotNull List<JetFile> files, @NotNull BindingContext libraryContext, @NotNull ModuleDescriptor module);
}
@@ -1,55 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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.
*/
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;
import java.util.List;
public class TestConfigWithUnitTests extends TestConfig {
@NotNull
public static TestConfigFactory FACTORY = new TestConfigFactory() {
@Override
public TestConfig create(@NotNull Project project,
@NotNull EcmaVersion version,
@NotNull List<JetFile> files,
@NotNull BindingContext libraryContext,
@NotNull ModuleDescriptor module) {
return new TestConfigWithUnitTests(project, version, files, libraryContext, module);
}
};
public TestConfigWithUnitTests(@NotNull Project project,
@NotNull EcmaVersion version,
@NotNull List<JetFile> files,
@NotNull BindingContext libraryContext,
@NotNull ModuleDescriptor module) {
super(project, version, files, libraryContext, module, false, true);
}
@Override
public boolean isTestConfig() {
return true;
}
}