Unverified Commit d1ce7b7c authored by Nils Kaspersson's avatar Nils Kaspersson Committed by GitHub

[ESLint] Prevent `no-html-link-for-pages` from warning for static files (#31495)

The rule [`no-html-link-for-pages`](https://nextjs.org/docs/messages/no-html-link-for-pages) will incorrectly flag an `<a>`-tag intended to download a local asset. This PR adds an exception to the rule for any anchor element with a `download` attribute.
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent 1d2ac3b2
...@@ -98,6 +98,15 @@ module.exports = { ...@@ -98,6 +98,15 @@ module.exports = {
return return
} }
const hasDownloadAttr = node.attributes.find(
(attr) =>
attr.type === 'JSXAttribute' && attr.name.name === 'download'
)
if (hasDownloadAttr) {
return
}
const hrefPath = normalizeURL(href.value.value) const hrefPath = normalizeURL(href.value.value)
// Outgoing links are ignored // Outgoing links are ignored
if (/^(https?:\/\/|\/\/)/.test(hrefPath)) { if (/^(https?:\/\/|\/\/)/.test(hrefPath)) {
......
...@@ -43,7 +43,7 @@ function getUrlFromPagesDirectories(urlPrefix, directories) { ...@@ -43,7 +43,7 @@ function getUrlFromPagesDirectories(urlPrefix, directories) {
) )
) )
).map((urlReg) => { ).map((urlReg) => {
urlReg = urlReg.replace(/\[.*\]/g, '.*') urlReg = urlReg.replace(/\[.*\]/g, '((?!.+?\\..+?).*?)')
return new RegExp(urlReg) return new RegExp(urlReg)
}) })
} }
...@@ -62,6 +62,8 @@ function parseUrlForPages(urlprefix, directory) { ...@@ -62,6 +62,8 @@ function parseUrlForPages(urlprefix, directory) {
fsReadDirSyncCache[directory] || fs.readdirSync(directory) fsReadDirSyncCache[directory] || fs.readdirSync(directory)
const res = [] const res = []
fsReadDirSyncCache[directory].forEach((fname) => { fsReadDirSyncCache[directory].forEach((fname) => {
// TODO: this should account for all page extensions
// not just js(x) and ts(x)
if (/(\.(j|t)sx?)$/.test(fname)) { if (/(\.(j|t)sx?)$/.test(fname)) {
if (/^index(\.(j|t)sx?)$/.test(fname)) { if (/^index(\.(j|t)sx?)$/.test(fname)) {
res.push(`${urlprefix}${fname.replace(/^index(\.(j|t)sx?)$/, '')}`) res.push(`${urlprefix}${fname.replace(/^index(\.(j|t)sx?)$/, '')}`)
......
...@@ -82,6 +82,36 @@ export class Blah extends Head { ...@@ -82,6 +82,36 @@ export class Blah extends Head {
} }
` `
const validDownloadLinkCode = `
import Link from 'next/link';
export class Blah extends Head {
render() {
return (
<div>
<a href='/static-file.csv' download>Download</a>
<h1>Hello title</h1>
</div>
);
}
}
`
const validPublicFile = `
import Link from 'next/link';
export class Blah extends Head {
render() {
return (
<div>
<a href='/presentation.pdf'>View PDF</a>
<h1>Hello title</h1>
</div>
);
}
}
`
const invalidStaticCode = ` const invalidStaticCode = `
import Link from 'next/link'; import Link from 'next/link';
...@@ -174,6 +204,20 @@ describe('no-html-link-for-pages', function () { ...@@ -174,6 +204,20 @@ describe('no-html-link-for-pages', function () {
assert.deepEqual(report, []) assert.deepEqual(report, [])
}) })
it('valid download link element', function () {
const report = linter.verify(validDownloadLinkCode, linterConfig, {
filename: 'foo.js',
})
assert.deepEqual(report, [])
})
it('valid public file link element', function () {
const report = linter.verify(validPublicFile, linterConfig, {
filename: 'foo.js',
})
assert.deepEqual(report, [])
})
it('invalid static route', function () { it('invalid static route', function () {
const [report] = linter.verify(invalidStaticCode, linterConfig, { const [report] = linter.verify(invalidStaticCode, linterConfig, {
filename: 'foo.js', filename: 'foo.js',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment