1 | /**
|
2 | * @fileoverview Rule to flag references to undeclared variables.
|
3 | * @author Mark Macdonald
|
4 | * @copyright 2015 Nicholas C. Zakas. All rights reserved.
|
5 | * @copyright 2013 Mark Macdonald. All rights reserved.
|
6 | */
|
7 | ;
|
8 |
|
9 | //------------------------------------------------------------------------------
|
10 | // Helpers
|
11 | //------------------------------------------------------------------------------
|
12 |
|
13 | /**
|
14 | * Checks if the given node is the argument of a typeof operator.
|
15 | * @param {ASTNode} node The AST node being checked.
|
16 | * @returns {boolean} Whether or not the node is the argument of a typeof operator.
|
17 | */
|
18 | function hasTypeOfOperator(node) {
|
19 | var parent = node.parent;
|
20 | return parent.type === "UnaryExpression" && parent.operator === "typeof";
|
21 | }
|
22 |
|
23 | //------------------------------------------------------------------------------
|
24 | // Rule Definition
|
25 | //------------------------------------------------------------------------------
|
26 |
|
27 | module.exports = function(context) {
|
28 | var options = context.options[0];
|
29 | var considerTypeOf = options && options.typeof === true || false;
|
30 |
|
31 | return {
|
32 | "Program:exit": function(/* node */) {
|
33 | var globalScope = context.getScope();
|
34 |
|
35 | globalScope.through.forEach(function(ref) {
|
36 | var identifier = ref.identifier;
|
37 |
|
38 | if (!considerTypeOf && hasTypeOfOperator(identifier)) {
|
39 | return;
|
40 | }
|
41 |
|
42 | context.report({
|
43 | node: identifier,
|
44 | message: "'{{name}}' is not defined.",
|
45 | data: identifier
|
46 | });
|
47 | });
|
48 | }
|
49 | };
|
50 | };
|
51 |
|
52 | module.exports.schema = [
|
53 | {
|
54 | "type": "object",
|
55 | "properties": {
|
56 | "typeof": {
|
57 | "type": "boolean"
|
58 | }
|
59 | },
|
60 | "additionalProperties": false
|
61 | }
|
62 | ];
|