Skip to content

Commit

Permalink
Merge pull request #37 from Expensify/marcaaron-addImportRule
Browse files Browse the repository at this point in the history
Add `prefer-import-module-contents` custom rule
  • Loading branch information
aldo-expensify authored Nov 24, 2021
2 parents 5ec12a8 + e1edb2c commit cbfbf09
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
44 changes: 43 additions & 1 deletion eslint-plugin-expensify/prefer-import-module-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,40 @@ function isEverySpecifierImport(specifiers = []) {
return _.every(specifiers, specifier => specifier.type === 'ImportSpecifier');
}

/**
* Make an exception for propTypes since they are sometimes bundled with modules.
*
* @param {Array} specifiers
* @returns {Boolean}
*/
function hasPropTypesSpecifier(specifiers) {
return _.some(specifiers, specifier => /proptypes/.test(lodashGet(specifier, 'imported.name', '').toLowerCase()));
}

/**
* @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());
}

/**
* @param {String} source
* @returns {Boolean}
*/
function isJSONFile(source) {
return /\.json/.test(source.toLowerCase());
}

module.exports = {
create: context => ({
ImportDeclaration(node) {
Expand All @@ -26,14 +60,22 @@ module.exports = {
return;
}

if (isFromNodeModules(sourceValue)) {
if (isFromNodeModules(sourceValue)
|| isHigherOrderComponent(sourceValue)
|| isContextComponent(sourceValue)
|| isJSONFile(sourceValue)
) {
return;
}

if (!node.specifiers || !node.specifiers.length) {
return;
}

if (hasPropTypesSpecifier(node.specifiers)) {
return;
}

if (!isEverySpecifierImport(node.specifiers)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-config-expensify",
"version": "2.0.18",
"version": "2.0.19",
"description": "Expensify's ESLint configuration following our style guide",
"main": "index.js",
"repository": {
Expand Down
1 change: 1 addition & 0 deletions rules/expensify.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ module.exports = {
'rulesdir/no-inline-named-export': 'error',
'rulesdir/prefer-underscore-method': 'error',
'rulesdir/no-useless-compose': 'error',
'rulesdir/prefer-import-module-contents': 'error',
},
};

0 comments on commit cbfbf09

Please sign in to comment.