no-styled-jsx-in-document.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const path = require('path')
  2. const url = 'https://nextjs.org/docs/messages/no-styled-jsx-in-document'
  3. module.exports = {
  4. meta: {
  5. docs: {
  6. description: 'Prevent usage of `styled-jsx` in `pages/_document.js`.',
  7. recommended: true,
  8. url,
  9. },
  10. type: 'problem',
  11. schema: [],
  12. },
  13. create: function (context) {
  14. return {
  15. JSXOpeningElement(node) {
  16. const document = context.getFilename().split('pages')[1]
  17. if (!document) {
  18. return
  19. }
  20. const { name, dir } = path.parse(document)
  21. if (
  22. !(
  23. name.startsWith('_document') ||
  24. (dir === '/_document' && name === 'index')
  25. )
  26. ) {
  27. return
  28. }
  29. if (
  30. node.name.name === 'style' &&
  31. node.attributes.find(
  32. (attr) => attr.type === 'JSXAttribute' && attr.name.name === 'jsx'
  33. )
  34. ) {
  35. context.report({
  36. node,
  37. message: `\`styled-jsx\` should not be used in \`pages/_document.js\`. See: ${url}`,
  38. })
  39. }
  40. },
  41. }
  42. },
  43. }