Skip to content

[no-restricted-imports] No way to forbid only regular imports from a package & allow type imports #2403

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

Closed
3 tasks done
mgol opened this issue Aug 19, 2020 · 4 comments
Closed
3 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new base rule extension New base rule extension required to handle a TS specific case package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@mgol
Copy link
Contributor

mgol commented Aug 19, 2020

  • I have first restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "sourceType": "module"
  },
  "plugins": ["@typescript-eslint/eslint-plugin"],
  "rules": {
    "no-restricted-imports": ["error", {
        "patterns": ["/forbidden-package"]
    }]
  }
}
import something from 'forbidden-package';

Expected Result

I expected to be able to apply different rules to type imports.

Actual Result

There's no way of allowing only type imports from a specific package. An ESLint core rule can't differentiate between them as pure JS doesn't have the import type concept.

Additional Info

The issue is that a core ESLint rule cannot distinguish between type imports & regular imports since there is no type import concept in pure JavaScript. To fix this, we need a new typescript-eslint rule that doesn't exist yet (from what I could see).

If such a rule existed, I'd also expect it to be mentioned in https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/ROADMAP.md as the replacement for TSLint's import-blacklist instead of the current core ESLint no-restricted-imports rule.

Versions

package version
@typescript-eslint/eslint-plugin 3.9.1
@typescript-eslint/parser 3.9.1
TypeScript 3.9.7
ESLint 7.7.0
node 14.8.0
@mgol mgol added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Aug 19, 2020
@mgol mgol closed this as completed Aug 19, 2020
@mgol mgol changed the title [no-restricted-imports] Allow to differentiate between type imports & regular imports [disregard, my mistake] Aug 19, 2020
@mgol mgol changed the title [disregard, my mistake] [no-restricted-imports] No way to forbid only regular imports from a package & allow type imports Aug 19, 2020
@mgol mgol reopened this Aug 19, 2020
@bradzacher bradzacher added enhancement: new base rule extension New base rule extension required to handle a TS specific case and removed triage Waiting for team members to take a look labels Aug 19, 2020
@nicu-chiciuc
Copy link

A good use case for this rule is Meteor. Most of the code is isomorphic but if a file is somewhere inside a /server/ directory it can only be imported from a another file in a /server/ directory. Same applies to /client/ directories.

@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
@stefnotch
Copy link

stefnotch commented Jul 28, 2022

For those coming from Google, the relevant upstream issue used to be eslint/eslint#13758 before this one was created.
Sadly it has been locked since and thus I cannot link from there to this one.

@stefnotch
Copy link

And there is also a workaround rule:

If you want to disallow imports from ../dist, then the rule would be

"no-restricted-syntax": [
      "error",
      {
        selector:
          "ImportDeclaration[importKind!='type'][source.value=/..\\u002Fdist/]",
        message: "Must use 'import type' when importing from '../electron'.",
      },
    ],

(The \\u002F has to be used instead of the forwards slash because of estools/esquery#68 )

@Josh-Cena
Copy link
Member

Should be fixed by #3775

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new base rule extension New base rule extension required to handle a TS specific case package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

6 participants