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.

sort-import does not auto fix

See original GitHub issue

Tell us about your environment

  • ESLint Version: 5.15.0
  • Node Version: 10.15.3
  • yarn Version: 1.13

What parser (default, Babel-ESLint, etc.) are you using? @typescript-eslint/parser

Please show your full configuration:

Configuration
{
	"parser": "@typescript-eslint/parser",
	"parserOptions": {
		"project": "./tsconfig.json",
		"sourceType": "module",
		"ecmaVersion": 6
	},
	"env": {
		"es6": true,
		"node": true
	},
	"plugins": [
		"@typescript-eslint"
	],
	"rules": {
		"@typescript-eslint/adjacent-overload-signatures": "error",
		"@typescript-eslint/array-type": [
			"error",
			"array"
		],
		"@typescript-eslint/ban-ts-ignore": "error",
		"@typescript-eslint/camelcase": [
			"error",
			{
				"properties": "always"
			}
		],
		"@typescript-eslint/class-name-casing": "error",
		"@typescript-eslint/explicit-function-return-type": [
			"error",
			{
				"allowExpressions": true
			}
		],
		"@typescript-eslint/explicit-member-accessibility": "error",
		"@typescript-eslint/indent": [
			"error",
			"tab"
		],
		"@typescript-eslint/interface-name-prefix": [
			"error",
			"always"
		],
		"@typescript-eslint/member-ordering": [
			"error",
			{
				"default": [
					"private-field",
					"protected-field",
					"public-field",
					"private-constructor",
					"protected-constructor",
					"public-constructor",
					"private-method",
					"protected-method",
					"public-method"
				]
			}
		],
		"@typescript-eslint/no-angle-bracket-type-assertion": "error",
		"@typescript-eslint/no-array-constructor": "error",
		"@typescript-eslint/no-empty-interface": "off",
		"@typescript-eslint/no-explicit-any": "error",
		"@typescript-eslint/no-extraneous-class": "error",
		"@typescript-eslint/no-for-in-array": "error",
		"@typescript-eslint/no-inferrable-types": [
			"error",
			{
				"ignoreParameters": true,
				"ignoreProperties": true
			}
		],
		"@typescript-eslint/no-misused-new": "error",
		"@typescript-eslint/no-namespace": "error",
		"@typescript-eslint/no-non-null-assertion": "off",
		"@typescript-eslint/no-object-literal-type-assertion": "error",
		"@typescript-eslint/no-parameter-properties": "error",
		"@typescript-eslint/no-require-imports": "error",
		"@typescript-eslint/no-this-alias": "error",
		"@typescript-eslint/no-triple-slash-reference": "error",
		"@typescript-eslint/no-type-alias": [
			"error",
			{
				"allowLiterals": "in-unions-and-intersections",
				"allowMappedTypes": "always"
			}
		],
		"@typescript-eslint/no-unnecessary-qualifier": "error",
		"@typescript-eslint/no-unnecessary-type-assertion": "error",
		"@typescript-eslint/no-unused-vars": "error",
		"@typescript-eslint/no-use-before-define": "error",
		"@typescript-eslint/no-useless-constructor": "error",
		"@typescript-eslint/no-var-requires": "error",
		"@typescript-eslint/prefer-function-type": "error",
		"@typescript-eslint/prefer-interface": "error",
		"@typescript-eslint/prefer-namespace-keyword": "error",
		"@typescript-eslint/promise-function-async": "error",
		"@typescript-eslint/restrict-plus-operands": "error",
		"@typescript-eslint/type-annotation-spacing": [
			"error",
			{
				"before": false,
				"after": true
			}
		],
		"array-bracket-newline": [
			"error",
			{
				"multiline": true
			}
		],
		"array-bracket-spacing": [
			"error",
			"never"
		],
		"array-element-newline": [
			"error",
			{
				"multiline": true
			}
		],
		"arrow-body-style": [
			"error",
			"as-needed"
		],
		"arrow-parens": [
			"error",
			"as-needed"
		],
		"arrow-spacing": [
			"error",
			{
				"before": true,
				"after": true
			}
		],
		"block-scoped-var": "error",
		"block-spacing": [
			"error",
			"always"
		],
		"brace-style": [
			"error",
			"1tbs"
		],
		"camelcase": [
			"error",
			{
				"properties": "always"
			}
		],
		"capitalized-comments": [
			"error",
			"always"
		],
		"class-methods-use-this": "error",
		"comma-dangle": [
			"error",
			"only-multiline"
		],
		"comma-spacing": [
			"error",
			{
				"before": false,
				"after": true
			}
		],
		"comma-style": [
			"error",
			"last"
		],
		"computed-property-spacing": [
			"error",
			"never"
		],
		"consistent-this": "error",
		"constructor-super": "error",
		"curly": [
			"error",
			"all"
		],
		"default-case": "error",
		"dot-location": [
			"error",
			"property"
		],
		"dot-notation": "error",
		"eol-last": [
			"error",
			"always"
		],
		"eqeqeq": "error",
		"for-direction": "error",
		"func-call-spacing": [
			"error",
			"never"
		],
		"generator-star-spacing": [
			"error",
			{
				"before": false,
				"after": true
			}
		],
		"getter-return": "error",
		"global-require": "error",
		"implicit-arrow-linebreak": [
			"error",
			"beside"
		],
		"indent": [
			"error",
			"tab"
		],
		"jsx-quotes": [
			"error",
			"prefer-double"
		],
		"key-spacing": [
			"error",
			{
				"beforeColon": false,
				"afterColon": true,
				"mode": "strict"
			}
		],
		"keyword-spacing": [
			"error",
			{
				"before": true,
				"after": true
			}
		],
		"linebreak-style": [
			"error",
			"unix"
		],
		"lines-around-comment": [
			"error",
			{
				"beforeBlockComment": true,
				"beforeLineComment": true,
				"allowBlockStart": true,
				"allowBlockEnd": false,
				"allowClassStart": true,
				"allowClassEnd": false,
				"allowObjectStart": true,
				"allowObjectEnd": false,
				"allowArrayStart": false,
				"allowArrayEnd": false
			}
		],
		"lines-between-class-members": [
			"error",
			"always",
			{
				"exceptAfterSingleLine": true
			}
		],
		"max-len": [
			"error",
			{
				"code": 100,
				"comments": 100,
				"ignoreUrls": true
			}
		],
		"multiline-comment-style": [
			"error",
			"starred-block"
		],
		"multiline-ternary": [
			"error",
			"always-multiline"
		],
		"new-parens": "error",
		"newline-per-chained-call": "error",
		"no-alert": "error",
		"no-array-constructor": "error",
		"no-async-promise-executor": "error",
		"no-await-in-loop": "error",
		"no-buffer-constructor": "error",
		"no-caller": "error",
		"no-case-declarations": "error",
		"no-class-assign": "error",
		"no-compare-neg-zero": "error",
		"no-cond-assign": "error",
		"no-confusing-arrow": "off",
		"no-console": "error",
		"no-const-assign": "error",
		"no-constant-condition": [
			"error",
			{
				"checkLoops": false
			}
		],
		"no-control-regex": "error",
		"no-debugger": "error",
		"no-delete-var": "error",
		"no-div-regex": "error",
		"no-dupe-args": "error",
		"no-dupe-keys": "error",
		"no-dupe-class-members": "error",
		"no-duplicate-case": "error",
		"no-duplicate-imports": "error",
		"no-else-return": "error",
		"no-empty": "error",
		"no-empty-character-class": "error",
		"no-empty-function": "error",
		"no-empty-pattern": "error",
		"no-eval": "error",
		"no-ex-assign": "error",
		"no-extend-native": "error",
		"no-extra-bind": "error",
		"no-extra-boolean-cast": "error",
		"no-extra-label": "error",
		"no-extra-semi": "error",
		"no-fallthrough": "error",
		"no-floating-decimal": "error",
		"no-func-assign": "error",
		"no-global-assign": "error",
		"no-implicit-coercion": "error",
		"no-implied-eval": "error",
		"no-inner-declarations": "error",
		"no-invalid-regexp": "error",
		"no-invalid-this": "error",
		"no-irregular-whitespace": "error",
		"no-iterator": "error",
		"no-label-var": "error",
		"no-labels": "error",
		"no-lone-blocks": "error",
		"no-lonely-if": "error",
		"no-loop-func": "error",
		"no-magic-numbers": "error",
		"no-misleading-character-class": "error",
		"no-mixed-spaces-and-tabs": "error",
		"no-multi-assign": "off",
		"no-multi-spaces": "error",
		"no-multi-str": "error",
		"no-multiple-empty-lines": "error",
		"no-nested-ternary": "error",
		"no-new": "error",
		"no-new-func": "error",
		"no-new-object": "error",
		"no-new-require": "error",
		"no-new-symbol": "error",
		"no-new-wrappers": "error",
		"no-obj-calls": "error",
		"no-octal": "off",
		"no-octal-escape": "error",
		"no-param-reassign": "error",
		"no-path-concat": "error",
		"no-plusplus": "off",
		"no-proto": "error",
		"no-redeclare": "error",
		"no-regex-spaces": "error",
		"no-return-assign": "error",
		"no-return-await": "error",
		"no-script-url": "error",
		"no-self-assign": "error",
		"no-self-compare": "error",
		"no-sequences": "error",
		"no-shadow": "error",
		"no-shadow-restricted-names": "error",
		"no-sparse-arrays": "error",
		"no-sync": "error",
		"no-template-curly-in-string": "error",
		"no-this-before-super": "error",
		"no-throw-literal": "error",
		"no-trailing-spaces": "error",
		"no-undef": "error",
		"no-undef-init": "error",
		"no-undefined": "error",
		"no-unexpected-multiline": "error",
		"no-unmodified-loop-condition": "error",
		"no-unreachable": "error",
		"no-unused-expressions": "error",
		"no-unused-vars": "error",
		"no-unsafe-finally": "error",
		"no-unused-labels": "error",
		"no-use-before-define": "error",
		"no-useless-call": "error",
		"no-useless-catch": "error",
		"no-useless-computed-key": "error",
		"no-useless-concat": "error",
		"no-useless-constructor": "error",
		"no-useless-escape": "error",
		"no-useless-rename": "error",
		"no-useless-return": "error",
		"no-var": "error",
		"no-void": "error",
		"no-whitespace-before-property": "error",
		"no-with": "error",
		"object-curly-newline": [
			"error",
			{
				"ObjectExpression": {
					"multiline": true
				},
				"ObjectPattern": {
					"multiline": true
				},
				"ImportDeclaration": {
					"multiline": true,
					"minProperties": 3
				},
				"ExportDeclaration": {
					"multiline": true,
					"minProperties": 3
				}
			}
		],
		"object-curly-spacing": [
			"error",
			"always"
		],
		"object-property-newline": "error",
		"object-shorthand": "error",
		"operator-assignment": [
			"error",
			"always"
		],
		"operator-linebreak": [
			"error",
			"before"
		],
		"padded-blocks": [
			"error",
			"never"
		],
		"padding-line-between-statements": [
			"error",
			{
				"blankLine": "always",
				"prev": "*",
				"next": "return"
			}
		],
		"prefer-arrow-callback": "error",
		"prefer-const": "error",
		"prefer-numeric-literals": "error",
		"prefer-object-spread": "error",
		"prefer-promise-reject-errors": "error",
		"prefer-spread": "error",
		"prefer-template": "error",
		"quote-props": [
			"error",
			"as-needed"
		],
		"quotes": [
			"error",
			"double",
			{
				"allowTemplateLiterals": true,
				"avoidEscape": true
			}
		],
		"radix": "error",
		"require-atomic-updates": "error",
		"require-await": "off",
		"require-unicode-regexp": "error",
		"require-yield": "error",
		"rest-spread-spacing": [
			"error",
			"never"
		],
		"semi": [
			"error",
			"always"
		],
		"semi-spacing": [
			"error",
			{
				"before": false,
				"after": true
			}
		],
		"semi-style": [
			"error",
			"last"
		],
		"sort-imports": [
			"error",
			{
				"ignoreCase": true,
				"memberSyntaxSortOrder": [
					"none",
					"all",
					"single",
					"multiple"
				]
			}
		],
		"space-before-blocks": [
			"error",
			"always"
		],
		"space-before-function-paren": [
			"error",
			{
				"asyncArrow": "always"
			}
		],
		"space-in-parens": [
			"error",
			"never"
		],
		"space-infix-ops": "error",
		"space-unary-ops": "error",
		"spaced-comment": [
			"error",
			"always"
		],
		"strict": "error",
		"switch-colon-spacing": [
			"error",
			{
				"after": true,
				"before": false
			}
		],
		"template-curly-spacing": [
			"error",
			"never"
		],
		"template-tag-spacing": [
			"error",
			"never"
		],
		"unicode-bom": [
			"error",
			"never"
		],
		"use-isnan": "error",
		"valid-typeof": "error",
		"wrap-iife": "error",
		"wrap-regex": "error",
		"yield-star-spacing": [
			"error",
			{
				"before": true,
				"after": false
			}
		],
		"yoda": "error"
	}
}

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

