UNPKG

1.84 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 },
26
27 schema: []
28 },
29
30 create(context) {
31
32 /**
33 * Reports if node does not conform the rule in case rule is set to
34 * report missing description
35 *
36 * @param {ASTNode} node - A CallExpression node to check.
37 * @returns {void}
38 */
39 function checkArgument(node) {
40 if (node.arguments.length === 0) {
41 context.report({
42 node,
43 message: "Expected Symbol to have a description."
44 });
45 }
46 }
47
48 return {
49 "Program:exit"() {
50 const scope = context.getScope();
51 const variable = astUtils.getVariableByName(scope, "Symbol");
52
53 if (variable && variable.defs.length === 0) {
54 variable.references.forEach(function(reference) {
55 const node = reference.identifier;
56
57 if (astUtils.isCallee(node)) {
58 checkArgument(node.parent);
59 }
60 });
61 }
62 }
63 };
64
65 }
66};