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.

needlessDisables.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. const _ = require('lodash');
  3. const optionsMatches = require('./utils/optionsMatches');
  4. const putIfAbsent = require('./utils/putIfAbsent');
  5. const validateDisableSettings = require('./validateDisableSettings');
  6. /** @typedef {import('postcss/lib/comment')} PostcssComment */
  7. /** @typedef {import('stylelint').DisabledRange} DisabledRange */
  8. /** @typedef {import('stylelint').RangeType} RangeType */
  9. /** @typedef {import('stylelint').DisableReportRange} DisableReportRange */
  10. /**
  11. * @param {import('stylelint').StylelintResult[]} results
  12. */
  13. module.exports = function (results) {
  14. results.forEach((result) => {
  15. const settings = validateDisableSettings(result._postcssResult, 'reportNeedlessDisables');
  16. if (!settings) return;
  17. const [enabled, options, stylelintResult] = settings;
  18. /** @type {{[ruleName: string]: Array<DisabledRange>}} */
  19. const rangeData = _.cloneDeep(stylelintResult.disabledRanges);
  20. if (!rangeData) return;
  21. const disabledWarnings = stylelintResult.disabledWarnings || [];
  22. // A map from `stylelint-disable` comments to the set of rules that
  23. // are usefully disabled by each comment. We track this
  24. // comment-by-comment rather than range-by-range because ranges that
  25. // disable *all* rules are duplicated for each rule they apply to in
  26. // practice.
  27. /** @type {Map<PostcssComment, Set<string>>}} */
  28. const usefulDisables = new Map();
  29. for (const warning of disabledWarnings) {
  30. const rule = warning.rule;
  31. const ruleRanges = rangeData[rule];
  32. if (ruleRanges) {
  33. for (const range of ruleRanges) {
  34. if (isWarningInRange(warning, range)) {
  35. putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule);
  36. }
  37. }
  38. }
  39. for (const range of rangeData.all) {
  40. if (isWarningInRange(warning, range)) {
  41. putIfAbsent(usefulDisables, range.comment, () => new Set()).add(rule);
  42. }
  43. }
  44. }
  45. const rangeEntries = Object.entries(rangeData);
  46. // Get rid of the duplicated ranges for each `all` rule. We only care
  47. // if the entire `all` rule is useful as a whole or not.
  48. for (const range of rangeData.all) {
  49. for (const [rule, ranges] of rangeEntries) {
  50. if (rule === 'all') continue;
  51. _.remove(ranges, (otherRange) => range.comment === otherRange.comment);
  52. }
  53. }
  54. for (const [rule, ranges] of rangeEntries) {
  55. for (const range of ranges) {
  56. if (enabled === optionsMatches(options, 'except', rule)) continue;
  57. const useful = usefulDisables.get(range.comment) || new Set();
  58. // Only emit a warning if this range's comment isn't useful for this rule.
  59. // For the special rule "all", only emit a warning if it's not useful for
  60. // *any* rules, because it covers all of them.
  61. if (rule === 'all' ? useful.size !== 0 : useful.has(rule)) continue;
  62. // If the comment doesn't have a location, we can't report a useful error.
  63. // In practice we expect all comments to have locations, though.
  64. if (!range.comment.source || !range.comment.source.start) continue;
  65. result.warnings.push({
  66. text: `Needless disable for "${rule}"`,
  67. rule: '--report-needless-disables',
  68. line: range.comment.source.start.line,
  69. column: range.comment.source.start.column,
  70. severity: options.severity,
  71. });
  72. }
  73. }
  74. });
  75. };
  76. /**
  77. * @param {import('stylelint').DisabledWarning} warning
  78. * @param {RangeType} range
  79. * @return {boolean}
  80. */
  81. function isWarningInRange(warning, range) {
  82. const line = warning.line;
  83. // Need to check if range.end exist, because line number type cannot be compared to undefined
  84. return (
  85. range.start <= line &&
  86. ((range.end !== undefined && range.end >= line) || range.end === undefined)
  87. );
  88. }