UNPKG

1.9 kBJavaScriptView Raw
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("../ast-utils");
13
14//------------------------------------------------------------------------------
15// Rule Definition
16//------------------------------------------------------------------------------
17
18
19module.exports = {
20 meta: {
21 docs: {
22 description: "require symbol descriptions",
23 category: "ECMAScript 6",
24 recommended: false,
25 url: "https://eslint.org/docs/rules/symbol-description"
26 },
27
28 schema: []
29 },
30
31 create(context) {
32
33 /**
34 * Reports if node does not conform the rule in case rule is set to
35 * report missing description
36 *
37 * @param {ASTNode} node - A CallExpression node to check.
38 * @returns {void}
39 */
40 function checkArgument(node) {
41 if (node.arguments.length === 0) {
42 context.report({
43 node,
44 message: "Expected Symbol to have a description."
45 });
46 }
47 }
48
49 return {
50 "Program:exit"() {
51 const scope = context.getScope();
52 const variable = astUtils.getVariableByName(scope, "Symbol");
53
54 if (variable && variable.defs.length === 0) {
55 variable.references.forEach(reference => {
56 const node = reference.identifier;
57
58 if (astUtils.isCallee(node)) {
59 checkArgument(node.parent);
60 }
61 });
62 }
63 }
64 };
65
66 }
67};