Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Adds no-namespaced-import rule #262

Merged
merged 10 commits into from
Nov 14, 2019
Merged

Adds no-namespaced-import rule #262

merged 10 commits into from
Nov 14, 2019

Conversation

cartogram
Copy link
Contributor

@cartogram cartogram commented May 12, 2019

This PR adds a new rule to prevent namespace imports.

ToDo:

  • Add Fixer
  • Add options for additional modules and ignored/whitelisted modules
  • Add documentation
  • Add configurations
  • More tests probably
  • Add to Changelog

Reviewers:

I'd like reviewers to focus on the following:

  1. The description of the rule. Does it communicate enough about why we don't want namespace imports? Should we include any information about typescript specifically (--esModuleInterop, etc...).
  2. The fixer for the rule. Converts namespace to default imports, which is not always the solution, for example import * as foo from 'Foo' might actually want to be import {foo, bar, baz} from 'Foo'. The fixer could also lead to breaking code.
  3. Is the allow option. Is this enough configuration or should we an an array option to match modules to report (the opposite of allow).

@cartogram cartogram force-pushed the no-namespaced-import branch 2 times, most recently from cc17a25 to 43a1759 Compare May 13, 2019 08:16
@GoodForOneFare
Copy link
Member

Approach LGTM.

@cartogram cartogram changed the title [WIP] Adds no-namespaced-import rule Adds no-namespaced-import rule May 16, 2019
@cartogram cartogram force-pushed the no-namespaced-import branch 3 times, most recently from da0bd38 to fe9cf0d Compare June 8, 2019 19:22
@@ -0,0 +1,46 @@
# Prevent namespace import declarations. (no-namespace-imports)

Namespace imports grab all exported variables from a module as nested properties under a single variable name. This approach of importing everything, rather than only what is required from the module, leads to a polluted local scope from the unused components that were brought in under the module namespace.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not polite the local scope, that's half the benefit of namespace imports. If we're going to sell this on anything, it should be that this style of import breaks tree shaking.


```ts

import React from 'react';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather show an example showing named imports > namespace import. Yes, this rule does help us enforce the right style for React, but that's something that could conceivably be addressed in types instead.

"shopify/no-namespace-imports": [
"error",
{
"allow": ["react", "some-custom-module"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably best not to show it for React, we'd never want them to do that, would we?

node,
messageId: 'namespaceImport',
data: {name},
fix: (fixer) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's safe to provide a fixer, we have no idea what members they reference, and turning it into a default import is not going to be correct in many cases.


function ignoreModule(node, allowed) {
return allowed.some((stringOrRegex) => {
const regex = new RegExp(stringOrRegex);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to construct this regex array upfront.

@cartogram
Copy link
Contributor Author

superseded by #305

@cartogram cartogram closed this Jun 17, 2019
@cartogram cartogram reopened this Jun 22, 2019
@cartogram
Copy link
Contributor Author

After trying the import/no-namespace rule, I do think we want a custom rule after all. I'll continue with this work and get it ready for another review.

@cartogram cartogram force-pushed the no-namespaced-import branch 2 times, most recently from 210039f to 80063ce Compare November 14, 2019 08:47
@cartogram cartogram merged commit 874c9ae into master Nov 14, 2019
@cartogram cartogram deleted the no-namespaced-import branch November 14, 2019 08:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants