Skip to content

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

Closed
@mdziekon

Description

@mdziekon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions