options.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. const {
  3. validate
  4. } = require('schema-utils'); // @ts-ignore
  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. */
  38. /** @typedef {PluginOptions & ESLintOptions} Options */
  39. /**
  40. * @param {Options} pluginOptions
  41. * @returns {PluginOptions}
  42. */
  43. function getOptions(pluginOptions) {
  44. const options = {
  45. extensions: 'js',
  46. emitError: true,
  47. emitWarning: true,
  48. failOnError: true,
  49. ...pluginOptions,
  50. ...(pluginOptions.quiet ? {
  51. emitError: true,
  52. emitWarning: false
  53. } : {})
  54. }; // @ts-ignore
  55. validate(schema, options, {
  56. name: 'ESLint Webpack Plugin',
  57. baseDataPath: 'options'
  58. });
  59. return options;
  60. }
  61. /**
  62. * @param {Options} loaderOptions
  63. * @returns {ESLintOptions}
  64. */
  65. function getESLintOptions(loaderOptions) {
  66. const eslintOptions = { ...loaderOptions
  67. }; // Keep the fix option because it is common to both the loader and ESLint.
  68. const {
  69. fix,
  70. extensions,
  71. ...eslintOnlyOptions
  72. } = schema.properties; // No need to guard the for-in because schema.properties has hardcoded keys.
  73. // eslint-disable-next-line guard-for-in
  74. for (const option in eslintOnlyOptions) {
  75. // @ts-ignore
  76. delete eslintOptions[option];
  77. }
  78. return eslintOptions;
  79. }
  80. module.exports = {
  81. getOptions,
  82. getESLintOptions
  83. };