From 4ed78671abf9768af2aec4ca61c377fed2e93f5f Mon Sep 17 00:00:00 2001 From: Jack Bates Date: Sat, 11 Sep 2021 07:36:41 -0700 Subject: [PATCH] [Fix] `no-unresolved`: ignore type-only imports --- CHANGELOG.md | 11 +++++++---- src/rules/no-unresolved.js | 5 +++++ tests/src/rules/no-unresolved.js | 22 +++++++++++++++++++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c430385c3..dbb9ecb82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,16 +6,19 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ## [Unreleased] -### Changed -- [Refactor] switch to an internal replacement for `pkg-up` and `read-pkg-up` ([#2047], thanks [@mgwalker]) -- [patch] TypeScript config: remove `.d.ts` from [`import/parsers` setting] and [`import/extensions` setting] ([#2220], thanks [@jablko]) - ### Added - [`no-unresolved`]: add `caseSensitiveStrict` option ([#1262], thanks [@sergei-startsev]) - [`no-unused-modules`]: add eslint v8 support ([#2194], thanks [@coderaiser]) - [`no-restricted-paths`]: add/restore glob pattern support ([#2219], thanks [@stropho]) - [`no-unused-modules`]: support dynamic imports ([#1660], [#2212], thanks [@maxkomarychev], [@aladdin-add], [@Hypnosphi]) +### Fixed +- [`no-unresolved`]: ignore type-only imports ([#2220], thanks [@jablko]) + +### Changed +- [Refactor] switch to an internal replacement for `pkg-up` and `read-pkg-up` ([#2047], thanks [@mgwalker]) +- [patch] TypeScript config: remove `.d.ts` from [`import/parsers` setting] and [`import/extensions` setting] ([#2220], thanks [@jablko]) + ## [2.24.2] - 2021-08-24 ### Fixed diff --git a/src/rules/no-unresolved.js b/src/rules/no-unresolved.js index d7212560c..408b6ff5e 100644 --- a/src/rules/no-unresolved.js +++ b/src/rules/no-unresolved.js @@ -27,6 +27,11 @@ module.exports = { const options = context.options[0] || {}; function checkSourceValue(source) { + // ignore type-only imports + if (source.parent && source.parent.importKind === 'type') { + return; + } + const caseSensitive = !CASE_SENSITIVE_FS && options.caseSensitive !== false; const caseSensitiveStrict = !CASE_SENSITIVE_FS && options.caseSensitiveStrict; diff --git a/tests/src/rules/no-unresolved.js b/tests/src/rules/no-unresolved.js index 19203074e..9e6db8c04 100644 --- a/tests/src/rules/no-unresolved.js +++ b/tests/src/rules/no-unresolved.js @@ -1,6 +1,6 @@ import * as path from 'path'; -import { test, SYNTAX_CASES, testVersion } from '../utils'; +import { getTSParsers, test, SYNTAX_CASES, testVersion } from '../utils'; import { CASE_SENSITIVE_FS } from 'eslint-module-utils/resolve'; @@ -441,3 +441,23 @@ ruleTester.run('import() with built-in parser', rule, { })) || [], ), }); + +context('TypeScript', () => { + getTSParsers().filter(x => x !== require.resolve('typescript-eslint-parser')).forEach((parser) => { + ruleTester.run(`${parser}: no-unresolved ignore type-only`, rule, { + valid: [ + test({ + code: 'import type { JSONSchema7Type } from "@types/json-schema";', + parser, + }), + ], + invalid: [ + test({ + code: 'import { JSONSchema7Type } from "@types/json-schema";', + errors: [ "Unable to resolve path to module '@types/json-schema'." ], + parser, + }), + ], + }); + }); +});