import-js/eslint-plugin-import

using 'import type' and module.exports in the same file should not raise an error for no-import-module-exports

Open

#2725 aperta il 23 feb 2023

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)JavaScript (1540 fork)batch import
help wantedtypescript

Metriche repository

Star
 (4946 star)
Metriche merge PR
 (Merge medio 138g 22h) (3 PR mergiate in 30 g)

Descrizione

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch eslint-plugin-import@2.27.5 for the project I'm working on.

in typescript 'import type' gets dropped in the build, so it is safe to use 'import type' with 'module.exports' (it is the only valid way to import types in a file with modules.exports).

Here is the diff that solved my problem:

diff --git a/node_modules/eslint-plugin-import/lib/rules/no-import-module-exports.js b/node_modules/eslint-plugin-import/lib/rules/no-import-module-exports.js
index b93d96e..d14049f 100644
--- a/node_modules/eslint-plugin-import/lib/rules/no-import-module-exports.js
+++ b/node_modules/eslint-plugin-import/lib/rules/no-import-module-exports.js
@@ -64,9 +64,7 @@ module.exports = {
           importDeclarations.forEach(function (importDeclaration) {
             context.report({
               node: importDeclaration,
-              message: 'Cannot use import declarations in modules that export using ' + 'CommonJS (module.exports = \'foo\' or exports.bar = \'hi\')' });
-
-
+              message: 'Cannot use import declarations (that are not top level type imports) in modules that export using ' + 'CommonJS (module.exports = \'foo\' or exports.bar = \'hi\')' });
           });
           alreadyReported = true;
         }
@@ -74,7 +72,9 @@ module.exports = {
 
       return {
         ImportDeclaration: function () {function ImportDeclaration(node) {
+          if(node.importKind !== 'type'){
             importDeclarations.push(node);
+          }
           }return ImportDeclaration;}(),
         MemberExpression: function () {function MemberExpression(node) {
             if (!alreadyReported) {

This issue body was partially generated by patch-package.

Guida contributor