options.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 {number | "auto" | "off"} [concurrency] Number of linting threads, "auto" to choose automatically, "off" for no multithreading
  22. * @property {string} [config] Use this configuration, overriding .eslintrc.* config options if present
  23. * @property {boolean} debug Output debugging information
  24. * @property {string[]} [env] Specify environments
  25. * @property {boolean} envInfo Output execution environment information
  26. * @property {boolean} errorOnUnmatchedPattern Prevent errors when pattern is unmatched
  27. * @property {boolean} eslintrc Disable use of configuration from .eslintrc.*
  28. * @property {string[]} [ext] Specify JavaScript file extensions
  29. * @property {string[]} [flag] Feature flags
  30. * @property {boolean} fix Automatically fix problems
  31. * @property {boolean} fixDryRun Automatically fix problems without saving the changes to the file system
  32. * @property {("directive" | "problem" | "suggestion" | "layout")[]} [fixType] Specify the types of fixes to apply (directive, problem, suggestion, layout)
  33. * @property {string} format Use a specific output format
  34. * @property {string[]} [global] Define global variables
  35. * @property {boolean} [help] Show help
  36. * @property {boolean} ignore Disable use of ignore files and patterns
  37. * @property {string} [ignorePath] Specify path of ignore file
  38. * @property {string[]} [ignorePattern] Patterns of files to ignore. In eslintrc mode, these are in addition to `.eslintignore`
  39. * @property {boolean} init Run config initialization wizard
  40. * @property {boolean} inlineConfig Prevent comments from changing config or rules
  41. * @property {number} maxWarnings Number of warnings to trigger nonzero exit code
  42. * @property {string} [outputFile] Specify file to write report to
  43. * @property {string} [parser] Specify the parser to be used
  44. * @property {Object} [parserOptions] Specify parser options
  45. * @property {boolean} [passOnNoPatterns=false] When set to true, missing patterns cause
  46. * the linting operation to short circuit and not report any failures.
  47. * @property {boolean} [passOnUnprunedSuppressions] Ignore unused suppressions
  48. * @property {string[]} [plugin] Specify plugins
  49. * @property {string} [printConfig] Print the configuration for the given file
  50. * @property {boolean} [pruneSuppressions] Prune unused suppressions
  51. * @property {boolean} quiet Report errors only
  52. * @property {boolean | undefined} reportUnusedDisableDirectives Adds reported errors for unused eslint-disable and eslint-enable directives
  53. * @property {string | undefined} reportUnusedDisableDirectivesSeverity A severity string indicating if and how unused disable and enable directives should be tracked and reported.
  54. * @property {string} [resolvePluginsRelativeTo] A folder where plugins should be resolved from, CWD by default
  55. * @property {Object} [rule] Specify rules
  56. * @property {string[]} [rulesdir] Load additional rules from this directory. Deprecated: Use rules from plugins
  57. * @property {boolean} [stats] Report additional statistics
  58. * @property {boolean} stdin Lint code provided on <STDIN>
  59. * @property {string} [stdinFilename] Specify filename to process STDIN as
  60. * @property {boolean} [suppressAll] Suppress all error violations
  61. * @property {string} [suppressionsLocation] Path to the suppressions file or directory
  62. * @property {string[]} [suppressRule] Suppress specific rules
  63. * @property {boolean} [version] Output the version number
  64. * @property {boolean} warnIgnored Show warnings when the file list includes ignored files
  65. * @property {string[]} _ Positional filenames or patterns
  66. */
  67. //------------------------------------------------------------------------------
  68. // Initialization and Public Interface
  69. //------------------------------------------------------------------------------
  70. // exports "parse(args)", "generateHelp()", and "generateHelpForOption(optionName)"
  71. /**
  72. * Creates the CLI options for ESLint.
  73. * @param {boolean} usingFlatConfig Indicates if flat config is being used.
  74. * @returns {Object} The optionator instance.
  75. */
  76. module.exports = function (usingFlatConfig) {
  77. let lookupFlag;
  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. let envFlag;
  94. if (!usingFlatConfig) {
  95. envFlag = {
  96. option: "env",
  97. type: "[String]",
  98. description: "Specify environments",
  99. };
  100. }
  101. let inspectConfigFlag;
  102. if (usingFlatConfig) {
  103. inspectConfigFlag = {
  104. option: "inspect-config",
  105. type: "Boolean",
  106. description:
  107. "Open the config inspector with the current configuration",
  108. };
  109. }
  110. let extFlag;
  111. if (!usingFlatConfig) {
  112. extFlag = {
  113. option: "ext",
  114. type: "[String]",
  115. description: "Specify JavaScript file extensions",
  116. };
  117. } else {
  118. extFlag = {
  119. option: "ext",
  120. type: "[String]",
  121. description: "Specify additional file extensions to lint",
  122. };
  123. }
  124. let resolvePluginsFlag;
  125. if (!usingFlatConfig) {
  126. resolvePluginsFlag = {
  127. option: "resolve-plugins-relative-to",
  128. type: "path::String",
  129. description:
  130. "A folder where plugins should be resolved from, CWD by default",
  131. };
  132. }
  133. let rulesDirFlag;
  134. if (!usingFlatConfig) {
  135. rulesDirFlag = {
  136. option: "rulesdir",
  137. type: "[path::String]",
  138. description:
  139. "Load additional rules from this directory. Deprecated: Use rules from plugins",
  140. };
  141. }
  142. let ignorePathFlag;
  143. if (!usingFlatConfig) {
  144. ignorePathFlag = {
  145. option: "ignore-path",
  146. type: "path::String",
  147. description: "Specify path of ignore file",
  148. };
  149. }
  150. let statsFlag;
  151. if (usingFlatConfig) {
  152. statsFlag = {
  153. option: "stats",
  154. type: "Boolean",
  155. default: "false",
  156. description: "Add statistics to the lint report",
  157. };
  158. }
  159. let warnIgnoredFlag;
  160. if (usingFlatConfig) {
  161. warnIgnoredFlag = {
  162. option: "warn-ignored",
  163. type: "Boolean",
  164. default: "true",
  165. description:
  166. "Suppress warnings when the file list includes ignored files",
  167. };
  168. }
  169. let flagFlag;
  170. if (usingFlatConfig) {
  171. flagFlag = {
  172. option: "flag",
  173. type: "[String]",
  174. description: "Enable a feature flag",
  175. };
  176. }
  177. let reportUnusedInlineConfigsFlag;
  178. if (usingFlatConfig) {
  179. reportUnusedInlineConfigsFlag = {
  180. option: "report-unused-inline-configs",
  181. type: "String",
  182. default: void 0,
  183. description:
  184. "Adds reported errors for unused eslint inline config comments",
  185. enum: ["off", "warn", "error", "0", "1", "2"],
  186. };
  187. }
  188. let mcpFlag;
  189. if (usingFlatConfig) {
  190. mcpFlag = {
  191. option: "mcp",
  192. type: "Boolean",
  193. description: "Start the ESLint MCP server",
  194. };
  195. }
  196. let concurrencyFlag;
  197. if (usingFlatConfig) {
  198. concurrencyFlag = {
  199. option: "concurrency",
  200. type: "Int|String",
  201. default: "off",
  202. description:
  203. "Number of linting threads, auto to choose automatically, off for no multithreading",
  204. };
  205. }
  206. return optionator({
  207. prepend: "eslint [options] file.js [file.js] [dir]",
  208. defaults: {
  209. concatRepeatedArrays: true,
  210. mergeRepeatedObjects: true,
  211. },
  212. options: [
  213. {
  214. heading: "Basic configuration",
  215. },
  216. lookupFlag,
  217. {
  218. option: "config",
  219. alias: "c",
  220. type: "path::String",
  221. description: usingFlatConfig
  222. ? "Use this configuration instead of eslint.config.js, eslint.config.mjs, or eslint.config.cjs"
  223. : "Use this configuration, overriding .eslintrc.* config options if present",
  224. },
  225. inspectConfigFlag,
  226. envFlag,
  227. extFlag,
  228. {
  229. option: "global",
  230. type: "[String]",
  231. description: "Define global variables",
  232. },
  233. {
  234. option: "parser",
  235. type: "String",
  236. description: "Specify the parser to be used",
  237. },
  238. {
  239. option: "parser-options",
  240. type: "Object",
  241. description: "Specify parser options",
  242. },
  243. resolvePluginsFlag,
  244. {
  245. heading: "Specify Rules and Plugins",
  246. },
  247. {
  248. option: "plugin",
  249. type: "[String]",
  250. description: "Specify plugins",
  251. },
  252. {
  253. option: "rule",
  254. type: "Object",
  255. description: "Specify rules",
  256. },
  257. rulesDirFlag,
  258. {
  259. heading: "Fix Problems",
  260. },
  261. {
  262. option: "fix",
  263. type: "Boolean",
  264. default: false,
  265. description: "Automatically fix problems",
  266. },
  267. {
  268. option: "fix-dry-run",
  269. type: "Boolean",
  270. default: false,
  271. description:
  272. "Automatically fix problems without saving the changes to the file system",
  273. },
  274. {
  275. option: "fix-type",
  276. type: "Array",
  277. description:
  278. "Specify the types of fixes to apply (directive, problem, suggestion, layout)",
  279. },
  280. {
  281. heading: "Ignore Files",
  282. },
  283. ignorePathFlag,
  284. {
  285. option: "ignore",
  286. type: "Boolean",
  287. default: "true",
  288. description: "Disable use of ignore files and patterns",
  289. },
  290. {
  291. option: "ignore-pattern",
  292. type: "[String]",
  293. description: `Patterns of files to ignore${usingFlatConfig ? "" : " (in addition to those in .eslintignore)"}`,
  294. concatRepeatedArrays: [
  295. true,
  296. {
  297. oneValuePerFlag: true,
  298. },
  299. ],
  300. },
  301. {
  302. heading: "Use stdin",
  303. },
  304. {
  305. option: "stdin",
  306. type: "Boolean",
  307. default: "false",
  308. description: "Lint code provided on <STDIN>",
  309. },
  310. {
  311. option: "stdin-filename",
  312. type: "String",
  313. description: "Specify filename to process STDIN as",
  314. },
  315. {
  316. heading: "Handle Warnings",
  317. },
  318. {
  319. option: "quiet",
  320. type: "Boolean",
  321. default: "false",
  322. description: "Report errors only",
  323. },
  324. {
  325. option: "max-warnings",
  326. type: "Int",
  327. default: "-1",
  328. description: "Number of warnings to trigger nonzero exit code",
  329. },
  330. {
  331. heading: "Output",
  332. },
  333. {
  334. option: "output-file",
  335. alias: "o",
  336. type: "path::String",
  337. description: "Specify file to write report to",
  338. },
  339. {
  340. option: "format",
  341. alias: "f",
  342. type: "String",
  343. default: "stylish",
  344. description: "Use a specific output format",
  345. },
  346. {
  347. option: "color",
  348. type: "Boolean",
  349. alias: "no-color",
  350. description: "Force enabling/disabling of color",
  351. },
  352. {
  353. heading: "Inline configuration comments",
  354. },
  355. {
  356. option: "inline-config",
  357. type: "Boolean",
  358. default: "true",
  359. description: "Prevent comments from changing config or rules",
  360. },
  361. {
  362. option: "report-unused-disable-directives",
  363. type: "Boolean",
  364. default: void 0,
  365. description:
  366. "Adds reported errors for unused eslint-disable and eslint-enable directives",
  367. },
  368. {
  369. option: "report-unused-disable-directives-severity",
  370. type: "String",
  371. default: void 0,
  372. description:
  373. "Chooses severity level for reporting unused eslint-disable and eslint-enable directives",
  374. enum: ["off", "warn", "error", "0", "1", "2"],
  375. },
  376. reportUnusedInlineConfigsFlag,
  377. {
  378. heading: "Caching",
  379. },
  380. {
  381. option: "cache",
  382. type: "Boolean",
  383. default: "false",
  384. description: "Only check changed files",
  385. },
  386. {
  387. option: "cache-file",
  388. type: "path::String",
  389. default: ".eslintcache",
  390. description:
  391. "Path to the cache file. Deprecated: use --cache-location",
  392. },
  393. {
  394. option: "cache-location",
  395. type: "path::String",
  396. description: "Path to the cache file or directory",
  397. },
  398. {
  399. option: "cache-strategy",
  400. dependsOn: ["cache"],
  401. type: "String",
  402. default: "metadata",
  403. enum: ["metadata", "content"],
  404. description:
  405. "Strategy to use for detecting changed files in the cache",
  406. },
  407. {
  408. heading: "Suppressing Violations",
  409. },
  410. {
  411. option: "suppress-all",
  412. type: "Boolean",
  413. default: "false",
  414. description: "Suppress all violations",
  415. },
  416. {
  417. option: "suppress-rule",
  418. type: "[String]",
  419. description: "Suppress specific rules",
  420. },
  421. {
  422. option: "suppressions-location",
  423. type: "path::String",
  424. description: "Specify the location of the suppressions file",
  425. },
  426. {
  427. option: "prune-suppressions",
  428. type: "Boolean",
  429. default: "false",
  430. description: "Prune unused suppressions",
  431. },
  432. {
  433. option: "pass-on-unpruned-suppressions",
  434. type: "Boolean",
  435. default: "false",
  436. description: "Ignore unused suppressions",
  437. },
  438. {
  439. heading: "Miscellaneous",
  440. },
  441. {
  442. option: "init",
  443. type: "Boolean",
  444. default: "false",
  445. description: "Run config initialization wizard",
  446. },
  447. {
  448. option: "env-info",
  449. type: "Boolean",
  450. default: "false",
  451. description: "Output execution environment information",
  452. },
  453. {
  454. option: "error-on-unmatched-pattern",
  455. type: "Boolean",
  456. default: "true",
  457. description: "Prevent errors when pattern is unmatched",
  458. },
  459. {
  460. option: "exit-on-fatal-error",
  461. type: "Boolean",
  462. default: "false",
  463. description: "Exit with exit code 2 in case of fatal error",
  464. },
  465. warnIgnoredFlag,
  466. {
  467. option: "pass-on-no-patterns",
  468. type: "Boolean",
  469. default: false,
  470. description:
  471. "Exit with exit code 0 in case no file patterns are passed",
  472. },
  473. {
  474. option: "debug",
  475. type: "Boolean",
  476. default: false,
  477. description: "Output debugging information",
  478. },
  479. {
  480. option: "help",
  481. alias: "h",
  482. type: "Boolean",
  483. description: "Show help",
  484. },
  485. {
  486. option: "version",
  487. alias: "v",
  488. type: "Boolean",
  489. description: "Output the version number",
  490. },
  491. {
  492. option: "print-config",
  493. type: "path::String",
  494. description: "Print the configuration for the given file",
  495. },
  496. statsFlag,
  497. flagFlag,
  498. mcpFlag,
  499. concurrencyFlag,
  500. ].filter(value => !!value),
  501. });
  502. };