Skip to content

Commit ef3d0e7

Browse files
committed
[Fix] #2882 exactCount with considerComments false positive
1 parent 8705121 commit ef3d0e7

File tree

2 files changed

+81
-12
lines changed

2 files changed

+81
-12
lines changed

Diff for: src/rules/newline-after-import.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ module.exports = {
169169
let nextComment;
170170

171171
if (typeof parent.comments !== 'undefined' && options.considerComments) {
172-
nextComment = parent.comments.find((o) => o.loc.start.line === endLine + 1);
172+
nextComment = parent.comments.find((o) => o.loc.start.line <= endLine + options.count + 1);
173173
}
174174

175175
// skip "export import"s

Diff for: tests/src/rules/newline-after-import.js

+80-11
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,25 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
133133
options: [{ count: 2 }],
134134
},
135135
{
136-
code: `import foo from 'foo';\n\n\nvar bar = 'bar';`,
136+
code: `import foo from 'foo';\n\n// Some random comment\nvar bar = 'bar';`,
137137
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
138138
options: [{ count: 2, exactCount: true }],
139139
},
140140
{
141-
code: `import foo from 'foo';\n\nvar bar = 'bar';`,
141+
code: `import foo from 'foo';\n// Some random comment\nvar bar = 'bar';`,
142142
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
143143
options: [{ count: 1, exactCount: true }],
144144
},
145+
{
146+
code: `import foo from 'foo';\n\n\n// Some random comment\nvar bar = 'bar';`,
147+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
148+
options: [{ count: 2, exactCount: true, considerComments: true }],
149+
},
150+
{
151+
code: `import foo from 'foo';\n\n// Some random comment\nvar bar = 'bar';`,
152+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
153+
options: [{ count: 1, exactCount: true, considerComments: true }],
154+
},
145155
{
146156
code: `import foo from 'foo';\n\n\nvar bar = 'bar';`,
147157
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
@@ -167,10 +177,15 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
167177
options: [{ count: 4 }],
168178
},
169179
{
170-
code: `var foo = require('foo-module');\n\n\n\n\nvar foo = 'bar';`,
180+
code: `var foo = require('foo-module');\n\n// Some random comment\n\n\nvar foo = 'bar';`,
171181
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
172182
options: [{ count: 4, exactCount: true }],
173183
},
184+
{
185+
code: `var foo = require('foo-module');\n\n\n\n// Some random comment\nvar foo = 'bar';`,
186+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
187+
options: [{ count: 4, exactCount: true, considerComments: true }],
188+
},
174189
{
175190
code: `require('foo-module');\n\nvar foo = 'bar';`,
176191
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
@@ -651,8 +666,8 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
651666
parser: parsers.BABEL_OLD,
652667
})) || [],
653668
{
654-
code: `import foo from 'foo';\n\nexport default function() {};`,
655-
output: `import foo from 'foo';\n\n\nexport default function() {};`,
669+
code: `import foo from 'foo';\n// some random comment\nexport default function() {};`,
670+
output: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
656671
options: [{ count: 2, exactCount: true }],
657672
errors: [{
658673
line: 1,
@@ -662,8 +677,8 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
662677
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
663678
},
664679
{
665-
code: `import foo from 'foo';\n\n\n\nexport default function() {};`,
666-
output: `import foo from 'foo';\n\n\n\nexport default function() {};`,
680+
code: `import foo from 'foo';\n// some random comment\n\n\nexport default function() {};`,
681+
output: `import foo from 'foo';\n// some random comment\n\n\nexport default function() {};`,
667682
options: [{ count: 2, exactCount: true }],
668683
errors: [{
669684
line: 1,
@@ -673,8 +688,8 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
673688
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
674689
},
675690
{
676-
code: `import foo from 'foo';\n\n\n\n\nexport default function() {};`,
677-
output: `import foo from 'foo';\n\n\n\n\nexport default function() {};`,
691+
code: `import foo from 'foo';\n// some random comment\n\n\n\nexport default function() {};`,
692+
output: `import foo from 'foo';\n// some random comment\n\n\n\nexport default function() {};`,
678693
options: [{ count: 2, exactCount: true }],
679694
errors: [{
680695
line: 1,
@@ -683,6 +698,39 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
683698
}],
684699
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
685700
},
701+
{
702+
code: `import foo from 'foo';\n// some random comment\nexport default function() {};`,
703+
output: `import foo from 'foo';\n\n\n// some random comment\nexport default function() {};`,
704+
options: [{ count: 2, exactCount: true, considerComments: true }],
705+
errors: [{
706+
line: 1,
707+
column: 1,
708+
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
709+
}],
710+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
711+
},
712+
{
713+
code: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
714+
output: `import foo from 'foo';\n\n\n// some random comment\nexport default function() {};`,
715+
options: [{ count: 2, exactCount: true, considerComments: true }],
716+
errors: [{
717+
line: 1,
718+
column: 1,
719+
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
720+
}],
721+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
722+
},
723+
{
724+
code: `import foo from 'foo';\n\n\n\n// some random comment\nexport default function() {};`,
725+
output: `import foo from 'foo';\n\n\n\n// some random comment\nexport default function() {};`,
726+
options: [{ count: 2, exactCount: true, considerComments: true }],
727+
errors: [{
728+
line: 1,
729+
column: 1,
730+
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
731+
}],
732+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
733+
},
686734
{
687735
code: `
688736
import foo from 'foo';
@@ -718,8 +766,8 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
718766
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
719767
},
720768
{
721-
code: `const foo = require('foo');\n\n\n\nconst bar = function() {};`,
722-
output: `const foo = require('foo');\n\n\n\nconst bar = function() {};`,
769+
code: `const foo = require('foo');\n\n\n\n// some random comment\nconst bar = function() {};`,
770+
output: `const foo = require('foo');\n\n\n\n// some random comment\nconst bar = function() {};`,
723771
options: [{ count: 2, exactCount: true }],
724772
errors: [{
725773
line: 1,
@@ -728,5 +776,26 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
728776
}],
729777
parserOptions: { ecmaVersion: 2015 },
730778
},
779+
{
780+
code: `import foo from 'foo';// some random comment\nexport default function() {};`,
781+
output: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
782+
options: [{ count: 1, exactCount: true, considerComments: true }],
783+
errors: [{
784+
line: 1,
785+
column: 1,
786+
message: IMPORT_ERROR_MESSAGE,
787+
}],
788+
parserOptions: { ecmaVersion: 2015, considerComments: true, sourceType: 'module' },
789+
},
790+
{
791+
code: `const foo = require('foo');\n\n\n// some random comment\nconst bar = function() {};`,
792+
options: [{ count: 2, exactCount: true, considerComments: true }],
793+
errors: [{
794+
line: 1,
795+
column: 1,
796+
message: 'Expected 2 empty lines after require statement not followed by another require.',
797+
}],
798+
parserOptions: { ecmaVersion: 2015 },
799+
},
731800
),
732801
});

0 commit comments

Comments
 (0)