Skip to content

Commit

Permalink
Update JsHint to 2.9.5 (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpOOnman authored Jan 8, 2018
1 parent 6ebfab3 commit 56a0283
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 67 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ dependencies {
compile 'com.googlecode.jslint4java:jslint4java:2.0.5'

// JSHint
compile 'pl.gildur:jshint4j:1.0.1'
compile 'pl.touk:jshint4j:2.9.5'

// github connector
compile 'com.jcabi:jcabi-github:0.24'
Expand Down
28 changes: 11 additions & 17 deletions src/main/java/pl/touk/sputnik/processor/jshint/JsHintProcessor.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
package pl.touk.sputnik.processor.jshint;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import com.google.common.base.Strings;
import com.google.common.io.CharStreams;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import pl.gildur.jshint4j.Error;
import pl.gildur.jshint4j.JsHint;
import pl.touk.jshint4j.Error;
import pl.touk.jshint4j.JsHint;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.GeneralOption;
import pl.touk.sputnik.review.Review;
import pl.touk.sputnik.review.ReviewException;
import pl.touk.sputnik.review.ReviewProcessor;
import pl.touk.sputnik.review.ReviewResult;
import pl.touk.sputnik.review.Severity;
import pl.touk.sputnik.review.Violation;
import pl.touk.sputnik.review.*;
import pl.touk.sputnik.review.filter.JavaScriptFilter;
import pl.touk.sputnik.review.transformer.IOFileTransformer;

import com.google.common.base.Strings;
import com.google.common.io.CharStreams;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

@Slf4j
@AllArgsConstructor
Expand Down Expand Up @@ -70,13 +64,13 @@ private String readConfiguration() {
throw new ReviewException("IO exception when reading JSHint configuration.", e);
}
}

private String getConfigurationFileName() {
String configurationFile = config.getProperty(GeneralOption.JSHINT_CONFIGURATION_FILE);
log.info("Using JSHint configuration file {}", configurationFile);
return configurationFile;
}

@Override
public String getName() {
return SOURCE_NAME;
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/pl/touk/sputnik/SputnikAssertions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pl.touk.sputnik;

import org.assertj.core.api.Assertions;
import pl.touk.sputnik.assertion.ViolationAssert;
import pl.touk.sputnik.review.Violation;

public class SputnikAssertions extends Assertions {

public static ViolationAssert assertThat(Violation actual) {
return new ViolationAssert(actual);
}
}
32 changes: 32 additions & 0 deletions src/test/java/pl/touk/sputnik/assertion/ViolationAssert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pl.touk.sputnik.assertion;

import org.assertj.core.api.AbstractAssert;
import pl.touk.sputnik.review.Severity;
import pl.touk.sputnik.review.Violation;

import static org.assertj.core.api.Assertions.assertThat;

public class ViolationAssert extends AbstractAssert<ViolationAssert, Violation> {

public ViolationAssert(Violation actual) {
super(actual, ViolationAssert.class);
}

public ViolationAssert hasLine(int line) {
isNotNull();
assertThat(actual.getLine()).isEqualTo(line);
return this;
}

public ViolationAssert hasMessage(String message) {
isNotNull();
assertThat(actual.getMessage()).isEqualTo(message);
return this;
}

public ViolationAssert hasSeverity(Severity severity) {
isNotNull();
assertThat(actual.getSeverity()).isEqualTo(severity);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,20 @@

import com.google.common.collect.ImmutableList;
import com.google.common.io.Resources;
import org.junit.Before;
import org.junit.Test;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.ConfigurationBuilder;
import pl.touk.sputnik.review.Review;
import pl.touk.sputnik.review.ReviewFile;
import pl.touk.sputnik.review.ReviewFormatterFactory;
import pl.touk.sputnik.review.ReviewResult;
import pl.touk.sputnik.review.*;

import static org.assertj.core.api.Assertions.assertThat;
import static pl.touk.sputnik.SputnikAssertions.assertThat;

public class JsHintProcessorTest {

private JsHintProcessor fixture;
private Configuration config;

@Before
public void setUp() throws Exception {
config = ConfigurationBuilder.initFromResource("jshint/sputnik/noConfigurationFile.properties");
fixture = new JsHintProcessor(config);
}


@Test
public void shouldReturnEmptyResultWhenNoFilesToReview() {
// given
Configuration config = ConfigurationBuilder.initFromResource("jshint/sputnik/noConfigurationFile.properties");
JsHintProcessor fixture = new JsHintProcessor(config);
Review review = new Review(ImmutableList.of(new ReviewFile("test")), ReviewFormatterFactory.get(config));

// when
Expand All @@ -39,35 +27,65 @@ public void shouldReturnEmptyResultWhenNoFilesToReview() {
}

@Test
public void shouldReturnNoViolationsOnSimpleFunction() {
public void shouldReturnOneViolationWithConfigurationOnSimpleFunction() {
// given
Configuration config = ConfigurationBuilder.initFromResource("jshint/sputnik/withConfigurationFile.properties");
JsHintProcessor fixture = new JsHintProcessor(config);
Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile())), ReviewFormatterFactory.get(config));

// when
ReviewResult reviewResult = fixture.process(review);

// then
assertThat(reviewResult).isNotNull();
assertThat(reviewResult.getViolations()).isEmpty();
assertThat(reviewResult.getViolations()).hasSize(2);
assertThat(reviewResult.getViolations().get(0)).hasLine(2)
.hasMessage("Missing semicolon.")
.hasSeverity(Severity.INFO);
assertThat(reviewResult.getViolations().get(1)).hasLine(1)
.hasMessage("'test' is defined but never used.")
.hasSeverity(Severity.INFO);
}

@Test
public void shouldReturnOneViolationWithConfigurationOnSimpleFunction() {
public void shouldReturnViolationsWithConfigurationOnReducerExample() {
// given
config = ConfigurationBuilder.initFromResource("jshint/sputnik/withConfigurationFile.properties");
fixture = new JsHintProcessor(config);
Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile())), ReviewFormatterFactory.get(config));
Configuration config = ConfigurationBuilder.initFromResource("jshint/sputnik/withConfigurationFile.properties");
JsHintProcessor fixture = new JsHintProcessor(config);
Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/reducers.js").getFile())), ReviewFormatterFactory.get(config));

// when
ReviewResult reviewResult = fixture.process(review);

// then
assertThat(reviewResult).isNotNull();
assertThat(reviewResult.getViolations()).hasSize(1);
assertThat(reviewResult.getViolations())
.extracting("message")
.containsOnly(
"'alert' is not defined."
);
assertThat(reviewResult.getViolations()).hasSize(9);
assertThat(reviewResult.getViolations().get(0)).hasLine(4)
.hasMessage("Missing semicolon.")
.hasSeverity(Severity.INFO);
assertThat(reviewResult.getViolations().get(1)).hasLine(10)
.hasMessage("Missing semicolon.")
.hasSeverity(Severity.INFO);
assertThat(reviewResult.getViolations().get(2)).hasLine(11)
.hasMessage("Missing semicolon.")
.hasSeverity(Severity.INFO);
assertThat(reviewResult.getViolations().get(3)).hasLine(13)
.hasMessage("Regular parameters should not come after default parameters.")
.hasSeverity(Severity.INFO);
assertThat(reviewResult.getViolations().get(4)).hasLine(16)
.hasMessage("Missing semicolon.")
.hasSeverity(Severity.INFO);
assertThat(reviewResult.getViolations().get(5)).hasLine(18)
.hasMessage("Missing semicolon.")
.hasSeverity(Severity.INFO);
assertThat(reviewResult.getViolations().get(6)).hasLine(4)
.hasMessage("'combineReducers' is defined but never used.")
.hasSeverity(Severity.INFO);
assertThat(reviewResult.getViolations().get(7)).hasLine(6)
.hasMessage("'ADD_TODO' is defined but never used.")
.hasSeverity(Severity.INFO);
assertThat(reviewResult.getViolations().get(8)).hasLine(7)
.hasMessage("'TOGGLE_TODO' is defined but never used.")
.hasSeverity(Severity.INFO);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
import org.junit.Test;
import pl.touk.sputnik.TestEnvironment;
import pl.touk.sputnik.configuration.ConfigurationBuilder;
import pl.touk.sputnik.review.Review;
import pl.touk.sputnik.review.ReviewFile;
import pl.touk.sputnik.review.ReviewFormatterFactory;
import pl.touk.sputnik.review.ReviewResult;
import pl.touk.sputnik.review.*;

import static org.assertj.core.api.Assertions.assertThat;
import static pl.touk.sputnik.SputnikAssertions.assertThat;

public class JsLintProcessorTest extends TestEnvironment {

Expand Down Expand Up @@ -46,12 +43,13 @@ public void shouldReturnNoViolationsOnSimpleFunction() {

// then
assertThat(reviewResult).isNotNull();
assertThat(reviewResult.getViolations()).hasSize(1);
assertThat(reviewResult.getViolations())
.extracting("message")
.containsOnly(
"Missing 'use strict' statement."
);
assertThat(reviewResult.getViolations()).hasSize(2);
assertThat(reviewResult.getViolations().get(0)).hasLine(2)
.hasMessage("Missing 'use strict' statement.")
.hasSeverity(Severity.INFO);
assertThat(reviewResult.getViolations().get(1)).hasLine(2)
.hasMessage("Expected ';' and instead saw '}'.")
.hasSeverity(Severity.INFO);
}

@Test
Expand All @@ -65,11 +63,12 @@ public void shouldReturnOneViolationWithConfigurationOnSimpleFunction() {

// then
assertThat(reviewResult).isNotNull();
assertThat(reviewResult.getViolations()).hasSize(1);
assertThat(reviewResult.getViolations())
.extracting("message")
.containsOnly(
"Missing 'use strict' statement."
);
assertThat(reviewResult.getViolations()).hasSize(2);
assertThat(reviewResult.getViolations().get(0)).hasLine(2)
.hasMessage("Missing 'use strict' statement.")
.hasSeverity(Severity.INFO);
assertThat(reviewResult.getViolations().get(1)).hasLine(2)
.hasMessage("Expected ';' and instead saw '}'.")
.hasSeverity(Severity.INFO);
}
}
22 changes: 22 additions & 0 deletions src/test/resources/js/reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//Part of an original file copied from a Redux documentation:
//https://redux.js.org/docs/basics/Reducers.html

import { combineReducers } from 'redux'
import {
ADD_TODO,
TOGGLE_TODO,
SET_VISIBILITY_FILTER,
VisibilityFilters
} from './actions'
const { SHOW_ALL } = VisibilityFilters

function visibilityFilter(state = SHOW_ALL, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return action.filter
default:
return state
}
}

export default visibilityFilter;
4 changes: 2 additions & 2 deletions src/test/resources/js/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function test() {
alert('foo bar');
}
alert('foo bar')
}
5 changes: 4 additions & 1 deletion src/test/resources/jshint/jshint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"undef": true
"undef": true,
"unused": true,
"devel": true,
"esversion": 6
}

0 comments on commit 56a0283

Please sign in to comment.