import constants from "../constants";
import Product from "./product.entity";
import { Inject, Injectable } from "@nestjs/common";
import { Repository } from "typeorm";
import { CreateProductDto, ProductResponseDto } from "./dto";

@Injectable()
export default class ProductService {
	private readonly productRepository: Repository<Product>;

	public constructor (
		@Inject(constants.PRODUCT_REPOSITORY) productRepository: Repository<Product>) {
		this.productRepository = productRepository;
	}

	public async create (createProductDto: CreateProductDto): Promise<ProductResponseDto> {
		const product = await this.productRepository.save(this.productRepository.create({ ...createProductDto }));

		return { ...product };
	}
}
eslint --fix src/**/*.ts

What did you expect to happen? Sort the imports

What actually happened? Please include the actual, raw output from ESLint. The imports were not sorted

/home/tristan957/Projects/myma-store-server/src/product/product.module.ts
  4:1  error  Imports should be sorted alphabetically  sort-imports

/home/tristan957/Projects/myma-store-server/src/product/product.providers.ts
  2:1  error  Imports should be sorted alphabetically  sort-imports
  4:1  error  Imports should be sorted alphabetically  sort-imports

/home/tristan957/Projects/myma-store-server/src/product/product.service.ts
   4:1  error  Expected 'single' syntax before 'multiple' syntax  sort-imports

