eslint-plugin-import icon indicating copy to clipboard operation
eslint-plugin-import copied to clipboard

🐞: [import/newline-after-import] False positively reports wrong empty line numbers between imports with comments

Open xsjcTony opened this issue 2 years ago • 2 comments

Something like that:

import './index.scss';

// other imports
import { ErrorBoundary } from 'react-error-boundary';

And my setting is

'import/newline-after-import': [
  'error',
  {
    count: 2,
    exactCount: true,
    considerComments: true
  }
]

It reports like this: image

But this is not the correct behavior, as in the documentation it clearly says:

Enforces having one or more empty lines after the last top-level import statement or require call.

after the last top-level import statement

Note: the exactCount doesn't matter, as I've tried to remove it but it's still causing issue.

Package version: ALL LATEST

xsjcTony avatar Oct 31 '23 23:10 xsjcTony

To bump up priority (hopefully) I want to add some more details.

This bug is very much visible if you use TypeScript and you need to // ts-ignore some import (because of missing types for example):

import typescript from "@typescript-eslint/eslint-plugin";
import parserTs from "@typescript-eslint/parser"; // <--- error reported here
// @ts-ignore
import react from "eslint-plugin-react"; // <--- error reported here
// @ts-ignore
import importPlugin from "eslint-plugin-import";

OP example is "just a comment", that can be omitted (not ideal, but possible), with TS you may be forced to have this comment between imports.

This bug exists for 4 years at least (I think it was there since I started using this plugin) and its autofix can trigger import/order rule errors (if you prohibit new lines between imports).

dzek69 avatar May 26 '24 08:05 dzek69

Am I understanding correctly that this is the same thing you're talking about?

Where, given this configuration:

import { defineConfig } from 'eslint/config';

// test
export default defineConfig([
  {
    rules: {
      'import/newline-after-import': [
        'error',
        { exactCount: true }
      ],
    },
  },
]);

We get a false positive on this error because of the code comment?

If so I'm also running into this, and it's pretty annoying. It prevents a code comment from existing after the last import.

Expected 1 empty line after import statement not followed by another import.eslintimport/newline-after-import

Screenshot of the issue:

Image

smckinneywwt avatar Apr 07 '25 16:04 smckinneywwt