Skip to content

Commit

Permalink
jsx regex patterns support jsx docs
Browse files Browse the repository at this point in the history
  • Loading branch information
The-x-Theorist committed Dec 1, 2021
1 parent 88bf400 commit 518c6a4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
25 changes: 14 additions & 11 deletions packages/babel-plugin-transform-react-jsx/src/create-plugin.ts
Expand Up @@ -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}`);
Expand Down Expand Up @@ -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;
Expand Down
@@ -0,0 +1,5 @@
/**
* @jsx jsx
*/

<foo />
@@ -0,0 +1,4 @@
/**
* @jsx jsx
*/
jsx("foo", null);

0 comments on commit 518c6a4

Please sign in to comment.