Are you willing to submit a pull request to fix this bug? no

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
not-an-aardvarkcommented, Mar 23, 2019

Hi, thanks for the proposal. However, I don’t think it would be safe for sort-imports to autofix code because it could change the evaluation order of the imported modules, which could unexpectedly affect how the code works. In general, we try to avoid having autofixes that can change the behavior of working code.

4reactions
g-planecommented, Jun 3, 2019

@lostm1nd You can consider the order rule from eslint-plugin-import. It provides auto-fixing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sort-imports - ESLint - Pluggable JavaScript Linter
This rule checks all import declarations and verifies that all imports are first sorted by the used member syntax and then alphabetically by...
Read more >
eslint-plugin-sort-imports-es6-autofix - npm
A sort-imports rule that properly distinguishes between ES6 import types.. Latest version: 0.6.0, last published: 2 years ago.
Read more >
ESLint sort imports - remarkablemark
As a result, there's no automatic fix for this rule. You'll either need to manually fix it or ignore the sorting of import...
Read more >
Sorting your imports with ESLint - DEV Community ‍ ‍
This is an ESLint plugin that enables not only sorting with some nice ... happen automatically when ESLint is run in the auto-fix...
Read more >
Run autofix to sort these imports! on eslint simple-import-sort ...
I also have: "plugins": [ "@typescript-eslint", "import", "simple-import-sort" ],. Why is it not autofixing the imports on save though?
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