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

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

Merged
merged 4 commits into from
Nov 19, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ module.exports = {
return
}

const hasDownloadAttr = node.attributes.find(
(attr) =>
attr.type === 'JSXAttribute' && attr.name.name === 'download'
)

if (hasDownloadAttr) {
return
}

const hrefPath = normalizeURL(href.value.value)
// Outgoing links are ignored
if (/^(https?:\/\/|\/\/)/.test(hrefPath)) {
Expand Down
4 changes: 3 additions & 1 deletion packages/eslint-plugin-next/lib/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function getUrlFromPagesDirectories(urlPrefix, directories) {
)
)
).map((urlReg) => {
urlReg = urlReg.replace(/\[.*\]/g, '.*')
urlReg = urlReg.replace(/\[.*\]/g, '((?!.+?\\..+?).*?)')
return new RegExp(urlReg)
})
}
Expand All @@ -62,6 +62,8 @@ function parseUrlForPages(urlprefix, directory) {
fsReadDirSyncCache[directory] || fs.readdirSync(directory)
const res = []
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 (/^index(\.(j|t)sx?)$/.test(fname)) {
res.push(`${urlprefix}${fname.replace(/^index(\.(j|t)sx?)$/, '')}`)
Expand Down
44 changes: 44 additions & 0 deletions test/unit/eslint-plugin-next/no-html-link-for-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
import Link from 'next/link';

Expand Down Expand Up @@ -174,6 +204,20 @@ describe('no-html-link-for-pages', function () {
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 () {
const [report] = linter.verify(invalidStaticCode, linterConfig, {
filename: 'foo.js',
Expand Down