From 029cd15bd95b70e1164ac4d05ac2a4c178be5769 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Thu, 2 Dec 2021 21:05:44 +0530 Subject: [PATCH] Fix JSX pragma anywhere in comment (#14012) * Fix JSX pragma anywhere in comment * jsx regex patterns support jsx docs * removed extra spaces from regex patterns --- .../src/create-plugin.ts | 9 +++++---- .../should-allow-jsx-docs-comment-with-pragma/input.js | 5 +++++ .../should-allow-jsx-docs-comment-with-pragma/output.js | 4 ++++ .../input.js | 2 ++ .../output.js | 4 ++++ 5 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-allow-jsx-docs-comment-with-pragma/input.js create mode 100644 packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-allow-jsx-docs-comment-with-pragma/output.js create mode 100644 packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-not-allow-jsx-pragma-to-be-anywhere-in-comment/input.js create mode 100644 packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-not-allow-jsx-pragma-to-be-anywhere-in-comment/output.js 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 d4afe577f2..982083f167 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 0000000000..91ca8e5f2f --- /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 0000000000..2e9ea6eff2 --- /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 0000000000..3aa8b3c8fe --- /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 0000000000..940ac582d7 --- /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);