{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-eslint/src/client/reactive/babel-plugin-active-expression-proxies/index.js"],"names":["Preferences","AEXPR_IDENTIFIER_NAME","FLAG_SHOULD_NOT_REWRITE_IDENTIFIER","Symbol","types","t","template","traverse","GENERATED_IMPORT_IDENTIFIER","addCustomTemplate","file","name","declar","declarations","identifier","addImport","visitor","Program","enter","path","state","hasDirective","foundDirective","Directive","get","node","value","shouldTransform","proxyDirective","proxyPreference","inWorkspace","opts","executedIn","inFile","replaceNode","wrapType","unwrap","__already_transformed__","transformed","callExpression","stringLiteral","replaceWith","ObjectExpression","parent","callee","property","e","CallExpression","arguments","type","__should_unwrap__","NewExpression","ArrayExpression","Identifier","isCallExpression","scope","hasBinding"],"mappings":";;;;;;AAAOA,iB;;;;;;;;;;;;;;;;;;;;AAEP,YAAMC,wBAAwB,OAA9B;;;;;;;;;;;;;;AACA,YAAMC,qCAAqCC,OAAO,qCAAP,CAA3C;;;;;;;;;;;;;;;;yBAEe,UAAS,EAAEC,OAAOC,CAAT,EAAYC,QAAZ,EAAsBC,QAAtB,EAAT,EAA2C;AACxD,cAAMC,8BAA8BL,OAAO,6BAAP,CAApC;;AAEA,iBAASM,iBAAT,CAA2BC,IAA3B,EAAiCC,IAAjC,EAAuC;AACrC,cAAIC,SAASF,KAAKG,YAAL,CAAkBF,IAAlB,CAAb;AACA,cAAIC,MAAJ,EAAY,OAAOA,MAAP;;AAEZ,cAAIE,aAAaJ,KAAKG,YAAL,CAAkBF,IAAlB,IAA0BD,KAAKK,SAAL,CAAe,2BAAf,EAA4CJ,IAA5C,EAAkDA,IAAlD,CAA3C;AACAG,qBAAWN,2BAAX,IAA0C,IAA1C;AACAM,qBAAWZ,kCAAX,IAAiD,IAAjD;AACA,iBAAOY,UAAP;AACD;;AAED,eAAO;AACLE,mBAAS;AACPC,qBAAS;AACPC,oBAAMC,IAAN,EAAYC,KAAZ,EAAmB;AACjB,yBAASC,YAAT,CAAsBF,IAAtB,EAA4BR,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AEA,CAACkB,KAAKoC,KAAL,CAAWC,UAAX,CAAsBvD,qBAAtB,CAHH,EAIE;AACA;AACAkB,2BAAKsB,WAAL,CACEhC,kBAAkBW,MAAMV,IAAxB,EAA8BT,qBAA9B,CADF;AAGA;AACD;;AAED,wBAAI;AACF,0BAAGkB,KAAKwB,MAAL,CAAYC,MAAZ,CAAmBC,QAAnB,CAA4BlC,IAA5B,KAAqC,gBAArC,IAAyDQ,KAAKwB,MAAL,CAAYK,SAAZ,CAAsB,CAAtB,MAA6B7B,KAAKM,IAA9F,EAAoG;AAClGS,oCAAYf,IAAZ,EAAkB,OAAlB,EAA2B,IAA3B;AACD;AACF,qBAJD,CAIE,OAAM2B,CAAN,EAAS;AACT;AACA;AACD;AACF;AA7DW,iBAAd;AA+DD;AAhHM;AADF;AADJ,SAAP;AAsHD,O","file":"index.js","sourcesContent":["import Preferences from 'src/client/preferences.js';\n\nconst AEXPR_IDENTIFIER_NAME = 'aexpr';\nconst FLAG_SHOULD_NOT_REWRITE_IDENTIFIER = Symbol('FLAG: should not rewrite identifier');\n\nexport default function({ types: t, template, traverse }) {\n  const GENERATED_IMPORT_IDENTIFIER = Symbol(\"generated import identifier\");\n  \n  function addCustomTemplate(file, name) {\n    let declar = file.declarations[name];\n    if (declar) return declar;\n\n    let identifier = file.declarations[name] = file.addImport(\"active-expression-proxies\", name, name);\n    identifier[GENERATED_IMPORT_IDENTIFIER] = true;\n    identifier[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true;\n    return identifier;\n  }\n  \n  return {\n    visitor: {\n      Program: {\n        enter(path, state) {\n          function hasDirective(path, name) {\n            let foundDirective = false;\n            path.traverse({\n              Directive(path) {\n                if(path.get(\"value\").node.value === name) {\n                  foundDirective = true;\n                }\n              }\n            });\n            return foundDirective;\n          }\n\n          function shouldTransform() {\n            const proxyDirective = hasDirective(path, 'use proxies for aexprs');\n            const proxyPreference = typeof Preferences == 'undefined' ? true : Preferences.get('UseProxiesForAExprs');\n            const inWorkspace = state.opts.executedIn === 'workspace';\n            const inFile = state.opts.executedIn === 'file';\n\n            if (inWorkspace) {\n              return proxyPreference;\n            } else if (inFile) {\n              return proxyDirective;\n            }\n            return true;\n            // throw new Error('This should not be possible');\n          }\n\n          if (!shouldTransform()) { return; }\n\n          function replaceNode(path, wrapType, unwrap = false) {\n            // do not wrap the same object twice\n            if (path.node.__already_transformed__) { return; }\n            path.node.__already_transformed__ = true;           \n            \n            let transformed;\n            if (unwrap){\n              transformed = t.callExpression(\n                addCustomTemplate(state.file, 'unwrap'), [path.node])\n              \n            } else {\n              transformed = t.callExpression(\n                addCustomTemplate(state.file, 'wrap'), [t.stringLiteral(wrapType),path.node]\n              );\n            }\n            path.replaceWith(transformed);   \n          }\n          \n          path.traverse({\n            ObjectExpression(path) {\n              // do not replace objects in calls to Object.defineProperty\n              try {\n                if(path.parent.callee.property.name === \"defineProperty\") { \n                  return;\n                }\n              } catch(e) {\n                // Once we can use the new ecma script2020 syntax this try-catch can be replaced by optional chaining\n                // https://iolap.com/2019/09/27/whats-next-for-javascript-top-5-new-features-for-2020/\n              }\n              replaceNode(path, 'Object')\n            },\n\n            CallExpression(path) {\n              // unwrap proxies in Object.defineProperty \n              try {\n                if(path.node.callee.property.name === \"defineProperty\" && path.node.arguments[2].type === \"Identifier\") { \n                  path.node.arguments[2].__should_unwrap__ = true;\n                }\n              } catch(e) {\n                // Once we can use the new ecma script2020 syntax this try-catch can be replaced by optional chaining\n                // https://iolap.com/2019/09/27/whats-next-for-javascript-top-5-new-features-for-2020/\n              }\n            },\n            \n            NewExpression(path) {\n              replaceNode(path, path.node.callee.name)\n\n            },\n\n            ArrayExpression(path) {\n              replaceNode(path, 'Array')\n            },\n            \n            Identifier(path) {\n              if (path.node[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER]) {\n                return;\n              }\n\n              // Check for a call to undeclared aexpr:\n              if (\n                t.isCallExpression(path.parent) &&\n                path.node.name === AEXPR_IDENTIFIER_NAME &&\n                !path.scope.hasBinding(AEXPR_IDENTIFIER_NAME)\n              ) {\n                //logIdentifier(\"call to aexpr\", path);\n                path.replaceWith(\n                  addCustomTemplate(state.file, AEXPR_IDENTIFIER_NAME)\n                );\n                return;\n              }\n\n              try {\n                if(path.parent.callee.property.name === \"defineProperty\" && path.parent.arguments[2] === path.node) { \n                  replaceNode(path, 'Proxy', true);           \n                }\n              } catch(e) {\n                // Once we can use the new ecma script2020 syntax this try-catch can be replaced by optional chaining\n                // https://iolap.com/2019/09/27/whats-next-for-javascript-top-5-new-features-for-2020/\n              }\n            }\n          })\n        }\n      }\n    }\n  };\n}\n"]}