fix(compiler): suppression for jsx comments
MODIFY
internal/compiler/suppressions.test.md
internal/compiler/suppressions.test.md
**DO NOT MODIFY**. This file has been autogenerated. Run `rome test internal/compiler/suppressions.test.ts --update-snapshots` to update.
+## `JSX comment`
+
+```javascript
+{
+ diagnostics: []
+ suppressions: [
+ {
+ category: ["lint", "react", "noChildrenProp"]
+ endLine: OneIndexedNumber<4>
+ path: UIDPath<unknown>
+ startLine: OneIndexedNumber<3>
+ loc: SourceLocation unknown 2:17-2:42
+ }
+ ]
+}
+```
+
## `duplicates`
```javascript

MODIFY
internal/compiler/suppressions.test.ts
internal/compiler/suppressions.test.ts
@@ -321,3 +321,23 @@ test(
t.snapshot(result);
},
);
+
+test(
+ "JSX comment",
+ async (t) => {
+ const result = extractSuppressionsFromSource(
+ dedent`
+ <div>
+ {/* rome-ignore lint/react/noChildrenProp: this is intentional */}
+ <Cmp children={"foo"} text="something">
+ </Cmp>
+ </div>;
+ `,
+ );
+
+ t.is(result.suppressions.length, 1);
+ t.is(result.diagnostics.length, 0);
+
+ t.snapshot(result);
+ },
+);

MODIFY
internal/compiler/suppressions.ts
internal/compiler/suppressions.ts
@@ -63,7 +63,43 @@ export function createSuppressionsVisitor(): AnyVisitor {
enter(path) {
const {node, context} = path;
- if (node.loc !== undefined && node.leadingComments !== undefined) {
+ if (node.loc !== undefined) {
+ if (
+ node.type === "JSXExpressionContainer" &&
+ node.expression.innerComments !== undefined
+ ) {
+ for (const comment of context.comments.getCommentsFromIds(
+ node.expression.innerComments,
+ )) {
+ if (visitedComments.has(comment)) {
+ continue;
+ }
+ if (path.parent.type === "JSXElement") {
+ let currentNodeFound = false;
+ let nextSibling: AnyNode | undefined = undefined;
+ // scan the children in order to find the next sibling that should have the suppression
+ for (const child of path.parent.children) {
+ if (
+ child.loc?.start === node.loc?.start &&
+ child.loc?.end === node.loc?.end
+ ) {
+ currentNodeFound = true;
+ continue;
+ }
+ // here we don't check text because our parser tracks newlines and tabs/spaces as text
+ if (currentNodeFound && child.type !== "JSXText") {
+ nextSibling = child;
+ visitedComments.add(comment);
+ break;
+ }
+ }
+
+ if (nextSibling) {
+ storeSuppressions(context, comment, nextSibling);
+ }
+ }
+ }
+ } else if (node.leadingComments !== undefined) {
for (const comment of context.comments.getCommentsFromIds(
node.leadingComments,
)) {
@@ -72,12 +108,7 @@ export function createSuppressionsVisitor(): AnyVisitor {
}
visitedComments.add(comment);
- const result = extractSuppressionsFromComment(context, comment, node);
- if (result !== undefined) {
- context.diagnostics.addDiagnostics(result.diagnostics);
- context.suppressions = context.suppressions.concat(
- result.suppressions,
- );
+ storeSuppressions(context, comment, node);
}
}
}
@@ -87,6 +118,18 @@ export function createSuppressionsVisitor(): AnyVisitor {
});
}
+function storeSuppressions(
+ context: CompilerContext,
+ comment: AnyComment,
+ node: AnyNode,
+) {
+ const result = extractSuppressionsFromComment(context, comment, node);
+ if (result !== undefined) {
+ context.diagnostics.addDiagnostics(result.diagnostics);
+ context.suppressions = context.suppressions.concat(result.suppressions);
+ }
+}
+
export function matchesSuppression(
category: DiagnosticCategory,
categoryValue: undefined | string,

fix(compiler): suppression for jsx comments (#1547)