Skip to content

Commit

Permalink
[Fix] JSX pragma: fail gracefully
Browse files Browse the repository at this point in the history
Fixes #3632
  • Loading branch information
ljharb committed Sep 10, 2024
1 parent bdc5791 commit 07503b7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

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

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

[#3632]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3632

[#3629]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3629
[#3817]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3817
[#3807]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3807
Expand Down
3 changes: 2 additions & 1 deletion lib/util/pragma.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ function getFromContext(context) {
}

if (!JS_IDENTIFIER_REGEX.test(pragma)) {
throw new Error(`React pragma ${pragma} is not a valid identifier`);
console.warn(`React pragma ${pragma} is not a valid identifier`);
return 'React';
}
return pragma;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,14 @@ ruleTester.run('boolean-prop-naming', rule, {
options: [{ rule: '(^(is|has|should|without)[A-Z]([A-Za-z0-9]?)+|disabled|required|checked|defaultChecked)' }],
features: ['types'],
},
{
code: `
// Strip @jsx comments, see https://github.com/microsoft/fluentui/issues/29126
const resultCode = result.code
.replace('/** @jsxRuntime automatic */', '')
.replace('/** @jsxImportSource @fluentui/react-jsx-runtime */', '');
`,
},
]),

invalid: parsers.all([
Expand Down
4 changes: 2 additions & 2 deletions tests/util/pragma.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ describe('pragma', () => {
);
});

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

0 comments on commit 07503b7

Please sign in to comment.