Skip to content

Bug: no-restricted-imports crashes when paths & patterns are both empty, but defined #8103

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
4 tasks done
mdziekon opened this issue Dec 20, 2023 · 0 comments · Fixed by #8108
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@mdziekon
Copy link
Contributor

mdziekon commented Dec 20, 2023

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.3.2&fileType=.tsx&code=JYWwDg9gTgLgBAKjgQwM5wCoFNXwGZQQhwBEMOMAdAFaokDcAUIwMYQB2qEANlpdxADmACmy4AlPSA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQHYHsBaaFAF2gEsBjUxAE0IoFsAHfaU1DSfAM18gAacNkgABUgE8WKKpRalCKeBVykA9AWJlKNeo1btO6MAG0R2KImjR2Qi9iyXRLAIala0XFzMBdBxAAvg7%2BQSCBQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

import * as Test from "test.js";

console.log(Test);

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "rules": {
      "no-restricted-imports": "off",
      "@typescript-eslint/no-restricted-imports": [
        "error",
        {
          "patterns": [],
          // (alternatively)
          // "paths": []
        }
      ]
    }
  },
};

tsconfig

No response

Expected Result

  • ESLint does not crash

Actual Result

  • ESLint crashes with the following error:

    TypeError: rules.ImportDeclaration is not a function
    Occurred while linting ...filename...
    Rule: "@typescript-eslint/no-restricted-imports"

Additional Info

Looking at the code of both base rule & extension rule, it seems the base rule does the following check and bails out if there's nothing to lint (it returns nothing in this case):
https://github.com/eslint/eslint/blob/5aa9c499da48b2d3187270d5d8ece71ad7521f56/lib/rules/no-restricted-imports.js#L192-L195

However, the extension rule does not check for that, nor checks whether base rule did not return anything useful:

const rules = baseRule.create(context);
const { options } = context;
if (options.length === 0) {
return {};
}

This is most likely not a usual case to handle (as "no paths and patterns" usually means we don't need the rule enabled), I've discovered it only because our shared eslint config has this rule configurable, and one of the projects did not need any exclude patterns. However, it would still be nice if this rule didn't crash the entire ESLint's execution, and handle it like the base rule does.

Tested with:

  • ESLint: 8.56.0
  • @typescript-eslint/*: 6.15.0
@mdziekon mdziekon added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Dec 20, 2023
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Dec 20, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 6, 2024
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 bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants