Skip to content

Commit 4f35dc2

Browse files
committed
Make JSHint errors optional
It turned out that there are cases where JSHint errors are too strict and should not be treated as errors in Eclipse. Make JSHint errors optional. They can be enabled in the JSHint preference page. Unless enabled, all JSHint errors are treated as warnings. Fix #71
1 parent 1816f46 commit 4f35dc2

File tree

5 files changed

+148
-47
lines changed

5 files changed

+148
-47
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616
import com.eclipsesource.jshint.ProblemHandler;
1717
import com.eclipsesource.jshint.Text;
1818
import com.eclipsesource.jshint.ui.internal.builder.JSHintBuilder.CoreExceptionWrapper;
19+
import com.eclipsesource.jshint.ui.internal.preferences.JSHintPreferences;
1920

2021

2122
final class MarkerHandler implements ProblemHandler {
2223

2324
private final MarkerAdapter markerAdapter;
2425
private final Text code;
26+
private final boolean enableErrorMarkers;
2527

2628
MarkerHandler( MarkerAdapter markerAdapter, Text code ) {
2729
this.markerAdapter = markerAdapter;
2830
this.code = code;
31+
enableErrorMarkers = new JSHintPreferences().getEnableErrorMarkers();
2932
}
3033

3134
public void handleProblem( Problem problem ) {
@@ -46,7 +49,7 @@ private void createMarker( int line, int character, String message, boolean isEr
4649
throws CoreExceptionWrapper
4750
{
4851
try {
49-
if( isError ) {
52+
if( enableErrorMarkers && isError ) {
5053
markerAdapter.createError( line, character, character, message );
5154
} else {
5255
markerAdapter.createWarning( line, character, character, message );

bundles/com.eclipsesource.jshint.ui/src/com/eclipsesource/jshint/ui/internal/preferences/JSHintPreferences.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2012 EclipseSource.
2+
* Copyright (c) 2012, 2013 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
@@ -37,14 +37,17 @@ public class JSHintPreferences {
3737

3838
private static final String KEY_USE_CUSTOM_LIB = "useCustomJshint";
3939
private static final String KEY_CUSTOM_LIB_PATH = "customJshintPath";
40+
private static final String KEY_ENABLE_ERROR_MARKERS = "enableErrorMarkers";
4041
private static final boolean DEF_USE_CUSTOM_LIB = false;
4142
private static final String DEF_CUSTOM_LIB_PATH = "";
43+
private static final boolean DEF_ENABLE_ERROR_MARKERS = false;
4244

4345
private final Lock readLock;
4446
private final Lock writeLock;
4547
private final Preferences node;
4648
private boolean useCustomLib;
4749
private String customLibPath;
50+
private boolean enableErrorMarkers;
4851
private boolean dirty;
4952

5053
public JSHintPreferences() {
@@ -54,12 +57,14 @@ public JSHintPreferences() {
5457
node = PreferencesFactory.getWorkspacePreferences();
5558
useCustomLib = node.getBoolean( KEY_USE_CUSTOM_LIB, DEF_USE_CUSTOM_LIB );
5659
customLibPath = node.get( KEY_CUSTOM_LIB_PATH, DEF_CUSTOM_LIB_PATH );
60+
enableErrorMarkers = node.getBoolean( KEY_ENABLE_ERROR_MARKERS, DEF_ENABLE_ERROR_MARKERS );
5761
dirty = false;
5862
}
5963

6064
public void resetToDefaults() {
6165
setUseCustomLib( DEF_USE_CUSTOM_LIB );
6266
setCustomLibPath( DEF_CUSTOM_LIB_PATH );
67+
setEnableErrorMarkers( DEF_ENABLE_ERROR_MARKERS );
6368
}
6469

6570
public boolean getUseCustomLib() {
@@ -104,6 +109,27 @@ public void setCustomLibPath( String customLibPath ) {
104109
}
105110
}
106111

112+
public boolean getEnableErrorMarkers() {
113+
try {
114+
readLock.lock();
115+
return enableErrorMarkers;
116+
} finally {
117+
readLock.unlock();
118+
}
119+
}
120+
121+
public void setEnableErrorMarkers( boolean enableErrorMarkers ) {
122+
try {
123+
writeLock.lock();
124+
if( enableErrorMarkers != this.enableErrorMarkers ) {
125+
this.enableErrorMarkers = enableErrorMarkers;
126+
dirty = true;
127+
}
128+
} finally {
129+
writeLock.unlock();
130+
}
131+
}
132+
107133
public boolean hasChanged() {
108134
try {
109135
readLock.lock();
@@ -116,6 +142,7 @@ public boolean hasChanged() {
116142
public void save() throws CoreException {
117143
putUseCustomLib();
118144
putCustomLibPath();
145+
putEnableErrorMarkers();
119146
flushNode();
120147
try {
121148
writeLock.lock();
@@ -151,6 +178,19 @@ private void putCustomLibPath() {
151178
}
152179
}
153180

181+
private void putEnableErrorMarkers() {
182+
try {
183+
readLock.lock();
184+
if( enableErrorMarkers == DEF_ENABLE_ERROR_MARKERS ) {
185+
node.remove( KEY_ENABLE_ERROR_MARKERS );
186+
} else {
187+
node.putBoolean( KEY_ENABLE_ERROR_MARKERS, enableErrorMarkers );
188+
}
189+
} finally {
190+
readLock.unlock();
191+
}
192+
}
193+
154194
private void flushNode() throws CoreException {
155195
try {
156196
node.flush();
@@ -160,4 +200,5 @@ private void flushNode() throws CoreException {
160200
throw new CoreException( status );
161201
}
162202
}
203+
163204
}

bundles/com.eclipsesource.jshint.ui/src/com/eclipsesource/jshint/ui/internal/preferences/ui/JSHintPreferencePage.java

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
import org.eclipse.jface.preference.IPreferenceStore;
2424
import org.eclipse.jface.preference.PreferencePage;
2525
import org.eclipse.swt.SWT;
26-
import org.eclipse.swt.events.ModifyEvent;
27-
import org.eclipse.swt.events.ModifyListener;
2826
import org.eclipse.swt.events.SelectionAdapter;
2927
import org.eclipse.swt.events.SelectionEvent;
3028
import org.eclipse.swt.widgets.Button;
3129
import org.eclipse.swt.widgets.Composite;
3230
import org.eclipse.swt.widgets.Control;
3331
import org.eclipse.swt.widgets.Display;
32+
import org.eclipse.swt.widgets.Event;
3433
import org.eclipse.swt.widgets.FileDialog;
34+
import org.eclipse.swt.widgets.Listener;
3535
import org.eclipse.swt.widgets.Text;
3636
import org.eclipse.ui.IWorkbench;
3737
import org.eclipse.ui.IWorkbenchPreferencePage;
@@ -49,10 +49,11 @@
4949
public class JSHintPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
5050

5151
private final JSHintPreferences preferences;
52-
private Button defaultLibButton;
53-
private Button customLibButton;
52+
private Button defaultLibRadio;
53+
private Button customLibRadio;
5454
private Text customLibPathText;
5555
private Button customLibPathButton;
56+
private Button enableErrorsCheckbox;
5657

5758
public JSHintPreferencePage() {
5859
setPreferenceStore( Activator.getDefault().getPreferenceStore() );
@@ -71,9 +72,11 @@ protected IPreferenceStore doGetPreferenceStore() {
7172
@Override
7273
protected Control createContents( Composite parent ) {
7374
Composite composite = new Composite( parent, SWT.NONE );
74-
gridLayout( composite ).columns( 3 ).marginTop( 10 );
75+
gridLayout( composite ).columns( 3 ).spacing( 3 ).marginTop( 10 );
7576
createCustomJSHintArea( composite );
76-
updateControls();
77+
createEnableErrorMarkersArea( composite );
78+
updateControlsFromPrefs();
79+
updateControlsEnabled();
7780
return composite;
7881
}
7982

@@ -94,45 +97,59 @@ public boolean performOk() {
9497
@Override
9598
protected void performDefaults() {
9699
preferences.resetToDefaults();
97-
updateControls();
100+
updateControlsFromPrefs();
101+
updateControlsEnabled();
98102
super.performDefaults();
99103
}
100104

101105
private void createCustomJSHintArea( Composite parent ) {
102-
defaultLibButton = new Button( parent, SWT.RADIO );
103-
defaultLibButton.setText( "Use the &built-in JSHint library (version "
104-
+ JSHint.getDefaultLibraryVersion()
105-
+ ")" );
106-
gridData( defaultLibButton ).fillHorizontal().span( 3, 1 );
107-
customLibButton = new Button( parent, SWT.RADIO );
108-
customLibButton.setText( "Provide a &custom JSHint library file" );
109-
gridData( customLibButton ).fillHorizontal().span( 3, 1 );
110-
customLibButton.addSelectionListener( new SelectionAdapter() {
111-
@Override
112-
public void widgetSelected( SelectionEvent e ) {
113-
updateValuesFromControls();
114-
updateControls();
106+
defaultLibRadio = new Button( parent, SWT.RADIO );
107+
String version = JSHint.getDefaultLibraryVersion();
108+
defaultLibRadio.setText( "Use the &built-in JSHint library (version " + version + ")" );
109+
gridData( defaultLibRadio ).fillHorizontal().span( 3, 1 );
110+
customLibRadio = new Button( parent, SWT.RADIO );
111+
customLibRadio.setText( "Provide a &custom JSHint library file" );
112+
gridData( customLibRadio ).fillHorizontal().span( 3, 1 );
113+
customLibRadio.addListener( SWT.Selection, new Listener() {
114+
public void handleEvent( Event event ) {
115+
preferences.setUseCustomLib( customLibRadio.getSelection() );
116+
validate();
117+
updateControlsEnabled();
115118
}
116119
} );
117120
customLibPathText = new Text( parent, SWT.BORDER );
118121
gridData( customLibPathText ).fillHorizontal().span( 2, 1 ).indent( 25, 0 );
119-
customLibPathText.addModifyListener( new ModifyListener() {
120-
public void modifyText( ModifyEvent e ) {
121-
updateValuesFromControls();
122+
customLibPathText.addListener( SWT.Modify, new Listener() {
123+
public void handleEvent( Event event ) {
124+
preferences.setCustomLibPath( customLibPathText.getText() );
125+
validate();
122126
}
123127
} );
124128
customLibPathButton = new Button( parent, SWT.PUSH );
125129
customLibPathButton.setText( "Select" );
126-
customLibPathButton.addSelectionListener( new SelectionAdapter() {
127-
@Override
128-
public void widgetSelected( SelectionEvent e ) {
130+
customLibPathButton.addListener( SWT.Selection, new Listener() {
131+
public void handleEvent( Event event ) {
129132
selectFile();
130133
}
131134
} );
132135
Text customLibPathLabelText = new Text( parent, SWT.READ_ONLY | SWT.WRAP );
133136
customLibPathLabelText.setText( "This file is usually named 'jshint.js'." );
134137
customLibPathLabelText.setBackground( parent.getBackground() );
135-
gridData( customLibPathLabelText ).fillBoth().span( 2, 1 ).indent( 25, 1 );
138+
gridData( customLibPathLabelText ).fillHorizontal().span( 3, 1 ).indent( 25, 1 );
139+
}
140+
141+
private void createEnableErrorMarkersArea( Composite parent ) {
142+
enableErrorsCheckbox = new Button( parent, SWT.CHECK );
143+
enableErrorsCheckbox.setText( "Enable JSHint errors" );
144+
enableErrorsCheckbox.setToolTipText( "If unchecked, errors will be shown as warnings" );
145+
gridData( enableErrorsCheckbox ).fillHorizontal().span( 3, 1 );
146+
enableErrorsCheckbox.addSelectionListener( new SelectionAdapter() {
147+
@Override
148+
public void widgetSelected( SelectionEvent e ) {
149+
preferences.setEnableErrorMarkers( enableErrorsCheckbox.getSelection() );
150+
validate();
151+
}
152+
} );
136153
}
137154

138155
private void selectFile() {
@@ -145,17 +162,10 @@ private void selectFile() {
145162
fileDialog.setFilterExtensions( new String[] { "*.js", "" } );
146163
String selectedPath = fileDialog.open();
147164
if( selectedPath != null ) {
148-
preferences.setCustomLibPath( selectedPath );
149-
updateControls();
165+
customLibPathText.setText( selectedPath );
150166
}
151167
}
152168

153-
private void updateValuesFromControls() {
154-
preferences.setUseCustomLib( customLibButton.getSelection() );
155-
preferences.setCustomLibPath( customLibPathText.getText() );
156-
validate();
157-
}
158-
159169
private void validate() {
160170
setErrorMessage( null );
161171
setValid( false );
@@ -189,8 +199,7 @@ public void run() {
189199
private void validatePrefs() {
190200
if( preferences.getUseCustomLib() ) {
191201
String path = preferences.getCustomLibPath();
192-
File file = new File( path );
193-
validateFile( file );
202+
validateFile( new File( path ) );
194203
}
195204
}
196205

@@ -214,13 +223,17 @@ private static void validateFile( File file ) throws IllegalArgumentException {
214223
}
215224
}
216225

217-
private void updateControls() {
218-
boolean useCustomLib = preferences.getUseCustomLib();
219-
defaultLibButton.setSelection( !useCustomLib );
220-
customLibButton.setSelection( useCustomLib );
226+
private void updateControlsFromPrefs() {
227+
customLibRadio.setSelection( preferences.getUseCustomLib() );
228+
defaultLibRadio.setSelection( !customLibRadio.getSelection() );
221229
customLibPathText.setText( preferences.getCustomLibPath() );
222-
customLibPathText.setEnabled( useCustomLib );
223-
customLibPathButton.setEnabled( useCustomLib );
230+
enableErrorsCheckbox.setSelection( preferences.getEnableErrorMarkers() );
231+
}
232+
233+
private void updateControlsEnabled() {
234+
boolean enabled = customLibRadio.getSelection();
235+
customLibPathText.setEnabled( enabled );
236+
customLibPathButton.setEnabled( enabled );
224237
}
225238

226239
private void triggerRebuild() throws CoreException {

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
package com.eclipsesource.jshint.ui.internal.builder;
1212

1313
import org.eclipse.core.runtime.CoreException;
14+
import org.junit.After;
1415
import org.junit.Before;
1516
import org.junit.Test;
1617

1718
import com.eclipsesource.jshint.Problem;
1819
import com.eclipsesource.jshint.Text;
20+
import com.eclipsesource.jshint.ui.internal.preferences.JSHintPreferences;
1921

2022
import static org.mockito.Mockito.*;
2123

@@ -29,6 +31,13 @@ public void setUp() {
2931
adapter = mock( MarkerAdapter.class );
3032
}
3133

34+
@After
35+
public void tearDown() throws CoreException {
36+
JSHintPreferences prefs = new JSHintPreferences();
37+
prefs.resetToDefaults();
38+
prefs.save();
39+
}
40+
3241
@Test
3342
public void handleProblem_createsWarning() throws CoreException {
3443
MarkerHandler handler = new MarkerHandler( adapter, new Text( "test" ) );
@@ -39,7 +48,19 @@ public void handleProblem_createsWarning() throws CoreException {
3948
}
4049

4150
@Test
42-
public void handleProblem_createsError() throws CoreException {
51+
public void handleProblem_createsWarningForError() throws CoreException {
52+
MarkerHandler handler = new MarkerHandler( adapter, new Text( "test" ) );
53+
54+
handler.handleProblem( mockError( 1, 2, "foo" ) );
55+
56+
verify( adapter ).createWarning( 1, 2, 2, "foo" );
57+
}
58+
59+
@Test
60+
public void handleProblem_createsError_ifEnabled() throws CoreException {
61+
JSHintPreferences prefs = new JSHintPreferences();
62+
prefs.setEnableErrorMarkers( true );
63+
prefs.save();
4364
MarkerHandler handler = new MarkerHandler( adapter, new Text( "test" ) );
4465

4566
handler.handleProblem( mockError( 1, 2, "foo" ) );

0 commit comments

Comments
 (0)