1 | /**
|
2 | * @fileoverview RuleContext utility for rules
|
3 | * @author Nicholas C. Zakas
|
4 | * @copyright 2013 Nicholas C. Zakas. All rights reserved.
|
5 | * See LICENSE file in root directory for full license.
|
6 | */
|
7 | ;
|
8 |
|
9 | //------------------------------------------------------------------------------
|
10 | // Requirements
|
11 | //------------------------------------------------------------------------------
|
12 |
|
13 | var RuleFixer = require("./util/rule-fixer");
|
14 |
|
15 | //------------------------------------------------------------------------------
|
16 | // Constants
|
17 | //------------------------------------------------------------------------------
|
18 |
|
19 | var PASSTHROUGHS = [
|
20 | "getAllComments",
|
21 | "getAncestors",
|
22 | "getComments",
|
23 | "getDeclaredVariables",
|
24 | "getFilename",
|
25 | "getFirstToken",
|
26 | "getFirstTokens",
|
27 | "getJSDocComment",
|
28 | "getLastToken",
|
29 | "getLastTokens",
|
30 | "getNodeByRangeIndex",
|
31 | "getScope",
|
32 | "getSource",
|
33 | "getSourceLines",
|
34 | "getTokenAfter",
|
35 | "getTokenBefore",
|
36 | "getTokenByRangeStart",
|
37 | "getTokens",
|
38 | "getTokensAfter",
|
39 | "getTokensBefore",
|
40 | "getTokensBetween",
|
41 | "markVariableAsUsed",
|
42 | "isMarkedAsUsed"
|
43 | ];
|
44 |
|
45 | //------------------------------------------------------------------------------
|
46 | // Typedefs
|
47 | //------------------------------------------------------------------------------
|
48 |
|
49 | /**
|
50 | * An error message description
|
51 | * @typedef {Object} MessageDescriptor
|
52 | * @property {string} nodeType The type of node.
|
53 | * @property {Location} loc The location of the problem.
|
54 | * @property {string} message The problem message.
|
55 | * @property {Object} [data] Optional data to use to fill in placeholders in the
|
56 | * message.
|
57 | * @property {Function} fix The function to call that creates a fix command.
|
58 | */
|
59 |
|
60 | //------------------------------------------------------------------------------
|
61 | // Rule Definition
|
62 | //------------------------------------------------------------------------------
|
63 |
|
64 | /**
|
65 | * Acts as an abstraction layer between rules and the main eslint object.
|
66 | * @constructor
|
67 | * @param {string} ruleId The ID of the rule using this object.
|
68 | * @param {eslint} eslint The eslint object.
|
69 | * @param {number} severity The configured severity level of the rule.
|
70 | * @param {array} options The configuration information to be added to the rule.
|
71 | * @param {object} settings The configuration settings passed from the config file.
|
72 | * @param {object} ecmaFeatures The ecmaFeatures settings passed from the config file.
|
73 | */
|
74 | function RuleContext(ruleId, eslint, severity, options, settings, ecmaFeatures) {
|
75 |
|
76 | /**
|
77 | * The read-only ID of the rule.
|
78 | */
|
79 | Object.defineProperty(this, "id", {
|
80 | value: ruleId
|
81 | });
|
82 |
|
83 | /**
|
84 | * The read-only options of the rule
|
85 | */
|
86 | Object.defineProperty(this, "options", {
|
87 | value: options
|
88 | });
|
89 |
|
90 | /**
|
91 | * The read-only settings shared between all rules
|
92 | */
|
93 | Object.defineProperty(this, "settings", {
|
94 | value: settings
|
95 | });
|
96 |
|
97 | /**
|
98 | * The read-only ecmaFeatures shared across all rules
|
99 | */
|
100 | Object.defineProperty(this, "ecmaFeatures", {
|
101 | value: Object.create(ecmaFeatures)
|
102 | });
|
103 | Object.freeze(this.ecmaFeatures);
|
104 |
|
105 | // copy over passthrough methods
|
106 | PASSTHROUGHS.forEach(function(name) {
|
107 | this[name] = function() {
|
108 | return eslint[name].apply(eslint, arguments);
|
109 | };
|
110 | }, this);
|
111 |
|
112 | /**
|
113 | * Passthrough to eslint.report() that automatically assigns the rule ID and severity.
|
114 | * @param {ASTNode|MessageDescriptor} nodeOrDescriptor The AST node related to the message or a message
|
115 | * descriptor.
|
116 | * @param {Object=} location The location of the error.
|
117 | * @param {string} message The message to display to the user.
|
118 | * @param {Object} opts Optional template data which produces a formatted message
|
119 | * with symbols being replaced by this object's values.
|
120 | * @returns {void}
|
121 | */
|
122 | this.report = function(nodeOrDescriptor, location, message, opts) {
|
123 |
|
124 | var descriptor,
|
125 | fix = null;
|
126 |
|
127 | // check to see if it's a new style call
|
128 | if (arguments.length === 1) {
|
129 | descriptor = nodeOrDescriptor;
|
130 |
|
131 | // if there's a fix specified, get it
|
132 | if (typeof descriptor.fix === "function") {
|
133 | fix = descriptor.fix(new RuleFixer());
|
134 | }
|
135 |
|
136 | eslint.report(
|
137 | ruleId, severity, descriptor.node,
|
138 | descriptor.loc || descriptor.node.loc.start,
|
139 | descriptor.message, descriptor.data, fix
|
140 | );
|
141 |
|
142 | return;
|
143 | }
|
144 |
|
145 | // old style call
|
146 | eslint.report(ruleId, severity, nodeOrDescriptor, location, message, opts);
|
147 | };
|
148 |
|
149 | /**
|
150 | * Passthrough to eslint.getSourceCode().
|
151 | * @returns {SourceCode} The SourceCode object for the code.
|
152 | */
|
153 | this.getSourceCode = function() {
|
154 | return eslint.getSourceCode();
|
155 | };
|
156 |
|
157 | }
|
158 |
|
159 | RuleContext.prototype = {
|
160 | constructor: RuleContext
|
161 | };
|
162 |
|
163 | module.exports = RuleContext;
|