| 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( |