add diagnostics for overlap suppressions and update tests
MODIFY
packages/@romefrontend/compiler/suppressions.test.md
packages/@romefrontend/compiler/suppressions.test.md
@@ -272,6 +272,397 @@ Object {
}
```
+## `multiple overlap suppressions`
+
+```javascript
+Object {
+ diagnostics: Array [
+ Object {
+ description: Object {
+ advice: Array []
+ category: "suppressions/overlap"
+ message: PARTIAL_BLESSED_DIAGNOSTIC_MESSAGE {value: "overlap suppression category <emphasis>foo</emphasis>"}
+ }
+ location: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 60
+ line: 3
+ }
+ start: Object {
+ column: 2
+ index: 42
+ line: 3
+ }
+ }
+ }
+ Object {
+ description: Object {
+ advice: Array []
+ category: "suppressions/overlap"
+ message: PARTIAL_BLESSED_DIAGNOSTIC_MESSAGE {value: "overlap suppression category <emphasis>foo</emphasis>"}
+ }
+ location: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 81
+ line: 4
+ }
+ start: Object {
+ column: 2
+ index: 63
+ line: 4
+ }
+ }
+ }
+ ]
+ suppressions: Array [
+ Object {
+ category: "foo"
+ endLine: 6
+ filename: "unknown"
+ startLine: 2
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 18
+ index: 18
+ line: 1
+ }
+ start: Object {
+ column: 0
+ index: 0
+ line: 1
+ }
+ }
+ }
+ Object {
+ category: "foo"
+ endLine: 5
+ filename: "unknown"
+ startLine: 5
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 60
+ line: 3
+ }
+ start: Object {
+ column: 2
+ index: 42
+ line: 3
+ }
+ }
+ }
+ Object {
+ category: "foo"
+ endLine: 5
+ filename: "unknown"
+ startLine: 5
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 81
+ line: 4
+ }
+ start: Object {
+ column: 2
+ index: 63
+ line: 4
+ }
+ }
+ }
+ Object {
+ category: "foo"
+ endLine: 9
+ filename: "unknown"
+ startLine: 9
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 18
+ index: 114
+ line: 8
+ }
+ start: Object {
+ column: 0
+ index: 96
+ line: 8
+ }
+ }
+ }
+ ]
+}
+```
+
+## `overlap suppression with a non-overlap suppression`
+
+```javascript
+Object {
+ diagnostics: Array [
+ Object {
+ description: Object {
+ advice: Array []
+ category: "suppressions/overlap"
+ message: PARTIAL_BLESSED_DIAGNOSTIC_MESSAGE {value: "overlap suppression category <emphasis>foo</emphasis>"}
+ }
+ location: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 60
+ line: 3
+ }
+ start: Object {
+ column: 2
+ index: 42
+ line: 3
+ }
+ }
+ }
+ ]
+ suppressions: Array [
+ Object {
+ category: "foo"
+ endLine: 5
+ filename: "unknown"
+ startLine: 2
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 18
+ index: 18
+ line: 1
+ }
+ start: Object {
+ column: 0
+ index: 0
+ line: 1
+ }
+ }
+ }
+ Object {
+ category: "foo"
+ endLine: 4
+ filename: "unknown"
+ startLine: 4
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 60
+ line: 3
+ }
+ start: Object {
+ column: 2
+ index: 42
+ line: 3
+ }
+ }
+ }
+ Object {
+ category: "foo"
+ endLine: 8
+ filename: "unknown"
+ startLine: 8
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 18
+ index: 93
+ line: 7
+ }
+ start: Object {
+ column: 0
+ index: 75
+ line: 7
+ }
+ }
+ }
+ ]
+}
+```
+
+## `overlap suppressions`
+
+```javascript
+Object {
+ diagnostics: Array [
+ Object {
+ description: Object {
+ advice: Array []
+ category: "suppressions/overlap"
+ message: PARTIAL_BLESSED_DIAGNOSTIC_MESSAGE {value: "overlap suppression category <emphasis>foo</emphasis>"}
+ }
+ location: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 60
+ line: 3
+ }
+ start: Object {
+ column: 2
+ index: 42
+ line: 3
+ }
+ }
+ }
+ ]
+ suppressions: Array [
+ Object {
+ category: "foo"
+ endLine: 5
+ filename: "unknown"
+ startLine: 2
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 18
+ index: 18
+ line: 1
+ }
+ start: Object {
+ column: 0
+ index: 0
+ line: 1
+ }
+ }
+ }
+ Object {
+ category: "foo"
+ endLine: 4
+ filename: "unknown"
+ startLine: 4
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 60
+ line: 3
+ }
+ start: Object {
+ column: 2
+ index: 42
+ line: 3
+ }
+ }
+ }
+ ]
+}
+```
+
+## `overlap suppressions with suppressions in between overlaps`
+
+```javascript
+Object {
+ diagnostics: Array [
+ Object {
+ description: Object {
+ advice: Array []
+ category: "suppressions/overlap"
+ message: PARTIAL_BLESSED_DIAGNOSTIC_MESSAGE {value: "overlap suppression category <emphasis>foo</emphasis>"}
+ }
+ location: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 102
+ line: 5
+ }
+ start: Object {
+ column: 2
+ index: 84
+ line: 5
+ }
+ }
+ }
+ ]
+ suppressions: Array [
+ Object {
+ category: "foo"
+ endLine: 7
+ filename: "unknown"
+ startLine: 2
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 18
+ index: 18
+ line: 1
+ }
+ start: Object {
+ column: 0
+ index: 0
+ line: 1
+ }
+ }
+ }
+ Object {
+ category: "bar"
+ endLine: 6
+ filename: "unknown"
+ startLine: 6
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 60
+ line: 3
+ }
+ start: Object {
+ column: 2
+ index: 42
+ line: 3
+ }
+ }
+ }
+ Object {
+ category: "baz"
+ endLine: 6
+ filename: "unknown"
+ startLine: 6
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 81
+ line: 4
+ }
+ start: Object {
+ column: 2
+ index: 63
+ line: 4
+ }
+ }
+ }
+ Object {
+ category: "foo"
+ endLine: 6
+ filename: "unknown"
+ startLine: 6
+ commentLocation: Object {
+ filename: "unknown"
+ end: Object {
+ column: 20
+ index: 102
+ line: 5
+ }
+ start: Object {
+ column: 2
+ index: 84
+ line: 5
+ }
+ }
+ }
+ ]
+}
+```
+
## `single category`
```javascript

