• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @fileoverview Rule to enforce description with the `Symbol` object
3 * @author Jarek Rencz
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const astUtils = require("./utils/ast-utils");
13
14//------------------------------------------------------------------------------
15// Rule Definition
16//------------------------------------------------------------------------------
17
18
19module.exports = {
20    meta: {
21        type: "suggestion",
22
23        docs: {
24            description: "require symbol descriptions",
25            category: "ECMAScript 6",
26            recommended: false,
27            url: "https://eslint.org/docs/rules/symbol-description"
28        },
29        fixable: null,
30        schema: [],
31        messages: {
32            expected: "Expected Symbol to have a description."
33        }
34    },
35
36    create(context) {
37
38        /**
39         * Reports if node does not conform the rule in case rule is set to
40         * report missing description
41         * @param {ASTNode} node A CallExpression node to check.
42         * @returns {void}
43         */
44        function checkArgument(node) {
45            if (node.arguments.length === 0) {
46                context.report({
47                    node,
48                    messageId: "expected"
49                });
50            }
51        }
52
53        return {
54            "Program:exit"() {
55                const scope = context.getScope();
56                const variable = astUtils.getVariableByName(scope, "Symbol");
57
58                if (variable && variable.defs.length === 0) {
59                    variable.references.forEach(reference => {
60                        const node = reference.identifier;
61
62                        if (astUtils.isCallee(node)) {
63                            checkArgument(node.parent);
64                        }
65                    });
66                }
67            }
68        };
69
70    }
71};
72