daidodo / format-imports-vscode

Format imports and exports for JavaScript and TypeScript in VS Code.
https://marketplace.visualstudio.com/items?itemName=dozerg.tsimportsorter
MIT License
62 stars 5 forks source link

Doesn't respect eslint indent settings #46

Closed FFdhorkin closed 3 years ago

FFdhorkin commented 3 years ago

Describe the bug The @typescript-eslint/indent rule appears to be ignored, as when reformatting imports to multi-line form, it is only using 2 spaces, not 4 spaces.

By way of example: the following import line import { parse, stringify } from 'teleport-javascript'; gets reformatted as

import {
  parse,
  stringify,
} from 'teleport-javascript';

when it should be

import {
    parse,
    stringify,
} from 'teleport-javascript';

To Reproduce Steps to reproduce the behavior:

  1. have eslint indentation set to 4 spaces rather than 2
  2. reformat imports when it would switch to multi-line styling

Expected behavior Either it should respect eslint spacing (which auto-fixes on save) or it should use the correct spacing. Or there should be a separate option to configure indents for this plugin.

Actual behavior Eslint auto-fix and import sorter fight about spacing. Import sorter wins, meaning you can't use reoder-imports-on-save if you're using indents of 4.

Screenshots N/A

OS (please complete the following information):

VS Code (please complete the following information):

daidodo commented 3 years ago

@FFdhorkin - Thanks for your feedback!

Could you share your ESLint configs?

daidodo commented 3 years ago

@FFdhorkin - I'll close the issue since there is no update. But feel free to re-open if you're still having the issue. Thanks!

FFdhorkin commented 3 years ago

Sorry, I didn't see the notification until just now.

Here's a (simplified) version of my .eslint.cjs file. I stripped out most of the rules that weren't about import/export or indents. This still does two-space indents rather than 4.

module.exports = {
    env: {
        browser: true,
        node: true,
        commonjs: true,
        es6: true,
    },
    extends: ['eslint:recommended',
        'airbnb-typescript/base', // TS+JS version
        'plugin:cypress/recommended',
        'plugin:chai-expect/recommended',
    ],
    parser: '@typescript-eslint/parser',
    parserOptions: {
        sourceType: 'module',
        ecmaVersion: 2020,
        project: './tsconfig.eslint.json',
        tsconfigRootDir: ".",
    },
    overrides: [
        {
            files: ["*.js"],
            rules: {
                indent: ['error', 4, {
                    SwitchCase: 1,
                    VariableDeclarator: 1,
                    outerIIFEBody: 1,
                    MemberExpression: 1,
                    FunctionDeclaration: {
                        parameters: 1,
                        body: 1,
                    },
                    FunctionExpression: {
                        parameters: 1,
                        body: 1,
                    },
                    CallExpression: {
                        arguments: 1,
                    },
                    ArrayExpression: 1,
                    ObjectExpression: 1,
                    ImportDeclaration: 1,
                    flatTernaryExpressions: false,
                    ignoreComments: false,
                }],
                "@typescript-eslint/indent": ['off']
            }
        },
        {
            files: ["*.ts"],
            extends: ['plugin:@typescript-eslint/recommended'],
            rules: {
                "spaced-comment": ["error", "always", {
                    "line": {
                        "markers": ["/"],
                        "exceptions": ["-", "+"]
                    },
                    "block": {
                        "markers": ["!"],
                        "exceptions": ["*"],
                        "balanced": true
                    }
                }],
                "@typescript-eslint/no-non-null-assertion": "off",
                "indent": "off", // we need the TS version
                "@typescript-eslint/indent": ['error', 4, {
                    SwitchCase: 1,
                    VariableDeclarator: 1,
                    outerIIFEBody: 1,
                    MemberExpression: 1,
                    FunctionDeclaration: {
                        parameters: 1,
                        body: 1,
                    },
                    FunctionExpression: {
                        parameters: 1,
                        body: 1,
                    },
                    CallExpression: {
                        arguments: 1,
                    },
                    ArrayExpression: 1,
                    ObjectExpression: 1,
                    ImportDeclaration: 1,
                    flatTernaryExpressions: false,
                    ignoreComments: false,
                }]
            }
        },
    ],
    rules: {
        "import/extensions": [
            "off",
            "ignorePackages",
            {
                "js": "true",
                "jsx": "true",
                "ts": "never",
                "tsx": "never"
            }
        ],
        'import/prefer-default-export': ['off'],
        'import/no-default-export': ['error'],
        'max-len': ['error', 125, 4, {
            ignoreUrls: true,
            ignoreComments: false,
            ignoreRegExpLiterals: true,
            ignoreStrings: true,
            ignoreTemplateLiterals: true,
        }],
        'import/no-unresolved': [
            'off'
        ],
        '@typescript-eslint/comma-dangle': ['error', {
            arrays: 'always-multiline',
            objects: 'always-multiline',
            imports: 'always-multiline',
            exports: 'always-multiline',
            functions: 'ignore',
        }]
    },
};

and my import-sorter.json:

{
    "keepUnused":[".*"],
    "wrappingStyle": {
        "maxBindingNamesPerLine": 5,
        "maxDefaultAndBindingNamesPerLine": 5,
        "maxExportNamesPerLine": 5,
        "maxNamesPerWrappedLine": 1,
        "ignoreComments": false
    }
}
daidodo commented 3 years ago

@FFdhorkin - Thanks very much for the configs! I've re-opened the issue and will check them soon.

daidodo commented 3 years ago

@FFdhorkin - Sorry for the late response! It's due to ESLint can't find your config file because the standard name should be .eslintrc.cjs.

Could you share your script/command to run ESLint CLI? And why not use the default name?

FFdhorkin commented 3 years ago

Sorry, my file is actually called .eslintrc.cjs and I mis-typed it before.

I do this on the CLI: pnpx eslint --cache --ext .js,.jsx,.ts,.tsx --fix "src/**/*"

daidodo commented 3 years ago

Hmm, it's working on my end. Could you share the extension logs following https://github.com/daidodo/format-imports-vscode/blob/master/CONTRIBUTING.md#debug-mode?

FFdhorkin commented 3 years ago

I'm not sure what changed since I opened this 3 weeks ago, but the issue appears to have resolved itself somehow. Possibly because I just bumped all my package.json dependencies yesterday? Perhaps there was a compatibility issue with an older eslint, etc.

Anyway, AFAIK, this issue no longer reproduces.