Skip to content

Commit 1251dc8

Browse files
committed
Filter comments from JSHint config in workspace prefs
The CommentsFilter had been added in commit 8ed2bdc, but enabled only for project-specific configs, not for the config in the workspace preferences. Fixes #76
1 parent 4f35dc2 commit 1251dc8

File tree

3 files changed

+79
-22
lines changed

3 files changed

+79
-22
lines changed

bundles/com.eclipsesource.jshint.ui/src/com/eclipsesource/jshint/ui/internal/builder/CommentsFilter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2013 EclipseSource and others.
2+
* Copyright (c) 2013, 2014 EclipseSource and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -11,7 +11,6 @@
1111
package com.eclipsesource.jshint.ui.internal.builder;
1212

1313

14-
1514
public class CommentsFilter {
1615

1716
private final char[] chars;
@@ -56,4 +55,8 @@ public String toString() {
5655
return new String( chars );
5756
}
5857

58+
public static String filterComments( String input ) {
59+
return new CommentsFilter( input ).toString();
60+
}
61+
5962
}
Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2013 EclipseSource and others.
2+
* Copyright (c) 2013, 2014 EclipseSource and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -10,16 +10,21 @@
1010
******************************************************************************/
1111
package com.eclipsesource.jshint.ui.internal.builder;
1212

13+
import java.io.IOException;
14+
1315
import org.eclipse.core.resources.IFile;
1416
import org.eclipse.core.resources.IProject;
17+
import org.eclipse.core.runtime.CoreException;
1518
import org.osgi.service.prefs.Preferences;
1619

1720
import com.eclipsesource.jshint.ui.internal.Activator;
1821
import com.eclipsesource.jshint.ui.internal.preferences.OptionsPreferences;
1922
import com.eclipsesource.jshint.ui.internal.preferences.PreferencesFactory;
20-
import com.eclipsesource.jshint.ui.internal.util.IOUtil;
2123
import com.eclipsesource.json.JsonObject;
2224

25+
import static com.eclipsesource.jshint.ui.internal.builder.CommentsFilter.filterComments;
26+
import static com.eclipsesource.jshint.ui.internal.util.IOUtil.readFileUtf8;
27+
2328

