Skip to content

Commit dab1d1e

Browse files
author
angelozerr
committed
Validate ts files according the tsconfig.json (files and exclude). See
#40
1 parent 9fa83af commit dab1d1e

File tree

5 files changed

+194
-42
lines changed

5 files changed

+194
-42
lines changed

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

+46-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.InputStream;
1515
import java.io.InputStreamReader;
1616
import java.io.Reader;
17+
import java.util.List;
1718

1819
import com.google.gson.Gson;
1920
import com.google.gson.GsonBuilder;
@@ -28,6 +29,10 @@ public class TsconfigJson {
2829

2930
private boolean compileOnSave;
3031

32+
private List<String> files;
33+
34+
private List<String> exclude;
35+
3136
public boolean isCompileOnSave() {
3237
return compileOnSave;
3338
}
@@ -36,28 +41,50 @@ public void setCompileOnSave(boolean compileOnSave) {
3641
this.compileOnSave = compileOnSave;
3742
}
3843

44+
public List<String> getFiles() {
45+
return files;
46+
}
47+
48+
public void setFiles(List<String> files) {
49+
this.files = files;
50+
}
51+
52+
public boolean hasFiles() {
53+
return files != null;
54+
}
55+
56+
public List<String> getExclude() {
57+
return exclude;
58+
}
59+
60+
public void setExclude(List<String> exclude) {
61+
this.exclude = exclude;
62+
}
63+
64+
public boolean hasExclude() {
65+
return exclude != null;
66+
}
67+
3968
/**
4069
* Load tsconfig.json instance from the given reader.
4170
*
4271
* @param reader
4372
* @return tsconfig.json instance from the given reader.
4473
*/
4574
public static TsconfigJson load(Reader reader) {
75+
return load(reader, TsconfigJson.class);
76+
}
77+
78+
public static <T extends TsconfigJson> T load(Reader json, Class<T> classOfT) {
4679
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
47-
return gson.fromJson(reader, TsconfigJson.class);
80+
return gson.fromJson(json, classOfT);
4881
}
4982

50-
/**
51-
* Load tsconfig.json instance from the given input stream.
52-
*
53-
* @param in
54-
* @return tsconfig.json instance from the given input stream
55-
*/
56-
public static TsconfigJson load(InputStream in) {
83+
public static <T extends TsconfigJson> T load(InputStream in, Class<T> classOfT) {
5784
Reader isr = null;
5885
try {
5986
isr = new InputStreamReader(in);
60-
return load(isr);
87+
return load(isr, classOfT);
6188
} finally {
6289
if (isr != null) {
6390
try {
@@ -68,4 +95,14 @@ public static TsconfigJson load(InputStream in) {
6895
}
6996
}
7097

98+
/**
99+
* Load tsconfig.json instance from the given input stream.
100+
*
101+
* @param in
102+
* @return tsconfig.json instance from the given input stream
103+
*/
104+
public static TsconfigJson load(InputStream in) {
105+
return load(in, TsconfigJson.class);
106+
}
107+
71108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
11+
package ts.eclipse.ide.core.resources.jsconfig;
12+
13+
import org.eclipse.core.resources.IFile;
14+
import org.eclipse.core.resources.IResource;
15+
import org.eclipse.core.runtime.CoreException;
16+
import org.eclipse.core.runtime.IPath;
17+
18+
import ts.resources.jsonconfig.TsconfigJson;
19+
20+
/**
21+
* tsconfig.json loadef from {@link IFile} tsconfig.json.
22+
*
23+
* @author azerr
24+
*
25+
*/
26+
public class IDETsconfigJson extends TsconfigJson {
27+
28+
private IFile tsconfigFile;
29+
30+
public IFile getTsconfigFile() {
31+
return tsconfigFile;
32+
}
33+
34+
public static IDETsconfigJson load(IFile tsconfigFile) throws CoreException {
35+
IDETsconfigJson tsconfig = load(tsconfigFile.getContents(), IDETsconfigJson.class);
36+
tsconfig.tsconfigFile = tsconfigFile;
37+
return tsconfig;
38+
}
39+
40+
/**
41+
* Returns true if the given file is declared in the "files" section and
42+
* false otherwise.
43+
*
44+
* @param resource
45+
* @return true if the given file is declared in the "files" section and
46+
* false otherwise.
47+
*/
48+
public boolean isInFiles(IResource resource) {
49+
if (!hasFiles()) {
50+
return false;
51+
}
52+
String filename = getRelativePath(resource);
53+
return getFiles().contains(filename);
54+
}
55+
56+
/**
57+
* Returns true if the given resource is excluded (declared as "exclude"
58+
* section) and false otherwise.
59+
*
60+
* @param resource
61+
* @return true if the given resource is excluded (declared as "exclude"
62+
* section) and false otherwise.
63+
*/
64+
public boolean isExcluded(IResource resource) {
65+
if (!hasExclude()) {
66+
return false;
67+
}
68+
String filename = getRelativePath(resource);
69+
for (String exclude : getExclude()) {
70+
if (filename.startsWith(exclude)) {
71+
return true;
72+
}
73+
}
74+
return false;
75+
}
76+
77+
/**
78+
* Returns the relative path of teh given resource to tsconfig.json folder.
79+
*
80+
* @param resource
81+
* @return the relative path of teh given resource to tsconfig.json folder.
82+
*/
83+
private String getRelativePath(IResource resource) {
84+
IPath path = resource.getLocation().makeRelativeTo(tsconfigFile.getParent().getLocation());
85+
return path.toString();
86+
}
87+
88+
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
/*******************************************************************************
2-
* Copyright (c) 2015, 2016 Red Hat, Inc.
3-
* All rights reserved. This program and the accompanying materials
4-
* are made available under the terms of the Eclipse Public License v1.0
5-
* which accompanies this distribution, and is available at
6-
* http://www.eclipse.org/legal/epl-v10.html
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
77
*
8-
* Contributors:
9-
* Red Hat Inc. - initial API and implementation and/or initial documentation
10-
*******************************************************************************/
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
1111
package ts.eclipse.ide.core.utils;
1212

1313
import org.eclipse.core.resources.IContainer;
1414
import org.eclipse.core.resources.IFile;
1515
import org.eclipse.core.resources.IResource;
1616
import org.eclipse.core.runtime.CoreException;
17+
import org.eclipse.core.runtime.IPath;
1718

1819
/**
1920
* Utilities for Eclipse resources.
2021
*
2122
*/
2223
public class WorkbenchResourceUtil {
2324

24-
public static IFile findFileRecursively(IResource resource, String name) throws CoreException {
25+
public static IFile findFileRecursively(IResource resource, IPath name) throws CoreException {
2526
IContainer parent = getContainer(resource);
2627
return findFileRecursively(parent, name);
2728
}
@@ -33,17 +34,14 @@ private static IContainer getContainer(IResource resource) {
3334
return resource.getParent();
3435
}
3536

36-
public static IFile findFileRecursively(IContainer container, String name) throws CoreException {
37-
for (IResource r : container.members()) {
38-
if (r instanceof IContainer) {
39-
IFile file = findFileRecursively((IContainer) r, name);
40-
if (file != null && file.exists()) {
41-
return file;
42-
}
43-
} else if (r instanceof IFile && r.getName().equals(name) && r.exists()) {
44-
return (IFile) r;
45-
}
37+
public static IFile findFileRecursively(IContainer container, IPath name) throws CoreException {
38+
if (container == null) {
39+
return null;
40+
}
41+
IFile file = container.getFile(name);
42+
if (file != null && file.exists()) {
43+
return file;
4644
}
47-
return null;
45+
return findFileRecursively(container.getParent(), name);
4846
}
4947
}

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import ts.eclipse.ide.core.resources.IIDETypeScriptFile;
2727
import ts.eclipse.ide.core.resources.IIDETypeScriptProject;
2828
import ts.eclipse.ide.core.resources.IIDETypeScriptProjectSettings;
29+
import ts.eclipse.ide.core.resources.jsconfig.IDETsconfigJson;
2930
import ts.eclipse.ide.core.resources.watcher.IFileWatcherListener;
3031
import ts.eclipse.ide.core.resources.watcher.ProjectWatcherListenerAdapter;
3132
import ts.eclipse.ide.internal.core.Trace;
@@ -191,9 +192,14 @@ public IIDETypeScriptProjectSettings getProjectSettings() {
191192
@Override
192193
public boolean canValidate(IResource resource) {
193194
try {
194-
TsconfigJson tsconfig = JsonConfigResourcesManager.getInstance().findTsconfig(resource);
195+
IDETsconfigJson tsconfig = JsonConfigResourcesManager.getInstance().findTsconfig(resource);
195196
if (tsconfig != null) {
196-
// TODO: check exclude + files
197+
// check if the given file is declared in the "files"
198+
if (tsconfig.hasFiles()) {
199+
return tsconfig.isInFiles(resource);
200+
} else if (tsconfig.hasExclude()) {
201+
return !tsconfig.isExcluded(resource);
202+
}
197203
}
198204
} catch (CoreException e) {
199205
Trace.trace(Trace.SEVERE, "Error while getting tsconfig.json for canValidate", e);

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

+33-10
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
import org.eclipse.core.resources.IFile;
1717
import org.eclipse.core.resources.IResource;
1818
import org.eclipse.core.runtime.CoreException;
19+
import org.eclipse.core.runtime.IPath;
20+
import org.eclipse.core.runtime.Path;
1921

22+
import ts.eclipse.ide.core.resources.jsconfig.IDETsconfigJson;
2023
import ts.eclipse.ide.core.utils.WorkbenchResourceUtil;
21-
import ts.resources.jsonconfig.TsconfigJson;
2224
import ts.utils.FileUtils;
2325

2426
/**
@@ -29,16 +31,23 @@ public class JsonConfigResourcesManager {
2931

3032
private static final JsonConfigResourcesManager INSTANCE = new JsonConfigResourcesManager();
3133

34+
private static final IPath TSCONFIG_JSON_PATH = new Path(FileUtils.TSCONFIG_JSON);
35+
3236
public static JsonConfigResourcesManager getInstance() {
3337
return INSTANCE;
3438
}
3539

36-
private final Map<IFile, TsconfigJson> jsconConfig;
40+
private final Map<IFile, IDETsconfigJson> jsconConfig;
3741

3842
public JsonConfigResourcesManager() {
39-
this.jsconConfig = new HashMap<IFile, TsconfigJson>();
43+
this.jsconConfig = new HashMap<IFile, IDETsconfigJson>();
4044
}
4145

46+
/**
47+
* Remove the given tsconfig.json from the cache.
48+
*
49+
* @param file
50+
*/
4251
public void remove(IFile file) {
4352
synchronized (jsconConfig) {
4453
jsconConfig.remove(file);
@@ -53,29 +62,43 @@ public void remove(IFile file) {
5362
* @return
5463
* @throws CoreException
5564
*/
56-
public TsconfigJson findTsconfig(IResource resource) throws CoreException {
57-
IFile tsconfigFile = WorkbenchResourceUtil.findFileRecursively(resource, FileUtils.TSCONFIG_JSON);
65+
public IDETsconfigJson findTsconfig(IResource resource) throws CoreException {
66+
IFile tsconfigFile = WorkbenchResourceUtil.findFileRecursively(resource, TSCONFIG_JSON_PATH);
5867
if (tsconfigFile != null) {
5968
return getTsconfig(tsconfigFile);
6069
}
6170
return null;
6271
}
6372

64-
private TsconfigJson getTsconfig(IFile tsconfigFile) throws CoreException {
65-
TsconfigJson tsconfig = jsconConfig.get(tsconfigFile);
73+
/**
74+
* Returns the Pojo of the given tsconfig.json file.
75+
*
76+
* @param tsconfigFile
77+
* @return the Pojo of the given tsconfig.json file.
78+
* @throws CoreException
79+
*/
80+
private IDETsconfigJson getTsconfig(IFile tsconfigFile) throws CoreException {
81+
IDETsconfigJson tsconfig = jsconConfig.get(tsconfigFile);
6682
if (tsconfig == null) {
6783
return createTsConfig(tsconfigFile);
6884
}
6985
return tsconfig;
7086
}
7187

72-
private synchronized TsconfigJson createTsConfig(IFile tsconfigFile) throws CoreException {
73-
TsconfigJson tsconfig = jsconConfig.get(tsconfigFile);
88+
/**
89+
* Create Pojo instance of the given tsconfig.json file.
90+
*
91+
* @param tsconfigFile
92+
* @return Pojo instance of the given tsconfig.json file.
93+
* @throws CoreException
94+
*/
95+
private synchronized IDETsconfigJson createTsConfig(IFile tsconfigFile) throws CoreException {
96+
IDETsconfigJson tsconfig = jsconConfig.get(tsconfigFile);
7497
if (tsconfig != null) {
7598
return tsconfig;
7699
}
77100

78-
tsconfig = TsconfigJson.load(tsconfigFile.getContents());
101+
tsconfig = IDETsconfigJson.load(tsconfigFile);
79102
synchronized (jsconConfig) {
80103
jsconConfig.put(tsconfigFile, tsconfig);
81104
}

0 commit comments

Comments
 (0)