Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] no-unresolved, extensions: ignore type-only exports #2436

Merged
merged 1 commit into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`no-cycle`]: add ExportNamedDeclaration statements to dependencies ([#2511], thanks [@BenoitZugmeyer])
- [`dynamic-import-chunkname`]: prevent false report on a valid webpack magic comment ([#2330], thanks [@mhmadhamster])
- [`export`]: do not error on TS export overloads ([#1590], thanks [@ljharb])
- [`no-unresolved`], [`extensions`]: ignore type only exports ([#2436], thanks [@Lukas-Kullmann])

### Changed
- [Tests] [`named`]: Run all TypeScript test ([#2427], thanks [@ProdigySim])
Expand Down Expand Up @@ -1014,6 +1015,7 @@ for info on changes for earlier releases.
[#2466]: https://github.com/import-js/eslint-plugin-import/pull/2466
[#2440]: https://github.com/import-js/eslint-plugin-import/pull/2440
[#2438]: https://github.com/import-js/eslint-plugin-import/pull/2438
[#2436]: https://github.com/import-js/eslint-plugin-import/pull/2436
[#2427]: https://github.com/import-js/eslint-plugin-import/pull/2427
[#2424]: https://github.com/import-js/eslint-plugin-import/pull/2424
[#2419]: https://github.com/import-js/eslint-plugin-import/pull/2419
Expand Down Expand Up @@ -1645,6 +1647,7 @@ for info on changes for earlier releases.
[@loganfsmyth]: https://github.com/loganfsmyth
[@luczsoma]: https://github.com/luczsoma
[@ludofischer]: https://github.com/ludofischer
[@Lukas-Kullmann]: https://github.com/Lukas-Kullmann
[@lukeapage]: https://github.com/lukeapage
[@lydell]: https://github.com/lydell
[@magarcia]: https://github.com/magarcia
Expand Down
6 changes: 3 additions & 3 deletions src/rules/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ module.exports = {
function checkFileExtension(source, node) {
// bail if the declaration doesn't have a source, e.g. "export { foo };", or if it's only partially typed like in an editor
if (!source || !source.value) return;

const importPathWithQueryString = source.value;

// don't enforce anything on builtins
Expand All @@ -164,8 +164,8 @@ module.exports = {
) || isScoped(importPath);

if (!extension || !importPath.endsWith(`.${extension}`)) {
// ignore type-only imports
if (node.importKind === 'type') return;
// ignore type-only imports and exports
if (node.importKind === 'type' || node.exportKind === 'type') return;
const extensionRequired = isUseOfExtensionRequired(extension, isPackage);
const extensionForbidden = isUseOfExtensionForbidden(extension);
if (extensionRequired && !extensionForbidden) {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-unresolved.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ module.exports = {
const options = context.options[0] || {};

function checkSourceValue(source, node) {
// ignore type-only imports
if (node.importKind === 'type') {
// ignore type-only imports and exports
if (node.importKind === 'type' || node.exportKind === 'type') {
return;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/src/rules/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,14 @@ describe('TypeScript', () => {
],
parser,
}),
test({
code: 'export type { MyType } from "./typescript-declare";',
options: [
'always',
{ ts: 'never', tsx: 'never', js: 'never', jsx: 'never' },
],
parser,
}),
],
invalid: [
test({
Expand All @@ -624,6 +632,15 @@ describe('TypeScript', () => {
],
parser,
}),
test({
code: 'export { MyType } from "./typescript-declare";',
errors: ['Missing file extension for "./typescript-declare"'],
options: [
'always',
{ ts: 'never', tsx: 'never', js: 'never', jsx: 'never' },
],
parser,
}),
],
});
});
Expand Down
9 changes: 9 additions & 0 deletions tests/src/rules/no-unresolved.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,22 @@ context('TypeScript', () => {
code: 'import type { JSONSchema7Type } from "@types/json-schema";',
parser,
}),
test({
code: 'export 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,
}),
test({
code: 'export { JSONSchema7Type } from "@types/json-schema";',
errors: [ "Unable to resolve path to module '@types/json-schema'." ],
parser,
}),
],
});
});
Expand Down