Welcome to mirror list, hosted at ThFree Co, Russian Federation.

prefer-primordials.js « eslint-rules « tools - github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 51fb6ab8c2ad441a50d750cddd19ecd713996259 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
 * @fileoverview We shouldn't use global built-in object for security and
 *               performance reason. This linter rule reports replacable codes
 *               that can be replaced with primordials.
 * @author Leko <leko.noor@gmail.com>
 */
'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

function toPrimordialsName(obj, prop) {
  return obj + toUcFirst(prop);
}

function toUcFirst(str) {
  return str[0].toUpperCase() + str.slice(1);
}

function isTarget(map, varName) {
  return map.has(varName);
}

function isIgnored(map, varName, propName) {
  if (!map.has(varName) || !map.get(varName).has(propName)) {
    return false;
  }
  return map.get(varName).get(propName).ignored;
}

function getReportName({ name, parentName, into }) {
  if (into) {
    return toPrimordialsName(into, name);
  }
  if (parentName) {
    return toPrimordialsName(parentName, name);
  }
  return name;
}

/**
 * Get identifier of object spread assignment
 *
 * code: 'const { ownKeys } = Reflect;'
 * argument: 'ownKeys'
 * return: 'Reflect'
 */
function getDestructuringAssignmentParent(scope, node) {
  const declaration = scope.set.get(node.name);
  if (
    !declaration ||
    !declaration.defs ||
    declaration.defs.length === 0 ||
    declaration.defs[0].type !== 'Variable' ||
    !declaration.defs[0].node.init
  ) {
    return null;
  }
  return declaration.defs[0].node.init.name;
}

const identifierSelector =
  '[type!=VariableDeclarator][type!=MemberExpression]>Identifier';

module.exports = {
  meta: {
    messages: {
      error: 'Use `const { {{name}} } = primordials;` instead of the global.'
    }
  },
  create(context) {
    const globalScope = context.getSourceCode().scopeManager.globalScope;
    const nameMap = context.options.reduce((acc, option) =>
      acc.set(
        option.name,
        (option.ignore || [])
          .reduce((acc, name) => acc.set(name, {
            ignored: true
          }), new Map())
      )
    , new Map());
    const renameMap = context.options
      .filter((option) => option.into)
      .reduce((acc, option) =>
        acc.set(option.name, option.into)
      , new Map());
    let reported;

    return {
      Program() {
        reported = new Map();
      },
      [identifierSelector](node) {
        if (reported.has(node.range[0])) {
          return;
        }
        const name = node.name;
        const parentName = getDestructuringAssignmentParent(
          context.getScope(),
          node
        );
        if (!isTarget(nameMap, name) && !isTarget(nameMap, parentName)) {
          return;
        }

        const defs = (globalScope.set.get(name) || {}).defs || null;
        if (parentName && isTarget(nameMap, parentName)) {
          if (!defs || defs[0].name.name !== 'primordials') {
            reported.set(node.range[0], true);
            const into = renameMap.get(name);
            context.report({
              node,
              messageId: 'error',
              data: {
                name: getReportName({ into, parentName, name })
              }
            });
          }
          return;
        }
        if (defs.length === 0 || defs[0].node.init.name !== 'primordials') {
          reported.set(node.range[0], true);
          const into = renameMap.get(name);
          context.report({
            node,
            messageId: 'error',
            data: {
              name: getReportName({ into, parentName, name })
            }
          });
        }
      },
      MemberExpression(node) {
        const obj = node.object.name;
        const prop = node.property.name;
        if (!prop || !isTarget(nameMap, obj) || isIgnored(nameMap, obj, prop)) {
          return;
        }

        const variables =
          context.getSourceCode().scopeManager.getDeclaredVariables(node);
        if (variables.length === 0) {
          context.report({
            node,
            messageId: 'error',
            data: {
              name: toPrimordialsName(obj, prop),
            }
          });
        }
      }
    };
  }
};