Skip to content

Commit

Permalink
[Fix] no-duplicates: do not unconditionally require typescript
Browse files Browse the repository at this point in the history
Fixes #2665
  • Loading branch information
ljharb committed Jan 12, 2023
1 parent 167f16c commit 07171ef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## [Unreleased]

### Fixed
- [`no-duplicates`]: do not unconditionally require `typescript` ([#2665])

## [2.27.1] - 2023-01-11

### Fixed
Expand Down Expand Up @@ -1361,6 +1364,7 @@ for info on changes for earlier releases.
[#211]: https://github.com/import-js/eslint-plugin-import/pull/211
[#164]: https://github.com/import-js/eslint-plugin-import/pull/164
[#157]: https://github.com/import-js/eslint-plugin-import/pull/157
[#2665]: https://github.com/import-js/eslint-plugin-import/issues/2665
[#2444]: https://github.com/import-js/eslint-plugin-import/issues/2444
[#2412]: https://github.com/import-js/eslint-plugin-import/issues/2412
[#2392]: https://github.com/import-js/eslint-plugin-import/issues/2392
Expand Down
8 changes: 6 additions & 2 deletions src/rules/no-duplicates.js
@@ -1,7 +1,11 @@
import resolve from 'eslint-module-utils/resolve';
import docsUrl from '../docsUrl';
import semver from 'semver';
import typescriptPkg from 'typescript/package.json';

let typescriptPkg;
try {
typescriptPkg = require('typescript/package.json');
} catch (e) { /**/ }

function checkImports(imported, context) {
for (const [module, nodes] of imported.entries()) {
Expand Down Expand Up @@ -114,7 +118,7 @@ function getFix(first, rest, sourceCode, context) {

const preferInline = context.options[0] && context.options[0]['prefer-inline'];
// a user might set prefer-inline but not have a supporting TypeScript version. Flow does not support inline types so this should fail in that case as well.
if (preferInline && !semver.satisfies(typescriptPkg.version, '>= 4.5')) {
if (preferInline && (!typescriptPkg || !semver.satisfies(typescriptPkg.version, '>= 4.5'))) {
throw new Error('Your version of TypeScript does not support inline type imports.');
}

Expand Down

0 comments on commit 07171ef

Please sign in to comment.