'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 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Farzad |
