options.js 13 KB

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