Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Respect package.json#imports #70

Closed
2 tasks done
jgoux opened this issue Sep 12, 2023 · 6 comments
Closed
2 tasks done

Bug: Respect package.json#imports #70

jgoux opened this issue Sep 12, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@jgoux
Copy link

jgoux commented Sep 12, 2023

Describe the bug

Currently when using package.json's imports key to map internal files like:

"imports": {
  "#*": "./src/*"
}

The recommended-natural rules aren't placing the internal pattern where it should be.

For example it considers this to be valid:

import { type SDK } from "#config.ts";
import fs from "node:fs"

while it should be:

import fs from "node:fs"
import { type SDK } from "#config.ts";

Code example

I tried using the internal-pattern option without success, I tried both { "internal-pattern": ["#*"] } and { "internal-pattern": ["#**"] }.

ESLint version

v8.49.0

ESLint Plugin Perfectionist version

v2.0.1

Additional comments

No response

Validations

  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
@jgoux jgoux added the bug Something isn't working label Sep 12, 2023
@azat-io
Copy link
Owner

azat-io commented Sep 12, 2023

Could you show me your current ESLint config?

@jgoux
Copy link
Author

jgoux commented Sep 12, 2023

@azat-io sure, here it is:

import typescriptPlugin from '@typescript-eslint/eslint-plugin'
import typescriptParser from '@typescript-eslint/parser'
import importPlugin from 'eslint-plugin-import'
import nodeImportPlugin from 'eslint-plugin-node-import'
import perfectionistPlugin from 'eslint-plugin-perfectionist'
import unusedImportsPlugin from "eslint-plugin-unused-imports"
import globals from "globals";

export default [
  {
    files: [`**/*.@(js|jsx|ts|tsx)`],
    ignores: [`.dts/**`],
    languageOptions: {
      globals: {
        ...globals.browser,
        ...globals.node,
      },
      parser: typescriptParser,
      parserOptions: {
        project: `./tsconfig.json`,
      },
    },
    plugins: {
      '@typescript-eslint': typescriptPlugin,
      'import': importPlugin,
      'node-import': nodeImportPlugin,
      'perfectionist': perfectionistPlugin,
      'unused-imports': unusedImportsPlugin,
    },
    rules: {
      ...typescriptPlugin.configs['eslint-recommended'].rules,
      ...typescriptPlugin.configs['strict-type-checked'].rules,
      ...typescriptPlugin.configs['stylistic-type-checked'].rules,
      ...importPlugin.configs.recommended.rules,
      ...perfectionistPlugin.configs['recommended-natural'].rules,
      "@typescript-eslint/array-type": ["error", { default: "generic" }],
      "@typescript-eslint/consistent-type-imports": "error",
      "@typescript-eslint/no-unused-vars": "off",
      "import/consistent-type-specifier-style": ["error", "prefer-inline"],
      "import/no-duplicates": ["error", { "prefer-inline": true }],
      "import/no-unresolved": "off",
      "node-import/prefer-node-protocol": "error",
      "perfectionist/sort-imports": ["error", { "internal-pattern": ["#*", "#**"] }],
      "unused-imports/no-unused-imports": "error",
      "unused-imports/no-unused-vars": ["error", { "vars": "all", "varsIgnorePattern": "^_", "args": "after-used", "argsIgnorePattern": "^_" }],
    },
  },
]

@azat-io
Copy link
Owner

azat-io commented Sep 12, 2023

It looks like you forgot to configure the import groups.

    'perfectionist/sort-imports': [
      'error',
      {
        groups: [
          'type',
          ['builtin', 'external'],
          'internal-type',
          'internal',
          ['parent-type', 'sibling-type', 'index-type'],
          ['parent', 'sibling', 'index'],
          'object',
          'unknown',
        ],
        'internal-pattern': ['#/**'],
      },
    ],

@jgoux
Copy link
Author

jgoux commented Sep 12, 2023

Yes I just realized that I override the whole sort-imports rule. 😅

Unfortunately it's still not working:

"perfectionist/sort-imports": ["error", {
  ...perfectionistPlugin.configs["recommended-natural"].rules["perfectionist/sort-imports"][1],
  "newlines-between": "never",
  "internal-pattern": ["#*", "#**"],
}],

I think the issue is that minimatch is not able to apply the patterns #* or #**. With imports aliases, you can't have #/ (notice the slash here).

@azat-io
Copy link
Owner

azat-io commented Sep 12, 2023

Fixed in f35deef.
Will be released today.

@azat-io azat-io closed this as completed Sep 12, 2023
@jgoux
Copy link
Author

jgoux commented Sep 12, 2023

I just tested v2.1.0, it seems to work for files directly in the root but not for paths with folders:

// it works
import fs from "node:fs/promises";
import { type SDK } from "#config.js";

// it doesn't work
import { type SDK } from "#config/index.js";
import fs from "node:fs/promises";

My config:

"perfectionist/sort-imports": ["error", {
  ...perfectionistPlugin.configs["recommended-natural"].rules["perfectionist/sort-imports"][1],
  "newlines-between": "never",
  "internal-pattern": ["#**"],
}],

Edit

This internal-pattern syntax seems to do the trick! 💯

"internal-pattern": ["#*", "#*/**"],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants