diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts index c13940ed10a21..1ebdb68f9501e 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts @@ -135,7 +135,12 @@ export type PluginOptions = { */ eslintSuppressionRules: Array | null | undefined; + /** + * Whether to report "suppression" errors for Flow suppressions. If false, suppression errors + * are only emitted for ESLint suppressions + */ flowSuppressions: boolean; + /* * Ignore 'use no forget' annotations. Helpful during testing but should not be used in production. */ diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Suppression.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Suppression.ts index a0d06f96f0e52..45db4b2cc8db6 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Suppression.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Suppression.ts @@ -87,12 +87,18 @@ export function findProgramSuppressions( let enableComment: t.Comment | null = null; let source: SuppressionSource | null = null; - const rulePattern = `(${ruleNames.join('|')})`; - const disableNextLinePattern = new RegExp( - `eslint-disable-next-line ${rulePattern}`, - ); - const disablePattern = new RegExp(`eslint-disable ${rulePattern}`); - const enablePattern = new RegExp(`eslint-enable ${rulePattern}`); + let disableNextLinePattern: RegExp | null = null; + let disablePattern: RegExp | null = null; + let enablePattern: RegExp | null = null; + if (ruleNames.length !== 0) { + const rulePattern = `(${ruleNames.join('|')})`; + disableNextLinePattern = new RegExp( + `eslint-disable-next-line ${rulePattern}`, + ); + disablePattern = new RegExp(`eslint-disable ${rulePattern}`); + enablePattern = new RegExp(`eslint-enable ${rulePattern}`); + } + const flowSuppressionPattern = new RegExp( '\\$(FlowFixMe\\w*|FlowExpectedError|FlowIssue)\\[react\\-rule', ); @@ -108,6 +114,7 @@ export function findProgramSuppressions( * CommentLine within the block. */ disableComment == null && + disableNextLinePattern != null && disableNextLinePattern.test(comment.value) ) { disableComment = comment; @@ -125,12 +132,16 @@ export function findProgramSuppressions( source = 'Flow'; } - if (disablePattern.test(comment.value)) { + if (disablePattern != null && disablePattern.test(comment.value)) { disableComment = comment; source = 'Eslint'; } - if (enablePattern.test(comment.value) && source === 'Eslint') { + if ( + enablePattern != null && + enablePattern.test(comment.value) && + source === 'Eslint' + ) { enableComment = comment; } diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.expect.md new file mode 100644 index 0000000000000..eeb0ba6c96db9 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.expect.md @@ -0,0 +1,53 @@ + +## Input + +```javascript +// @eslintSuppressionRules:[] + +// The suppression here shouldn't cause compilation to get skipped +// Previously we had a bug where an empty list of suppressions would +// create a regexp that matched any suppression +function Component(props) { + 'use forget'; + // eslint-disable-next-line foo/not-react-related + return
{props.text}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{text: 'Hello'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @eslintSuppressionRules:[] + +// The suppression here shouldn't cause compilation to get skipped +// Previously we had a bug where an empty list of suppressions would +// create a regexp that matched any suppression +function Component(props) { + "use forget"; + const $ = _c(2); + let t0; + if ($[0] !== props.text) { + t0 =
{props.text}
; + $[0] = props.text; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ text: "Hello" }], +}; + +``` + +### Eval output +(kind: ok)
Hello
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.js new file mode 100644 index 0000000000000..b81132d3b8269 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.js @@ -0,0 +1,15 @@ +// @eslintSuppressionRules:[] + +// The suppression here shouldn't cause compilation to get skipped +// Previously we had a bug where an empty list of suppressions would +// create a regexp that matched any suppression +function Component(props) { + 'use forget'; + // eslint-disable-next-line foo/not-react-related + return
{props.text}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{text: 'Hello'}], +};