question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-restricted-imports: Allow only certain styles of imports (default / named / namespace)

See original GitHub issue

What rule do you want to change?

no-restricted-imports

Does this change cause the rule to produce more or fewer warnings?

Same (the change is non-breaking)

How will the change be implemented? (New option, new default behavior, etc.)?

New option: importStyles: Array<"default" | "named" | "namespace">

Please provide some example code that this change will affect:

/*eslint no-restricted-imports: ["error", { paths: [{
    name: "react",
    importStyles: ["default", "named"],
    message: "Please use `import * as React from "react"` instead."
}]}]*/

// ❌
import React from "react";
import { useState } from "react";

// ✅
import * as React from "react";

What does the rule currently do for this code?

Currently, the rule does not help code collaborators to stick with only one specific style for imports for a given library. Thus, neighbouring files end up with a variety of import styles.

What will the rule do after it’s changed?

Codebase owners will be able to limit imports styles for specific libraries. Thus, the ecosystem can get prepared to an upcoming removal of the default export in React.

For context, see:

Are you willing to submit a pull request to implement this change?

Yep

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Nov 24, 2020

Speaking of adding importStyle to "no-restricted-imports", which drawbacks do you see?

It looks to me more like a separate rule than an enhancement in no-restricted-imports, since it’s about style rather than restricting the use of certain modules. Curious to hear opinions from other team members.

An alternative to plugin rules is the core no-restricted-syntax rule, which can be configured for this case:

/* eslint no-restricted-syntax: ["error",
  {
      selector: "ImportDeclaration[source.value='react'] > :matches(ImportSpecifier, ImportDefaultSpecifier)",
      message: "Please use `import * as React from 'react'` instead."
  }
]*/

// ❌
import React from "react";
import { useState } from "react";

// ✅
import * as React_ from "react";

Online Demo

1reaction
nzakascommented, Nov 27, 2020

Closing, as we won’t be moving forward with this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-restricted-imports - ESLint - Pluggable JavaScript Linter
This rule allows you to specify imports that you don't want to use in your application. It applies to static imports only, not...
Read more >
no-restricted-imports | typescript-eslint
Examples​. This rule extends the base eslint/no-restricted-imports rule. ... (default: false ) ... When set to true , the rule will allow Type-Only...
Read more >
Kent C. Dodds on Twitter: "Importing React Through the Ages ...
no-restricted-imports : Allow only certain styles of imports (default / named / namespace) · Issue... What rule do you want to change?
Read more >
Creating a Custom ESLint Rule with TDD - DEV Community ‍ ‍
I call my rule no-namespace-imports, and that means I have a directory with that name and residing in it are 2 files: index.js...
Read more >
eslint no-restricted-imports - prevent an import from from path ...
import b from "../Components/Foo";. Is there a way to specify 'end of string' in a gitignore style pattern? eslint · glob ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found