question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[no-duplicates] additional new lines are generated when the list of imports to squash is long

See original GitHub issue

I stubled upon a weird problem. My eslint --fix results in excessive spacing created, a few additional new lines are inserted with no reason.

I narrowed down the case to the clash of those 3 rules:

  • no-multiple-empty-lines
  • import/no-duplicates
  • import/order

The original file:

import { One } from '../fragments/one'
import { Two } from '../fragments/two'
import { Three } from '../fragments/three'
import { Four } from '../fragments/four'
import { Five } from '../fragments/five'
import { Six } from '../fragments/six'
import { Seven } from '../fragments/seven'
import { Eight } from '../fragments/eight'
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'
import { OneDoc } from '../fragments/one'
import { TwoDoc } from '../fragments/two'
import { ThreeDoc } from '../fragments/three'
import { FourDoc } from '../fragments/four'
import { FiveDoc } from '../fragments/five'
import { SixDoc } from '../fragments/six'
import { SevenDoc } from '../fragments/seven'
import { EightDoc } from '../fragments/eight'
export const Vars = any

The result of eslint --fix

import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'

import { One , OneDoc } from '../fragments/one'
import { Two , TwoDoc } from '../fragments/two'
import { Three , ThreeDoc } from '../fragments/three'
import { Four , FourDoc } from '../fragments/four'
import { Five , FiveDoc } from '../fragments/five'
import { Six , SixDoc } from '../fragments/six'
import { Seven , SevenDoc } from '../fragments/seven'
import { Eight , EightDoc } from '../fragments/eight'








export const Vars = any

The final configuration looks like below

{
  "plugins": ["import"],
  "parserOptions": {
    "sourceType": "module",
    "ecmaVersion": 2015
  },
  "rules": {
    "import/no-duplicates": ["error"],
    "import/order": [
      "error",
      {
        "groups": [
          [
            "builtin",
            "external"
          ],
          "internal",
          [
            "parent",
            "sibling",
            "index"
          ]
        ],
        "newlines-between": "always"
      }
    ],
    "no-multiple-empty-lines": ["error", {
      "max": 1,
      "maxEOF": 0
    }]
  }
}

I put together an example repo affected by the issue: https://github.com/ertrzyiks/import-order-multiple-empty-lines-demo

The result of eslint --fix is the following error:

12:1  error  More than 1 blank line not allowed  no-multiple-empty-lines

Examples

8 imports

A file with 8 imports to squash looks good 👍

https://github.com/ertrzyiks/import-order-multiple-empty-lines-demo/blob/main/example8.js https://github.com/ertrzyiks/import-order-multiple-empty-lines-demo/blob/main/results/example8.js

9 imports

A file with 9 imports starts to generate empty lines at the end of the imports block

https://github.com/ertrzyiks/import-order-multiple-empty-lines-demo/blob/main/example9.js https://github.com/ertrzyiks/import-order-multiple-empty-lines-demo/blob/main/results/example9.js

10 imports

A file with 10 imports starts to generate empty lines between the imports block and left-overs from merging

https://github.com/ertrzyiks/import-order-multiple-empty-lines-demo/blob/main/example10.js https://github.com/ertrzyiks/import-order-multiple-empty-lines-demo/blob/main/results/example10.js

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
ertrzyikscommented, Apr 15, 2021

I marked #2028 as ready for review. Turned out that this problem occurs with any number of imports. I guess it’s not so popular to have more than 2 duplicated imports so it was not causing any issues for most of the users.

1reaction
ertrzyikscommented, Apr 17, 2021

I have some trouble with failing tests, they highlighted some more scenarios not covered by my change. The test cases are formatted with leading spaces like

    import ...
    import 

so removing just a new line character produces a very weird formatting. I considered reworking the test to ignore the leading spaces, but IMO it shows that the fix tries to be too smart now.

The problem occurred in autogenerated files, it may be much easier to update the generator to not produce duplicates.

Any thoughts on this @ljharb ? I’m leaning towards closing this issue as works as intended.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ESLint plugin with rules that help validate proper imports.
This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, and prevent issues with misspelling of file paths and import names.
Read more >
Imports - Bloomreach Documentation
It is possible to import data to your project from sources outside of Bloomreach Engagement. These can be CSV or XML files (uploaded...
Read more >
Package 'data.table'
Character vector of one or more column names which is passed to setkey. ... When i is a list (or data.frame or data.table)...
Read more >
Python import: Advanced Techniques and Tips
In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring...
Read more >
Squash: Effect of Imports on U.S. Seasonal Markets ... - USITC
squash produced in the United States have some advantages over imported products, there are a number of other factors—such as the relatively high...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found