MODIFY
packages/@romefrontend/compiler/suppressions.test.ts
packages/@romefrontend/compiler/suppressions.test.ts
@@ -24,8 +24,7 @@ function extractSuppressionsFromSource(sourceText: string) {
test(
"single category",
async (t) => {
- t.snapshot(
- extractSuppressionsFromSource(
+ const suppressions = extractSuppressionsFromSource(
dedent`
// rome-ignore foo
foo();
@@ -44,16 +43,19 @@ test(
*/
wow();
`,
- ),
);
+
+ t.is(suppressions.suppressions.length, 4);
+ t.is(suppressions.diagnostics.length, 0);
+
+ t.snapshot(suppressions);
},
);
test(
"multiple categories",
async (t) => {
- t.snapshot(
- extractSuppressionsFromSource(
+ const suppressions = extractSuppressionsFromSource(
dedent`
// rome-ignore foo dog
foo();
@@ -72,16 +74,19 @@ test(
*/
wow();
`,
- ),
);
+
+ t.is(suppressions.suppressions.length, 8);
+ t.is(suppressions.diagnostics.length, 0);
+
+ t.snapshot(suppressions);
},
);
test(
"duplicates",
async (t) => {
- t.snapshot(
- extractSuppressionsFromSource(
+ const suppressions = extractSuppressionsFromSource(
dedent`
// rome-ignore dog dog
foo();
@@ -89,7 +94,115 @@ test(
// rome-ignore dog cat dog
bar();
`,
- ),
);
+
+ t.is(suppressions.suppressions.length, 3);
+ t.is(suppressions.diagnostics.length, 2);
+ for (const diagnostic of suppressions.diagnostics) {
+ t.is(diagnostic.description.category, "suppressions/duplicate");
+ }
+
+ t.snapshot(suppressions);
+ },
+);
+
+test(
+ "overlap suppressions",
+ async (t) => {
+ const suppressions = extractSuppressionsFromSource(
+ dedent`
+ // rome-ignore foo
+ function foo_bar() {
+ // rome-ignore foo
+ bar_foo;
+ }
+ `,
+ );
+
+ t.is(suppressions.suppressions.length, 2);
+ t.is(suppressions.diagnostics.length, 1);
+ for (const diagnostic of suppressions.diagnostics) {
+ t.is(diagnostic.description.category, "suppressions/overlap");
+ }
+
+ t.snapshot(suppressions);
+ },
+);
+
+test(
+ "overlap suppressions with suppressions in between overlaps",
+ async (t) => {
+ const suppressions = extractSuppressionsFromSource(
+ dedent`
+ // rome-ignore foo
+ function foo_bar() {
+ // rome-ignore bar
+ // rome-ignore baz
+ // rome-ignore foo
+ bar_foo;
+ }
+ `,
+ );
+
+ t.is(suppressions.suppressions.length, 4);
+ t.is(suppressions.diagnostics.length, 1);
+ for (const diagnostic of suppressions.diagnostics) {
+ t.is(diagnostic.description.category, "suppressions/overlap");
+ }
+
+ t.snapshot(suppressions);
+ },
+);
+
+test(
+ "overlap suppression with a non-overlap suppression",
+ async (t) => {
+ const suppressions = extractSuppressionsFromSource(
+ dedent`
+ // rome-ignore foo
+ function foo_bar() {
+ // rome-ignore foo
+ bar_foo;
+ }
+
+ // rome-ignore foo
+ baz()
+ `,
+ );
+
+ t.is(suppressions.suppressions.length, 3);
+ t.is(suppressions.diagnostics.length, 1);
+ for (const diagnostic of suppressions.diagnostics) {
+ t.is(diagnostic.description.category, "suppressions/overlap");
+ }
+
+ t.snapshot(suppressions);
+ },
+);
+
+test(
+ "multiple overlap suppressions",
+ async (t) => {
+ const suppressions = extractSuppressionsFromSource(
+ dedent`
+ // rome-ignore foo
+ function foo_bar() {
+ // rome-ignore foo
+ // rome-ignore foo
+ bar_foo;
+ }
+
+ // rome-ignore foo
+ baz()
+ `,
+ );
+
+ t.is(suppressions.suppressions.length, 4);
+ t.is(suppressions.diagnostics.length, 2);
+ for (const diagnostic of suppressions.diagnostics) {
+ t.is(diagnostic.description.category, "suppressions/overlap");
+ }
+
+ t.snapshot(suppressions);
},
);

MODIFY
packages/@romefrontend/compiler/suppressions.ts
packages/@romefrontend/compiler/suppressions.ts
@@ -85,7 +85,6 @@ function extractSuppressionsFromComment(
shouldBreak = true;
category = category.slice(-1);
}
-
if (suppressedCategories.has(category)) {
diagnostics.push({
description: descriptions.SUPPRESSIONS.DUPLICATE(category),
@@ -159,6 +158,31 @@ export function extractSuppressionsFromProgram(
}
}
+ // check for overlap suppressions
+ const nonOverlapSuppressions = new Map();
+ for (const suppression of suppressions) {
+ if (nonOverlapSuppressions.has(suppression.category)) {
+ const previousSuppression = nonOverlapSuppressions.get(
+ suppression.category,
+ );
+ const currentSuppression = suppression;
+ if (
+ currentSuppression.startLine > previousSuppression.startLine &&
+ currentSuppression.endLine <= previousSuppression.endLine
+ ) {
+ diagnostics.push({
+ description: descriptions.SUPPRESSIONS.OVERLAP(suppression.category),
+ location: suppression.commentLocation,
+ });
+ } else {
+ // replace suppression to compare to later suppressions
+ nonOverlapSuppressions.set(suppression.category, suppression);
+ }
+ } else {
+ nonOverlapSuppressions.set(suppression.category, suppression);
+ }
+ }
+
return {suppressions, diagnostics};
}

MODIFY
packages/@romefrontend/diagnostics/categories.ts
packages/@romefrontend/diagnostics/categories.ts
@@ -50,6 +50,7 @@ export type DiagnosticCategory =
| "resolver/unknownExport"
| "resolver/unsupported"
| "suppressions/duplicate"
+ | "suppressions/overlap"
| "suppressions/incorrectPrefix"
| "suppressions/missingSpace"
| "suppressions/missingTarget"

MODIFY
packages/@romefrontend/diagnostics/descriptions/suppressions.ts
packages/@romefrontend/diagnostics/descriptions/suppressions.ts
@@ -35,4 +35,8 @@ export const suppressions = createDiagnosticsCategory({
category: "suppressions/duplicate",
message: markup`Duplicate suppression category <emphasis>${category}</emphasis>`,
}),
+ OVERLAP: (category: string) => ({
+ category: "suppressions/overlap",
+ message: markup`overlap suppression category <emphasis>${category}</emphasis>`,
+ }),
});

add diagnostics for overlap suppressions and update tests (#754)