Search code examples
eslinttypescript-eslint

How to disallow empty imports in eslint?


I've been trying to disallow empty imports like this:

import {} from 'module-foo';

Does anybody know how to do it? (I have a TypeScript Node.js project.)


Solution

  • Although there are no predefined rules for it, you can use no-restricted-syntax rule and AST Selectors to disallow this certain syntax:

    // .eslintrc.js
    
    module.exports = {
      // ... rest of the options
      rules: {
        // ... rest of the rules
        'no-restricted-syntax': [
          'error',
          {
            selector: 'ImportDeclaration[specifiers.length = 0]',
            message: 'Empty imports are not allowed',
          },
        ],
      }
    }
    
    

    Check this AST Tree to understand how the selector is targeting empty import.