Introduce and use JSConfigurationKeys similar to JVMConfigurationKeys
Use type-safe keys in CompilerConfiguration instead of multiple parameters, duplicated in the base class (JsConfig), derived class (LibrarySourcesConfig) and its builder (LibrarySourcesConfig.Builder)
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.kotlin.js.config;
|
||||
|
||||
import org.jetbrains.kotlin.config.CompilerConfigurationKey;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class JSConfigurationKeys {
|
||||
public static final CompilerConfigurationKey<List<String>> LIBRARY_FILES =
|
||||
CompilerConfigurationKey.create("library file paths");
|
||||
|
||||
public static final CompilerConfigurationKey<Boolean> SOURCE_MAP =
|
||||
CompilerConfigurationKey.create("generate source map");
|
||||
public static final CompilerConfigurationKey<Boolean> META_INFO =
|
||||
CompilerConfigurationKey.create("generate metadata");
|
||||
public static final CompilerConfigurationKey<Boolean> KJSM =
|
||||
CompilerConfigurationKey.create("generate .kjsm files");
|
||||
|
||||
public static final CompilerConfigurationKey<EcmaVersion> TARGET =
|
||||
CompilerConfigurationKey.create("ECMA version target");
|
||||
|
||||
public static final CompilerConfigurationKey<Boolean> UNIT_TEST_CONFIG =
|
||||
CompilerConfigurationKey.create("unit test config");
|
||||
|
||||
public static final CompilerConfigurationKey<String> MODULE_ID =
|
||||
CompilerConfigurationKey.create("module id");
|
||||
}
|
||||
@@ -23,7 +23,6 @@ import kotlin.Unit;
|
||||
import kotlin.collections.CollectionsKt;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration;
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider;
|
||||
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl;
|
||||
@@ -43,48 +42,18 @@ import java.util.List;
|
||||
* Base class representing a configuration of translator.
|
||||
*/
|
||||
public abstract class JsConfig {
|
||||
@NotNull
|
||||
private final Project project;
|
||||
@NotNull
|
||||
private final CompilerConfiguration configuration;
|
||||
@NotNull
|
||||
private final LockBasedStorageManager storageManager = new LockBasedStorageManager();
|
||||
@NotNull
|
||||
private final List<KtFile> sourceFilesFromLibraries = new SmartList<KtFile>();
|
||||
@NotNull
|
||||
private final EcmaVersion target;
|
||||
|
||||
@NotNull
|
||||
private final String moduleId;
|
||||
|
||||
private final boolean sourcemap;
|
||||
private final boolean metaInfo;
|
||||
private final boolean kjsm;
|
||||
|
||||
@NotNull
|
||||
protected final List<KotlinJavascriptMetadata> metadata = new SmartList<KotlinJavascriptMetadata>();
|
||||
|
||||
@Nullable
|
||||
private List<ModuleDescriptorImpl> moduleDescriptors = null;
|
||||
|
||||
private boolean initialized = false;
|
||||
|
||||
protected JsConfig(
|
||||
@NotNull Project project,
|
||||
@NotNull CompilerConfiguration configuration,
|
||||
@NotNull String moduleId,
|
||||
@NotNull EcmaVersion ecmaVersion,
|
||||
boolean sourcemap,
|
||||
boolean metaInfo,
|
||||
boolean kjsm
|
||||
) {
|
||||
protected JsConfig(@NotNull Project project, @NotNull CompilerConfiguration configuration) {
|
||||
this.project = project;
|
||||
this.configuration = configuration;
|
||||
this.target = ecmaVersion;
|
||||
this.moduleId = moduleId;
|
||||
this.sourcemap = sourcemap;
|
||||
this.metaInfo = metaInfo;
|
||||
this.kjsm = kjsm;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -92,31 +61,14 @@ public abstract class JsConfig {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public boolean isSourcemap() {
|
||||
return sourcemap;
|
||||
}
|
||||
|
||||
public boolean isMetaInfo() {
|
||||
return metaInfo;
|
||||
}
|
||||
|
||||
public boolean isKjsm() {
|
||||
return kjsm;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Project getProject() {
|
||||
return project;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public EcmaVersion getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getModuleId() {
|
||||
return moduleId;
|
||||
return configuration.getNotNull(JSConfigurationKeys.MODULE_ID);
|
||||
}
|
||||
|
||||
public abstract boolean checkLibFilesAndReportErrors(@NotNull Function1<String, Unit> report);
|
||||
@@ -140,16 +92,11 @@ public abstract class JsConfig {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<KtFile> getSourceFilesFromLibraries() {
|
||||
private List<KtFile> getSourceFilesFromLibraries() {
|
||||
init();
|
||||
return sourceFilesFromLibraries;
|
||||
}
|
||||
|
||||
|
||||
public boolean isTestConfig() {
|
||||
return false;
|
||||
}
|
||||
|
||||
private void init() {
|
||||
if (initialized) return;
|
||||
|
||||
|
||||
@@ -48,9 +48,7 @@ public class LibrarySourcesConfig extends JsConfig {
|
||||
public static final List<String> JS_STDLIB =
|
||||
Collections.singletonList(getKotlinPathsForDistDirectory().getJsStdLibJarPath().getAbsolutePath());
|
||||
|
||||
@NotNull
|
||||
public static final Key<String> EXTERNAL_MODULE_NAME = Key.create("externalModule");
|
||||
@NotNull
|
||||
public static final String UNKNOWN_EXTERNAL_MODULE_NAME = "<unknown>";
|
||||
|
||||
public static final String STDLIB_JS_MODULE_NAME = "stdlib";
|
||||
@@ -58,40 +56,18 @@ public class LibrarySourcesConfig extends JsConfig {
|
||||
public static final String BUILTINS_JS_FILE_NAME = BUILTINS_JS_MODULE_NAME + JavaScript.DOT_EXTENSION;
|
||||
public static final String STDLIB_JS_FILE_NAME = STDLIB_JS_MODULE_NAME + JavaScript.DOT_EXTENSION;
|
||||
|
||||
private final boolean isUnitTestConfig;
|
||||
|
||||
@NotNull
|
||||
private final List<String> files;
|
||||
|
||||
private LibrarySourcesConfig(
|
||||
@NotNull Project project,
|
||||
@NotNull CompilerConfiguration configuration,
|
||||
@NotNull String moduleId,
|
||||
@NotNull List<String> files,
|
||||
@NotNull EcmaVersion ecmaVersion,
|
||||
boolean sourceMap,
|
||||
boolean isUnitTestConfig,
|
||||
boolean metaInfo,
|
||||
boolean kjsm
|
||||
) {
|
||||
super(project, configuration, moduleId, ecmaVersion, sourceMap, metaInfo, kjsm);
|
||||
this.files = files;
|
||||
this.isUnitTestConfig = isUnitTestConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTestConfig() {
|
||||
return isUnitTestConfig;
|
||||
public LibrarySourcesConfig(@NotNull Project project, @NotNull CompilerConfiguration configuration) {
|
||||
super(project, configuration);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<String> getLibraries() {
|
||||
return files;
|
||||
return getConfiguration().getList(JSConfigurationKeys.LIBRARY_FILES);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init(@NotNull final List<KtFile> sourceFilesInLibraries, @NotNull final List<KotlinJavascriptMetadata> metadata) {
|
||||
if (files.isEmpty()) return;
|
||||
if (getLibraries().isEmpty()) return;
|
||||
|
||||
final PsiManager psiManager = PsiManager.getInstance(getProject());
|
||||
|
||||
@@ -129,14 +105,15 @@ public class LibrarySourcesConfig extends JsConfig {
|
||||
}
|
||||
|
||||
private boolean checkLibFilesAndReportErrors(@NotNull Function1<String, Unit> report, @Nullable Function2<String, VirtualFile, Unit> action) {
|
||||
if (files.isEmpty()) {
|
||||
List<String> libraries = getLibraries();
|
||||
if (libraries.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
VirtualFileSystem fileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL);
|
||||
VirtualFileSystem jarFileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.JAR_PROTOCOL);
|
||||
|
||||
for (String path : files) {
|
||||
for (String path : libraries) {
|
||||
VirtualFile file;
|
||||
|
||||
File filePath = new File(path);
|
||||
@@ -189,62 +166,7 @@ public class LibrarySourcesConfig extends JsConfig {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private final Project project;
|
||||
private final CompilerConfiguration configuration;
|
||||
private final String moduleId;
|
||||
private final List<String> files;
|
||||
private EcmaVersion ecmaVersion = EcmaVersion.defaultVersion();
|
||||
boolean sourceMap = false;
|
||||
boolean isUnitTestConfig = false;
|
||||
boolean metaInfo = false;
|
||||
boolean kjsm = false;
|
||||
|
||||
public Builder(
|
||||
@NotNull Project project,
|
||||
@NotNull CompilerConfiguration configuration,
|
||||
@NotNull String moduleId,
|
||||
@NotNull List<String> files
|
||||
) {
|
||||
this.project = project;
|
||||
this.configuration = configuration;
|
||||
this.moduleId = moduleId;
|
||||
this.files = files;
|
||||
}
|
||||
|
||||
public Builder ecmaVersion(@NotNull EcmaVersion ecmaVersion) {
|
||||
this.ecmaVersion = ecmaVersion;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder sourceMap(boolean sourceMap) {
|
||||
this.sourceMap = sourceMap;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder isUnitTestConfig(boolean isUnitTestConfig) {
|
||||
this.isUnitTestConfig = isUnitTestConfig;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder metaInfo(boolean metaInfo) {
|
||||
this.metaInfo = metaInfo;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder kjsm(boolean kjsm) {
|
||||
this.kjsm = kjsm;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JsConfig build() {
|
||||
return new LibrarySourcesConfig(
|
||||
project, configuration, moduleId, files, ecmaVersion, sourceMap, isUnitTestConfig, metaInfo, kjsm
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected static KtFile getJetFileByVirtualFile(VirtualFile file, String moduleName, PsiManager psiManager) {
|
||||
private static KtFile getJetFileByVirtualFile(VirtualFile file, String moduleName, PsiManager psiManager) {
|
||||
PsiFile psiFile = psiManager.findFile(file);
|
||||
assert psiFile != null;
|
||||
|
||||
@@ -252,7 +174,7 @@ public class LibrarySourcesConfig extends JsConfig {
|
||||
return (KtFile) psiFile;
|
||||
}
|
||||
|
||||
protected static void setupPsiFile(PsiFile psiFile, String moduleName) {
|
||||
private static void setupPsiFile(PsiFile psiFile, String moduleName) {
|
||||
psiFile.putUserData(EXTERNAL_MODULE_NAME, moduleName);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user