Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

apply-disable-directives.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @fileoverview A module that filters reported problems based on `eslint-disable` and `eslint-enable` comments
  3. * @author Teddy Katz
  4. */
  5. "use strict";
  6. /**
  7. * Compares the locations of two objects in a source file
  8. * @param {{line: number, column: number}} itemA The first object
  9. * @param {{line: number, column: number}} itemB The second object
  10. * @returns {number} A value less than 1 if itemA appears before itemB in the source file, greater than 1 if
  11. * itemA appears after itemB in the source file, or 0 if itemA and itemB have the same location.
  12. */
  13. function compareLocations(itemA, itemB) {
  14. return itemA.line - itemB.line || itemA.column - itemB.column;
  15. }
  16. /**
  17. * This is the same as the exported function, except that it
  18. * doesn't handle disable-line and disable-next-line directives, and it always reports unused
  19. * disable directives.
  20. * @param {Object} options options for applying directives. This is the same as the options
  21. * for the exported function, except that `reportUnusedDisableDirectives` is not supported
  22. * (this function always reports unused disable directives).
  23. * @returns {{problems: Problem[], unusedDisableDirectives: Problem[]}} An object with a list
  24. * of filtered problems and unused eslint-disable directives
  25. */
  26. function applyDirectives(options) {
  27. const problems = [];
  28. let nextDirectiveIndex = 0;
  29. let currentGlobalDisableDirective = null;
  30. const disabledRuleMap = new Map();
  31. // enabledRules is only used when there is a current global disable directive.
  32. const enabledRules = new Set();
  33. const usedDisableDirectives = new Set();
  34. for (const problem of options.problems) {
  35. while (
  36. nextDirectiveIndex < options.directives.length &&
  37. compareLocations(options.directives[nextDirectiveIndex], problem) <= 0
  38. ) {
  39. const directive = options.directives[nextDirectiveIndex++];
  40. switch (directive.type) {
  41. case "disable":
  42. if (directive.ruleId === null) {
  43. currentGlobalDisableDirective = directive;
  44. disabledRuleMap.clear();
  45. enabledRules.clear();
  46. } else if (currentGlobalDisableDirective) {
  47. enabledRules.delete(directive.ruleId);
  48. disabledRuleMap.set(directive.ruleId, directive);
  49. } else {
  50. disabledRuleMap.set(directive.ruleId, directive);
  51. }
  52. break;
  53. case "enable":
  54. if (directive.ruleId === null) {
  55. currentGlobalDisableDirective = null;
  56. disabledRuleMap.clear();
  57. } else if (currentGlobalDisableDirective) {
  58. enabledRules.add(directive.ruleId);
  59. disabledRuleMap.delete(directive.ruleId);
  60. } else {
  61. disabledRuleMap.delete(directive.ruleId);
  62. }
  63. break;
  64. // no default
  65. }
  66. }
  67. if (disabledRuleMap.has(problem.ruleId)) {
  68. usedDisableDirectives.add(disabledRuleMap.get(problem.ruleId));
  69. } else if (currentGlobalDisableDirective && !enabledRules.has(problem.ruleId)) {
  70. usedDisableDirectives.add(currentGlobalDisableDirective);
  71. } else {
  72. problems.push(problem);
  73. }
  74. }
  75. const unusedDisableDirectives = options.directives
  76. .filter(directive => directive.type === "disable" && !usedDisableDirectives.has(directive))
  77. .map(directive => ({
  78. ruleId: null,
  79. message: directive.ruleId
  80. ? `Unused eslint-disable directive (no problems were reported from '${directive.ruleId}').`
  81. : "Unused eslint-disable directive (no problems were reported).",
  82. line: directive.unprocessedDirective.line,
  83. column: directive.unprocessedDirective.column,
  84. severity: options.reportUnusedDisableDirectives === "warn" ? 1 : 2,
  85. nodeType: null
  86. }));
  87. return { problems, unusedDisableDirectives };
  88. }
  89. /**
  90. * Given a list of directive comments (i.e. metadata about eslint-disable and eslint-enable comments) and a list
  91. * of reported problems, determines which problems should be reported.
  92. * @param {Object} options Information about directives and problems
  93. * @param {{
  94. * type: ("disable"|"enable"|"disable-line"|"disable-next-line"),
  95. * ruleId: (string|null),
  96. * line: number,
  97. * column: number
  98. * }} options.directives Directive comments found in the file, with one-based columns.
  99. * Two directive comments can only have the same location if they also have the same type (e.g. a single eslint-disable
  100. * comment for two different rules is represented as two directives).
  101. * @param {{ruleId: (string|null), line: number, column: number}[]} options.problems
  102. * A list of problems reported by rules, sorted by increasing location in the file, with one-based columns.
  103. * @param {"off" | "warn" | "error"} options.reportUnusedDisableDirectives If `"warn"` or `"error"`, adds additional problems for unused directives
  104. * @returns {{ruleId: (string|null), line: number, column: number}[]}
  105. * A list of reported problems that were not disabled by the directive comments.
  106. */
  107. module.exports = ({ directives, problems, reportUnusedDisableDirectives = "off" }) => {
  108. const blockDirectives = directives
  109. .filter(directive => directive.type === "disable" || directive.type === "enable")
  110. .map(directive => Object.assign({}, directive, { unprocessedDirective: directive }))
  111. .sort(compareLocations);
  112. /**
  113. * Returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level.
  114. * TODO(stephenwade): Replace this with array.flatMap when we drop support for Node v10
  115. * @param {any[]} array The array to process
  116. * @param {Function} fn The function to use
  117. * @returns {any[]} The result array
  118. */
  119. function flatMap(array, fn) {
  120. const mapped = array.map(fn);
  121. const flattened = [].concat(...mapped);
  122. return flattened;
  123. }
  124. const lineDirectives = flatMap(directives, directive => {
  125. switch (directive.type) {
  126. case "disable":
  127. case "enable":
  128. return [];
  129. case "disable-line":
  130. return [
  131. { type: "disable", line: directive.line, column: 1, ruleId: directive.ruleId, unprocessedDirective: directive },
  132. { type: "enable", line: directive.line + 1, column: 0, ruleId: directive.ruleId, unprocessedDirective: directive }
  133. ];
  134. case "disable-next-line":
  135. return [
  136. { type: "disable", line: directive.line + 1, column: 1, ruleId: directive.ruleId, unprocessedDirective: directive },
  137. { type: "enable", line: directive.line + 2, column: 0, ruleId: directive.ruleId, unprocessedDirective: directive }
  138. ];
  139. default:
  140. throw new TypeError(`Unrecognized directive type '${directive.type}'`);
  141. }
  142. }).sort(compareLocations);
  143. const blockDirectivesResult = applyDirectives({
  144. problems,
  145. directives: blockDirectives,
  146. reportUnusedDisableDirectives
  147. });
  148. const lineDirectivesResult = applyDirectives({
  149. problems: blockDirectivesResult.problems,
  150. directives: lineDirectives,
  151. reportUnusedDisableDirectives
  152. });
  153. return reportUnusedDisableDirectives !== "off"
  154. ? lineDirectivesResult.problems
  155. .concat(blockDirectivesResult.unusedDisableDirectives)
  156. .concat(lineDirectivesResult.unusedDisableDirectives)
  157. .sort(compareLocations)
  158. : lineDirectivesResult.problems;
  159. };