Unverified Commit 75b0bfff authored by Prateek Bhatnagar's avatar Prateek Bhatnagar Committed by GitHub

bug fixes for Lint routing (#13111)

- bug fixes in `@next/eslint-plugin-next`.
- adds rules to `recommended` config.
parent 57535b5a
......@@ -2,12 +2,14 @@ module.exports = {
rules: {
'no-css-tags': require('./rules/no-css-tags'),
'no-sync-scripts': require('./rules/no-sync-scripts'),
'no-html-link-for-pages': require('./rules/no-html-link-for-pages'),
},
configs: {
recommended: {
rules: {
'@next/next/no-css-tags': 1,
'@next/next/no-sync-scripts': 1,
'@next/next/no-html-link-for-pages': 1,
},
},
},
......
......@@ -36,13 +36,20 @@ module.exports = {
return
}
const href = node.attributes.find((attr) => attr.name.name === 'href')
const href = node.attributes.find(
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'href'
)
if (!href || href.value.type !== 'Literal') {
return
}
const hrefPath = normalizeURL(href.value.value)
// Outgoing links are ignored
if (/^(https?:\/\/|\/\/)/.test(hrefPath)) {
return
}
urls.forEach((url) => {
if (url.test(normalizeURL(hrefPath))) {
context.report({
......
......@@ -24,7 +24,8 @@ function isSymlink(source) {
*/
function getUrlFromPagesDirectory(urlPrefix, directory) {
return parseUrlForPages(urlPrefix, directory).map(
(url) => new RegExp(normalizeURL(url))
// Since the URLs are normalized we add `^` and `$` to the RegExp to make sure they match exactly.
(url) => new RegExp(`^${normalizeURL(url)}$`)
)
}
......@@ -67,6 +68,10 @@ function normalizeURL(url) {
url = url.split('?')[0]
url = url.split('#')[0]
url = url = url.replace(/(\/index\.html)$/, '/')
// empty URLs should not be trailed with `/`, e.g. `#heading`
if (url === '') {
return url
}
url = url.endsWith('/') ? url : url + '/'
return url
}
......
......@@ -40,6 +40,36 @@ export class Blah extends Head {
}
`
const validAnchorCode = `
import Link from 'next/link';
export class Blah extends Head {
render() {
return (
<div>
<a href='#heading'>Homepage</a>
<h1>Hello title</h1>
</div>
);
}
}
`
const validExternalLinkCode = `
import Link from 'next/link';
export class Blah extends Head {
render() {
return (
<div>
<a href='https://google.com/'>Homepage</a>
<h1>Hello title</h1>
</div>
);
}
}
`
const invalidStaticCode = `
import Link from 'next/link';
......@@ -71,13 +101,27 @@ export class Blah extends Head {
`
describe('no-html-link-for-pages', function () {
it('valid', function () {
it('valid link element', function () {
const report = linter.verify(validCode, linterConfig, {
filename: 'foo.js',
})
assert.deepEqual(report, [])
})
it('valid anchor element', function () {
const report = linter.verify(validAnchorCode, linterConfig, {
filename: 'foo.js',
})
assert.deepEqual(report, [])
})
it('valid external link element', function () {
const report = linter.verify(validExternalLinkCode, linterConfig, {
filename: 'foo.js',
})
assert.deepEqual(report, [])
})
it('invalid static route', function () {
const [report] = linter.verify(invalidStaticCode, linterConfig, {
filename: 'foo.js',
......
......@@ -3494,7 +3494,6 @@ array-unique@^0.3.2:
array.prototype.flat@^1.2.1:
version "1.2.3"
resolved "https://registry.yarnpkg.com/array.prototype.flat/-/array.prototype.flat-1.2.3.tgz#0de82b426b0318dbfdb940089e38b043d37f6c7b"
integrity sha512-gBlRZV0VSmfPIeWfuuy56XZMvbVfbEUnOXUvt3F/eUUUSyzlgLxhEX4YAEpxNAogRGehPSnfXyPtYyKAhkzQhQ==
dependencies:
define-properties "^1.1.3"
es-abstract "^1.17.0-next.1"
......@@ -4964,7 +4963,6 @@ constants-browserify@^1.0.0:
contains-path@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/contains-path/-/contains-path-0.1.0.tgz#fe8cf184ff6670b6baef01a9d4861a5cbec4120a"
integrity sha1-/ozxhP9mcLa67wGp1IYaXL7EEgo=
content-disposition@0.5.3:
version "0.5.3"
......@@ -5909,7 +5907,6 @@ dir-glob@^2.2.2:
doctrine@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-1.5.0.tgz#379dce730f6166f76cefa4e6707a159b02c5a6fa"
integrity sha1-N53Ocw9hZvds76TmcHoVmwLFpvo=
dependencies:
esutils "^2.0.2"
isarray "^1.0.0"
......@@ -6262,7 +6259,6 @@ escodegen@^1.9.1:
eslint-import-resolver-node@^0.3.2:
version "0.3.3"
resolved "https://registry.yarnpkg.com/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.3.tgz#dbaa52b6b2816b50bc6711af75422de808e98404"
integrity sha512-b8crLDo0M5RSe5YG8Pu2DYBj71tSB6OvXkfzwbJU2w7y8P4/yo0MyF8jU26IEuEuHF2K5/gcAJE3LhQGqBBbVg==
dependencies:
debug "^2.6.9"
resolve "^1.13.1"
......@@ -6270,7 +6266,6 @@ eslint-import-resolver-node@^0.3.2:
eslint-module-utils@^2.4.1:
version "2.6.0"
resolved "https://registry.yarnpkg.com/eslint-module-utils/-/eslint-module-utils-2.6.0.tgz#579ebd094f56af7797d19c9866c9c9486629bfa6"
integrity sha512-6j9xxegbqe8/kZY8cYpcp0xhbK0EgJlg3g9mib3/miLaExuuwc3n5UEfSnU6hWMbT0FAYVvDbL9RrRgpUeQIvA==
dependencies:
debug "^2.6.9"
pkg-dir "^2.0.0"
......@@ -6278,7 +6273,6 @@ eslint-module-utils@^2.4.1:
eslint-plugin-import@2.20.2:
version "2.20.2"
resolved "https://registry.yarnpkg.com/eslint-plugin-import/-/eslint-plugin-import-2.20.2.tgz#91fc3807ce08be4837141272c8b99073906e588d"
integrity sha512-FObidqpXrR8OnCh4iNsxy+WACztJLXAHBO5hK79T1Hc77PgQZkyDGA5Ag9xAvRpglvLNxhH/zSmZ70/pZ31dHg==
dependencies:
array-includes "^3.0.3"
array.prototype.flat "^1.2.1"
......@@ -13188,7 +13182,6 @@ read-pkg-up@^1.0.1:
read-pkg-up@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-2.0.0.tgz#6b72a8048984e0c41e79510fd5e9fa99b3b549be"
integrity sha1-a3KoBImE4MQeeVEP1en6mbO1Sb4=
dependencies:
find-up "^2.0.0"
read-pkg "^2.0.0"
......
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