1 | /**
|
2 | * @fileoverview RuleContext utility for rules
|
3 | * @author Nicholas C. Zakas
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Constants
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | var PASSTHROUGHS = [
|
12 | "getSource",
|
13 | "getSourceLines",
|
14 | "getTokens",
|
15 | "getTokensBefore",
|
16 | "getTokenBefore",
|
17 | "getTokensAfter",
|
18 | "getTokenAfter",
|
19 | "getFirstTokens",
|
20 | "getFirstToken",
|
21 | "getLastTokens",
|
22 | "getLastToken",
|
23 | "getComments",
|
24 | "getAncestors",
|
25 | "getScope",
|
26 | "getJSDocComment",
|
27 | "getFilename"
|
28 | ];
|
29 |
|
30 | //------------------------------------------------------------------------------
|
31 | // Rule Definition
|
32 | //------------------------------------------------------------------------------
|
33 |
|
34 | /**
|
35 | * Acts as an abstraction layer between rules and the main eslint object.
|
36 | * @constructor
|
37 | * @param {string} ruleId The ID of the rule using this object.
|
38 | * @param {eslint} eslint The eslint object.
|
39 | * @param {number} severity The configured severity level of the rule.
|
40 | * @param {array} options the configuration information to be added to the rule
|
41 | */
|
42 | function RuleContext(ruleId, eslint, severity, options) {
|
43 |
|
44 | /**
|
45 | * The read-only ID of the rule.
|
46 | */
|
47 | Object.defineProperty(this, "id", {
|
48 | value: ruleId
|
49 | });
|
50 |
|
51 | /**
|
52 | * The read-only options of the rule
|
53 | */
|
54 | Object.defineProperty(this, "options", {
|
55 | value: options
|
56 | });
|
57 |
|
58 | // copy over passthrough methods
|
59 | PASSTHROUGHS.forEach(function(name) {
|
60 | this[name] = function() {
|
61 | return eslint[name].apply(eslint, arguments);
|
62 | };
|
63 | }, this);
|
64 |
|
65 | /**
|
66 | * Passthrough to eslint.report() that automatically assigns the rule ID and severity.
|
67 | * @param {ASTNode} node The AST node related to the message.
|
68 | * @param {Object=} location The location of the error.
|
69 | * @param {string} message The message to display to the user.
|
70 | * @param {Object} opts Optional template data which produces a formatted message
|
71 | * with symbols being replaced by this object's values.
|
72 | * @returns {void}
|
73 | */
|
74 | this.report = function(node, location, message, opts) {
|
75 | eslint.report(ruleId, severity, node, location, message, opts);
|
76 | };
|
77 |
|
78 | }
|
79 |
|
80 | RuleContext.prototype = {
|
81 | constructor: RuleContext
|
82 | };
|
83 |
|
84 | module.exports = RuleContext;
|