options.js 15 KB

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