Skip to content

Commit e022aba

Browse files
authored
feat(no-missing-import): Add ignoreTypeImport options (#344)
* test: Add test for ignoreTypeImport on no-missing-import * feat: Add `ignoreTypeImport` to `no-missing-import` * docs: Add ignoreTypeImport docs
1 parent b16a475 commit e022aba

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

docs/rules/no-missing-import.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ Please see the shared settings documentation for more information.
6262
This can be configured in the rule options or as a shared setting [`settings.typescriptExtensionMap`](../shared-settings.md#typescriptextensionmap).
6363
Please see the shared settings documentation for more information.
6464

65+
### ignoreTypeImport
66+
67+
If using typescript, you may want to ignore type imports.
68+
69+
```json
70+
{
71+
"rules": {
72+
"n/no-missing-import": ["error", {
73+
"ignoreTypeImport": true
74+
}]
75+
}
76+
}
77+
```
78+
79+
In this way, the following code will not be reported:
80+
81+
```ts
82+
import type { TypeOnly } from "@types/only-types";
83+
```
84+
6585
## 🔎 Implementation
6686

6787
- [Rule source](../../lib/rules/no-missing-import.js)

lib/rules/no-missing-import.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = {
3030
allowModules: getAllowModules.schema,
3131
resolvePaths: getResolvePaths.schema,
3232
tryExtensions: getTryExtensions.schema,
33+
ignoreTypeImport: { type: "boolean", default: false },
3334
tsconfigPath: getTSConfig.schema,
3435
typescriptExtensionMap: getTypescriptExtensionMap.schema,
3536
},
@@ -39,12 +40,15 @@ module.exports = {
3940
messages,
4041
},
4142
create(context) {
43+
const options = context.options[0] ?? {}
44+
const ignoreTypeImport = options.ignoreTypeImport ?? false
45+
4246
const filePath = context.filename ?? context.getFilename()
4347
if (filePath === "<input>") {
4448
return {}
4549
}
4650

47-
return visitImport(context, {}, targets => {
51+
return visitImport(context, { ignoreTypeImport }, targets => {
4852
checkExistence(context, targets)
4953
})
5054
},

tests/lib/rules/no-missing-import.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,14 @@ ruleTester.run("no-missing-import", rule, {
342342
code: "import isIp from '#is-ip';",
343343
},
344344

345+
// ignoreTypeImport
346+
{
347+
filename: fixture("test.ts"),
348+
code: "import type missing from '@type/this-does-not-exists';",
349+
languageOptions: { parser: require("@typescript-eslint/parser") },
350+
options: [{ ignoreTypeImport: true }],
351+
},
352+
345353
// import()
346354
...(DynamicImportSupported
347355
? [

0 commit comments

Comments
 (0)