Skip to content

Commit

Permalink
chore: Upgrade to ESLint 9 and use Prettier separately (amannn#1400)
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn authored Oct 17, 2024
1 parent 22ea012 commit 78c8892
Show file tree
Hide file tree
Showing 54 changed files with 303 additions and 314 deletions.
21 changes: 0 additions & 21 deletions .eslintrc.js

This file was deleted.

264 changes: 132 additions & 132 deletions CHANGELOG.md

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {getPresets} from 'eslint-config-molindo';
import reactCompilerPlugin from 'eslint-plugin-react-compiler';

export default (await getPresets('typescript', 'react', 'vitest')).concat({
plugins: {
'react-compiler': reactCompilerPlugin
},
rules: {
'react-compiler/react-compiler': 'error'
}
});
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"build": "rm -rf dist && rollup -c",
"test": "TZ=Europe/Berlin vitest",
"lint": "pnpm run lint:source && pnpm run lint:package",
"lint:source": "eslint src test && tsc --noEmit",
"lint:source": "eslint src test && tsc --noEmit && pnpm run lint:prettier",
"lint:package": "publint && attw --pack",
"lint:prettier": "prettier src --check",
"prepublishOnly": "turbo build && cp ../../README.md .",
"postpublish": "git checkout . && rm ./README.md",
"size": "size-limit"
Expand Down Expand Up @@ -102,12 +103,12 @@
"@types/node": "^20.14.5",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"eslint": "^8.56.0",
"eslint-config-molindo": "^7.0.0",
"eslint-plugin-deprecation": "^3.0.0",
"eslint": "^9.11.1",
"eslint-config-molindo": "^8.0.0",
"eslint-plugin-react-compiler": "0.0.0-experimental-8e3b87c-20240822",
"next": "^14.2.4",
"path-to-regexp": "^6.2.2",
"prettier": "^3.3.3",
"publint": "^0.2.8",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand All @@ -117,5 +118,5 @@
"typescript": "^5.5.3",
"vitest": "^2.0.2"
},
"prettier": "../../.prettierrc.json"
"prettier": "eslint-config-molindo/.prettierrc.json"
}
2 changes: 1 addition & 1 deletion src/middleware/getAlternateLinksHeaderValue.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment edge-runtime

import {NextRequest} from 'next/server';
import {it, expect, describe, beforeEach, afterEach} from 'vitest';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {Pathnames} from '../routing';
import {receiveRoutingConfig} from '../routing/config';
import getAlternateLinksHeaderValue from './getAlternateLinksHeaderValue';
Expand Down
7 changes: 3 additions & 4 deletions src/middleware/getAlternateLinksHeaderValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ export default function getAlternateLinksHeaderValue<
let url: URL;

