Skip to content

Commit e6bfd03

Browse files
authored
Merge pull request #137 from webratio/pr/incompatible-compiler-options
Warn about incompatibility between compileOnSave and path mapping
2 parents 7dff073 + 056aaad commit e6bfd03

File tree

5 files changed

+119
-6
lines changed

5 files changed

+119
-6
lines changed

core/ts.core/src/ts/cmd/tsc/CompilerOptions.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package ts.cmd.tsc;
22

3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
36
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
49

510
import ts.cmd.AbstractOptions;
611

@@ -49,12 +54,14 @@ public class CompilerOptions extends AbstractOptions {
4954
private String out;
5055
private String outDir;
5156
private String outFile;
57+
private Map<String, List<String>> paths;
5258
private boolean preserveConstEnums;
5359
private boolean pretty;
5460
private String project;
5561
private String reactNamespace;
5662
private boolean removeComments;
5763
private String rootDir;
64+
private List<String> rootDirs;
5865
private boolean skipDefaultLibCheck;
5966
private boolean sourceMap;
6067
private String sourceRoot;
@@ -819,6 +826,49 @@ public void setOutFile(String outFile) {
819826
this.outFile = outFile;
820827
}
821828

829+
/**
830+
* Specify path mapping to be computed relative to baseUrl option.
831+
*
832+
* @return key patterns to which paths are mapped.
833+
*/
834+
public Set<String> getPathsKeys() {
835+
if (paths == null) {
836+
return Collections.emptySet();
837+
}
838+
return Collections.unmodifiableSet(paths.keySet());
839+
}
840+
841+
/**
842+
* Specify path mapping to be computed relative to baseUrl option.
843+
*
844+
* @param pathsKey
845+
* a path key pattern.
846+
* @return paths mapped to the key pattern.
847+
*/
848+
public List<String> getPathsKeyValues(String pathsKey) {
849+
if (paths == null) {
850+
return Collections.emptyList();
851+
}
852+
List<String> values = paths.get(pathsKey);
853+
if (values == null) {
854+
return Collections.emptyList();
855+
}
856+
return Collections.unmodifiableList(values);
857+
}
858+
859+
/**
860+
* Specify path mapping to be computed relative to baseUrl option.
861+
*
862+
* @param paths
863+
*/
864+
public void setPaths(Map<String, List<String>> paths) {
865+
Map<String, List<String>> pathsCopy = new HashMap<>(paths.size());
866+
for (Map.Entry<String, List<String>> entry : paths.entrySet()) {
867+
pathsCopy.put(entry.getKey(), new ArrayList<>(entry.getValue()));
868+
}
869+
this.paths = pathsCopy;
870+
}
871+
822872
/**
823873
* Do not erase const enum declarations in generated code. See const enums
824874
* https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#94-constant-enum-declarations
@@ -939,6 +989,27 @@ public void setRootDir(String rootDir) {
939989
this.rootDir = rootDir;
940990
}
941991

992+
/**
993+
* Specify list of root directory to be used when resolving modules.
994+
*
995+
* @return
996+
*/
997+
public List<String> getRootDirs() {
998+
if (rootDirs == null) {
999+
return Collections.emptyList();
1000+
}
1001+
return Collections.unmodifiableList(rootDirs);
1002+
}
1003+
1004+
/**
1005+
* Specify list of root directory to be used when resolving modules.
1006+
*
1007+
* @param rootDirs
1008+
*/
1009+
public void setRootDirs(List<String> rootDirs) {
1010+
this.rootDirs = new ArrayList<>(rootDirs);
1011+
}
1012+
9421013
/**
9431014
* Don’t check a user-defined default lib file’s valitidy.
9441015
*

core/ts.core/src/ts/resources/jsonconfig/TsconfigJson.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,36 @@ public boolean hasOutFile() {
141141
return !StringUtils.isEmpty(options.getOutFile()) || !StringUtils.isEmpty(options.getOut());
142142
}
143143

144+
/**
145+
* Returns true if the "compilerOptions" defines "paths" and false
146+
* otherwise.
147+
*
148+
* @return true if the "compilerOptions" defines "paths" and false
149+
* otherwise.
150+
*/
151+
public boolean hasPaths() {
152+
CompilerOptions options = getCompilerOptions();
153+
if (options == null) {
154+
return false;
155+
}
156+
return !options.getPathsKeys().isEmpty();
157+
}
158+
159+
/**
160+
* Returns true if the "compilerOptions" defines "rootDirs" and false
161+
* otherwise.
162+
*
163+
* @return true if the "compilerOptions" defines "rootDirs" and false
164+
* otherwise.
165+
*/
166+
public boolean hasRootDirs() {
167+
CompilerOptions options = getCompilerOptions();
168+
if (options == null) {
169+
return false;
170+
}
171+
return !options.getRootDirs().isEmpty();
172+
}
173+
144174
/**
145175
* Returns the defined "exclude" list from the tsconfig.json other exclude
146176
* by default "node_modules" and "bower_components".

eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/internal/core/TypeScriptCoreMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class TypeScriptCoreMessages extends NLS {
3333
public static String tsconfig_compileOnSave_disable_error;
3434
public static String tsconfig_compilation_context_error;
3535
public static String tsconfig_cannot_use_compileOnSave_with_outFile_error;
36+
public static String tsconfig_cannot_use_compileOnSave_with_path_mapping_error;
3637

3738
// Launch
3839
public static String TypeScriptCompilerLaunchConfigurationDelegate_invalidBuildPath;

eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/internal/core/TypeScriptCoreMessages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SaveProjectPreferencesJob_taskName=Saving preferences of project {0}
1717
tsconfig_compileOnSave_disable_error=TypeScript file cannot be compiled on save because tsconfig.json disable it. If this is not intended, please set "compileOnSave" to "true" of your "{0}" file.
1818
tsconfig_compilation_context_error=TypeScript file is not included in the TypeScript compilation context. If this is not intended, please check the "files" or "filesGlob" section of your "{0}" file.
1919
tsconfig_cannot_use_compileOnSave_with_outFile_error=TypeScript file cannot be compiled on save because tsconfig.json defines "out"/outFile". If this is not intended, please set "buildOnSave" to "true" of your "{0}" file.
20+
tsconfig_cannot_use_compileOnSave_with_path_mapping_error=TypeScript file cannot be compiled on save because tsconfig.json uses path mapping features ("paths", "rootDirs"). If this is not intended, please set "buildOnSave" to "true" of your "{0}" file.
2021

2122
# Launch
2223
TypeScriptCompilerLaunchConfigurationDelegate_invalidBuildPath=The tsconfig file {0} does not exist for the compiler launch named {1}.

eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/internal/core/compiler/IDETypeScriptCompiler.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
import org.eclipse.core.resources.IContainer;
1818
import org.eclipse.core.resources.IFile;
1919
import org.eclipse.core.resources.IMarker;
20-
import org.eclipse.core.resources.IResource;
2120
import org.eclipse.core.runtime.CoreException;
2221
import org.eclipse.osgi.util.NLS;
2322

2423
import ts.TypeScriptException;
25-
import ts.cmd.tsc.CompilerOptionCapability;
2624
import ts.cmd.tsc.CompilerOptions;
2725
import ts.cmd.tsc.TypeScriptCompiler;
2826
import ts.eclipse.ide.core.compiler.IIDETypeScriptCompiler;
@@ -53,16 +51,18 @@ public void compile(IDETsconfigJson tsconfig, List<IFile> tsFiles) throws TypeSc
5351
} else {
5452
if (tsconfig.isCompileOnSave()) {
5553
// compileOnSave is activated
56-
if (tsconfig.hasOutFile()) {
57-
// tsconfig.json defines "compilerOptions/outFile" or
58-
// "compilerOptions/out", the ts files cannot be compiled
54+
String tsconfigErrorMessage = checkForInvalidCompileOnSave(tsconfig);
55+
if (tsconfigErrorMessage != null) {
56+
// tsconfig.json uses features incompatible with
57+
// compileOnSave,
58+
// the ts files cannot be compiled;
5959
// add a warning by suggesting to use "buildOnSave"
6060
for (IFile tsFile : tsFiles) {
6161
// delete existing marker
6262
TypeScriptResourceUtil.deleteTscMarker(tsFile);
6363
// add warning marker
6464
TypeScriptResourceUtil.addTscMarker(tsFile,
65-
NLS.bind(TypeScriptCoreMessages.tsconfig_cannot_use_compileOnSave_with_outFile_error,
65+
NLS.bind(tsconfigErrorMessage,
6666
tsconfig.getTsconfigFile().getProjectRelativePath().toString()),
6767
IMarker.SEVERITY_WARNING, 1);
6868
// delete emitted files *.js, *.js.map
@@ -103,6 +103,16 @@ public void compile(IDETsconfigJson tsconfig, List<IFile> tsFiles) throws TypeSc
103103
}
104104
}
105105

106+
private String checkForInvalidCompileOnSave(IDETsconfigJson tsconfig) {
107+
if (tsconfig.hasOutFile()) {
108+
return TypeScriptCoreMessages.tsconfig_cannot_use_compileOnSave_with_outFile_error;
109+
}
110+
if (tsconfig.hasPaths() || tsconfig.hasRootDirs()) {
111+
return TypeScriptCoreMessages.tsconfig_cannot_use_compileOnSave_with_path_mapping_error;
112+
}
113+
return null;
114+
}
115+
106116
private void compile(IFile tsConfigFile, CompilerOptions tsconfigOptions, List<IFile> tsFiles, boolean buildOnSave)
107117
throws TypeScriptException, CoreException {
108118
IContainer container = tsConfigFile.getParent();

0 commit comments

Comments
 (0)