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

Adds `prefer-name-spaced-react’ #133

Closed
wants to merge 1 commit into from

Conversation

cartogram
Copy link
Contributor

@cartogram cartogram commented Aug 3, 2018

closes Shopify/web-configs#115

If I can get approval on the general approach and if we want this rule, I'll fill in the docs, configs, changelog, etc...

Copy link
Member

@lemonmade lemonmade left a comment

Choose a reason for hiding this comment

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

Approach looks good 👍

ImportDeclaration(node) {
if (
node.source.value === 'react' &&
node.specifiers[0].type === 'ImportDefaultSpecifier'
Copy link
Member

Choose a reason for hiding this comment

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

You probably need to guard against no specifiers

@GoodForOneFare
Copy link
Member

Is just forbidding allowSyntheticDefaultImports an option?

@cartogram
Copy link
Contributor Author

I might prefer the rule over allowSyntheticDefaultImports, @lemonmade?

Also, would this be under the typescript namespace as well?

@lemonmade
Copy link
Member

I think this can be applied in JS or TS, it works the same either way.

Disallowing allowSyntheticDefaultImports could work but it is bad for other modules like express, so I would prefer to avoid

@@ -0,0 +1,36 @@
# Prefer React import be name-spaced. (prefer-name-spaced-react)
Copy link

@TzviPM TzviPM Aug 15, 2018

Choose a reason for hiding this comment

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

did you mean to fill this in?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yes, sorry, I forgot i was just looking for general approach approval at first. Will clean this all up.

Copy link

@TzviPM TzviPM left a comment

Choose a reason for hiding this comment

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

I believe we actually want the opposite behaviour here as per https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html:

We highly recommend applying it (esModuleInterop) both to new and existing projects. For existing projects, namespace imports (import * as express from "express"; express();) will need to be converted to default imports (import express from "express"; express();).


context.report({
node,
message: `Prefer name-spaced React import. (import * as React from 'react';).`,
Copy link

Choose a reason for hiding this comment

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

I think we actually want the opposite here.

Copy link
Member

Choose a reason for hiding this comment

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

I am curious whether TS will complain about the namespace import for React with that flag applied. My gut is that a namespace import is still more correct for React. In express, it made a lot less sense, because the namespace import got you a function, where it would always give you an object in “true” modules. React, in contrast, uses module.exports as an object that just exposes named properties, so a namespace import is more appropriate.

@cartogram
Copy link
Contributor Author

superseded by #262

@cartogram cartogram closed this May 12, 2019
@alexandcote alexandcote deleted the prefer-name-spaced-react branch March 27, 2020 23:51
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.

Prefer named space import React
4 participants