Skip to content
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

Add prefer-import-module-contents custom rule #37

Merged
merged 1 commit into from
Nov 24, 2021

Conversation

marcaaron
Copy link
Contributor

@marcaaron marcaaron commented Nov 12, 2021

Should be tested in connection with Expensify/App#6279

Adds a rule to enforce the import * as Something from './somewhere' syntax for most things. There are a few exceptions:

  • Anything in /node_modules as we can't control 3rd party APIs
  • propTypes because they are sometimes bundled with components and it is convenient to break out propTypes and defaultProps
  • Higher order components because they often have default and named exports mixed
  • Anything dealing with Context (i.e. provider|context) for the same reason
  • .json file imports because it is convenient to break off a single property from something in a JSON file

Part 1 of 2

Expensify/App#6072

@marcaaron marcaaron self-assigned this Nov 12, 2021
fix config;

ouch

make exception for propTypes

allow hoc and context

add json
@marcaaron
Copy link
Contributor Author

Hey @jasperhuangg adding you here since you got PullerBeared on Expensify/App#6279

We need to merge this PR and publish the changes to NPM before we can review that one.

There are a LOT of changes there so let me know if you think we should add some more reviewers.

@marcaaron marcaaron requested review from aldo-expensify and removed request for jasperhuangg November 23, 2021 18:22
@marcaaron
Copy link
Contributor Author

@jasperhuangg is busy so @aldo-expensify you got the lucky spin of Puller Bear 😄

@aldo-expensify
Copy link
Contributor

Just read through this GH issue to be up to date of the decisions made. I'll finish reviewing this tomorrow morning!

There are some conflicts you may need to resolve here: Expensify/App#6279

@marcaaron
Copy link
Contributor Author

There are some conflicts you may need to resolve here: Expensify/App#6279

Thanks I expect more soon :) will do them all at once as soon as we are done with this.

Copy link

@jasperhuangg jasperhuangg left a comment

Choose a reason for hiding this comment

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

Quick question!

Comment on lines +31 to +46
/**
* @param {String} source
* @returns {Boolean}
*/
function isHigherOrderComponent(source) {
return /with/.test(source.toLowerCase());
}

/**
* @param {String} source
* @returns {Boolean}
*/
function isContextComponent(source) {
return /context|provider/.test(source.toLowerCase());
}

Choose a reason for hiding this comment

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

Can you explain why we're ignoring these types of components?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess we can re-visit whether we want to do this or not later. But basically there are some really common patterns where an HOC is a default export and it's propTypes or something else is a named export which makes it harder to follow the advice to use import * as syntax.

I couldn't work out how to improve this so opted to just make exceptions for things that are basically "internal" libraries like HOCs or Providers. My thinking is that it is OK to treat these like 3rd party libs or Onyx - but mostly I am lazy and didn't want to update too many things at once.

Copy link
Contributor

@aldo-expensify aldo-expensify left a comment

Choose a reason for hiding this comment

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

This seems to be working well. I tried it by changing my package json with:

"eslint-config-expensify": "git+https://github.com/Expensify/eslint-config-expensify.git#e1edb2cfe7fd91b9b131187cb19481ba9d29705b",

Then tested exceptions commenting them out to see the red underline appear. If you modify the linter configuration files, you can force the linter to reload by doing CMD + SHIFT + P and selecting the "Developer: Reload Window" command.

@aldo-expensify aldo-expensify merged commit cbfbf09 into master Nov 24, 2021
@aldo-expensify aldo-expensify deleted the marcaaron-addImportRule branch November 24, 2021 14:31
@marcaaron marcaaron mentioned this pull request Nov 24, 2021
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants