Skip to content

Commit

Permalink
Fix JSX pragma anywhere in comment
Browse files Browse the repository at this point in the history
  • Loading branch information
The-x-Theorist committed Nov 30, 2021
1 parent 29de280 commit 88bf400
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
27 changes: 13 additions & 14 deletions packages/babel-plugin-transform-react-jsx/src/create-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ 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*@jsxImportSource\s+([^\s]+)\s*\/*\*\//;
const JSX_RUNTIME_ANNOTATION_REGEX =
/^\/*\*?\/*\*\s*@jsxRuntime\s+([^\s]+)\s*\/*\*\//;

const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/;
const JSX_FRAG_ANNOTATION_REGEX = /\*?\s*@jsxFrag\s+([^\s]+)/;
const JSX_ANNOTATION_REGEX = /^\/*\*?\/*\*\s*@jsx\s+([^\s]+)\s*\/*\*\//;
const JSX_FRAG_ANNOTATION_REGEX =
/^\/*\*?\/*\*\s*@jsxFrag\s+([^\s]+)\s*\/*\*\//;

const get = (pass: PluginPass, name: string) =>
pass.get(`@babel/plugin-react-jsx/${name}`);
Expand Down Expand Up @@ -167,29 +170,25 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,

if (file.ast.comments) {
for (const comment of file.ast.comments) {
const sourceMatches = JSX_SOURCE_ANNOTATION_REGEX.exec(
comment.value,
);
const value = `/*${comment.value}*/`;

const sourceMatches = JSX_SOURCE_ANNOTATION_REGEX.exec(value);
if (sourceMatches) {
source = sourceMatches[1];
sourceSet = true;
}

const runtimeMatches = JSX_RUNTIME_ANNOTATION_REGEX.exec(
comment.value,
);
const runtimeMatches = JSX_RUNTIME_ANNOTATION_REGEX.exec(value);
if (runtimeMatches) {
runtime = runtimeMatches[1];
}

const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value);
const jsxMatches = JSX_ANNOTATION_REGEX.exec(value);
if (jsxMatches) {
pragma = jsxMatches[1];
pragmaSet = true;
}
const jsxFragMatches = JSX_FRAG_ANNOTATION_REGEX.exec(
comment.value,
);
const jsxFragMatches = JSX_FRAG_ANNOTATION_REGEX.exec(value);
if (jsxFragMatches) {
pragmaFrag = jsxFragMatches[1];
pragmaFragSet = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Make sure not to use a jsx pragma here (like "@jsx Something"), we need this to be React.createElement!
<blah/>
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 88bf400

Please sign in to comment.