MCPcopy Index your code
hub / github.com/nodejs/node / processTopLevelAwait

Function processTopLevelAwait

lib/internal/repl/await.js:154–258  ·  view source on GitHub ↗
(src)

Source from the content-addressed store, hash-verified

152}
153
154function processTopLevelAwait(src) {
155 const wrapPrefix = '(async () => { ';
156 const wrapped = `${wrapPrefix}${src} })()`;
157 const wrappedArray = StringPrototypeSplit(wrapped, '');
158 let root;
159 try {
160 root = parser.parse(wrapped, { ecmaVersion: 'latest' });
161 } catch (e) {
162 if (StringPrototypeStartsWith(e.message, 'Unterminated '))
163 throw new Recoverable(e);
164 // If the parse error is before the first "await", then use the execution
165 // error. Otherwise we must emit this parse error, making it look like a
166 // proper syntax error.
167 const awaitPos = StringPrototypeIndexOf(src, 'await');
168 const errPos = e.pos - wrapPrefix.length;
169 if (awaitPos > errPos)
170 return null;
171 // Convert keyword parse errors on await into their original errors when
172 // possible.
173 if (errPos === awaitPos + 6 &&
174 StringPrototypeIncludes(e.message, 'Expecting Unicode escape sequence'))
175 return null;
176 if (errPos === awaitPos + 7 &&
177 StringPrototypeIncludes(e.message, 'Unexpected token'))
178 return null;
179 const line = e.loc.line;
180 const column = line === 1 ? e.loc.column - wrapPrefix.length : e.loc.column;
181 let message = '\n' + StringPrototypeSplit(src, '\n', line)[line - 1] + '\n' +
182 StringPrototypeRepeat(' ', column) +
183 '^\n\n' + RegExpPrototypeSymbolReplace(/ \([^)]+\)/, e.message, '');
184 // V8 unexpected token errors include the token string.
185 if (StringPrototypeEndsWith(message, 'Unexpected token'))
186 message += " '" +
187 // Wrapper end may cause acorn to report error position after the source
188 (src[e.pos - wrapPrefix.length] ?? src[src.length - 1]) +
189 "'";
190 // eslint-disable-next-line no-restricted-syntax
191 throw new SyntaxError(message);
192 }
193 const body = root.body[0].expression.callee.body;
194 const state = {
195 body,
196 ancestors: [],
197 hoistedDeclarationStatements: [],
198 replace(from, to, str) {
199 for (let i = from; i < to; i++) {
200 wrappedArray[i] = '';
201 }
202 if (from === to) str += wrappedArray[from];
203 wrappedArray[from] = str;
204 },
205 prepend(node, str) {
206 wrappedArray[node.start] = str + wrappedArray[node.start];
207 },
208 append(node, str) {
209 wrappedArray[node.end - 1] += str;
210 },
211 containsAwait: false,

Callers 2

defaultEvalMethod · 0.85

Calls 3

recursiveMethod · 0.80
parseMethod · 0.65
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…