MCPcopy Index your code
hub / github.com/microsoft/SandDance / codegen

Function codegen

docs/app/js/sanddance-app.js:144494–144558  ·  view source on GitHub ↗
(opt)

Source from the content-addressed store, hash-verified

144492 return n && (s[0] === '"' && s[n] === '"' || s[0] === "'" && s[n] === "'") ? s.slice(1, -1) : s;
144493}
144494function codegen(opt) {
144495 opt = opt || {};
144496 const allowed = opt.allowed ? (0, _vegaUtil.toSet)(opt.allowed) : {}, forbidden = opt.forbidden ? (0, _vegaUtil.toSet)(opt.forbidden) : {}, constants = opt.constants || Constants, functions = (opt.functions || Functions)(visit), globalvar = opt.globalvar, fieldvar = opt.fieldvar, outputGlobal = (0, _vegaUtil.isFunction)(globalvar) ? globalvar : (id)=>`${globalvar}["${id}"]`;
144497 let globals = {}, fields = {}, memberDepth = 0;
144498 function visit(ast) {
144499 if ((0, _vegaUtil.isString)(ast)) return ast;
144500 const generator = Generators[ast.type];
144501 if (generator == null) (0, _vegaUtil.error)("Unsupported type: " + ast.type);
144502 return generator(ast);
144503 }
144504 const Generators = {
144505 Literal: (n)=>n.raw,
144506 Identifier: (n)=>{
144507 const id = n.name;
144508 if (memberDepth > 0) return id;
144509 else if ((0, _vegaUtil.hasOwnProperty)(forbidden, id)) return (0, _vegaUtil.error)("Illegal identifier: " + id);
144510 else if ((0, _vegaUtil.hasOwnProperty)(constants, id)) return constants[id];
144511 else if ((0, _vegaUtil.hasOwnProperty)(allowed, id)) return id;
144512 else {
144513 globals[id] = 1;
144514 return outputGlobal(id);
144515 }
144516 },
144517 MemberExpression: (n)=>{
144518 const d = !n.computed, o = visit(n.object);
144519 if (d) memberDepth += 1;
144520 const p = visit(n.property);
144521 if (o === fieldvar) // strip quotes to sanitize field name (#1653)
144522 fields[stripQuotes(p)] = 1;
144523 if (d) memberDepth -= 1;
144524 return o + (d ? "." + p : "[" + p + "]");
144525 },
144526 CallExpression: (n)=>{
144527 if (n.callee.type !== "Identifier") (0, _vegaUtil.error)("Illegal callee type: " + n.callee.type);
144528 const callee = n.callee.name, args = n.arguments, fn = (0, _vegaUtil.hasOwnProperty)(functions, callee) && functions[callee];
144529 if (!fn) (0, _vegaUtil.error)("Unrecognized function: " + callee);
144530 return (0, _vegaUtil.isFunction)(fn) ? fn(args) : fn + "(" + args.map(visit).join(",") + ")";
144531 },
144532 ArrayExpression: (n)=>"[" + n.elements.map(visit).join(",") + "]",
144533 BinaryExpression: (n)=>"(" + visit(n.left) + " " + n.operator + " " + visit(n.right) + ")",
144534 UnaryExpression: (n)=>"(" + n.operator + visit(n.argument) + ")",
144535 ConditionalExpression: (n)=>"(" + visit(n.test) + "?" + visit(n.consequent) + ":" + visit(n.alternate) + ")",
144536 LogicalExpression: (n)=>"(" + visit(n.left) + n.operator + visit(n.right) + ")",
144537 ObjectExpression: (n)=>"{" + n.properties.map(visit).join(",") + "}",
144538 Property: (n)=>{
144539 memberDepth += 1;
144540 const k = visit(n.key);
144541 memberDepth -= 1;
144542 return k + ":" + visit(n.value);
144543 }
144544 };
144545 function codegen2(ast) {
144546 const result = {
144547 code: visit(ast),
144548 globals: Object.keys(globals),
144549 fields: Object.keys(fields)
144550 };
144551 globals = {};

Callers 2

refFunction · 0.70
internalScaleFunctionsFunction · 0.70

Calls 3

visitFunction · 0.70
stripQuotesFunction · 0.70
fnFunction · 0.70

Tested by

no test coverage detected