apply-disable-directives.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. // Typedefs
  8. //------------------------------------------------------------------------------
  9. /** @typedef {import("../shared/types").LintMessage} LintMessage */
  10. /** @typedef {import("@eslint/core").Language} Language */
  11. /** @typedef {import("@eslint/core").Position} Position */
  12. /** @typedef {import("@eslint/core").RulesConfig} RulesConfig */
  13. //------------------------------------------------------------------------------
  14. // Module Definition
  15. //------------------------------------------------------------------------------
  16. const escapeRegExp = require("escape-string-regexp");
  17. const {
  18. Legacy: {
  19. ConfigOps
  20. }
  21. } = require("@eslint/eslintrc/universal");
  22. /**
  23. * Compares the locations of two objects in a source file
  24. * @param {Position} itemA The first object
  25. * @param {Position} itemB The second object
  26. * @returns {number} A value less than 1 if itemA appears before itemB in the source file, greater than 1 if
  27. * itemA appears after itemB in the source file, or 0 if itemA and itemB have the same location.
  28. */
  29. function compareLocations(itemA, itemB) {
  30. return itemA.line - itemB.line || itemA.column - itemB.column;
  31. }
  32. /**
  33. * Groups a set of directives into sub-arrays by their parent comment.
  34. * @param {Iterable<Directive>} directives Unused directives to be removed.
  35. * @returns {Directive[][]} Directives grouped by their parent comment.
  36. */
  37. function groupByParentDirective(directives) {
  38. const groups = new Map();
  39. for (const directive of directives) {
  40. const { unprocessedDirective: { parentDirective } } = directive;
  41. if (groups.has(parentDirective)) {
  42. groups.get(parentDirective).push(directive);
  43. } else {
  44. groups.set(parentDirective, [directive]);
  45. }
  46. }
  47. return [...groups.values()];
  48. }
  49. /**
  50. * Creates removal details for a set of directives within the same comment.
  51. * @param {Directive[]} directives Unused directives to be removed.
  52. * @param {{node: Token, value: string}} parentDirective Data about the backing directive.
  53. * @param {SourceCode} sourceCode The source code object for the file being linted.
  54. * @returns {{ description, fix, unprocessedDirective }[]} Details for later creation of output Problems.
  55. */
  56. function createIndividualDirectivesRemoval(directives, parentDirective, sourceCode) {
  57. /*
  58. * Get the list of the rules text without any surrounding whitespace. In order to preserve the original
  59. * formatting, we don't want to change that whitespace.
  60. *
  61. * // eslint-disable-line rule-one , rule-two , rule-three -- comment
  62. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  63. */
  64. const listText = parentDirective.value.trim();
  65. // Calculate where it starts in the source code text
  66. const listStart = sourceCode.text.indexOf(listText, sourceCode.getRange(parentDirective.node)[0]);
  67. /*
  68. * We can assume that `listText` contains multiple elements.
  69. * Otherwise, this function wouldn't be called - if there is
  70. * only one rule in the list, then the whole comment must be removed.
  71. */
  72. return directives.map(directive => {
  73. const { ruleId } = directive;
  74. const regex = new RegExp(String.raw`(?:^|\s*,\s*)(?<quote>['"]?)${escapeRegExp(ruleId)}\k<quote>(?:\s*,\s*|$)`, "u");
  75. const match = regex.exec(listText);
  76. const matchedText = match[0];
  77. const matchStart = listStart + match.index;
  78. const matchEnd = matchStart + matchedText.length;
  79. const firstIndexOfComma = matchedText.indexOf(",");
  80. const lastIndexOfComma = matchedText.lastIndexOf(",");
  81. let removalStart, removalEnd;
  82. if (firstIndexOfComma !== lastIndexOfComma) {
  83. /*
  84. * Since there are two commas, this must one of the elements in the middle of the list.
  85. * Matched range starts where the previous rule name ends, and ends where the next rule name starts.
  86. *
  87. * // eslint-disable-line rule-one , rule-two , rule-three -- comment
  88. * ^^^^^^^^^^^^^^
  89. *
  90. * We want to remove only the content between the two commas, and also one of the commas.
  91. *
  92. * // eslint-disable-line rule-one , rule-two , rule-three -- comment
  93. * ^^^^^^^^^^^
  94. */
  95. removalStart = matchStart + firstIndexOfComma;
  96. removalEnd = matchStart + lastIndexOfComma;
  97. } else {
  98. /*
  99. * This is either the first element or the last element.
  100. *
  101. * If this is the first element, matched range starts where the first rule name starts
  102. * and ends where the second rule name starts. This is exactly the range we want
  103. * to remove so that the second rule name will start where the first one was starting
  104. * and thus preserve the original formatting.
  105. *
  106. * // eslint-disable-line rule-one , rule-two , rule-three -- comment
  107. * ^^^^^^^^^^^
  108. *
  109. * Similarly, if this is the last element, we've already matched the range we want to
  110. * remove. The previous rule name will end where the last one was ending, relative
  111. * to the content on the right side.
  112. *
  113. * // eslint-disable-line rule-one , rule-two , rule-three -- comment
  114. * ^^^^^^^^^^^^^
  115. */
  116. removalStart = matchStart;
  117. removalEnd = matchEnd;
  118. }
  119. return {
  120. description: `'${ruleId}'`,
  121. fix: {
  122. range: [
  123. removalStart,
  124. removalEnd
  125. ],
  126. text: ""
  127. },
  128. unprocessedDirective: directive.unprocessedDirective
  129. };
  130. });
  131. }
  132. /**
  133. * Creates a description of deleting an entire unused disable directive.
  134. * @param {Directive[]} directives Unused directives to be removed.
  135. * @param {Token} node The backing Comment token.
  136. * @param {SourceCode} sourceCode The source code object for the file being linted.
  137. * @returns {{ description, fix, unprocessedDirective }} Details for later creation of an output problem.
  138. */
  139. function createDirectiveRemoval(directives, node, sourceCode) {
  140. const range = sourceCode.getRange(node);
  141. const ruleIds = directives.filter(directive => directive.ruleId).map(directive => `'${directive.ruleId}'`);
  142. return {
  143. description: ruleIds.length <= 2
  144. ? ruleIds.join(" or ")
  145. : `${ruleIds.slice(0, ruleIds.length - 1).join(", ")}, or ${ruleIds.at(-1)}`,
  146. fix: {
  147. range,
  148. text: " "
  149. },
  150. unprocessedDirective: directives[0].unprocessedDirective
  151. };
  152. }
  153. /**
  154. * Parses details from directives to create output Problems.
  155. * @param {Iterable<Directive>} allDirectives Unused directives to be removed.
  156. * @param {SourceCode} sourceCode The source code object for the file being linted.
  157. * @returns {{ description, fix, unprocessedDirective }[]} Details for later creation of output Problems.
  158. */
  159. function processUnusedDirectives(allDirectives, sourceCode) {
  160. const directiveGroups = groupByParentDirective(allDirectives);
  161. return directiveGroups.flatMap(
  162. directives => {
  163. const { parentDirective } = directives[0].unprocessedDirective;
  164. const remainingRuleIds = new Set(parentDirective.ruleIds);
  165. for (const directive of directives) {
  166. remainingRuleIds.delete(directive.ruleId);
  167. }
  168. return remainingRuleIds.size
  169. ? createIndividualDirectivesRemoval(directives, parentDirective, sourceCode)
  170. : [createDirectiveRemoval(directives, parentDirective.node, sourceCode)];
  171. }
  172. );
  173. }
  174. /**
  175. * Collect eslint-enable comments that are removing suppressions by eslint-disable comments.
  176. * @param {Directive[]} directives The directives to check.
  177. * @returns {Set<Directive>} The used eslint-enable comments
  178. */
  179. function collectUsedEnableDirectives(directives) {
  180. /**
  181. * A Map of `eslint-enable` keyed by ruleIds that may be marked as used.
  182. * If `eslint-enable` does not have a ruleId, the key will be `null`.
  183. * @type {Map<string|null, Directive>}
  184. */
  185. const enabledRules = new Map();
  186. /**
  187. * A Set of `eslint-enable` marked as used.
  188. * It is also the return value of `collectUsedEnableDirectives` function.
  189. * @type {Set<Directive>}
  190. */
  191. const usedEnableDirectives = new Set();
  192. /*
  193. * Checks the directives backwards to see if the encountered `eslint-enable` is used by the previous `eslint-disable`,
  194. * and if so, stores the `eslint-enable` in `usedEnableDirectives`.
  195. */
  196. for (let index = directives.length - 1; index >= 0; index--) {
  197. const directive = directives[index];
  198. if (directive.type === "disable") {
  199. if (enabledRules.size === 0) {
  200. continue;
  201. }
  202. if (directive.ruleId === null) {
  203. // If encounter `eslint-disable` without ruleId,
  204. // mark all `eslint-enable` currently held in enabledRules as used.
  205. // e.g.
  206. // /* eslint-disable */ <- current directive
  207. // /* eslint-enable rule-id1 */ <- used
  208. // /* eslint-enable rule-id2 */ <- used
  209. // /* eslint-enable */ <- used
  210. for (const enableDirective of enabledRules.values()) {
  211. usedEnableDirectives.add(enableDirective);
  212. }
  213. enabledRules.clear();
  214. } else {
  215. const enableDirective = enabledRules.get(directive.ruleId);
  216. if (enableDirective) {
  217. // If encounter `eslint-disable` with ruleId, and there is an `eslint-enable` with the same ruleId in enabledRules,
  218. // mark `eslint-enable` with ruleId as used.
  219. // e.g.
  220. // /* eslint-disable rule-id */ <- current directive
  221. // /* eslint-enable rule-id */ <- used
  222. usedEnableDirectives.add(enableDirective);
  223. } else {
  224. const enabledDirectiveWithoutRuleId = enabledRules.get(null);
  225. if (enabledDirectiveWithoutRuleId) {
  226. // If encounter `eslint-disable` with ruleId, and there is no `eslint-enable` with the same ruleId in enabledRules,
  227. // mark `eslint-enable` without ruleId as used.
  228. // e.g.
  229. // /* eslint-disable rule-id */ <- current directive
  230. // /* eslint-enable */ <- used
  231. usedEnableDirectives.add(enabledDirectiveWithoutRuleId);
  232. }
  233. }
  234. }
  235. } else if (directive.type === "enable") {
  236. if (directive.ruleId === null) {
  237. // If encounter `eslint-enable` without ruleId, the `eslint-enable` that follows it are unused.
  238. // So clear enabledRules.
  239. // e.g.
  240. // /* eslint-enable */ <- current directive
  241. // /* eslint-enable rule-id *// <- unused
  242. // /* eslint-enable */ <- unused
  243. enabledRules.clear();
  244. enabledRules.set(null, directive);
  245. } else {
  246. enabledRules.set(directive.ruleId, directive);
  247. }
  248. }
  249. }
  250. return usedEnableDirectives;
  251. }
  252. /**
  253. * This is the same as the exported function, except that it
  254. * doesn't handle disable-line and disable-next-line directives, and it always reports unused
  255. * disable directives.
  256. * @param {Object} options options for applying directives. This is the same as the options
  257. * for the exported function, except that `reportUnusedDisableDirectives` is not supported
  258. * (this function always reports unused disable directives).
  259. * @returns {{problems: LintMessage[], unusedDirectives: LintMessage[]}} An object with a list
  260. * of problems (including suppressed ones) and unused eslint-disable directives
  261. */
  262. function applyDirectives(options) {
  263. const problems = [];
  264. const usedDisableDirectives = new Set();
  265. const { sourceCode } = options;
  266. for (const problem of options.problems) {
  267. let disableDirectivesForProblem = [];
  268. let nextDirectiveIndex = 0;
  269. while (
  270. nextDirectiveIndex < options.directives.length &&
  271. compareLocations(options.directives[nextDirectiveIndex], problem) <= 0
  272. ) {
  273. const directive = options.directives[nextDirectiveIndex++];
  274. if (directive.ruleId === null || directive.ruleId === problem.ruleId) {
  275. switch (directive.type) {
  276. case "disable":
  277. disableDirectivesForProblem.push(directive);
  278. break;
  279. case "enable":
  280. disableDirectivesForProblem = [];
  281. break;
  282. // no default
  283. }
  284. }
  285. }
  286. if (disableDirectivesForProblem.length > 0) {
  287. const suppressions = disableDirectivesForProblem.map(directive => ({
  288. kind: "directive",
  289. justification: directive.unprocessedDirective.justification
  290. }));
  291. if (problem.suppressions) {
  292. problem.suppressions = problem.suppressions.concat(suppressions);
  293. } else {
  294. problem.suppressions = suppressions;
  295. usedDisableDirectives.add(disableDirectivesForProblem.at(-1));
  296. }
  297. }
  298. problems.push(problem);
  299. }
  300. const unusedDisableDirectivesToReport = options.directives
  301. .filter(directive => directive.type === "disable" && !usedDisableDirectives.has(directive) && !options.rulesToIgnore.has(directive.ruleId));
  302. const unusedEnableDirectivesToReport = new Set(
  303. options.directives.filter(directive => directive.unprocessedDirective.type === "enable" && !options.rulesToIgnore.has(directive.ruleId))
  304. );
  305. /*
  306. * If directives has the eslint-enable directive,
  307. * check whether the eslint-enable comment is used.
  308. */
  309. if (unusedEnableDirectivesToReport.size > 0) {
  310. for (const directive of collectUsedEnableDirectives(options.directives)) {
  311. unusedEnableDirectivesToReport.delete(directive);
  312. }
  313. }
  314. const processed = processUnusedDirectives(unusedDisableDirectivesToReport, sourceCode)
  315. .concat(processUnusedDirectives(unusedEnableDirectivesToReport, sourceCode));
  316. const columnOffset = options.language.columnStart === 1 ? 0 : 1;
  317. const lineOffset = options.language.lineStart === 1 ? 0 : 1;
  318. const unusedDirectives = processed
  319. .map(({ description, fix, unprocessedDirective }) => {
  320. const { parentDirective, type, line, column } = unprocessedDirective;
  321. let message;
  322. if (type === "enable") {
  323. message = description
  324. ? `Unused eslint-enable directive (no matching eslint-disable directives were found for ${description}).`
  325. : "Unused eslint-enable directive (no matching eslint-disable directives were found).";
  326. } else {
  327. message = description
  328. ? `Unused eslint-disable directive (no problems were reported from ${description}).`
  329. : "Unused eslint-disable directive (no problems were reported).";
  330. }
  331. const loc = sourceCode.getLoc(parentDirective.node);
  332. return {
  333. ruleId: null,
  334. message,
  335. line: type === "disable-next-line" ? loc.start.line + lineOffset : line,
  336. column: type === "disable-next-line" ? loc.start.column + columnOffset : column,
  337. severity: options.reportUnusedDisableDirectives === "warn" ? 1 : 2,
  338. nodeType: null,
  339. ...options.disableFixes ? {} : { fix }
  340. };
  341. });
  342. return { problems, unusedDirectives };
  343. }
  344. /**
  345. * Given a list of directive comments (i.e. metadata about eslint-disable and eslint-enable comments) and a list
  346. * of reported problems, adds the suppression information to the problems.
  347. * @param {Object} options Information about directives and problems
  348. * @param {Language} options.language The language being linted.
  349. * @param {SourceCode} options.sourceCode The source code object for the file being linted.
  350. * @param {{
  351. * type: ("disable"|"enable"|"disable-line"|"disable-next-line"),
  352. * ruleId: (string|null),
  353. * line: number,
  354. * column: number,
  355. * justification: string
  356. * }} options.directives Directive comments found in the file, with one-based columns.
  357. * Two directive comments can only have the same location if they also have the same type (e.g. a single eslint-disable
  358. * comment for two different rules is represented as two directives).
  359. * @param {{ruleId: (string|null), line: number, column: number}[]} options.problems
  360. * A list of problems reported by rules, sorted by increasing location in the file, with one-based columns.
  361. * @param {"off" | "warn" | "error"} options.reportUnusedDisableDirectives If `"warn"` or `"error"`, adds additional problems for unused directives
  362. * @param {RulesConfig} options.configuredRules The rules configuration.
  363. * @param {Function} options.ruleFilter A predicate function to filter which rules should be executed.
  364. * @param {boolean} options.disableFixes If true, it doesn't make `fix` properties.
  365. * @returns {{ruleId: (string|null), line: number, column: number, suppressions?: {kind: string, justification: string}}[]}
  366. * An object with a list of reported problems, the suppressed of which contain the suppression information.
  367. */
  368. module.exports = ({ language, sourceCode, directives, disableFixes, problems, configuredRules, ruleFilter, reportUnusedDisableDirectives = "off" }) => {
  369. const blockDirectives = directives
  370. .filter(directive => directive.type === "disable" || directive.type === "enable")
  371. .map(directive => Object.assign({}, directive, { unprocessedDirective: directive }))
  372. .sort(compareLocations);
  373. const lineDirectives = directives.flatMap(directive => {
  374. switch (directive.type) {
  375. case "disable":
  376. case "enable":
  377. return [];
  378. case "disable-line":
  379. return [
  380. { type: "disable", line: directive.line, column: 1, ruleId: directive.ruleId, unprocessedDirective: directive },
  381. { type: "enable", line: directive.line + 1, column: 0, ruleId: directive.ruleId, unprocessedDirective: directive }
  382. ];
  383. case "disable-next-line":
  384. return [
  385. { type: "disable", line: directive.line + 1, column: 1, ruleId: directive.ruleId, unprocessedDirective: directive },
  386. { type: "enable", line: directive.line + 2, column: 0, ruleId: directive.ruleId, unprocessedDirective: directive }
  387. ];
  388. default:
  389. throw new TypeError(`Unrecognized directive type '${directive.type}'`);
  390. }
  391. }).sort(compareLocations);
  392. // This determines a list of rules that are not being run by the given ruleFilter, if present.
  393. const rulesToIgnore = configuredRules && ruleFilter
  394. ? new Set(Object.keys(configuredRules).filter(ruleId => {
  395. const severity = ConfigOps.getRuleSeverity(configuredRules[ruleId]);
  396. // Ignore for disabled rules.
  397. if (severity === 0) {
  398. return false;
  399. }
  400. return !ruleFilter({ severity, ruleId });
  401. }))
  402. : new Set();
  403. // If no ruleId is supplied that means this directive is applied to all rules, so we can't determine if it's unused if any rules are filtered out.
  404. if (rulesToIgnore.size > 0) {
  405. rulesToIgnore.add(null);
  406. }
  407. const blockDirectivesResult = applyDirectives({
  408. language,
  409. sourceCode,
  410. problems,
  411. directives: blockDirectives,
  412. disableFixes,
  413. reportUnusedDisableDirectives,
  414. rulesToIgnore
  415. });
  416. const lineDirectivesResult = applyDirectives({
  417. language,
  418. sourceCode,
  419. problems: blockDirectivesResult.problems,
  420. directives: lineDirectives,
  421. disableFixes,
  422. reportUnusedDisableDirectives,
  423. rulesToIgnore
  424. });
  425. return reportUnusedDisableDirectives !== "off"
  426. ? lineDirectivesResult.problems
  427. .concat(blockDirectivesResult.unusedDirectives)
  428. .concat(lineDirectivesResult.unusedDirectives)
  429. .sort(compareLocations)
  430. : lineDirectivesResult.problems;
  431. };