MCPcopy Create free account
hub / github.com/babel/babel / enter

Function enter

packages/babel-plugin-transform-react-jsx/src/create-plugin.ts:140–333  ·  view source on GitHub ↗
(path, state)

Source from the content-addressed store, hash-verified

138
139 Program: {
140 enter(path, state) {
141 const { file } = state;
142 let runtime: string = RUNTIME_DEFAULT;
143
144 let source: string = IMPORT_SOURCE_DEFAULT;
145 let pragma: string = PRAGMA_DEFAULT;
146 let pragmaFrag: string = PRAGMA_FRAG_DEFAULT;
147
148 let sourceSet = !!options.importSource;
149 let pragmaSet = !!options.pragma;
150 let pragmaFragSet = !!options.pragmaFrag;
151
152 if (file.ast.comments) {
153 for (const comment of file.ast.comments) {
154 const sourceMatches = JSX_SOURCE_ANNOTATION_REGEX.exec(
155 comment.value,
156 );
157 if (sourceMatches) {
158 source = sourceMatches[1];
159 sourceSet = true;
160 }
161
162 const runtimeMatches = JSX_RUNTIME_ANNOTATION_REGEX.exec(
163 comment.value,
164 );
165 if (runtimeMatches) {
166 runtime = runtimeMatches[1];
167 }
168
169 const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value);
170 if (jsxMatches) {
171 pragma = jsxMatches[1];
172 pragmaSet = true;
173 }
174 const jsxFragMatches = JSX_FRAG_ANNOTATION_REGEX.exec(
175 comment.value,
176 );
177 if (jsxFragMatches) {
178 pragmaFrag = jsxFragMatches[1];
179 pragmaFragSet = true;
180 }
181 }
182 }
183
184 set(state, "runtime", runtime);
185 if (runtime === "classic") {
186 if (sourceSet) {
187 throw path.buildCodeFrameError(
188 `importSource cannot be set when runtime is classic.`,
189 );
190 }
191
192 const createElement = toMemberExpression(pragma);
193 const fragment = toMemberExpression(pragmaFrag);
194
195 set(state, "id/createElement", () => t.cloneNode(createElement));
196 set(state, "id/fragment", () => t.cloneNode(fragment));
197

Callers

nothing calls this directly

Calls 9

toMemberExpressionFunction · 0.85
defineFunction · 0.85
isThisAllowedFunction · 0.85
makeSourceFunction · 0.85
traverseMethod · 0.80
isJSXOpeningElementMethod · 0.80
setFunction · 0.70
buildCodeFrameErrorMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected