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

chore: Strict type imports with ESLint #1524

Merged
merged 5 commits into from
Nov 14, 2024
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
8 changes: 7 additions & 1 deletion packages/next-intl/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export default (await getPresets('typescript', 'react', 'vitest')).concat({
}
]
}
]
],

// Strict type imports to avoid side effects
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/consistent-type-exports': 'error',
'@typescript-eslint/no-import-type-side-effects': 'error',
'import/no-duplicates': ['error', {'prefer-inline': true}]
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {NextRequest} from 'next/server.js';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {receiveRoutingConfig} from '../routing/config.tsx';
import {Pathnames} from '../routing.tsx';
import type {Pathnames} from '../routing.tsx';
import getAlternateLinksHeaderValue from './getAlternateLinksHeaderValue.tsx';

describe.each([{basePath: undefined}, {basePath: '/base'}])(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {NextRequest} from 'next/server.js';
import {ResolvedRoutingConfig} from '../routing/config.tsx';
import {
import type {NextRequest} from 'next/server.js';
import type {ResolvedRoutingConfig} from '../routing/config.tsx';
import type {
DomainsConfig,
LocalePrefixMode,
Locales,
Expand Down
12 changes: 10 additions & 2 deletions packages/next-intl/src/middleware/middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
import {RequestCookies} from 'next/dist/compiled/@edge-runtime/cookies';
import {NextRequest, NextResponse} from 'next/server.js';
import {pathToRegexp} from 'path-to-regexp';
import {Mock, afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {
type Mock,
afterEach,
beforeEach,
describe,
expect,
it,
vi
} from 'vitest';
import createMiddleware from '../middleware.tsx';
import {Pathnames, defineRouting} from '../routing.tsx';
import {type Pathnames, defineRouting} from '../routing.tsx';

const COOKIE_LOCALE_NAME = 'NEXT_LOCALE';

Expand Down
6 changes: 3 additions & 3 deletions packages/next-intl/src/middleware/middleware.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {NextRequest, NextResponse} from 'next/server.js';
import {RoutingConfig, receiveRoutingConfig} from '../routing/config.tsx';
import {
import {type NextRequest, NextResponse} from 'next/server.js';
import {type RoutingConfig, receiveRoutingConfig} from '../routing/config.tsx';
import type {
DomainsConfig,
LocalePrefixMode,
Locales,
Expand Down
6 changes: 3 additions & 3 deletions packages/next-intl/src/middleware/resolveLocale.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {match} from '@formatjs/intl-localematcher';
import Negotiator from 'negotiator';
import {RequestCookies} from 'next/dist/server/web/spec-extension/cookies.js';
import type {RequestCookies} from 'next/dist/server/web/spec-extension/cookies.js';
import type {Locale} from 'use-intl';
import {ResolvedRoutingConfig} from '../routing/config.tsx';
import {
import type {ResolvedRoutingConfig} from '../routing/config.tsx';
import type {
DomainConfig,
DomainsConfig,
LocalePrefixMode,
Expand Down
6 changes: 3 additions & 3 deletions packages/next-intl/src/middleware/syncCookie.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {NextRequest, NextResponse} from 'next/server.js';
import type {NextRequest, NextResponse} from 'next/server.js';
import type {Locale} from 'use-intl';
import {
import type {
InitializedLocaleCookieConfig,
ResolvedRoutingConfig
} from '../routing/config.tsx';
import {
import type {
DomainConfig,
DomainsConfig,
LocalePrefixMode,
Expand Down
2 changes: 1 addition & 1 deletion packages/next-intl/src/middleware/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Locale} from 'use-intl';
import {
import type {
DomainConfig,
DomainsConfig,
LocalePrefixConfigVerbose,
Expand Down
8 changes: 6 additions & 2 deletions packages/next-intl/src/navigation/createNavigation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import {
redirect as nextRedirect
} from 'next/navigation.js';
import {renderToString} from 'react-dom/server';
import {Locale, useLocale} from 'use-intl';
import {type Locale, useLocale} from 'use-intl';
import {beforeEach, describe, expect, it, vi} from 'vitest';
import {DomainsConfig, Pathnames, defineRouting} from '../routing.tsx';
import {
type DomainsConfig,
type Pathnames,
defineRouting
} from '../routing.tsx';
import createNavigationClient from './react-client/createNavigation.tsx';
import createNavigationServer from './react-server/createNavigation.tsx';
import getServerLocale from './react-server/getServerLocale.tsx';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import {
usePathname as useNextPathname,
useRouter as useNextRouter
} from 'next/navigation.js';
import type {Locale} from 'use-intl';
import {useLocale} from 'use-intl';
import {type Locale, useLocale} from 'use-intl';
import {beforeEach, describe, expect, it, vi} from 'vitest';
import {DomainsConfig, Pathnames} from '../../routing.tsx';
import type {DomainsConfig, Pathnames} from '../../routing.tsx';
import createNavigation from './createNavigation.tsx';

vi.mock('next/navigation.js');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
} from 'next/navigation.js';
import {useMemo} from 'react';
import {type Locale, useLocale} from 'use-intl';
import {
import type {
RoutingConfigLocalizedNavigation,
RoutingConfigSharedNavigation
} from '../../routing/config.tsx';
import {
import type {
DomainsConfig,
LocalePrefixMode,
Locales,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {usePathname as useNextPathname} from 'next/navigation.js';
import {useMemo} from 'react';
import {useLocale} from 'use-intl';
import {
import type {
LocalePrefixConfigVerbose,
LocalePrefixMode,
Locales
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
import type {
RoutingConfigLocalizedNavigation,
RoutingConfigSharedNavigation
} from '../../routing/config.tsx';
import {
import type {
DomainsConfig,
LocalePrefixMode,
Locales,
Expand Down
10 changes: 5 additions & 5 deletions packages/next-intl/src/navigation/shared/BaseLink.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use client';

import NextLink, {LinkProps} from 'next/link.js';
import NextLink, {type LinkProps} from 'next/link.js';
import {usePathname} from 'next/navigation.js';
import {
ComponentProps,
MouseEvent,
Ref,
type ComponentProps,
type MouseEvent,
type Ref,
forwardRef,
useEffect,
useState
} from 'react';
import {type Locale, useLocale} from 'use-intl';
import {InitializedLocaleCookieConfig} from '../../routing/config.tsx';
import type {InitializedLocaleCookieConfig} from '../../routing/config.tsx';
import syncLocaleCookie from './syncLocaleCookie.tsx';

type NextLinkProps = Omit<ComponentProps<'a'>, keyof LinkProps> &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ import {
permanentRedirect as nextPermanentRedirect,
redirect as nextRedirect
} from 'next/navigation.js';
import {ComponentProps, forwardRef} from 'react';
import {type ComponentProps, forwardRef} from 'react';
import type {Locale} from 'use-intl';
import {
RoutingConfigLocalizedNavigation,
RoutingConfigSharedNavigation,
type RoutingConfigLocalizedNavigation,
type RoutingConfigSharedNavigation,
receiveRoutingConfig
} from '../../routing/config.tsx';
import {
import type {
DomainConfig,
DomainsConfig,
LocalePrefixMode,
Locales,
Pathnames
} from '../../routing/types.tsx';
import {ParametersExceptFirst, Prettify} from '../../shared/types.tsx';
import type {ParametersExceptFirst, Prettify} from '../../shared/types.tsx';
import use from '../../shared/use.tsx';
import {isLocalizableHref} from '../../shared/utils.tsx';
import BaseLink from './BaseLink.tsx';
import {
HrefOrHrefWithParams,
HrefOrUrlObjectWithParams,
QueryParams,
type HrefOrHrefWithParams,
type HrefOrUrlObjectWithParams,
type QueryParams,
applyPathnamePrefix,
compileLocalizedPathname,
normalizeNameOrNameWithParams,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Locale} from 'use-intl';
import {InitializedLocaleCookieConfig} from '../../routing/config.tsx';
import type {InitializedLocaleCookieConfig} from '../../routing/config.tsx';
import {getBasePath} from './utils.tsx';

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/next-intl/src/navigation/shared/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {ParsedUrlQueryInput} from 'node:querystring';
import type {UrlObject} from 'url';
import type {Locale} from 'use-intl';
import {ResolvedRoutingConfig} from '../../routing/config.tsx';
import {
import type {ResolvedRoutingConfig} from '../../routing/config.tsx';
import type {
DomainsConfig,
LocalePrefixMode,
Locales,
Expand All @@ -16,7 +16,7 @@ import {
normalizeTrailingSlash,
prefixPathname
} from '../../shared/utils.tsx';
import StrictParams from './StrictParams.tsx';
import type StrictParams from './StrictParams.tsx';

type SearchParamValue = ParsedUrlQueryInput[keyof ParsedUrlQueryInput];

Expand Down
4 changes: 2 additions & 2 deletions packages/next-intl/src/plugin/getNextConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import {NextConfig} from 'next';
import {PluginConfig} from './types.tsx';
import type {NextConfig} from 'next';
import type {PluginConfig} from './types.tsx';
import {throwError} from './utils.tsx';

function withExtensions(localPath: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ComponentProps} from 'react';
import type {ComponentProps} from 'react';
import getConfigNow from '../server/react-server/getConfigNow.tsx';
import getFormats from '../server/react-server/getFormats.tsx';
import {getLocale, getTimeZone} from '../server.react-server.tsx';
Expand Down
5 changes: 3 additions & 2 deletions packages/next-intl/src/react-server/testUtils.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {ReactNode, Suspense} from 'react';
import {ReactDOMServerReadableStream} from 'react-dom/server';
import {type ReactNode, Suspense} from 'react';
import type {ReactDOMServerReadableStream} from 'react-dom/server';
// @ts-expect-error -- Not available in types
import {renderToReadableStream as _renderToReadableStream} from 'react-dom/server.browser';

// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const renderToReadableStream: typeof import('react-dom/server').renderToReadableStream =
_renderToReadableStream;

Expand Down
2 changes: 1 addition & 1 deletion packages/next-intl/src/routing/config.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {NextResponse} from 'next/server.js';
import {
import type {
DomainsConfig,
LocalePrefix,
LocalePrefixConfigVerbose,
Expand Down
9 changes: 7 additions & 2 deletions packages/next-intl/src/routing/defineRouting.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {RoutingConfig} from './config.tsx';
import {DomainsConfig, LocalePrefixMode, Locales, Pathnames} from './types.tsx';
import type {RoutingConfig} from './config.tsx';
import type {
DomainsConfig,
LocalePrefixMode,
Locales,
Pathnames
} from './types.tsx';

export default function defineRouting<
const AppLocales extends Locales,
Expand Down
2 changes: 1 addition & 1 deletion packages/next-intl/src/routing/types.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import {describe, it} from 'vitest';
import {DomainConfig, LocalePrefix} from './types.tsx';
import type {DomainConfig, LocalePrefix} from './types.tsx';

describe('LocalePrefix', () => {
it('does not require a type param for simple values', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {headers} from 'next/headers.js';
import {cache} from 'react';
import {Locale} from 'use-intl';
import type {Locale} from 'use-intl';
import {HEADER_LOCALE_NAME} from '../../shared/constants.tsx';
import {getCachedRequestLocale} from './RequestLocaleCache.tsx';

Expand Down
4 changes: 2 additions & 2 deletions packages/next-intl/src/server/react-server/getConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {cache} from 'react';
import {
IntlConfig,
type IntlConfig,
type Locale,
_createCache,
_createIntlFormatters,
initializeConfig
} from 'use-intl/core';
import {getRequestLocale} from './RequestLocale.tsx';
import createRequestConfig from './createRequestConfig.tsx';
import {GetRequestConfigParams} from './getRequestConfig.tsx';
import type {GetRequestConfigParams} from './getRequestConfig.tsx';

// This is automatically inherited by `NextIntlClientProvider` if
// the component is rendered from a Server Component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {cache} from 'react';
import {type Locale, createFormatter} from 'use-intl/core';
import type {Locale, createFormatter} from 'use-intl/core';
import getConfig from './getConfig.tsx';
import getServerFormatter from './getServerFormatter.tsx';

Expand Down
2 changes: 1 addition & 1 deletion packages/next-intl/src/server/react-server/getLocale.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {cache} from 'react';
import {Locale} from 'use-intl';
import type {Locale} from 'use-intl';
import getConfig from './getConfig.tsx';

async function getLocaleCachedImpl(): Promise<Locale> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {cache} from 'react';
import {
Messages,
NamespaceKeys,
NestedKeyOf,
type Messages,
type NamespaceKeys,
type NestedKeyOf,
createTranslator
} from 'use-intl/core';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {cache} from 'react';
import {
import type {
Locale,
Messages,
NamespaceKeys,
Expand Down
4 changes: 2 additions & 2 deletions packages/next-intl/src/shared/NextIntlClientProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import {ComponentProps} from 'react';
import {type Locale} from 'use-intl';
import type {ComponentProps} from 'react';
import type {Locale} from 'use-intl';
import {IntlProvider} from 'use-intl/react';

type Props = Omit<ComponentProps<typeof IntlProvider>, 'locale'> & {
Expand Down
4 changes: 2 additions & 2 deletions packages/next-intl/src/shared/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {LinkProps} from 'next/link.js';
import {
import type {LinkProps} from 'next/link.js';
import type {
LocalePrefixConfigVerbose,
LocalePrefixMode,
Locales
Expand Down
8 changes: 7 additions & 1 deletion packages/use-intl/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export default (await getPresets('typescript', 'react', 'vitest')).concat({
'react-compiler': reactCompilerPlugin
},
rules: {
'react-compiler/react-compiler': 'error'
'react-compiler/react-compiler': 'error',

// Strict type imports to avoid side effects
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/consistent-type-exports': 'error',
'@typescript-eslint/no-import-type-side-effects': 'error',
'import/no-duplicates': ['error', {'prefer-inline': true}]
}
});
Loading
Loading