|
1 | 1 | const stylelint = require('stylelint'); |
2 | 2 |
|
3 | | -const {isObject} = require('../../utils'); |
| 3 | +const {isObject, isNumber} = require('../../utils'); |
4 | 4 |
|
5 | 5 | const ruleName = 'stylelint-polaris/coverage'; |
6 | 6 |
|
@@ -55,10 +55,84 @@ module.exports = stylelint.createPlugin( |
55 | 55 | }, |
56 | 56 | ); |
57 | 57 | } |
| 58 | + |
| 59 | + const disabledCoverageWarnings = |
| 60 | + result.stylelint.disabledWarnings?.filter((disabledWarning) => |
| 61 | + disabledWarning.rule.startsWith(ruleName), |
| 62 | + ); |
| 63 | + |
| 64 | + if (!disabledCoverageWarnings?.length) return; |
| 65 | + |
| 66 | + const disabledCoverageLines = Array.from( |
| 67 | + new Set(disabledCoverageWarnings.map(({line}) => line)), |
| 68 | + ); |
| 69 | + |
| 70 | + // Ensure all stylelint-polaris/coverage disable comments |
| 71 | + // have a description prefixed with "polaris:" |
| 72 | + for (const disabledRange of result.stylelint.disabledRanges.all) { |
| 73 | + if ( |
| 74 | + !isDisabledCoverageRule(disabledCoverageLines, disabledRange) || |
| 75 | + disabledRange.description?.startsWith('polaris:') |
| 76 | + ) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + const stylelintDisableText = disabledRange.comment.text |
| 81 | + .split('--')[0] |
| 82 | + .trim(); |
| 83 | + |
| 84 | + stylelint.utils.report({ |
| 85 | + message: `Expected /* ${stylelintDisableText} -- polaris: Reason for disabling */`, |
| 86 | + ruleName, |
| 87 | + result, |
| 88 | + node: disabledRange.comment, |
| 89 | + // Note: `stylelint-disable` comments (without next-line) appear to |
| 90 | + // be special cased in that they do not trigger warnings when reported. |
| 91 | + // Setting `line` to an invalid line number forces the warning to be |
| 92 | + // reported and the above comment `node` is used to display the |
| 93 | + // location information: |
| 94 | + // https://github.com/stylelint/stylelint/blob/57cbcd4eb0ee809006a1e3d2ccfe73af48744ad5/lib/utils/report.js#L49-L52 |
| 95 | + line: -1, |
| 96 | + }); |
| 97 | + } |
58 | 98 | }; |
59 | 99 | }, |
60 | 100 | ); |
61 | 101 |
|
| 102 | +/** |
| 103 | + * @param {number[]} disabledCoverageLines |
| 104 | + * @param {import('stylelint').DisabledRange} disabledRange |
| 105 | + */ |
| 106 | +function isDisabledCoverageRule(disabledCoverageLines, disabledRange) { |
| 107 | + if (isUnclosedDisabledRange(disabledRange)) { |
| 108 | + return disabledCoverageLines.some( |
| 109 | + (disabledCoverageLine) => disabledCoverageLine >= disabledRange.start, |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + return disabledCoverageLines.some( |
| 114 | + (coverageDisabledLine) => |
| 115 | + coverageDisabledLine >= disabledRange.start && |
| 116 | + coverageDisabledLine <= disabledRange.end, |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Checks if the `disabledRange` is an unclosed `stylelint-disable` comment |
| 122 | + * e.g. The `stylelint-disable` comment is NOT followed by `stylelint-enable` |
| 123 | + * @param {import('stylelint').DisabledRange} disabledRange |
| 124 | + */ |
| 125 | +function isUnclosedDisabledRange(disabledRange) { |
| 126 | + if ( |
| 127 | + !disabledRange.comment.text.startsWith('stylelint-disable-next-line') && |
| 128 | + !isNumber(disabledRange.end) |
| 129 | + ) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + return false; |
| 134 | +} |
| 135 | + |
62 | 136 | function validatePrimaryOptions(primaryOptions) { |
63 | 137 | if (!isObject(primaryOptions)) return false; |
64 | 138 |
|
|
0 commit comments