UNPKG

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