if (routing.domains) {
const domainConfigs =
routing.domains.filter((cur) =>
isLocaleSupportedOnDomain(locale, cur)
) || [];
const domainConfigs = routing.domains.filter((cur) =>
isLocaleSupportedOnDomain(locale, cur)
);

return domainConfigs.map((domainConfig) => {
url = new URL(normalizedUrl);
Expand Down
18 changes: 9 additions & 9 deletions src/middleware/middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import {RequestCookies} from 'next/dist/compiled/@edge-runtime/cookies';
import {NextRequest, NextResponse} from 'next/server';
import {pathToRegexp} from 'path-to-regexp';
import {it, describe, vi, beforeEach, expect, Mock, afterEach} from 'vitest';
import {Mock, afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import createMiddleware from '../middleware';
import {defineRouting, Pathnames} from '../routing';
import {Pathnames, defineRouting} from '../routing';
import {COOKIE_LOCALE_NAME} from '../shared/constants';

vi.mock('next/server', async (importActual) => {
Expand Down Expand Up @@ -515,7 +515,7 @@ describe('prefix-based routing', () => {
);
});

it('serves requests for a non-default locale at nested paths', () => {
it('serves requests for a non-default locale at nested paths for "de"', () => {
middlewareWithPathnames(createMockRequest('/de/ueber', 'de'));
middlewareWithPathnames(createMockRequest('/de/benutzer', 'de'));
middlewareWithPathnames(createMockRequest('/de/benutzer/1', 'de'));
Expand Down Expand Up @@ -552,7 +552,7 @@ describe('prefix-based routing', () => {
);
});

it('serves requests for a non-default locale at nested paths', () => {
it('serves requests for a non-default locale at nested paths for "ja"', () => {
middlewareWithPathnames(createMockRequest('/ja/約', 'ja'));
middlewareWithPathnames(createMockRequest('/ja/ユーザー', 'ja'));
middlewareWithPathnames(createMockRequest('/ja/ユーザー/1', 'ja'));
Expand Down Expand Up @@ -695,7 +695,7 @@ describe('prefix-based routing', () => {
);
});

it('prioritizes static routes over dynamic and catch-all routes for the non-default locale', () => {
it('prioritizes static routes over dynamic and catch-all routes for the default locale', () => {
middlewareWithPathnames(createMockRequest('/products/add', 'en'));
middlewareWithPathnames(createMockRequest('/categories/new', 'en'));
expect(MockedNextResponse.next).not.toHaveBeenCalled();
Expand Down Expand Up @@ -2256,7 +2256,7 @@ describe('prefix-based routing', () => {
);
});

it('redirects a dynamic internal route for the default locale', () => {
it('redirects a dynamic internal route for the default locale (1)', () => {
middlewareWithPathnames(createMockRequest('/internal/22?hello', 'en'));
expect(MockedNextResponse.next).not.toHaveBeenCalled();
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
Expand All @@ -2266,7 +2266,7 @@ describe('prefix-based routing', () => {
);
});

it('redirects a dynamic internal route for the default locale', () => {
it('redirects a dynamic internal route for the default locale (2)', () => {
middlewareWithPathnames(
createMockRequest('/internal/22/foo/bar?hello', 'en')
);
Expand Down Expand Up @@ -3017,7 +3017,7 @@ describe('domain-based routing', () => {
);
});

it('redirects a dynamic internal route for the default locale', () => {
it('redirects a dynamic internal route for the default locale (1)', () => {
middlewareWithPathnames(
createMockRequest('/internal/22', 'en', 'http://ca.example.com')
);
Expand All @@ -3029,7 +3029,7 @@ describe('domain-based routing', () => {
);
});

it('redirects a dynamic internal route for the default locale', () => {
it('redirects a dynamic internal route for the default locale (2)', () => {
middlewareWithPathnames(
createMockRequest(
'/internal/22/foo/bar',
Expand Down
33 changes: 15 additions & 18 deletions src/middleware/middleware.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {NextRequest, NextResponse} from 'next/server';
import {receiveRoutingConfig, RoutingConfig} from '../routing/config';
import {RoutingConfig, receiveRoutingConfig} from '../routing/config';
import {Locales, Pathnames} from '../routing/types';
import {HEADER_LOCALE_NAME} from '../shared/constants';
import {
Expand All @@ -12,15 +12,15 @@ import getAlternateLinksHeaderValue from './getAlternateLinksHeaderValue';
import resolveLocale from './resolveLocale';
import syncCookie from './syncCookie';
import {
getInternalTemplate,
applyBasePath,
formatPathname,
formatTemplatePathname,
getBestMatchingDomain,
getPathnameMatch,
getInternalTemplate,
getLocaleAsPrefix,
getNormalizedPathname,
getPathnameMatch,
isLocaleSupportedOnDomain,
applyBasePath,
formatPathname,
getLocaleAsPrefix,
sanitizePathname
} from './utils';

Expand All @@ -36,16 +36,15 @@ export default function createMiddleware<
const resolvedRouting = receiveRoutingConfig(routing);
const resolvedOptions = {
alternateLinks: options?.alternateLinks ?? routing.alternateLinks ?? true,
localeDetection:
options?.localeDetection ?? routing?.localeDetection ?? true
localeDetection: options?.localeDetection ?? routing.localeDetection ?? true
};

return function middleware(request: NextRequest) {
let unsafeExternalPathname: string;
try {
// Resolve potential foreign symbols (e.g. /ja/%E7%B4%84 → /ja/約))
unsafeExternalPathname = decodeURI(request.nextUrl.pathname);
} catch (e) {
} catch {
// In case an invalid pathname is encountered, forward
// it to Next.js which in turn responds with a 400
return NextResponse.next();
Expand Down Expand Up @@ -153,7 +152,7 @@ export default function createMiddleware<
resolvedRouting.localePrefix.mode === 'as-needed');

let response;
let internalTemplateName: keyof AppPathnames | undefined;
let internalTemplateName: string | undefined;

let unprefixedInternalPathname = unprefixedExternalPathname;
if ('pathnames' in resolvedRouting) {
Expand All @@ -162,15 +161,14 @@ export default function createMiddleware<
resolvedRouting.pathnames,
unprefixedExternalPathname,
locale
);
) as [AppLocales[number] | undefined, string | undefined];

if (internalTemplateName) {
const pathnameConfig = resolvedRouting.pathnames[internalTemplateName];
const localeTemplate: string =
typeof pathnameConfig === 'string'
? pathnameConfig
: // @ts-expect-error -- This is fine
pathnameConfig[locale];
: pathnameConfig[locale];

if (matchesPathname(localeTemplate, unprefixedExternalPathname)) {
unprefixedInternalPathname = formatTemplatePathname(
Expand All @@ -179,14 +177,13 @@ export default function createMiddleware<
internalTemplateName as string
);
} else {
let sourceTemplate;
let sourceTemplate: string;
if (resolvedTemplateLocale) {
// A localized pathname from another locale has matched
sourceTemplate =
typeof pathnameConfig === 'string'
? pathnameConfig
: // @ts-expect-error -- This is fine
pathnameConfig[resolvedTemplateLocale];
: pathnameConfig[resolvedTemplateLocale];
} else {
// An internal pathname has matched that
// doesn't have a localized pathname
Expand Down Expand Up @@ -310,8 +307,8 @@ export default function createMiddleware<
getAlternateLinksHeaderValue({
routing: resolvedRouting,
localizedPathnames:
internalTemplateName! != null && 'pathnames' in resolvedRouting
? resolvedRouting.pathnames?.[internalTemplateName]
internalTemplateName != null && 'pathnames' in resolvedRouting
? resolvedRouting.pathnames[internalTemplateName]
: undefined,
request,
resolvedLocale: locale
Expand Down
18 changes: 9 additions & 9 deletions src/middleware/resolveLocale.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import Negotiator from 'negotiator';
import {RequestCookies} from 'next/dist/server/web/spec-extension/cookies';
import {ResolvedRoutingConfig} from '../routing/config';
import {
Locales,
Pathnames,
DomainConfig,
DomainsConfig,
DomainConfig
Locales,
Pathnames
} from '../routing/types';
import {COOKIE_LOCALE_NAME} from '../shared/constants';
import {ResolvedMiddlewareOptions} from './config';
Expand All @@ -21,7 +21,7 @@ function findDomainFromHost<AppLocales extends Locales>(
// Remove port (easier for local development)
host = host?.replace(/:\d+$/, '');

if (host && domains) {
if (host) {
return domains.find((cur) => cur.domain === host);
}

Expand Down Expand Up @@ -53,7 +53,7 @@ export function getAcceptLanguageLocale<AppLocales extends Locales>(
orderedLocales as unknown as Array<string>,
defaultLocale
);
} catch (e) {
} catch {
// Invalid language
}

Expand Down Expand Up @@ -94,12 +94,12 @@ function resolveLocaleFromPrefix<
}

// Prio 2: Use existing cookie
if (!locale && localeDetection && requestCookies) {
if (!locale && localeDetection) {
locale = getLocaleFromCookie(requestCookies, locales);
}

// Prio 3: Use the `accept-language` header
if (!locale && localeDetection && requestHeaders) {
if (!locale && localeDetection) {
locale = getAcceptLanguageLocale(requestHeaders, locales, defaultLocale);
}

Expand Down Expand Up @@ -157,7 +157,7 @@ function resolveLocaleFromDomain<
}

// Prio 2: Use existing cookie
if (!locale && options.localeDetection && requestCookies) {
if (!locale && options.localeDetection) {
const cookieLocale = getLocaleFromCookie(requestCookies, routing.locales);
if (cookieLocale) {
if (isLocaleSupportedOnDomain(cookieLocale, domain)) {
Expand All @@ -169,7 +169,7 @@ function resolveLocaleFromDomain<
}

// Prio 3: Use the `accept-language` header
if (!locale && options.localeDetection && requestHeaders) {
if (!locale && options.localeDetection) {
const headerLocale = getAcceptLanguageLocale(
requestHeaders,
domain.locales || routing.locales,
Expand Down
12 changes: 5 additions & 7 deletions src/middleware/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
Locales,
LocalePrefixConfigVerbose,
DomainConfig,
Pathnames,
DomainsConfig
DomainsConfig,
LocalePrefixConfigVerbose,
Locales,
Pathnames
} from '../routing/types';
import {
getLocalePrefix,
Expand Down Expand Up @@ -273,9 +273,7 @@ export function getBestMatchingDomain<AppLocales extends Locales>(

// Prio 3: Use alternative domain with restricted matching locale
if (!domainConfig) {
domainConfig = domainsConfig.find(
(cur) => cur.locales != null && cur.locales.includes(locale)
);
domainConfig = domainsConfig.find((cur) => cur.locales?.includes(locale));
}

// Prio 4: Stay on the current domain if it supports all locales
Expand Down
12 changes: 6 additions & 6 deletions src/navigation/createLocalizedPathnamesNavigation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {render, screen} from '@testing-library/react';
import {
usePathname as useNextPathname,
useParams,
redirect as nextRedirect,
RedirectType,
permanentRedirect as nextPermanentRedirect,
RedirectType
redirect as nextRedirect,
usePathname as useNextPathname,
useParams
} from 'next/navigation';
import React from 'react';
import {renderToString} from 'react-dom/server';
import {it, describe, vi, expect, beforeEach} from 'vitest';
import {defineRouting, Pathnames} from '../routing';
import {beforeEach, describe, expect, it, vi} from 'vitest';
import {Pathnames, defineRouting} from '../routing';
import {getRequestLocale} from '../server/react-server/RequestLocale';
import {getLocalePrefix} from '../shared/utils';
import createLocalizedPathnamesNavigationClient from './react-client/createLocalizedPathnamesNavigation';
Expand Down
10 changes: 5 additions & 5 deletions src/navigation/createSharedPathnamesNavigation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {render, screen} from '@testing-library/react';
import {
usePathname as useNextPathname,
useParams,
redirect as nextRedirect,
RedirectType,
permanentRedirect as nextPermanentRedirect,
RedirectType
redirect as nextRedirect,
usePathname as useNextPathname,
useParams
} from 'next/navigation';
import React from 'react';
import {renderToString} from 'react-dom/server';
import {it, describe, vi, expect, beforeEach} from 'vitest';
import {beforeEach, describe, expect, it, vi} from 'vitest';
import {defineRouting} from '../routing';
import {getRequestLocale} from '../server/react-server/RequestLocale';
import {getLocalePrefix} from '../shared/utils';
Expand Down
Loading

0 comments on commit 78c8892

Please sign in to comment.