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

Method ConvertForStatement

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

Source from the content-addressed store, hash-verified

191 }
192
193 static ConvertForStatement (n, ctx) {
194 const found = findOne(ctx.plan, x => fuzzyMatch(n, x[1]), 'Can\'t find the for statement')
195 convert(n.init, { ...ctx, nospace: true })
196 const body = convert(n.body, { ...ctx, nospace: true, context: 'statement' })
197
198 // Only handles most basic cases for now, like standard for (let i = 0; i < 3; i++)
199 const start = n.init?.declarations?.[0]?.init?.value ?? 0
200 const end = n.test?.right?.value ?? 1
201 const times = end - start
202 // For Junior, we have simplified dropdown block if the range is in 1-7
203 const type = times >= 1 && times <= 7 && _.find(ctx.plan, p => p[0].type === 'controls_repeat_dropdown') ? 'controls_repeat_dropdown' : found[0].type
204 let o
205 if (type === 'controls_repeat_dropdown') {
206 // Change input to field
207 o = { type, inputs: {}, fields: { TIMES: '' + times } }
208 } else {
209 // TODO: figure out how to always include controls_repeat_ext so that we can handle if the number is outside our target range
210 o = { type, inputs: { TIMES: { block: convert(n.test.right, ctx) } } }
211 }
212
213 if (body) o.inputs.DO = { block: body[0] }
214 return o
215 }
216
217 static ConvertForOfStatement (n, ctx) {
218 ctx.scope[n.left.name] = { type: 'var' }

Callers

nothing calls this directly

Calls 3

findOneFunction · 0.85
fuzzyMatchFunction · 0.85
convertFunction · 0.85

Tested by

no test coverage detected