Skip to content

Commit

Permalink
Fix offset in marker position
Browse files Browse the repository at this point in the history
Character positions in jshint errors are always 1-relative, but Eclipse
markers treat them as 0-relative. Adjust character index reported by
jshint to be 0-relative.

Fix eclipsesource#19
  • Loading branch information
ralfstx committed Jul 8, 2012
1 parent da45f44 commit cb326e1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import static org.hamcrest.Matchers.greaterThan;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;


@RunWith( value = Parameterized.class )
Expand Down Expand Up @@ -59,21 +58,31 @@ public void setUp() throws IOException {
}

@Test
public void checkValid_noProblems() {
public void check_noProblemsIfValid() {
jsHint.check( "var a = 23;", handler );

assertTrue( problems.isEmpty() );
}

@Test
@SuppressWarnings( "boxing" )
public void checkInvalid_problemFieldsSet() {
jsHint.check( "hmpf!", handler );

assertFalse( problems.isEmpty() );
assertThat( problems.get( 0 ).getLine(), greaterThan( 0 ) );
assertThat( problems.get( 0 ).getCharacter(), greaterThan( 0 ) );
assertThat( problems.get( 0 ).getMessage().length(), greaterThan( 0 ) );
public void check_problemLineIs_1_Relative() {
jsHint.check( "#", handler );

assertEquals( 1, problems.get( 0 ).getLine() );
}

@Test
public void check_problemCharacterIs_0_Relative() {
jsHint.check( "#", handler );

assertEquals( 0, problems.get( 0 ).getCharacter() );
}

@Test
public void check_problemMessageIsNotEmpty() {
jsHint.check( "#", handler );

assertTrue( problems.get( 0 ).getMessage().length() > 0 );
}

private void loadJsHint() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,28 +271,53 @@ public void checkEqNullWithConfig() {
assertTrue( problems.isEmpty() );
}

@Test
public void testProblemLineIs_1_Relative() {
jsHint.configure( new Configuration() );
jsHint.check( "#", handler );

assertEquals( 1, problems.get( 0 ).getLine() );
}

@Test
public void testProblemCharacterIs_0_Relative() {
jsHint.configure( new Configuration() );
jsHint.check( "#", handler );

assertEquals( 0, problems.get( 0 ).getCharacter() );
}

@Test
public void check_problemMessageIsNotEmpty() {
jsHint.configure( new Configuration() );
jsHint.check( "#", handler );

String message = problems.get( 0 ).getMessage();
assertTrue( message.length() > 0 );
}

@Test
public void testPosition() {
jsHint.configure( new Configuration() );
jsHint.check( "var a = x == null ? null : 1;", handler );

assertEquals( "1.11", getPosition( problems.get( 0 ) ) );
assertEquals( "1.10", getPosition( problems.get( 0 ) ) );
}

@Test
public void testPositionWithLeadingSpace() {
jsHint.configure( new Configuration() );
jsHint.check( " var a = x == null ? null : 1;", handler );

assertEquals( "1.12", getPosition( problems.get( 0 ) ) );
assertEquals( "1.11", getPosition( problems.get( 0 ) ) );
}

@Test
public void testPositionWithLeadingTab() {
jsHint.configure( new Configuration() );
jsHint.check( "\tvar a = x == null ? null : 1;", handler );

assertEquals( "1.12", getPosition( problems.get( 0 ) ) );
assertEquals( "1.11", getPosition( problems.get( 0 ) ) );
}

private static String getPosition( Problem problem ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ private ProblemImpl createProblem( ScriptableObject error ) {
String reason = getPropertyAsString( error, "reason", "" );
int line = getPropertyAsInt( error, "line", -1 );
int character = getPropertyAsInt( error, "character", -1 );
if( character > 0 ) {
character -= 1;
}
String message = reason.endsWith( "." ) ? reason.substring( 0, reason.length() - 1 ) : reason;
return new ProblemImpl( line, character, message );
}
Expand Down

0 comments on commit cb326e1

Please sign in to comment.