Skip to content

Commit 69395d6

Browse files
author
angelozerr
committed
Validate JS file only if jsconfig.json is found or
tsconfig.json/compilerOptions/alloyJs = true
1 parent 497efe6 commit 69395d6

File tree

4 files changed

+100
-13
lines changed

4 files changed

+100
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ts.resources.jsonconfig;
2+
3+
public class CompilerOptions {
4+
5+
private boolean alloyJs;
6+
7+
public boolean isAlloyJs() {
8+
return alloyJs;
9+
}
10+
11+
public void setAlloyJs(boolean alloyJs) {
12+
this.alloyJs = alloyJs;
13+
}
14+
15+
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@
2727
*/
2828
public class TsconfigJson {
2929

30+
private CompilerOptions compilerOptions;
31+
3032
private boolean compileOnSave;
3133

3234
private List<String> files;
3335

3436
private List<String> exclude;
3537

38+
public void setCompilerOptions(CompilerOptions compilerOptions) {
39+
this.compilerOptions = compilerOptions;
40+
}
41+
42+
public CompilerOptions getCompilerOptions() {
43+
return compilerOptions;
44+
}
45+
3646
public boolean isCompileOnSave() {
3747
return compileOnSave;
3848
}

eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/internal/core/resources/IDETypeScriptProject.java

+57-12
Original file line numberDiff line numberDiff line change
@@ -192,28 +192,73 @@ public IIDETypeScriptProjectSettings getProjectSettings() {
192192
@Override
193193
public boolean canValidate(IResource resource) {
194194
try {
195+
// Use project preferences, which defines exclude path
195196
if (!getProjectSettings().canValidate(resource)) {
196197
return false;
197198
}
198-
IDETsconfigJson tsconfig = JsonConfigResourcesManager.getInstance().findTsconfig(resource);
199-
if (tsconfig != null) {
200-
// check if the given file is declared in the "files"
201-
if (tsconfig.hasFiles()) {
202-
return tsconfig.isInFiles(resource);
203-
} else if (tsconfig.hasExclude()) {
204-
return !tsconfig.isExcluded(resource);
205-
}
206-
} else {
207-
// tsconfig.json was not found (ex : MyProject/node_modules),
208-
// validation must not be done.
209-
return false;
199+
boolean isJSFile = IDEResourcesManager.getInstance().isJsFile(resource)
200+
|| IDEResourcesManager.getInstance().isJsxFile(resource);
201+
if (isJSFile) {
202+
// Can validate js file?
203+
return canValidateJsFile(resource);
210204
}
205+
// Can validate ts file?
206+
return canValidateTsFile(resource);
211207
} catch (CoreException e) {
212208
Trace.trace(Trace.SEVERE, "Error while getting tsconfig.json for canValidate", e);
213209
}
214210
return true;
215211
}
216212

213+
/**
214+
* Returns true if the given js, jsx file can be validated and false
215+
* otherwise.
216+
*
217+
* @param resource
218+
* @return true if the given js, jsx file can be validated and false
219+
* otherwise.
220+
* @throws CoreException
221+
*/
222+
private boolean canValidateJsFile(IResource resource) throws CoreException {
223+
// Search if a jsconfig.json exists?
224+
IFile jsconfigFile = JsonConfigResourcesManager.getInstance().findJsconfigFile(resource);
225+
if (jsconfigFile != null) {
226+
return true;
227+
}
228+
// Search if tsconfig.json exists and defines alloyJs
229+
IDETsconfigJson tsconfig = JsonConfigResourcesManager.getInstance().findTsconfig(resource);
230+
if (tsconfig != null && tsconfig.getCompilerOptions() != null && tsconfig.getCompilerOptions().isAlloyJs()) {
231+
return true;
232+
}
233+
// jsconfig.json was not found (ex : MyProject/node_modules),
234+
// validation must not be done.
235+
return false;
236+
}
237+
238+
/**
239+
* Returns true if the given ts, tsx file can be validated and false
240+
* otherwise.
241+
*
242+
* @param resource
243+
* @return true if the given ts, tsx file can be validated and false
244+
* otherwise.
245+
* @throws CoreException
246+
*/
247+
private boolean canValidateTsFile(IResource resource) throws CoreException {
248+
IDETsconfigJson tsconfig = JsonConfigResourcesManager.getInstance().findTsconfig(resource);
249+
if (tsconfig != null) {
250+
// check if the given file is declared in the "files"
251+
if (tsconfig.hasFiles()) {
252+
return tsconfig.isInFiles(resource);
253+
} else if (tsconfig.hasExclude()) {
254+
return !tsconfig.isExcluded(resource);
255+
}
256+
}
257+
// tsconfig.json was not found (ex : MyProject/node_modules),
258+
// validation must not be done.
259+
return false;
260+
}
261+
217262
@Override
218263
public boolean canCompileOnSave(IResource resource) {
219264
try {

eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/internal/core/resources/jsonconfig/JsonConfigResourcesManager.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class JsonConfigResourcesManager {
3232
private static final JsonConfigResourcesManager INSTANCE = new JsonConfigResourcesManager();
3333

3434
private static final IPath TSCONFIG_JSON_PATH = new Path(FileUtils.TSCONFIG_JSON);
35+
private static final IPath JSCONFIG_JSON_PATH = new Path(FileUtils.JSCONFIG_JSON);
3536

3637
public static JsonConfigResourcesManager getInstance() {
3738
return INSTANCE;
@@ -63,13 +64,18 @@ public void remove(IFile file) {
6364
* @throws CoreException
6465
*/
6566
public IDETsconfigJson findTsconfig(IResource resource) throws CoreException {
66-
IFile tsconfigFile = WorkbenchResourceUtil.findFileInContainerOrParent(resource, TSCONFIG_JSON_PATH);
67+
IFile tsconfigFile = findTsconfigFile(resource);
6768
if (tsconfigFile != null) {
6869
return getTsconfig(tsconfigFile);
6970
}
7071
return null;
7172
}
7273

74+
public IFile findTsconfigFile(IResource resource) throws CoreException {
75+
IFile tsconfigFile = WorkbenchResourceUtil.findFileInContainerOrParent(resource, TSCONFIG_JSON_PATH);
76+
return tsconfigFile;
77+
}
78+
7379
/**
7480
* Returns the Pojo of the given tsconfig.json file.
7581
*
@@ -105,4 +111,15 @@ private synchronized IDETsconfigJson createTsConfig(IFile tsconfigFile) throws C
105111
return tsconfig;
106112
}
107113

114+
/**
115+
* Find jsconfig.json
116+
*
117+
* @param resource
118+
* @return
119+
* @throws CoreException
120+
*/
121+
public IFile findJsconfigFile(IResource resource) throws CoreException {
122+
return WorkbenchResourceUtil.findFileInContainerOrParent(resource, JSCONFIG_JSON_PATH);
123+
}
124+
108125
}

0 commit comments

Comments
 (0)