worker.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. /** @typedef {import('eslint').ESLint} ESLint */
  3. /** @typedef {import('eslint').ESLint.Options} ESLintOptions */
  4. Object.assign(module.exports, {
  5. lintFiles,
  6. setup
  7. });
  8. /** @type {{ new (arg0: import("eslint").ESLint.Options): import("eslint").ESLint; outputFixes: (arg0: import("eslint").ESLint.LintResult[]) => any; }} */
  9. let ESLint;
  10. /** @type {ESLint} */
  11. let eslint;
  12. /** @type {boolean} */
  13. let fix;
  14. /**
  15. * @typedef {object} setupOptions
  16. * @property {string=} eslintPath - import path of eslint
  17. * @property {ESLintOptions=} eslintOptions - linter options
  18. *
  19. * @param {setupOptions} arg0 - setup worker
  20. */
  21. function setup({
  22. eslintPath,
  23. eslintOptions = {}
  24. }) {
  25. fix = !!(eslintOptions && eslintOptions.fix);
  26. ({
  27. ESLint
  28. } = require(eslintPath || 'eslint'));
  29. eslint = new ESLint(eslintOptions);
  30. }
  31. /**
  32. * @param {string | string[]} files
  33. */
  34. async function lintFiles(files) {
  35. const result = await eslint.lintFiles(files); // if enabled, use eslint autofixing where possible
  36. if (fix) {
  37. await ESLint.outputFixes(result);
  38. }
  39. return result;
  40. }