{"version":3,"file":"babel-plugin-transform-logging.cjs","sources":["../cjs-src/transforms/babel-plugin-transform-logging.js"],"sourcesContent":["import { ImportUtil } from 'babel-import-util';\n\nfunction parentIsUnary(node) {\n  if (node.parent.type === 'UnaryExpression' && node.parent.operator === '!') {\n    return true;\n  }\n  return false;\n}\n\nexport default function (babel) {\n  const { types: t } = babel;\n\n  return {\n    name: 'ast-transform', // not required\n    visitor: {\n      ImportDeclaration(path, state) {\n        const importPath = path.node.source.value;\n\n        if (importPath === state.opts.source) {\n          const specifiers = path.get('specifiers');\n          specifiers.forEach((specifier) => {\n            let name = specifier.node.imported.name;\n            if (!(name in state.opts.flags)) {\n              throw new Error(`Unexpected flag ${name} imported from ${state.opts.source}`);\n            }\n            let localBindingName = specifier.node.local.name;\n            let binding = specifier.scope.getBinding(localBindingName);\n            const enableRuntimeActivation = Boolean(state.opts.runtimeKey);\n            const strippableKey = enableRuntimeActivation ? state.opts.runtimeKey : state.opts.configKey;\n\n            binding.referencePaths.forEach((p) => {\n              let negateStatement = false;\n              let node = p;\n              if (parentIsUnary(p)) {\n                negateStatement = true;\n                node = p.parentPath;\n              }\n              let getConfig = t.memberExpression(\n                t.memberExpression(\n                  t.memberExpression(\n                    t.callExpression(state.importer.import(p, '@embroider/macros', 'getGlobalConfig'), []),\n                    t.identifier('WarpDrive')\n                  ),\n                  t.identifier(strippableKey)\n                ),\n                t.identifier(name)\n              );\n\n              node.replaceWith(\n                // if (LOG_FOO) {\n                //   // ...\n                // }\n                // =>\n                // if (macroCondition(getGlobalConfig('WarpDrive').debug.LOG_FOO)) {\n                //     // ...\n                // }\n                t.callExpression(state.importer.import(p, '@embroider/macros', 'macroCondition'), [\n                  negateStatement ? t.unaryExpression('!', getConfig) : getConfig,\n                ])\n              );\n\n              if (enableRuntimeActivation) {\n                // we do not yet support arbitrary runtime activation locations,\n                // the only supported locations are `if (LOG)` style statements, no\n                // ternaries or other more complex expressions\n                const parentIfStatement = node.parentPath.type === 'IfStatement' ? node.parentPath : null;\n                if (!parentIfStatement) {\n                  throw new Error(\n                    `Runtime activation of logging flags is only supported in if statements, but found node '${node.parentPath.type}'`\n                  );\n                }\n\n                // if (LOG_FOO) {\n                //   // ...\n                // }\n                // =>\n                // if (macroCondition(getGlobalConfig('WarpDrive').activeLogging.LOG_FOO)) {\n                //   if (getGlobalConfig('WarpDrive').debug.LOG_FOO || globalThis.getWarpDriveRuntimeConfig().debug.LOG_FOO) {\n                //     // ...\n                //   }\n                // }\n                //\n                // the outer-if is generated by the node-replace above. The inner if is generated here.\n                const originalBody = parentIfStatement.node.consequent;\n\n                // getGlobalConfig('WarpDrive').debug.LOG_FOO\n                const getActualConfig = t.memberExpression(\n                  t.memberExpression(\n                    t.memberExpression(\n                      t.callExpression(state.importer.import(p, '@embroider/macros', 'getGlobalConfig'), []),\n                      t.identifier('WarpDrive')\n                    ),\n                    t.identifier(state.opts.configKey)\n                  ),\n                  t.identifier(name)\n                );\n\n                // globalThis.getWarpDriveRuntimeConfig().debug.LOG_FOO\n                const getRuntimeConfig = t.memberExpression(\n                  t.memberExpression(\n                    t.callExpression(\n                      t.memberExpression(t.identifier('globalThis'), t.identifier('getWarpDriveRuntimeConfig')),\n                      []\n                    ),\n                    t.identifier(state.opts.configKey)\n                  ),\n                  t.identifier(name)\n                );\n\n                // <getActualConfig> || <getRuntimeConfig>\n                const ifExp = t.logicalExpression('||', getActualConfig, getRuntimeConfig);\n\n                // if (<negateStatement>(<getActualConfig> || <getRuntimeConfig>)) <originalBody>\n                const innerIfStatement = t.ifStatement(\n                  negateStatement ? t.unaryExpression('!', ifExp) : ifExp,\n                  originalBody\n                );\n\n                // replace the original body with the new if statement\n                parentIfStatement.node.consequent = t.blockStatement([innerIfStatement]);\n              } else {\n              }\n            });\n            specifier.scope.removeOwnBinding(localBindingName);\n            specifier.remove();\n          });\n          if (path.get('specifiers').length === 0) {\n            path.remove();\n          }\n        }\n      },\n\n      Program(path, state) {\n        state.importer = new ImportUtil(t, path);\n      },\n    },\n  };\n}\n"],"names":["parentIsUnary","node","parent","type","operator","babel","types","t","name","visitor","ImportDeclaration","path","state","importPath","source","value","opts","specifiers","get","forEach","specifier","imported","flags","Error","localBindingName","local","binding","scope","getBinding","enableRuntimeActivation","Boolean","runtimeKey","strippableKey","configKey","referencePaths","p","negateStatement","parentPath","getConfig","memberExpression","callExpression","importer","import","identifier","replaceWith","unaryExpression","parentIfStatement","originalBody","consequent","getActualConfig","getRuntimeConfig","ifExp","logicalExpression","innerIfStatement","ifStatement","blockStatement","removeOwnBinding","remove","length","Program","ImportUtil"],"mappings":";;;;AAEA,SAASA,aAAaA,CAACC,IAAI,EAAE;AAC3B,EAAA,IAAIA,IAAI,CAACC,MAAM,CAACC,IAAI,KAAK,iBAAiB,IAAIF,IAAI,CAACC,MAAM,CAACE,QAAQ,KAAK,GAAG,EAAE;AAC1E,IAAA,OAAO,IAAI;AACb;AACA,EAAA,OAAO,KAAK;AACd;AAEe,oCAAA,EAAUC,KAAK,EAAE;EAC9B,MAAM;AAAEC,IAAAA,KAAK,EAAEC;AAAE,GAAC,GAAGF,KAAK;EAE1B,OAAO;AACLG,IAAAA,IAAI,EAAE,eAAe;AAAE;AACvBC,IAAAA,OAAO,EAAE;AACPC,MAAAA,iBAAiBA,CAACC,IAAI,EAAEC,KAAK,EAAE;QAC7B,MAAMC,UAAU,GAAGF,IAAI,CAACV,IAAI,CAACa,MAAM,CAACC,KAAK;AAEzC,QAAA,IAAIF,UAAU,KAAKD,KAAK,CAACI,IAAI,CAACF,MAAM,EAAE;AACpC,UAAA,MAAMG,UAAU,GAAGN,IAAI,CAACO,GAAG,CAAC,YAAY,CAAC;AACzCD,UAAAA,UAAU,CAACE,OAAO,CAAEC,SAAS,IAAK;YAChC,IAAIZ,IAAI,GAAGY,SAAS,CAACnB,IAAI,CAACoB,QAAQ,CAACb,IAAI;YACvC,IAAI,EAAEA,IAAI,IAAII,KAAK,CAACI,IAAI,CAACM,KAAK,CAAC,EAAE;AAC/B,cAAA,MAA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