Skip to content

Commit

Permalink
Use allowNewTargetOutsideFunction Babel parse option [improve]
Browse files Browse the repository at this point in the history
Closes #446.
  • Loading branch information
overlookmotel committed Aug 29, 2023
1 parent e94f90c commit 1e330f2
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions lib/instrument/instrument.js
Expand Up @@ -46,27 +46,15 @@ const babelOptionsCache = {};
function parseImpl(
code, filename, isEsm, isCommonJs, isDirectEval, isJsx, isStrict, sourceMaps, inputSourceMap
) {
// Parse code to AST.
// `errorRecovery` is workaround until https://github.com/babel/babel/pull/15114 is merged.
// TODO Just use `allowNewTargetOutsideFunction` option once it's available.
const allowNewTargetOutsideFunction = isCommonJs || isDirectEval;
// Parse code to AST
const ast = parse(code, {
sourceType: isEsm ? 'module' : 'script',
strictMode: isStrict,
allowReturnOutsideFunction: isCommonJs,
errorRecovery: allowNewTargetOutsideFunction,
allowNewTargetOutsideFunction: isCommonJs || isDirectEval,
plugins: ['v8intrinsic', ...(isJsx ? ['jsx'] : [])]
});

if (allowNewTargetOutsideFunction && ast.errors.length > 0) {
for (const error of ast.errors) {
if (error.code !== 'BABEL_PARSER_SYNTAX_ERROR' || error.reasonCode !== 'UnexpectedNewTarget') {
throw error;
}
}
ast.errors.length = 0;
}

// Parse source map comment if present, and remove source map comments from AST.
// Do not parse source map comment if source maps disabled, or input source map already provided.
const parsedSourceMap = parseSourceMapComment(
Expand Down

0 comments on commit 1e330f2

Please sign in to comment.