Skip to content

Commit

Permalink
[Fix] no-unresolved: ignore type-only imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko authored and ljharb committed Sep 11, 2021
1 parent 4d15e26 commit 4ed7867
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/rules/no-unresolved.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
22 changes: 21 additions & 1 deletion tests/src/rules/no-unresolved.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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,
}),
],
});
});
});

0 comments on commit 4ed7867

Please sign in to comment.