MCPcopy Create free account
hub / github.com/codecombat/codecombat / ConvertIfStatement

Method ConvertIfStatement

app/lib/code-to-blocks.js:272–307  ·  view source on GitHub ↗
(n, ctx)

Source from the content-addressed store, hash-verified

270 }
271
272 static ConvertIfStatement (n, ctx) {
273 // TODO: ELSE, ELSEIF
274 // console.log('IF', n)
275 const conq = convert(n.consequent, { ...ctx, context: 'statement', nospace: true })
276 const o = {
277 type: 'controls_if',
278 extraState: {},
279 inputs: {
280 IF0: { block: convert(n.test, { ...ctx, context: 'value' }) },
281 },
282 }
283
284 if (conq) o.inputs.DO0 = { block: conq[0] }
285
286 let N = 0
287 let alt = n.alternate
288
289 while (alt && (alt.type === 'IfStatement' || alt.type === 'BlockStatement')) {
290 if (alt.type === 'BlockStatement') {
291 if (alt.body.length !== 1 || alt.body[0].type !== 'IfStatement') break
292 alt = alt.body[0]
293 continue
294 }
295 ++N
296 o.extraState.elseIfCount = N
297 o.inputs[`IF${N}`] = { block: convert(alt.test, ctx, { ...ctx, context: 'value' }) }
298 o.inputs[`DO${N}`] = { block: convert(alt.consequent, ctx, { ...ctx, context: 'statement', nospace: true })[0] }
299 alt = alt.alternate
300 }
301
302 if (alt) {
303 o.inputs.ELSE = { block: convert(alt, { ...ctx, context: 'statement', nospace: true })[0] }
304 o.extraState.hasElse = true
305 }
306 return o
307 }
308
309 static ConvertAssignmentExpression (n, ctx) {
310 if (n.operator !== '=') throw new Error(`Weird assigment operator ${n.operator}`)

Callers

nothing calls this directly

Calls 1

convertFunction · 0.85

Tested by

no test coverage detected