rsnext/packages/eslint-plugin-next/lib/rules/link-passhref.js
2022-02-01 12:53:55 -06:00

64 lines
1.6 KiB
JavaScript

const NodeAttributes = require('../utils/node-attributes.js')
module.exports = {
meta: {
docs: {
description:
'Ensure passHref is assigned if child of Link component is a custom component',
category: 'HTML',
recommended: true,
url: 'https://nextjs.org/docs/messages/link-passhref',
},
fixable: null,
},
create: function (context) {
let linkImport = null
return {
ImportDeclaration(node) {
if (node.source.value === 'next/link') {
linkImport = node.specifiers[0].local.name
}
},
JSXOpeningElement(node) {
if (node.name.name !== linkImport) {
return
}
const attributes = new NodeAttributes(node)
const children = node.parent.children
if (
!attributes.hasAny() ||
!attributes.has('href') ||
!children.some((attr) => attr.type === 'JSXElement')
) {
return
}
const hasPassHref =
attributes.has('passHref') &&
(typeof attributes.value('passHref') === 'undefined' ||
attributes.value('passHref') === true)
const hasAnchorChild = children.some(
(attr) =>
attr.type === 'JSXElement' && attr.openingElement.name.name === 'a'
)
if (!hasAnchorChild && !hasPassHref) {
context.report({
node,
message: `passHref ${
attributes.value('passHref') !== true
? 'must be set to true'
: 'is missing'
}. See: https://nextjs.org/docs/messages/link-passhref`,
})
}
},
}
},
}