fix: support pageExtensions in no-html-link-for-pages ESLint rule
Description
Fixes #53473
The @next/next/no-html-link-for-pages ESLint rule now respects the pageExtensions configuration from next.config.js.
Problem
Previously, the rule only detected pages with hardcoded extensions (.js, .jsx, .ts, .tsx). When users configured custom pageExtensions in their next.config.js, the ESLint rule would fail to detect those pages, leading to false negatives.
Solution
- Modified
parseUrlForPagesandparseUrlForAppDirfunctions to accept apageExtensionsparameter - Added
buildExtensionRegexhelper to dynamically build regex patterns based on configured extensions - Updated the ESLint rule to read
pageExtensionsfromcontext.settings.next.pageExtensions - Pass the extensions to URL parsing functions
Usage
Users can now configure the rule in their .eslintrc.json:
{
"settings": {
"next": {
"pageExtensions": ["page.tsx", "page.ts", "tsx", "ts"]
}
},
"rules": {
"@next/next/no-html-link-for-pages": ["error"]
}
}
Changes
packages/eslint-plugin-next/src/utils/url.ts:
- Added
buildExtensionRegex()helper function - Updated
parseUrlForPages()to accept and usepageExtensionsparameter - Updated
parseUrlForAppDir()to accept and usepageExtensionsparameter - Updated
getUrlFromPagesDirectories()andgetUrlFromAppDirectory()to pass extensions
packages/eslint-plugin-next/src/rules/no-html-link-for-pages.ts:
- Read
pageExtensionsfromcontext.settings.next.pageExtensions - Pass extensions to URL parsing functions
Testing
- Maintains backward compatibility (defaults to
['tsx', 'ts', 'jsx', 'js']) - Works with custom extensions like
['page.tsx', 'page.ts'] - Correctly detects pages in both
/pagesand/appdirectories
Breaking Changes
None - fully backward compatible
Allow CI Workflow Run
- [ ] approve CI run for commit: e33ea8bcf0d32dd3b659867c2418a40de7dac557
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer