options.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. const {
  3. validate
  4. } = require('schema-utils');
  5. const schema = require('./options.json');
  6. /** @typedef {import("eslint").ESLint.Options} ESLintOptions */
  7. /** @typedef {import('eslint').ESLint.LintResult} LintResult */
  8. /** @typedef {import('eslint').ESLint.LintResultData} LintResultData */
  9. /**
  10. * @callback FormatterFunction
  11. * @param {LintResult[]} results
  12. * @param {LintResultData=} data
  13. * @returns {string}
  14. */
  15. /**
  16. * @typedef {Object} OutputReport
  17. * @property {string=} filePath
  18. * @property {string|FormatterFunction=} formatter
  19. */
  20. /**
  21. * @typedef {Object} PluginOptions
  22. * @property {string=} context
  23. * @property {boolean=} emitError
  24. * @property {boolean=} emitWarning
  25. * @property {string=} eslintPath
  26. * @property {string|string[]=} exclude
  27. * @property {string|string[]=} extensions
  28. * @property {boolean=} failOnError
  29. * @property {boolean=} failOnWarning
  30. * @property {string|string[]=} files
  31. * @property {boolean=} fix
  32. * @property {string|FormatterFunction=} formatter
  33. * @property {boolean=} lintDirtyModulesOnly
  34. * @property {boolean=} quiet
  35. * @property {OutputReport=} outputReport
  36. * @property {number|boolean=} threads
  37. * @property {RegExp|RegExp[]=} resourceQueryExclude
  38. */
  39. /** @typedef {PluginOptions & ESLintOptions} Options */
  40. /**
  41. * @param {Options} pluginOptions
  42. * @returns {PluginOptions}
  43. */
  44. function getOptions(pluginOptions) {
  45. const options = {
  46. extensions: 'js',
  47. emitError: true,
  48. emitWarning: true,
  49. failOnError: true,
  50. resourceQueryExclude: [],
  51. ...pluginOptions,
  52. ...(pluginOptions.quiet ? {
  53. emitError: true,
  54. emitWarning: false
  55. } : {})
  56. }; // @ts-ignore
  57. validate(schema, options, {
  58. name: 'ESLint Webpack Plugin',
  59. baseDataPath: 'options'
  60. });
  61. return options;
  62. }
  63. /**
  64. * @param {Options} loaderOptions
  65. * @returns {ESLintOptions}
  66. */
  67. function getESLintOptions(loaderOptions) {
  68. const eslintOptions = { ...loaderOptions
  69. }; // Keep the fix option because it is common to both the loader and ESLint.
  70. const {
  71. fix,
  72. extensions,
  73. ...eslintOnlyOptions
  74. } = schema.properties; // No need to guard the for-in because schema.properties has hardcoded keys.
  75. // eslint-disable-next-line guard-for-in
  76. for (const option in eslintOnlyOptions) {
  77. // @ts-ignore
  78. delete eslintOptions[option];
  79. }
  80. return eslintOptions;
  81. }
  82. module.exports = {
  83. getOptions,
  84. getESLintOptions
  85. };