Skip to content

Commit

Permalink
[Fix] #2882 exactCount with considerComments false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
kinland committed Sep 26, 2023
1 parent 8705121 commit dbd4ad7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ module.exports = {
let nextComment;

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

// skip "export import"s
Expand Down
92 changes: 81 additions & 11 deletions tests/src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,25 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
options: [{ count: 2 }],
},
{
code: `import foo from 'foo';\n\n\nvar bar = 'bar';`,
code: `import foo from 'foo';\n\n// Some random comment\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 2, exactCount: true }],
},
{
code: `import foo from 'foo';\n\nvar bar = 'bar';`,
code: `import foo from 'foo';\n// Some random comment\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 1, exactCount: true }],
},
{
code: `import foo from 'foo';\n\n\n// Some random comment\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 2, exactCount: true, considerComments: true }],
},
{
code: `import foo from 'foo';\n\n// Some random comment\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 1, exactCount: true, considerComments: true }],
},
{
code: `import foo from 'foo';\n\n\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
Expand All @@ -167,10 +177,15 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
options: [{ count: 4 }],
},
{
code: `var foo = require('foo-module');\n\n\n\n\nvar foo = 'bar';`,
code: `var foo = require('foo-module');\n\n// Some random comment\n\n\nvar foo = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 4, exactCount: true }],
},
{
code: `var foo = require('foo-module');\n\n\n\n// Some random comment\nvar foo = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 4, exactCount: true, considerComments: true }],
},
{
code: `require('foo-module');\n\nvar foo = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
Expand Down Expand Up @@ -651,8 +666,8 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parser: parsers.BABEL_OLD,
})) || [],
{
code: `import foo from 'foo';\n\nexport default function() {};`,
output: `import foo from 'foo';\n\n\nexport default function() {};`,
code: `import foo from 'foo';\n// some random comment\nexport default function() {};`,
output: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
options: [{ count: 2, exactCount: true }],
errors: [{
line: 1,
Expand All @@ -662,8 +677,8 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n\n\n\nexport default function() {};`,
output: `import foo from 'foo';\n\n\n\nexport default function() {};`,
code: `import foo from 'foo';\n// some random comment\n\n\nexport default function() {};`,
output: `import foo from 'foo';\n// some random comment\n\n\nexport default function() {};`,
options: [{ count: 2, exactCount: true }],
errors: [{
line: 1,
Expand All @@ -673,8 +688,8 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n\n\n\n\nexport default function() {};`,
output: `import foo from 'foo';\n\n\n\n\nexport default function() {};`,
code: `import foo from 'foo';\n// some random comment\n\n\n\nexport default function() {};`,
output: `import foo from 'foo';\n// some random comment\n\n\n\nexport default function() {};`,
options: [{ count: 2, exactCount: true }],
errors: [{
line: 1,
Expand All @@ -683,6 +698,39 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n// some random comment\nexport default function() {};`,
output: `import foo from 'foo';\n\n\n// some random comment\nexport default function() {};`,
options: [{ count: 2, exactCount: true, considerComments: true }],
errors: [{
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
output: `import foo from 'foo';\n\n\n// some random comment\nexport default function() {};`,
options: [{ count: 2, exactCount: true, considerComments: true }],
errors: [{
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n\n\n\n// some random comment\nexport default function() {};`,
output: `import foo from 'foo';\n\n\n\n// some random comment\nexport default function() {};`,
options: [{ count: 2, exactCount: true, considerComments: true }],
errors: [{
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `
import foo from 'foo';
Expand Down Expand Up @@ -718,8 +766,8 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `const foo = require('foo');\n\n\n\nconst bar = function() {};`,
output: `const foo = require('foo');\n\n\n\nconst bar = function() {};`,
code: `const foo = require('foo');\n\n\n\n// some random comment\nconst bar = function() {};`,
output: `const foo = require('foo');\n\n\n\n// some random comment\nconst bar = function() {};`,
options: [{ count: 2, exactCount: true }],
errors: [{
line: 1,
Expand All @@ -728,5 +776,27 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
}],
parserOptions: { ecmaVersion: 2015 },
},
{
code: `import foo from 'foo';// some random comment\nexport default function() {};`,
output: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
options: [{ count: 1, exactCount: true, considerComments: true }],
errors: [{
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE,
}],
parserOptions: { ecmaVersion: 2015, considerComments: true, sourceType: 'module' },
},
{
code: `const foo = require('foo');\n\n\n\n// some random comment\nconst bar = function() {};`,
code: `const foo = require('foo');\n\n\n// some random comment\nconst bar = function() {};`,
options: [{ count: 2, exactCount: true, considerComments: true }],
errors: [{
line: 1,
column: 1,
message: 'Expected 2 empty lines after require statement not followed by another require.',
}],
parserOptions: { ecmaVersion: 2015 },
},
),
});

0 comments on commit dbd4ad7

Please sign in to comment.