Skip to content

Commit a2bc92a

Browse files
committed
[compiler] Handle empty list of eslint suppression rules
1 parent c5362a3 commit a2bc92a

File tree

4 files changed

+92
-8
lines changed

4 files changed

+92
-8
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ export type PluginOptions = {
135135
*/
136136
eslintSuppressionRules: Array<string> | null | undefined;
137137

138+
/**
139+
* Whether to report "suppression" errors for Flow suppressions. If false, suppression errors
140+
* are only emitted for ESLint suppressions
141+
*/
138142
flowSuppressions: boolean;
143+
139144
/*
140145
* Ignore 'use no forget' annotations. Helpful during testing but should not be used in production.
141146
*/

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Suppression.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,18 @@ export function findProgramSuppressions(
8787
let enableComment: t.Comment | null = null;
8888
let source: SuppressionSource | null = null;
8989

90-
const rulePattern = `(${ruleNames.join('|')})`;
91-
const disableNextLinePattern = new RegExp(
92-
`eslint-disable-next-line ${rulePattern}`,
93-
);
94-
const disablePattern = new RegExp(`eslint-disable ${rulePattern}`);
95-
const enablePattern = new RegExp(`eslint-enable ${rulePattern}`);
90+
let disableNextLinePattern: RegExp | null = null;
91+
let disablePattern: RegExp | null = null;
92+
let enablePattern: RegExp | null = null;
93+
if (ruleNames.length !== 0) {
94+
const rulePattern = `(${ruleNames.join('|')})`;
95+
disableNextLinePattern = new RegExp(
96+
`eslint-disable-next-line ${rulePattern}`,
97+
);
98+
disablePattern = new RegExp(`eslint-disable ${rulePattern}`);
99+
enablePattern = new RegExp(`eslint-enable ${rulePattern}`);
100+
}
101+
96102
const flowSuppressionPattern = new RegExp(
97103
'\\$(FlowFixMe\\w*|FlowExpectedError|FlowIssue)\\[react\\-rule',
98104
);
@@ -108,6 +114,7 @@ export function findProgramSuppressions(
108114
* CommentLine within the block.
109115
*/
110116
disableComment == null &&
117+
disableNextLinePattern != null &&
111118
disableNextLinePattern.test(comment.value)
112119
) {
113120
disableComment = comment;
@@ -125,12 +132,16 @@ export function findProgramSuppressions(
125132
source = 'Flow';
126133
}
127134

128-
if (disablePattern.test(comment.value)) {
135+
if (disablePattern != null && disablePattern.test(comment.value)) {
129136
disableComment = comment;
130137
source = 'Eslint';
131138
}
132139

133-
if (enablePattern.test(comment.value) && source === 'Eslint') {
140+
if (
141+
enablePattern != null &&
142+
enablePattern.test(comment.value) &&
143+
source === 'Eslint'
144+
) {
134145
enableComment = comment;
135146
}
136147

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @eslintSuppressionRules:[]
6+
7+
// The suppression here shouldn't cause compilation to get skipped
8+
// Previously we had a bug where an empty list of suppressions would
9+
// create a regexp that matched any suppression
10+
function Component(props) {
11+
'use forget';
12+
// eslint-disable-next-line foo/not-react-related
13+
return <div>{props.text}</div>;
14+
}
15+
16+
export const FIXTURE_ENTRYPOINT = {
17+
fn: Component,
18+
params: [{text: 'Hello'}],
19+
};
20+
21+
```
22+
23+
## Code
24+
25+
```javascript
26+
import { c as _c } from "react/compiler-runtime"; // @eslintSuppressionRules:[]
27+
28+
// The suppression here shouldn't cause compilation to get skipped
29+
// Previously we had a bug where an empty list of suppressions would
30+
// create a regexp that matched any suppression
31+
function Component(props) {
32+
"use forget";
33+
const $ = _c(2);
34+
let t0;
35+
if ($[0] !== props.text) {
36+
t0 = <div>{props.text}</div>;
37+
$[0] = props.text;
38+
$[1] = t0;
39+
} else {
40+
t0 = $[1];
41+
}
42+
return t0;
43+
}
44+
45+
export const FIXTURE_ENTRYPOINT = {
46+
fn: Component,
47+
params: [{ text: "Hello" }],
48+
};
49+
50+
```
51+
52+
### Eval output
53+
(kind: ok) <div>Hello</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @eslintSuppressionRules:[]
2+
3+
// The suppression here shouldn't cause compilation to get skipped
4+
// Previously we had a bug where an empty list of suppressions would
5+
// create a regexp that matched any suppression
6+
function Component(props) {
7+
'use forget';
8+
// eslint-disable-next-line foo/not-react-related
9+
return <div>{props.text}</div>;
10+
}
11+
12+
export const FIXTURE_ENTRYPOINT = {
13+
fn: Component,
14+
params: [{text: 'Hello'}],
15+
};

0 commit comments

Comments
 (0)