no-sync-scripts.js 930 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const url = 'https://nextjs.org/docs/messages/no-sync-scripts'
  2. module.exports = {
  3. meta: {
  4. docs: {
  5. description: 'Prevent synchronous scripts.',
  6. recommended: true,
  7. url,
  8. },
  9. type: 'problem',
  10. schema: [],
  11. },
  12. create: function (context) {
  13. return {
  14. JSXOpeningElement(node) {
  15. if (node.name.name !== 'script') {
  16. return
  17. }
  18. if (node.attributes.length === 0) {
  19. return
  20. }
  21. const attributeNames = node.attributes
  22. .filter((attr) => attr.type === 'JSXAttribute')
  23. .map((attr) => attr.name.name)
  24. if (
  25. attributeNames.includes('src') &&
  26. !attributeNames.includes('async') &&
  27. !attributeNames.includes('defer')
  28. ) {
  29. context.report({
  30. node,
  31. message: `Synchronous scripts should not be used. See: ${url}`,
  32. })
  33. }
  34. },
  35. }
  36. },
  37. }