From 518c6a4ca61e5f57d6d5ae6ab1046f1e84224e84 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Wed, 1 Dec 2021 14:58:21 +0530 Subject: [PATCH] jsx regex patterns support jsx docs --- .../src/create-plugin.ts | 25 +++++++++++-------- .../input.js | 5 ++++ .../output.js | 4 +++ 3 files changed, 23 insertions(+), 11 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 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 158a960ff099..0fa5a3810452 100644 --- a/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts +++ b/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts @@ -32,13 +32,12 @@ const DEFAULT = { }; const JSX_SOURCE_ANNOTATION_REGEX = - /^\/*\*?\/*\*\s*@jsxImportSource\s+([^\s]+)\s*\/*\*\//; + /^\*?\s*?\*?\*?\s*@jsxImportSource\s+([^\s]+)\s*$/; const JSX_RUNTIME_ANNOTATION_REGEX = - /^\/*\*?\/*\*\s*@jsxRuntime\s+([^\s]+)\s*\/*\*\//; + /^\*?\s*?\*?\*?\s*@jsxRuntime\s+([^\s]+)\s*$/; -const JSX_ANNOTATION_REGEX = /^\/*\*?\/*\*\s*@jsx\s+([^\s]+)\s*\/*\*\//; -const JSX_FRAG_ANNOTATION_REGEX = - /^\/*\*?\/*\*\s*@jsxFrag\s+([^\s]+)\s*\/*\*\//; +const JSX_ANNOTATION_REGEX = /^\*?\s*?\*?\*?\s*@jsx\s+([^\s]+)\s*$/; +const JSX_FRAG_ANNOTATION_REGEX = /^\*?\s*?\*?\*?\s*@jsxFrag\s+([^\s]+)\s*$/; const get = (pass: PluginPass, name: string) => pass.get(`@babel/plugin-react-jsx/${name}`); @@ -170,25 +169,29 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, if (file.ast.comments) { for (const comment of file.ast.comments) { - const value = `/*${comment.value}*/`; - - const sourceMatches = JSX_SOURCE_ANNOTATION_REGEX.exec(value); + const sourceMatches = JSX_SOURCE_ANNOTATION_REGEX.exec( + comment.value, + ); if (sourceMatches) { source = sourceMatches[1]; sourceSet = true; } - const runtimeMatches = JSX_RUNTIME_ANNOTATION_REGEX.exec(value); + const runtimeMatches = JSX_RUNTIME_ANNOTATION_REGEX.exec( + comment.value, + ); if (runtimeMatches) { runtime = runtimeMatches[1]; } - const jsxMatches = JSX_ANNOTATION_REGEX.exec(value); + const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value); if (jsxMatches) { pragma = jsxMatches[1]; pragmaSet = true; } - const jsxFragMatches = JSX_FRAG_ANNOTATION_REGEX.exec(value); + const jsxFragMatches = JSX_FRAG_ANNOTATION_REGEX.exec( + comment.value, + ); if (jsxFragMatches) { pragmaFrag = jsxFragMatches[1]; pragmaFragSet = true; 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);