Skip to content

Commit 07503b7

Browse files
committed
[Fix] JSX pragma: fail gracefully
Fixes #3632
1 parent bdc5791 commit 07503b7

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1111

1212
### Fixed
1313
* [`function-component-definition`], [`boolean-prop-naming`], [`jsx-first-prop-new-line`], [`jsx-props-no-multi-spaces`], `propTypes`: use type args ([#3629][] @HenryBrown0)
14+
* JSX pragma: fail gracefully ([#3632][] @ljharb)
1415

1516
### Changed
1617
* [Tests] add @typescript-eslint/parser v6 ([#3629][] @HenryBrown0)
1718
* [Tests] add @typescript-eslint/parser v7 and v8 ([#3629][] @hampustagerud)
1819
* [Docs] [`no-danger`]: update broken link ([#3817][] @lucasrmendonca)
1920

21+
[#3632]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3632
22+
2023
[#3629]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3629
2124
[#3817]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3817
2225
[#3807]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3807

lib/util/pragma.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ function getFromContext(context) {
6262
}
6363

6464
if (!JS_IDENTIFIER_REGEX.test(pragma)) {
65-
throw new Error(`React pragma ${pragma} is not a valid identifier`);
65+
console.warn(`React pragma ${pragma} is not a valid identifier`);
66+
return 'React';
6667
}
6768
return pragma;
6869
}

tests/lib/rules/boolean-prop-naming.js

+8
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,14 @@ ruleTester.run('boolean-prop-naming', rule, {
505505
options: [{ rule: '(^(is|has|should|without)[A-Z]([A-Za-z0-9]?)+|disabled|required|checked|defaultChecked)' }],
506506
features: ['types'],
507507
},
508+
{
509+
code: `
510+
// Strip @jsx comments, see https://github.com/microsoft/fluentui/issues/29126
511+
const resultCode = result.code
512+
.replace('/** @jsxRuntime automatic */', '')
513+
.replace('/** @jsxImportSource @fluentui/react-jsx-runtime */', '');
514+
`,
515+
},
508516
]),
509517

510518
invalid: parsers.all([

tests/util/pragma.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ describe('pragma', () => {
5656
);
5757
});
5858

59-
it('throws an error if the pragma is invalid', () => {
59+
it('returns React if the pragma is invalid', () => {
6060
const code = '/* @jsx invalid-jsx-pragma */';
61-
assert.throws(() => getFromContext(fakeContext(code)));
61+
assert.equal(getFromContext(fakeContext(code)), 'React');
6262
});
6363
});
6464
});

0 commit comments

Comments
 (0)