UNPKG

1.35 kBJavaScriptView Raw
1const {docsUrl} = require('../utilities');
2
3module.exports = {
4 meta: {
5 docs: {
6 description:
7 'Prefer that screaming snake case variables always be defined using `const`, and always appear at module scope.',
8 category: 'Best Practices',
9 recommended: true,
10 uri: docsUrl('prefer-module-scope-constants'),
11 },
12 schema: [],
13 },
14
15 create(context) {
16 let inConstDeclaration = false;
17
18 return {
19 VariableDeclaration(node) {
20 inConstDeclaration = node.kind === 'const';
21 },
22 VariableDeclarator(node) {
23 const {id} = node;
24
25 if (id.type !== 'Identifier' || id.name !== id.name.toUpperCase()) {
26 return;
27 }
28
29 if (!inConstDeclaration) {
30 context.report(
31 node,
32 'You must use `const` when defining screaming snake case variables. If this is not a constant, use camelcase instead.',
33 );
34 return;
35 }
36
37 const scope = context.getScope();
38 if (scope.type !== 'module') {
39 context.report(
40 node,
41 'You must place screaming snake case at module scope. If this is not meant to be a module-scoped variable, use camelcase instead.',
42 );
43 }
44 },
45 'VariableDeclaration:exit': () => {
46 inConstDeclaration = false;
47 },
48 };
49 },
50};