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: no-restricted-imports doesn't distinguish between package names that have a substring as a name #18126

Closed
bengry opened this issue Feb 18, 2024 · 3 comments
Labels
works as intended The behavior described in this issue is working correctly

Comments

@bengry
Copy link

bengry commented Feb 18, 2024

Environment

Node version: 20.1.0
npm version: N/A
Local ESLint version: 8.56.0
Global ESLint version: 8.56.0
Operating System: macOS 14.3.1

What parser are you using?

@typescript-eslint/parser

What did you do?

/*eslint no-restricted-imports: ["error", {
        "patterns": [
          {
            "group": [
              "react"
            ],
            "importNames": [
              "forwardRef"
            ],
            "message": "Use our type-safe wrapper from \"foo\" instead."
          }
        ]
      }]*/

import { forwardRef as originalForwardRef } from "react";
import { forwardRef as modifiedForwardRef } from "@my-org/utils/react";

What did you expect to happen?

should error:

import { forwardRef as originalForwardRef } from 'react' 

shouldn't error:

import { forwardRef as modifiedForwardRef } from '@my-org/utils/react'

What actually happened?

Both imports error.

Link to Minimal Reproducible Example

https://eslint.org/play/#eyJ0ZXh0IjoiLyplc2xpbnQgbm8tcmVzdHJpY3RlZC1pbXBvcnRzOiBbXCJlcnJvclwiLCB7XG4gICAgICAgIFwicGF0dGVybnNcIjogW1xuICAgICAgICAgIHtcbiAgICAgICAgICAgIFwiZ3JvdXBcIjogW1xuICAgICAgICAgICAgICBcInJlYWN0XCJcbiAgICAgICAgICAgIF0sXG4gICAgICAgICAgICBcImltcG9ydE5hbWVzXCI6IFtcbiAgICAgICAgICAgICAgXCJmb3J3YXJkUmVmXCJcbiAgICAgICAgICAgIF0sXG4gICAgICAgICAgICBcIm1lc3NhZ2VcIjogXCJVc2Ugb3VyIHR5cGUtc2FmZSB3cmFwcGVyIGZyb20gXFxcImZvb1xcXCIgaW5zdGVhZC5cIlxuICAgICAgICAgIH1cbiAgICAgICAgXVxuICAgICAgfV0qL1xuXG5pbXBvcnQgeyBmb3J3YXJkUmVmIGFzIG9yaWdpbmFsRm9yd2FyZFJlZiB9IGZyb20gJ3JlYWN0J1xuaW1wb3J0IHsgZm9yd2FyZFJlZiBhcyBtb2RpZmllZEZvcndhcmRSZWYgfSBmcm9tICdAbXktb3JnL3V0aWxzL3JlYWN0J1xuIiwib3B0aW9ucyI6eyJlbnYiOnt9LCJydWxlcyI6e30sInBhcnNlck9wdGlvbnMiOnsiZWNtYUZlYXR1cmVzIjp7fSwiZWNtYVZlcnNpb24iOiJsYXRlc3QiLCJzb3VyY2VUeXBlIjoibW9kdWxlIn19fQ==

Additional comments

I tried changing the rule configuration slightly, but to no avail.

Note that using this:

{
  "name": "react",
  "importNames": ["forwardRef"],
  "message": "..."
}

Is not good enough since it now errors on all of these lines:

import * as React from 'react';

Which, while technically correct (since you can then use React.forwardRef) is too much of an overhead, and not what I want to fix here. I'll prevent usage of React.forwardRef using another rule (no-restricted-syntax maybe).

@bengry bengry added bug ESLint is working incorrectly repro:needed labels Feb 18, 2024
@mdjermanovic mdjermanovic added works as intended The behavior described in this issue is working correctly and removed bug ESLint is working incorrectly repro:needed labels Feb 19, 2024
@mdjermanovic mdjermanovic closed this as not planned Won't fix, can't repro, duplicate, stale Feb 19, 2024
@bengry
Copy link
Author

bengry commented Feb 19, 2024

Note that this also disallows import * as React from 'react'.

Thanks. This is something I wanted to avoid:

Note that using this:

{
  "name": "react",
  "importNames": ["forwardRef"],
  "message": "..."
}

Is not good enough since it now errors on all of these lines:

import * as React from 'react';

Which, while technically correct (since you can then use React.forwardRef) is too much of an overhead, and not what I want to fix here. I'll prevent usage of React.forwardRef using another rule (no-restricted-syntax maybe).

Is there any way to achieve what I want? Seems like a missing use-case for this rule. Perhaps an exact: true option could be added to the object?

@mdjermanovic
Copy link
Member

Is there any way to achieve what I want?

Currently not, import * is always disallowed. You can open a new rule change request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
works as intended The behavior described in this issue is working correctly
Projects
Archived in project
Development

No branches or pull requests

2 participants