MCPcopy Create free account
hub / github.com/babel/babel / Program

Function Program

packages/babel-node/src/_babel-node.ts:54–93  ·  view source on GitHub ↗
(path)

Source from the content-addressed store, hash-verified

52const 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

Callers

nothing calls this directly

Calls 5

isExpressionStatementMethod · 0.80
isExportDeclarationMethod · 0.80
isImportDeclarationMethod · 0.80
getMethod · 0.65
buildCodeFrameErrorMethod · 0.45

Tested by

no test coverage detected