lydell / eslint-plugin-simple-import-sort

Easy autofixable import sorting.
MIT License
2.09k stars 71 forks source link

Error with aliased imports and eslint-plugin-import #143

Closed klimnikova closed 1 year ago

klimnikova commented 1 year ago

I have made custom grouping in my @company/eslint-config with eslint-plugin-simple-import-sort and now I have problem with aliased imports like this in the project import { getFunction } from '~/store/selectors'; I have an error ~/store/selectors/ import should occur before import of ./features/AuthForm/AuthFormeslint import/order Although I don't use rule import/order somewhere explicitly

my @company/eslint-config/.eslintrc.js is

module.exports = {
  parser: '@babel/eslint-parser',
  env: {
    browser: true,
  },
  parserOptions: {
    sourceType: 'module',
    allowImportExportEverywhere: false,
    codeFrame: false,
  },
  extends: ['airbnb', 'airbnb/hooks', 'plugin:prettier/recommended'],
  globals: {
    document: true,
    window: true,
  },
  plugins: ['react', 'simple-import-sort'],
  rules: {
    'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
    'import/no-unresolved': 'off',
    'import/extensions': 'off',
    'import/prefer-default-export': 'off',
    'import/no-named-as-default': 'warn',
    'simple-import-sort/imports': 'error',
    'simple-import-sort/exports': 'error',  },
  overrides: [
    {
      files: ['**/*.tsx', '**/*.ts'],
      rules: {
        'simple-import-sort/imports': [
          'error',
          {
            groups: [
              [
                '^(assert|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|https|module|net|os|path|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|tty|url|util|vm|zlib|freelist|v8|process|async_hooks|http2|perf_hooks)(/.*|$)',
              ],
              // Packages. `react` related packages come first.
              ['^react', '^@?\\w'],
              // Internal packages.
              ['^(@|@company|@ui|components|utils|config|vendored-lib)(/.*|$)'],
              // // Feature imports.
              ['^\\.\\/features\\/', '^\\.\\/\\./?$'],
              // // Aliased imports.
              ['^~\\/', '^~\\/\\w'],
              // Parent imports. Put `..` last.
              ['^\\.\\.(?!/?$)', '^\\.\\./?$'],
              // Other relative imports. Put same-folder imports and `.` last.
              ['^\\./(?=.*/)(?!/?$)', '^\\.(?!/?$)', '^\\./?$'],
              // Style imports.
              ['^.+\\.s?css|less$'],
            ],
          },
        ],
      },
    },
  ],
};

my package.json

{
  "name": "@company/eslint-config",
  "files": [
    "index.js",
    "tslint.js",
    "prettier.js",
    "stylelint.js"
  ],
  "scripts": {
    "publish:beta": "npm publish --tag beta",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "@babel/eslint-parser": "^7.17.0",
    "@typescript-eslint/eslint-plugin": "^5.17.0",
    "@typescript-eslint/parser": "^5.17.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-airbnb-typescript": "^16.1.4",
    "eslint-config-prettier": "^8.5.0",
    "eslint-import-resolver-alias": "^1.1.2",
    "eslint-plugin-cypress": "^2.12.1",
    "eslint-plugin-import": "^2.28.1",
    "eslint-plugin-jasmine": "^4.1.3",
    "eslint-plugin-jsx-a11y": "^6.5.1",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-react": "^7.29.4",
    "eslint-plugin-react-hooks": "^4.4.0",
    "eslint-plugin-simple-import-sort": "^10.0.0",
    "stylelint": "^14.6.1",
    "stylelint-config-prettier": "^9.0.3",
    "stylelint-config-recommended": "^7.0.0",
    "stylelint-config-styled-components": "^0.1.1",
    "stylelint-order": "^5.0.0",
    "stylelint-prettier": "^2.0.0",
    "stylelint-processor-styled-components": "^1.10.0"
  },
  "peerDependencies": {
    "eslint": "^8.12.0",
    "prettier": "^2.6.1"
  }
}

and my .eslintrc.js in the project

const path = require('path');

module.exports = {
    extends: ['@company/eslint-config', '@company/eslint-config/tslint.js'],
    settings: {
        'import/resolver': {
            alias: {
                extensions: ['.ts', '.tsx', '.js', '.json'],
                map: [['~', path.resolve(__dirname, './src')]],
            },
        },
    },
};

Any ideas why this error occurs?

lydell commented 1 year ago

Hi! You can try the ESLint Discord, ESLint discussions or StackOverflow for help with configuration trouble not specific to this plugin. Hope you’ll figure it out!