diff --git a/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts b/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts index d4afe577f25c..982083f1671e 100644 --- a/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts +++ b/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts @@ -31,11 +31,12 @@ const DEFAULT = { pragmaFrag: "React.Fragment", }; -const JSX_SOURCE_ANNOTATION_REGEX = /\*?\s*@jsxImportSource\s+([^\s]+)/; -const JSX_RUNTIME_ANNOTATION_REGEX = /\*?\s*@jsxRuntime\s+([^\s]+)/; +const JSX_SOURCE_ANNOTATION_REGEX = + /^\s*\*?\s*@jsxImportSource\s+([^\s]+)\s*$/m; +const JSX_RUNTIME_ANNOTATION_REGEX = /^\s*\*?\s*@jsxRuntime\s+([^\s]+)\s*$/m; -const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/; -const JSX_FRAG_ANNOTATION_REGEX = /\*?\s*@jsxFrag\s+([^\s]+)/; +const JSX_ANNOTATION_REGEX = /^\s*\*?\s*@jsx\s+([^\s]+)\s*$/m; +const JSX_FRAG_ANNOTATION_REGEX = /^\s*\*?\s*@jsxFrag\s+([^\s]+)\s*$/m; const get = (pass: PluginPass, name: string) => pass.get(`@babel/plugin-react-jsx/${name}`); diff --git a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-allow-jsx-docs-comment-with-pragma/input.js b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-allow-jsx-docs-comment-with-pragma/input.js new file mode 100644 index 000000000000..91ca8e5f2fcd --- /dev/null +++ b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-allow-jsx-docs-comment-with-pragma/input.js @@ -0,0 +1,5 @@ +/** + * @jsx jsx + */ + + \ No newline at end of file diff --git a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-allow-jsx-docs-comment-with-pragma/output.js b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-allow-jsx-docs-comment-with-pragma/output.js new file mode 100644 index 000000000000..2e9ea6eff28e --- /dev/null +++ b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-allow-jsx-docs-comment-with-pragma/output.js @@ -0,0 +1,4 @@ +/** + * @jsx jsx + */ +jsx("foo", null); diff --git a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-not-allow-jsx-pragma-to-be-anywhere-in-comment/input.js b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-not-allow-jsx-pragma-to-be-anywhere-in-comment/input.js new file mode 100644 index 000000000000..3aa8b3c8fe05 --- /dev/null +++ b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-not-allow-jsx-pragma-to-be-anywhere-in-comment/input.js @@ -0,0 +1,2 @@ +// Make sure not to use a jsx pragma here (like "@jsx Something"), we need this to be React.createElement! + diff --git a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-not-allow-jsx-pragma-to-be-anywhere-in-comment/output.js b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-not-allow-jsx-pragma-to-be-anywhere-in-comment/output.js new file mode 100644 index 000000000000..940ac582d73d --- /dev/null +++ b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-not-allow-jsx-pragma-to-be-anywhere-in-comment/output.js @@ -0,0 +1,4 @@ +// Make sure not to use a jsx pragma here (like "@jsx Something"), we need this to be React.createElement! + +/*#__PURE__*/ +React.createElement("blah", null);