From e1edb2cfe7fd91b9b131187cb19481ba9d29705b Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Thu, 11 Nov 2021 11:50:00 -0500 Subject: [PATCH] add prefer-no-import-module-contents fix config; ouch make exception for propTypes allow hoc and context add json --- .../prefer-import-module-contents.js | 44 ++++++++++++++++++- package.json | 2 +- rules/expensify.js | 1 + 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/eslint-plugin-expensify/prefer-import-module-contents.js b/eslint-plugin-expensify/prefer-import-module-contents.js index d4942ac..571b796 100644 --- a/eslint-plugin-expensify/prefer-import-module-contents.js +++ b/eslint-plugin-expensify/prefer-import-module-contents.js @@ -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) { @@ -26,7 +60,11 @@ module.exports = { return; } - if (isFromNodeModules(sourceValue)) { + if (isFromNodeModules(sourceValue) + || isHigherOrderComponent(sourceValue) + || isContextComponent(sourceValue) + || isJSONFile(sourceValue) + ) { return; } @@ -34,6 +72,10 @@ module.exports = { return; } + if (hasPropTypesSpecifier(node.specifiers)) { + return; + } + if (!isEverySpecifierImport(node.specifiers)) { return; } diff --git a/package.json b/package.json index c4d07e6..e1b20aa 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/rules/expensify.js b/rules/expensify.js index eadd051..39681d2 100644 --- a/rules/expensify.js +++ b/rules/expensify.js @@ -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', }, };