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

Function ImportDeclaration

scripts/babel-plugin-toggle-boolean-flag/plugin.ts:152–221  ·  view source on GitHub ↗
(path)

Source from the content-addressed store, hash-verified

150 }
151 },
152 ImportDeclaration(path) {
153 if (!path.node.attributes) return;
154
155 const ifAttribute = path
156 .get("attributes")
157 .find(attr => (attr.node.key as t.Identifier).name === "if");
158 if (ifAttribute == null) {
159 return;
160 }
161
162 const condition = parseExpression(ifAttribute.node.value.value);
163
164 let res;
165 res_block: if (value !== null) {
166 res = evaluate(condition, value);
167 } else {
168 const ifTrue = evaluate(condition, true);
169 if (ifTrue.unrelated || ifTrue.value === true) {
170 res = ifTrue;
171 break res_block;
172 }
173 const ifFalse = evaluate(condition, false);
174 if (ifFalse.unrelated) throw new Error("Cannot be unrelated");
175 if (ifFalse.value === true) {
176 res = ifFalse;
177 break res_block;
178 }
179
180 if (ifTrue.value === false) {
181 res = ifFalse;
182 break res_block;
183 }
184 if (ifTrue.value === false) {
185 res = ifTrue;
186 break res_block;
187 }
188
189 if (!ifTrue.replacement && !ifFalse.replacement) {
190 throw new Error("Expected two replacements");
191 }
192
193 res = {
194 replacement: t.logicalExpression(
195 "||",
196 ifTrue.replacement,
197 ifFalse.replacement
198 ),
199 value: null,
200 unrelated: false,
201 };
202 }
203
204 if (res.unrelated) return;
205 if (res.replacement) {
206 ifAttribute
207 .get("value")
208 .replaceWith(
209 t.stringLiteral(

Callers

nothing calls this directly

Calls 6

findMethod · 0.80
removeMethod · 0.80
parseExpressionFunction · 0.70
evaluateFunction · 0.70
getMethod · 0.65
callMethod · 0.65

Tested by

no test coverage detected