File size: 974 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { defineRule } from '../utils/define-rule'

const url = 'https://nextjs.org/docs/messages/no-sync-scripts'

export = defineRule({
  meta: {
    docs: {
      description: 'Prevent synchronous scripts.',
      recommended: true,
      url,
    },
    type: 'problem',
    schema: [],
  },
  create(context) {
    return {
      JSXOpeningElement(node) {
        if (node.name.name !== 'script') {
          return
        }
        if (node.attributes.length === 0) {
          return
        }
        const attributeNames = node.attributes
          .filter((attr) => attr.type === 'JSXAttribute')
          .map((attr) => attr.name.name)
        if (
          attributeNames.includes('src') &&
          !attributeNames.includes('async') &&
          !attributeNames.includes('defer')
        ) {
          context.report({
            node,
            message: `Synchronous scripts should not be used. See: ${url}`,
          })
        }
      },
    }
  },
})