no-img-element.js 980 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const url = 'https://nextjs.org/docs/messages/no-img-element'
  2. module.exports = {
  3. meta: {
  4. docs: {
  5. description: 'Prevent usage of `<img>` element to prevent layout shift.',
  6. category: 'HTML',
  7. recommended: true,
  8. url,
  9. },
  10. type: 'problem',
  11. schema: [],
  12. },
  13. create: function (context) {
  14. return {
  15. JSXOpeningElement(node) {
  16. if (node.name.name !== 'img') {
  17. return
  18. }
  19. if (node.attributes.length === 0) {
  20. return
  21. }
  22. if (
  23. node.parent &&
  24. node.parent.openingElement &&
  25. node.parent.parent.openingElement &&
  26. node.parent.parent.openingElement.name &&
  27. node.parent.parent.openingElement.name.name === 'picture'
  28. ) {
  29. return
  30. }
  31. context.report({
  32. node,
  33. message: `Do not use \`<img>\` element. Use \`<Image />\` from \`next/image\` instead. See: ${url}`,
  34. })
  35. },
  36. }
  37. },
  38. }