Enforce (fix) imports to be fully on a single line

877 views Asked by At

I have the following eslint config:

   "object-curly-newline": ["error", {
        "ImportDeclaration": "never",
        "ExportDeclaration": "never"
    }],

That transforms:

import { 
    something,
    somethingelse, 
} from "../here";

to

import { something,
    somethingelse, } from "../here";

but I'd like to get my import so it goes to a single line. Like so:

import { something, somethingelse, } from "../here";

Is there a way to do this within "object-curly-newline"? Or can I accomplish it via some other rule? I've tried the various other properties, but haven't had any luck.

2

There are 2 answers

0
Danziger On

The problem comes from object-curly-newline, which can be configured specifically for named imports using ImportDeclaration:

{
    "object-curly-newline": ["error", {
        "ObjectExpression": ...,
        "ObjectPattern": ...,
        "ExportDeclaration": ...,
        "ImportDeclaration": "never"
    }]
}

As stated in the docs:

"never" disallows line breaks after opening and before closing braces

0
Michiel Pater On

I believe this might be related to the multiline setting of object-curly-newline, depending on your settings.

"multiline": true requires line breaks if there are line breaks inside properties or between properties. Otherwise, it disallows line breaks.

That is what I noticed in my situation.

enter image description here

You can see which rule applies before saving, by hovering over the red or yellow underlines. In my case these were shown underneath the curly braces.