Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit ea38457

Browse files
Alexej Yaroshevichmikesherov
authored andcommitted
(require|disallow)TrailingComma: fixed error location
Fixes #1018 Closes gh-1019
1 parent bdf0e99 commit ea38457

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

lib/token-assert.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ TokenAssert.prototype.tokenBefore = function(options) {
152152
}
153153
this.emit('error', {
154154
message: message,
155-
line: token.loc.start.line,
156-
column: token.loc.start.column
155+
line: actualTokenBefore.loc.end.line,
156+
column: actualTokenBefore.loc.end.column
157157
});
158158
}
159159
};
@@ -177,8 +177,8 @@ TokenAssert.prototype.noTokenBefore = function(options) {
177177
) {
178178
this.emit('error', {
179179
message: options.message || 'Illegal ' + expectedTokenBefore.value + ' was found before ' + token.value,
180-
line: token.loc.start.line,
181-
column: token.loc.start.column
180+
line: actualTokenBefore.loc.end.line,
181+
column: actualTokenBefore.loc.end.column
182182
});
183183
}
184184
};

test/rules/disallow-trailing-comma.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@ describe('rules/disallow-trailing-comma', function() {
2424
assert(checker.checkString('var x = [1, 2,\n]').getErrorCount() === 1);
2525
});
2626

27+
it('should report right location for trailing comma in object (#1018)', function() {
28+
var errs = checker.checkString('var obj = {\n foo: "foo",\n};').getErrorList();
29+
assert.equal(errs[0].line + ':' + errs[0].column, '2:15');
30+
});
31+
32+
it('should report right location for trailing comma in array (#1018)', function() {
33+
var errs = checker.checkString('var arr = [\n \'foo\',\n];').getErrorList();
34+
assert.equal(errs[0].line + ':' + errs[0].column, '2:10');
35+
});
36+
2737
});

test/rules/require-trailing-comma.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ describe('rules/require-trailing-comma', function() {
4545
it('should not report array access (#368)', function() {
4646
assert(checker.checkString('var foo = [\'Hello World\',\n];\nvar bar = foo[0];').getErrorCount() === 0);
4747
});
48+
49+
it('should report right location for no trailing comma in object (#1018)', function() {
50+
var errs = checker.checkString('var obj = {\n foo: "foo"\n};').getErrorList();
51+
assert.equal(errs[0].line + ':' + errs[0].column, '2:14');
52+
});
53+
54+
it('should report right location for no trailing comma in array (#1018)', function() {
55+
var errs = checker.checkString('var arr = [\n \'foo\'\n];').getErrorList();
56+
assert.equal(errs[0].line + ':' + errs[0].column, '2:9');
57+
});
4858
});
4959

5060
describe('ignoreSingleValue', function() {

0 commit comments

Comments
 (0)