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

Function codeToBlocks

app/lib/code-to-blocks.js:855–901  ·  view source on GitHub ↗
({ code, originalCode, codeLanguage, prepData })

Source from the content-addressed store, hash-verified

853}
854
855function codeToBlocks ({ code, originalCode, codeLanguage, prepData }) {
856 let ast
857 try {
858 code = addInsertionPoints({ code, originalCode, codeLanguage })
859 code = guardUnicode({ code, codeLanguage })
860 switch (codeLanguage) {
861 case 'javascript':
862 {
863 const { parse } = window.esper.plugins.babylon.babylon
864 ast = parse(code + '\n__donothing__()', { errorRecovery: true })
865 break
866 }
867 case 'python':
868 {
869 const { parse } = window.esper.plugins['lang-python'].skulpty
870 code = code.replace(/^(\s*)#(.*)$/gm, (_, s, c) => `${s}__comment__(${JSON.stringify(c)})`)
871 ast = parse(code, { errorRecovery: true, naive: true, locations: true, startend: true })
872 // console.log("PGC", ast, require("@babel/generator").default(ast).code)
873 ast = { type: 'File', program: ast }
874 // console.log('OYY', ast)
875 break
876 }
877 }
878 } catch (e) {
879 console.error("Oh no, couldn't parse", code, e)
880 }
881 const isJunior = _.find(prepData?.plan || [], (p) => /^Hero_go/.test(p?.[0]?.type))
882 const ctx = {
883 plan: prepData.plan,
884 scope: {},
885 context: 'statement',
886 code,
887 isJunior,
888 }
889 const out = {
890 blocks: convert(ast.program, ctx),
891 variables: [],
892 }
893 for (const v in ctx.scope) {
894 if (ctx.scope[v].type === 'var') out.variables.push({ name: v, id: v })
895 }
896 if (isJunior) {
897 addStartBlock(out)
898 }
899 // console.log('OUT', out)
900 return out
901}
902
903module.exports = {
904 prepareBlockIntelligence,

Callers 2

runCodeToBlocksMethod · 0.85

Calls 6

addInsertionPointsFunction · 0.85
guardUnicodeFunction · 0.85
convertFunction · 0.85
addStartBlockFunction · 0.85
stringifyMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected