summaryrefslogtreecommitdiff
path: root/tools/eslint-rules/lowercase-name-for-primitive.js
blob: cb603b62c0c6ffc5682fed8ac2db04bf2c51ad49 (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
/**
 * @fileoverview Check that TypeError[ERR_INVALID_ARG_TYPE] uses
 *               lowercase for primitive types
 * @author Weijia Wang <starkwang@126.com>
 */
'use strict';

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

const astSelector = 'NewExpression[callee.property.name="TypeError"]' +
                    '[arguments.0.value="ERR_INVALID_ARG_TYPE"]';

const primitives = [
  'number', 'string', 'boolean', 'null', 'undefined'
];

module.exports = function(context) {
  function checkNamesArgument(node) {
    const names = node.arguments[2];

    switch (names.type) {
      case 'Literal':
        checkName(node, names.value);
        break;
      case 'ArrayExpression':
        names.elements.forEach((name) => {
          checkName(node, name.value);
        });
        break;
    }
  }

  function checkName(node, name) {
    const lowercaseName = name.toLowerCase();
    if (primitives.includes(lowercaseName) && !primitives.includes(name)) {
      const msg = `primitive should use lowercase: ${name}`;
      context.report(node, msg);
    }
  }

  return {
    [astSelector]: (node) => checkNamesArgument(node)
  };
};