2429
public class ConfigLoader {
2530

@@ -38,34 +43,46 @@ public JsonObject getConfiguration() {
3843
return getWorkspaceConfig();
3944
}
4045

41-
private static JsonObject getWorkspaceConfig() {
42-
Preferences workspaceNode = PreferencesFactory.getWorkspacePreferences();
43-
return JsonObject.readFrom( new OptionsPreferences( workspaceNode ).getConfig() );
46+
private JsonObject getProjectConfig( OptionsPreferences projectPrefs ) {
47+
try {
48+
String json = getProjectConfigJson( projectPrefs );
49+
return JsonObject.readFrom( filterComments( json ) );
50+
} catch( Exception exception ) {
51+
String message = "Failed to read jshint configuration for project " + project.getName();
52+
Activator.logError( message, exception );
53+
return new JsonObject();
54+
}
4455
}
4556

46-
private JsonObject getProjectConfig( OptionsPreferences projectPrefs ) {
47-
IFile configFile = getConfigFile();
48-
// compatibility
57+
private String getProjectConfigJson( OptionsPreferences projectPrefs ) throws CoreException,
58+
IOException
59+
{
60+
IFile configFile = getProjectConfigFile();
4961
if( !configFile.exists() ) {
50-
return JsonObject.readFrom( projectPrefs.getConfig() );
62+
// compatibility
63+
return projectPrefs.getConfig();
5164
}
52-
return readConfig( configFile );
65+
return readFileUtf8( configFile );
5366
}
5467

55-
private IFile getConfigFile() {
68+
private IFile getProjectConfigFile() {
5669
return project.getFile( ".jshintrc" );
5770
}
5871

59-
private JsonObject readConfig( IFile file ) {
72+
private static JsonObject getWorkspaceConfig() {
6073
try {
61-
String contents = IOUtil.readFileUtf8( file );
62-
String filtered = new CommentsFilter( contents ).toString();
63-
return JsonObject.readFrom( filtered );
74+
String json = getWorkspaceConfigJson();
75+
return JsonObject.readFrom( filterComments( json ) );
6476
} catch( Exception exception ) {
65-
String message = "Failed to read jshint configuration for project " + project.getName();
77+
String message = "Failed to read jshint configuration from workspace preferences";
6678
Activator.logError( message, exception );
79+
return new JsonObject();
6780
}
68-
return new JsonObject();
81+
}
82+
83+
private static String getWorkspaceConfigJson() {
84+
Preferences workspaceNode = PreferencesFactory.getWorkspacePreferences();
85+
return new OptionsPreferences( workspaceNode ).getConfig();
6986
}
7087

7188
}

tests/com.eclipsesource.jshint.ui.test/src/com/eclipsesource/jshint/ui/internal/builder/ConfigLoader_Test.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2013 EclipseSource.
2+
* Copyright (c) 2013, 2014 EclipseSource and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -10,10 +10,13 @@
1010
******************************************************************************/
1111
package com.eclipsesource.jshint.ui.internal.builder;
1212

13+
import java.util.Arrays;
14+
1315
import org.eclipse.core.resources.IProject;
1416
import org.junit.After;
1517
import org.junit.Before;
1618
import org.junit.Test;
19+
import org.osgi.service.prefs.BackingStoreException;
1720

1821
import com.eclipsesource.jshint.ui.internal.preferences.OptionsPreferences;
1922
import com.eclipsesource.jshint.ui.internal.preferences.PreferencesFactory;
@@ -39,8 +42,9 @@ public void setUp() {
3942
}
4043

4144
@After
42-
public void tearDown() {
45+
public void tearDown() throws BackingStoreException {
4346
deleteProject( project );
47+
PreferencesFactory.getWorkspacePreferences().clear();
4448
}
4549

4650
@Test
@@ -56,7 +60,7 @@ public void usesWorkspaceOptionsByDefault() {
5660
}
5761

5862
@Test
59-
public void ignoresWorkspaceOptions_ifProjectSpecific() {
63+
public void ignoresWorkspaceOptionsIfProjectSpecific() {
6064
workspacePrefs.getNode().put( "options", "a: 1, b: 1" );
6165
createProjectConfig( new JsonObject().add( "b", 2 ).add( "c", 2 ) );
6266
projectPrefs.setProjectSpecific( true );
@@ -68,6 +72,18 @@ public void ignoresWorkspaceOptions_ifProjectSpecific() {
6872
assertEquals( 2, configuration.get( "c" ).asInt() );
6973
}
7074

75+
@Test
76+
public void usesWorkspaceConfigIfNotProjectSpecific() {
77+
workspacePrefs.setConfig( "{\"a\": 1, \"b\": 1}" );
78+
createProjectConfig( new JsonObject().add( "b", 2 ).add( "c", 2 ) );
79+
80+
JsonObject configuration = new ConfigLoader( project ).getConfiguration();
81+
82+
assertEquals( 1, configuration.get( "a" ).asInt() );
83+
assertEquals( 1, configuration.get( "b" ).asInt() );
84+
assertNull( configuration.get( "c" ) );
85+
}
86+
7187
@Test
7288
public void fallsBackToOldProjectProperties_ifConfigFileMissing() {
7389
projectPrefs.setProjectSpecific( true );
@@ -102,6 +118,27 @@ public void emptyConfigForProjectsWithoutConfigFileAndProperties() {
102118
assertEquals( new JsonObject(), configuration );
103119
}
104120

121+
@Test
122+
public void filtersCommentsFromProjectConfig() {
123+
projectPrefs.setConfig( "{\n// \"a\": 1,\n\"b\": 2 /*, \"c\": 3*/}" );
124+
projectPrefs.setProjectSpecific( true );
125+
126+
JsonObject configuration = new ConfigLoader( project ).getConfiguration();
127+
128+
assertEquals( new JsonObject().add( "b", 2 ), configuration );
129+
assertEquals( Arrays.asList( "b" ), configuration.names() );
130+
}
131+
132+
@Test
133+
public void filtersCommentsFromWorkspaceConfig() {
134+
workspacePrefs.setConfig( "{\n// \"a\": 1,\n\"b\": 2 /*, \"c\": 3*/}" );
135+
136+
JsonObject configuration = new ConfigLoader( project ).getConfiguration();
137+
138+
assertEquals( new JsonObject().add( "b", 2 ), configuration );
139+
assertEquals( Arrays.asList( "b" ), configuration.names() );
140+
}
141+
105142
private void createProjectConfig( JsonObject projectConfig ) {
106143
TestUtil.createFile( project, ".jshintrc", projectConfig.toString() );
107144
}

0 commit comments

Comments
 (0)