Unverified Commit 7c29a262 authored by Prateek Bhatnagar's avatar Prateek Bhatnagar Committed by GitHub

Bug fix for lint rule (#28677)

parent 3693ed2e
...@@ -42,7 +42,10 @@ function getUrlFromPagesDirectories(urlPrefix, directories) { ...@@ -42,7 +42,10 @@ function getUrlFromPagesDirectories(urlPrefix, directories) {
(url) => `^${normalizeURL(url)}$` (url) => `^${normalizeURL(url)}$`
) )
) )
).map((urlReg) => new RegExp(urlReg)) ).map((urlReg) => {
urlReg = urlReg.replace(/\[.*\]/g, '.*')
return new RegExp(urlReg)
})
} }
// Cache for fs.readdirSync lookup. // Cache for fs.readdirSync lookup.
...@@ -60,7 +63,6 @@ function parseUrlForPages(urlprefix, directory) { ...@@ -60,7 +63,6 @@ function parseUrlForPages(urlprefix, directory) {
const res = [] const res = []
fsReadDirSyncCache[directory].forEach((fname) => { fsReadDirSyncCache[directory].forEach((fname) => {
if (/(\.(j|t)sx?)$/.test(fname)) { if (/(\.(j|t)sx?)$/.test(fname)) {
fname = fname.replace(/\[.*\]/g, '.*')
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?)$/, '')}`)
} }
......
...@@ -104,7 +104,36 @@ export class Blah extends Head { ...@@ -104,7 +104,36 @@ export class Blah extends Head {
render() { render() {
return ( return (
<div> <div>
<a href='/list/blah'>Homepage</a> <a href='/list/foo/bar'>Homepage</a>
<h1>Hello title</h1>
</div>
);
}
}
`
const secondInvalidDynamicCode = `
import Link from 'next/link';
export class Blah extends Head {
render() {
return (
<div>
<a href='/list/foo/'>Homepage</a>
<h1>Hello title</h1>
</div>
);
}
}
`
const thirdInvalidDynamicCode = `
import Link from 'next/link';
export class Blah extends Head {
render() {
return (
<div>
<a href='/list/lorem-ipsum/'>Homepage</a>
<h1>Hello title</h1> <h1>Hello title</h1>
</div> </div>
); );
...@@ -163,7 +192,27 @@ describe('no-html-link-for-pages', function () { ...@@ -163,7 +192,27 @@ describe('no-html-link-for-pages', function () {
assert.notEqual(report, undefined, 'No lint errors found.') assert.notEqual(report, undefined, 'No lint errors found.')
assert.equal( assert.equal(
report.message, report.message,
"Do not use the HTML <a> tag to navigate to /list/blah/. Use Link from 'next/link' instead. See: https://nextjs.org/docs/messages/no-html-link-for-pages." "Do not use the HTML <a> tag to navigate to /list/foo/bar/. Use Link from 'next/link' instead. See: https://nextjs.org/docs/messages/no-html-link-for-pages."
)
const [secondReport] = linter.verify(
secondInvalidDynamicCode,
linterConfig,
{
filename: 'foo.js',
}
)
assert.notEqual(secondReport, undefined, 'No lint errors found.')
assert.equal(
secondReport.message,
"Do not use the HTML <a> tag to navigate to /list/foo/. Use Link from 'next/link' instead. See: https://nextjs.org/docs/messages/no-html-link-for-pages."
)
const [thirdReport] = linter.verify(thirdInvalidDynamicCode, linterConfig, {
filename: 'foo.js',
})
assert.notEqual(thirdReport, undefined, 'No lint errors found.')
assert.equal(
thirdReport.message,
"Do not use the HTML <a> tag to navigate to /list/lorem-ipsum/. Use Link from 'next/link' instead. See: https://nextjs.org/docs/messages/no-html-link-for-pages."
) )
}) })
}) })
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