Skip to content

Commit

Permalink
feat(no-missing-import): Add ignoreTypeImport options (#344)
Browse files Browse the repository at this point in the history
* test: Add test for ignoreTypeImport on no-missing-import

* feat: Add `ignoreTypeImport` to `no-missing-import`

* docs: Add ignoreTypeImport docs
  • Loading branch information
scagood authored Oct 9, 2024
1 parent b16a475 commit e022aba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
20 changes: 20 additions & 0 deletions docs/rules/no-missing-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ Please see the shared settings documentation for more information.
This can be configured in the rule options or as a shared setting [`settings.typescriptExtensionMap`](../shared-settings.md#typescriptextensionmap).
Please see the shared settings documentation for more information.

### ignoreTypeImport

If using typescript, you may want to ignore type imports.

```json
{
"rules": {
"n/no-missing-import": ["error", {
"ignoreTypeImport": true
}]
}
}
```

In this way, the following code will not be reported:

```ts
import type { TypeOnly } from "@types/only-types";
```

## 🔎 Implementation

- [Rule source](../../lib/rules/no-missing-import.js)
Expand Down
6 changes: 5 additions & 1 deletion lib/rules/no-missing-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
allowModules: getAllowModules.schema,
resolvePaths: getResolvePaths.schema,
tryExtensions: getTryExtensions.schema,
ignoreTypeImport: { type: "boolean", default: false },
tsconfigPath: getTSConfig.schema,
typescriptExtensionMap: getTypescriptExtensionMap.schema,
},
Expand All @@ -39,12 +40,15 @@ module.exports = {
messages,
},
create(context) {
const options = context.options[0] ?? {}
const ignoreTypeImport = options.ignoreTypeImport ?? false

const filePath = context.filename ?? context.getFilename()
if (filePath === "<input>") {
return {}
}

return visitImport(context, {}, targets => {
return visitImport(context, { ignoreTypeImport }, targets => {
checkExistence(context, targets)
})
},
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/no-missing-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ ruleTester.run("no-missing-import", rule, {
code: "import isIp from '#is-ip';",
},

// ignoreTypeImport
{
filename: fixture("test.ts"),
code: "import type missing from '@type/this-does-not-exists';",
languageOptions: { parser: require("@typescript-eslint/parser") },
options: [{ ignoreTypeImport: true }],
},

// import()
...(DynamicImportSupported
? [
Expand Down

0 comments on commit e022aba

Please sign in to comment.