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

[import/no-duplicates] prefer-inline option should work with Flow #2686

Open
Tracked by #2716
valscion opened this issue Jan 20, 2023 · 1 comment
Open
Tracked by #2716

[import/no-duplicates] prefer-inline option should work with Flow #2686

valscion opened this issue Jan 20, 2023 · 1 comment

Comments

@valscion
Copy link

valscion commented Jan 20, 2023

Hi!

I noticed that the prefer-inline option for import/no-duplicates actively forbids using that option with Flow:

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 && (!typescriptPkg || !semver.satisfies(typescriptPkg.version, '>= 4.5'))) {
throw new Error('Your version of TypeScript does not support inline type imports.');
}

However, Flow does support inline types (See this Try flow online REPL):

import React, { type Element } from 'react';

function Example(): Element<'div'> {
  return <div>Hello</div>;
}

So we don't need this:

import React from 'react';
import type { Element } from 'react';

function Example(): Element<'div'> {
  return <div>Hello</div>;
}

This is even implemented as explicitly allowed in another rule, import/consinstent-type-specifier-style: https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/consistent-type-specifier-style.md

I spotted this when upgrading from eslint-plugin-import v2.26.0 to v2.27.5 that this old code that works and didn't show lint errors:

import type { Localizable } from 'common/types/locale';
import { localize, localizable } from 'common/types/locale';
import type { FormattedMoney } from 'common/types/money';
import { money, formatMoney, type Money } from 'common/types/money';

Now should be written like this instead:

import type { Localizable } from 'common/types/locale';
import { localize, localizable } from 'common/types/locale';
import {
  money,
  formatMoney,
  type Money,
  type FormattedMoney
} from 'common/types/money';

Notice how the imports from common/types/localeremained the same while the imports from common/types/money showed errors.

  5:37  warning  '/path/to/common/types/money.js' imported multiple times  import/no-duplicates
  6:48  warning  '/path/to/common/types/money.js' imported multiple times  import/no-duplicates

Expected outcome

I'd expect this to work with Flow:

'import/no-duplicates': ['warn', { 'prefer-inline': true }]

Actual outcome

Error:

Your version of TypeScript does not support inline type imports. Occurred while linting /path/to/inquiry/utils/priceRangeToText.js:3

@valscion
Copy link
Author

This requirement to have a specific version of TypeScript installed comes from the original PR by @snewcomer and hasn't been changed since:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant