options.js 2.5 KB

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