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

#2.725 aberto em 23 de fev. de 2023

Ver no GitHub
 (1 comment) (0 reactions) (0 assignees)JavaScript (1.540 forks)batch import
help wantedtypescript

Métricas do repositório

Stars
 (4.946 stars)
Métricas de merge de PR
 (Mesclagem média 138d 22h) (3 fundiu PRs em 30d)

Description

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.

Guia do colaborador