rule.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. const path = require('node:path');
  3. const fs = require('node:fs');
  4. const getDocumentationUrl = require('./get-documentation-url.js');
  5. const isIterable = object => typeof object[Symbol.iterator] === 'function';
  6. class FixAbortError extends Error {}
  7. const fixOptions = {
  8. abort() {
  9. throw new FixAbortError('Fix aborted.');
  10. },
  11. };
  12. function wrapFixFunction(fix) {
  13. return fixer => {
  14. const result = fix(fixer, fixOptions);
  15. if (result && isIterable(result)) {
  16. try {
  17. return [...result];
  18. } catch (error) {
  19. if (error instanceof FixAbortError) {
  20. return;
  21. }
  22. /* c8 ignore next */
  23. throw error;
  24. }
  25. }
  26. return result;
  27. };
  28. }
  29. function reportListenerProblems(listener, context) {
  30. // Listener arguments can be `codePath, node` or `node`
  31. return function (...listenerArguments) {
  32. let problems = listener(...listenerArguments);
  33. if (!problems) {
  34. return;
  35. }
  36. if (!isIterable(problems)) {
  37. problems = [problems];
  38. }
  39. for (const problem of problems) {
  40. if (problem.fix) {
  41. problem.fix = wrapFixFunction(problem.fix);
  42. }
  43. if (Array.isArray(problem.suggest)) {
  44. for (const suggest of problem.suggest) {
  45. if (suggest.fix) {
  46. suggest.fix = wrapFixFunction(suggest.fix);
  47. }
  48. }
  49. }
  50. context.report(problem);
  51. }
  52. };
  53. }
  54. // `checkVueTemplate` function will wrap `create` function, there is no need to wrap twice
  55. const wrappedFunctions = new Set();
  56. function reportProblems(create) {
  57. if (wrappedFunctions.has(create)) {
  58. return create;
  59. }
  60. const wrapped = context => Object.fromEntries(
  61. Object.entries(create(context))
  62. .map(([selector, listener]) => [selector, reportListenerProblems(listener, context)]),
  63. );
  64. wrappedFunctions.add(wrapped);
  65. return wrapped;
  66. }
  67. function checkVueTemplate(create, options) {
  68. const {
  69. visitScriptBlock,
  70. } = {
  71. visitScriptBlock: true,
  72. ...options,
  73. };
  74. create = reportProblems(create);
  75. const wrapped = context => {
  76. const listeners = create(context);
  77. // `vue-eslint-parser`
  78. if (
  79. context.parserServices
  80. && context.parserServices.defineTemplateBodyVisitor
  81. ) {
  82. return visitScriptBlock
  83. ? context.parserServices.defineTemplateBodyVisitor(listeners, listeners)
  84. : context.parserServices.defineTemplateBodyVisitor(listeners);
  85. }
  86. return listeners;
  87. };
  88. wrappedFunctions.add(wrapped);
  89. return wrapped;
  90. }
  91. /** @returns {import('eslint').Rule.RuleModule} */
  92. function loadRule(ruleId) {
  93. const rule = require(`../${ruleId}`);
  94. return {
  95. meta: {
  96. // If there is are, options add `[]` so ESLint can validate that no data is passed to the rule.
  97. // https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-schema.md
  98. schema: [],
  99. ...rule.meta,
  100. docs: {
  101. ...rule.meta.docs,
  102. url: getDocumentationUrl(ruleId),
  103. },
  104. },
  105. create: reportProblems(rule.create),
  106. };
  107. }
  108. function loadRules() {
  109. return Object.fromEntries(
  110. fs.readdirSync(path.join(__dirname, '..'), {withFileTypes: true})
  111. .filter(file => file.isFile())
  112. .map(file => {
  113. const ruleId = path.basename(file.name, '.js');
  114. return [ruleId, loadRule(ruleId)];
  115. }),
  116. );
  117. }
  118. module.exports = {
  119. loadRule,
  120. loadRules,
  121. checkVueTemplate,
  122. };