UNPKG

14.2 kBJavaScriptView Raw
1/**
2 * @fileoverview Options configuration for optionator.
3 * @author George Zahariev
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const optionator = require("optionator");
13
14//------------------------------------------------------------------------------
15// Typedefs
16//------------------------------------------------------------------------------
17
18/**
19 * The options object parsed by Optionator.
20 * @typedef {Object} ParsedCLIOptions
21 * @property {boolean} cache Only check changed files
22 * @property {string} cacheFile Path to the cache file. Deprecated: use --cache-location
23 * @property {string} [cacheLocation] Path to the cache file or directory
24 * @property {"metadata" | "content"} cacheStrategy Strategy to use for detecting changed files in the cache
25 * @property {boolean} [color] Force enabling/disabling of color
26 * @property {string} [config] Use this configuration, overriding .eslintrc.* config options if present
27 * @property {boolean} debug Output debugging information
28 * @property {string[]} [env] Specify environments
29 * @property {boolean} envInfo Output execution environment information
30 * @property {boolean} errorOnUnmatchedPattern Prevent errors when pattern is unmatched
31 * @property {boolean} eslintrc Disable use of configuration from .eslintrc.*
32 * @property {string[]} [ext] Specify JavaScript file extensions
33 * @property {boolean} fix Automatically fix problems
34 * @property {boolean} fixDryRun Automatically fix problems without saving the changes to the file system
35 * @property {("directive" | "problem" | "suggestion" | "layout")[]} [fixType] Specify the types of fixes to apply (directive, problem, suggestion, layout)
36 * @property {string} format Use a specific output format
37 * @property {string[]} [global] Define global variables
38 * @property {boolean} [help] Show help
39 * @property {boolean} ignore Disable use of ignore files and patterns
40 * @property {string} [ignorePath] Specify path of ignore file
41 * @property {string[]} [ignorePattern] Pattern of files to ignore (in addition to those in .eslintignore)
42 * @property {boolean} init Run config initialization wizard
43 * @property {boolean} inlineConfig Prevent comments from changing config or rules
44 * @property {number} maxWarnings Number of warnings to trigger nonzero exit code
45 * @property {string} [outputFile] Specify file to write report to
46 * @property {string} [parser] Specify the parser to be used
47 * @property {Object} [parserOptions] Specify parser options
48 * @property {string[]} [plugin] Specify plugins
49 * @property {string} [printConfig] Print the configuration for the given file
50 * @property {boolean | undefined} reportUnusedDisableDirectives Adds reported errors for unused eslint-disable and eslint-enable directives
51 * @property {string | undefined} reportUnusedDisableDirectivesSeverity A severity string indicating if and how unused disable and enable directives should be tracked and reported.
52 * @property {string} [resolvePluginsRelativeTo] A folder where plugins should be resolved from, CWD by default
53 * @property {Object} [rule] Specify rules
54 * @property {string[]} [rulesdir] Load additional rules from this directory. Deprecated: Use rules from plugins
55 * @property {boolean} stdin Lint code provided on <STDIN>
56 * @property {string} [stdinFilename] Specify filename to process STDIN as
57 * @property {boolean} quiet Report errors only
58 * @property {boolean} [version] Output the version number
59 * @property {boolean} warnIgnored Show warnings when the file list includes ignored files
60 * @property {string[]} _ Positional filenames or patterns
61 */
62
63//------------------------------------------------------------------------------
64// Initialization and Public Interface
65//------------------------------------------------------------------------------
66
67// exports "parse(args)", "generateHelp()", and "generateHelpForOption(optionName)"
68
69/**
70 * Creates the CLI options for ESLint.
71 * @param {boolean} usingFlatConfig Indicates if flat config is being used.
72 * @returns {Object} The optionator instance.
73 */
74module.exports = function(usingFlatConfig) {
75
76 let lookupFlag;
77
78 if (usingFlatConfig) {
79 lookupFlag = {
80 option: "config-lookup",
81 type: "Boolean",
82 default: "true",
83 description: "Disable look up for eslint.config.js"
84 };
85 } else {
86 lookupFlag = {
87 option: "eslintrc",
88 type: "Boolean",
89 default: "true",
90 description: "Disable use of configuration from .eslintrc.*"
91 };
92 }
93
94 let envFlag;
95
96 if (!usingFlatConfig) {
97 envFlag = {
98 option: "env",
99 type: "[String]",
100 description: "Specify environments"
101 };
102 }
103
104 let extFlag;
105
106 if (!usingFlatConfig) {
107 extFlag = {
108 option: "ext",
109 type: "[String]",
110 description: "Specify JavaScript file extensions"
111 };
112 }
113
114 let resolvePluginsFlag;
115
116 if (!usingFlatConfig) {
117 resolvePluginsFlag = {
118 option: "resolve-plugins-relative-to",
119 type: "path::String",
120 description: "A folder where plugins should be resolved from, CWD by default"
121 };
122 }
123
124 let rulesDirFlag;
125
126 if (!usingFlatConfig) {
127 rulesDirFlag = {
128 option: "rulesdir",
129 type: "[path::String]",
130 description: "Load additional rules from this directory. Deprecated: Use rules from plugins"
131 };
132 }
133
134 let ignorePathFlag;
135
136 if (!usingFlatConfig) {
137 ignorePathFlag = {
138 option: "ignore-path",
139 type: "path::String",
140 description: "Specify path of ignore file"
141 };
142 }
143
144 let warnIgnoredFlag;
145
146 if (usingFlatConfig) {
147 warnIgnoredFlag = {
148 option: "warn-ignored",
149 type: "Boolean",
150 default: "true",
151 description: "Suppress warnings when the file list includes ignored files"
152 };
153 }
154
155 return optionator({
156 prepend: "eslint [options] file.js [file.js] [dir]",
157 defaults: {
158 concatRepeatedArrays: true,
159 mergeRepeatedObjects: true
160 },
161 options: [
162 {
163 heading: "Basic configuration"
164 },
165 lookupFlag,
166 {
167 option: "config",
168 alias: "c",
169 type: "path::String",
170 description: usingFlatConfig
171 ? "Use this configuration instead of eslint.config.js"
172 : "Use this configuration, overriding .eslintrc.* config options if present"
173 },
174 envFlag,
175 extFlag,
176 {
177 option: "global",
178 type: "[String]",
179 description: "Define global variables"
180 },
181 {
182 option: "parser",
183 type: "String",
184 description: "Specify the parser to be used"
185 },
186 {
187 option: "parser-options",
188 type: "Object",
189 description: "Specify parser options"
190 },
191 resolvePluginsFlag,
192 {
193 heading: "Specify Rules and Plugins"
194 },
195 {
196 option: "plugin",
197 type: "[String]",
198 description: "Specify plugins"
199 },
200 {
201 option: "rule",
202 type: "Object",
203 description: "Specify rules"
204 },
205 rulesDirFlag,
206 {
207 heading: "Fix Problems"
208 },
209 {
210 option: "fix",
211 type: "Boolean",
212 default: false,
213 description: "Automatically fix problems"
214 },
215 {
216 option: "fix-dry-run",
217 type: "Boolean",
218 default: false,
219 description: "Automatically fix problems without saving the changes to the file system"
220 },
221 {
222 option: "fix-type",
223 type: "Array",
224 description: "Specify the types of fixes to apply (directive, problem, suggestion, layout)"
225 },
226 {
227 heading: "Ignore Files"
228 },
229 ignorePathFlag,
230 {
231 option: "ignore",
232 type: "Boolean",
233 default: "true",
234 description: "Disable use of ignore files and patterns"
235 },
236 {
237 option: "ignore-pattern",
238 type: "[String]",
239 description: "Pattern of files to ignore (in addition to those in .eslintignore)",
240 concatRepeatedArrays: [true, {
241 oneValuePerFlag: true
242 }]
243 },
244 {
245 heading: "Use stdin"
246 },
247 {
248 option: "stdin",
249 type: "Boolean",
250 default: "false",
251 description: "Lint code provided on <STDIN>"
252 },
253 {
254 option: "stdin-filename",
255 type: "String",
256 description: "Specify filename to process STDIN as"
257 },
258 {
259 heading: "Handle Warnings"
260 },
261 {
262 option: "quiet",
263 type: "Boolean",
264 default: "false",
265 description: "Report errors only"
266 },
267 {
268 option: "max-warnings",
269 type: "Int",
270 default: "-1",
271 description: "Number of warnings to trigger nonzero exit code"
272 },
273 {
274 heading: "Output"
275 },
276 {
277 option: "output-file",
278 alias: "o",
279 type: "path::String",
280 description: "Specify file to write report to"
281 },
282 {
283 option: "format",
284 alias: "f",
285 type: "String",
286 default: "stylish",
287 description: "Use a specific output format"
288 },
289 {
290 option: "color",
291 type: "Boolean",
292 alias: "no-color",
293 description: "Force enabling/disabling of color"
294 },
295 {
296 heading: "Inline configuration comments"
297 },
298 {
299 option: "inline-config",
300 type: "Boolean",
301 default: "true",
302 description: "Prevent comments from changing config or rules"
303 },
304 {
305 option: "report-unused-disable-directives",
306 type: "Boolean",
307 default: void 0,
308 description: "Adds reported errors for unused eslint-disable and eslint-enable directives"
309 },
310 {
311 option: "report-unused-disable-directives-severity",
312 type: "String",
313 default: void 0,
314 description: "Chooses severity level for reporting unused eslint-disable and eslint-enable directives",
315 enum: ["off", "warn", "error", "0", "1", "2"]
316 },
317 {
318 heading: "Caching"
319 },
320 {
321 option: "cache",
322 type: "Boolean",
323 default: "false",
324 description: "Only check changed files"
325 },
326 {
327 option: "cache-file",
328 type: "path::String",
329 default: ".eslintcache",
330 description: "Path to the cache file. Deprecated: use --cache-location"
331 },
332 {
333 option: "cache-location",
334 type: "path::String",
335 description: "Path to the cache file or directory"
336 },
337 {
338 option: "cache-strategy",
339 dependsOn: ["cache"],
340 type: "String",
341 default: "metadata",
342 enum: ["metadata", "content"],
343 description: "Strategy to use for detecting changed files in the cache"
344 },
345 {
346 heading: "Miscellaneous"
347 },
348 {
349 option: "init",
350 type: "Boolean",
351 default: "false",
352 description: "Run config initialization wizard"
353 },
354 {
355 option: "env-info",
356 type: "Boolean",
357 default: "false",
358 description: "Output execution environment information"
359 },
360 {
361 option: "error-on-unmatched-pattern",
362 type: "Boolean",
363 default: "true",
364 description: "Prevent errors when pattern is unmatched"
365 },
366 {
367 option: "exit-on-fatal-error",
368 type: "Boolean",
369 default: "false",
370 description: "Exit with exit code 2 in case of fatal error"
371 },
372 warnIgnoredFlag,
373 {
374 option: "debug",
375 type: "Boolean",
376 default: false,
377 description: "Output debugging information"
378 },
379 {
380 option: "help",
381 alias: "h",
382 type: "Boolean",
383 description: "Show help"
384 },
385 {
386 option: "version",
387 alias: "v",
388 type: "Boolean",
389 description: "Output the version number"
390 },
391 {
392 option: "print-config",
393 type: "path::String",
394 description: "Print the configuration for the given file"
395 }
396 ].filter(value => !!value)
397 });
398};