(path)
| 52 | const replPlugin = ({ types: t }: PluginAPI): PluginObject => ({ |
| 53 | visitor: { |
| 54 | Program(path) { |
| 55 | hasTopLevelAwait = path.node.extra?.topLevelAwait as boolean; |
| 56 | |
| 57 | let hasExpressionStatement: boolean | undefined; |
| 58 | for (const bodyPath of path.get("body")) { |
| 59 | if (bodyPath.isExpressionStatement()) { |
| 60 | hasExpressionStatement = true; |
| 61 | } else if ( |
| 62 | bodyPath.isExportDeclaration() || |
| 63 | bodyPath.isImportDeclaration() |
| 64 | ) { |
| 65 | throw bodyPath.buildCodeFrameError( |
| 66 | "Modules aren't supported in the REPL", |
| 67 | ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (hasTopLevelAwait) { |
| 72 | const body = path.node.body; |
| 73 | for (let i = body.length - 1; i >= 0; i--) { |
| 74 | if (t.isExpressionStatement(body[i])) { |
| 75 | body[i] = t.returnStatement( |
| 76 | (body[i] as babel.types.ExpressionStatement).expression, |
| 77 | ); |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | if (hasExpressionStatement) return; |
| 86 | |
| 87 | // If the executed code doesn't evaluate to a value, |
| 88 | // prevent implicit strict mode from printing 'use strict'. |
| 89 | path.pushContainer( |
| 90 | "body", |
| 91 | t.expressionStatement(t.identifier("undefined")), |
| 92 | ); |
| 93 | }, |
| 94 | }, |
| 95 | }); |
| 96 |
nothing calls this directly
no test coverage detected