Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
path.replaceWith(
t.program([
WRAPPER_TEMPLATE({
NAME: getIdentifier(asset, 'exports'),
BODY: path.node.body,
}),
]),
);
asset.symbols.clear();
asset.meta.isCommonJS = true;
asset.meta.isES6Module = false;
} else {
// Re-crawl scope so we are sure to have all bindings.
traverse.cache.clearScope();
scope.crawl();
// Rename each binding in the top-level scope to something unique.
for (let name in scope.bindings) {
if (!name.startsWith('$' + t.toIdentifier(asset.id))) {
let newName = getName(asset, 'var', name);
rename(scope, name, newName);
}
}
let exportsIdentifier = getIdentifier(asset, 'exports');
// Add variable that represents module.exports if it is referenced and not declared.
if (
scope.hasGlobal(exportsIdentifier.name) &&
!scope.hasBinding(exportsIdentifier.name)
if (t.isMemberExpression(parentNode)) {
// remove the "this" from the member
parentPath.replaceWith(parentNode.property);
} else {
throw new FatalError(
`conversion of a simple class component to functional component failed due to "this" not being replaced`
);
}
}
},
},
undefined,
{},
undefined
);
traverse.cache.clear();
});
}
enter(path, asset: MutableAsset) {
asset.meta.id = asset.id;
asset.meta.exportsIdentifier = getName(asset, 'exports');
traverse.cache.clearScope();
path.scope.crawl();
let shouldWrap = false;
path.traverse({
CallExpression(path) {
// If we see an `eval` call, wrap the module in a function.
// Otherwise, local variables accessed inside the eval won't work.
let callee = path.node.callee;
if (
t.isIdentifier(callee) &&
callee.name === 'eval' &&
!path.scope.hasBinding('eval', true)
) {
asset.meta.isCommonJS = true;
shouldWrap = true;
path.stop();
let functionInfo = {
usesArguments: false,
usesThis: false,
usesReturn: false,
usesGotoToLabel: false,
usesThrow: false,
varPatternUnsupported: false,
};
traverse(
t.file(t.program([t.expressionStatement(t.functionExpression(null, [], body))])),
BailOutWrapperClosureRefVisitor,
null,
functionInfo
);
traverse.cache.clear();
let { usesReturn, usesThrow, usesArguments, usesGotoToLabel, varPatternUnsupported, usesThis } = functionInfo;
if (usesReturn || usesThrow || usesArguments || usesGotoToLabel || varPatternUnsupported) {
// We do not have support for these yet
let diagnostic = new CompilerDiagnostic(
`failed to recover from a for/while loop bail-out due to unsupported logic in loop body`,
realm.currentLocation,
"PP0037",
"FatalError"
);
realm.handleError(diagnostic);
throw new FatalError();
}
let args = [wrapperFunction];
if (usesThis) {
usesArguments: false,
usesThis: false,
};
let state = {
functionInfo,
realm: this.realm,
getModuleIdIfNodeIsRequireFunction: this.modules.getGetModuleIdIfNodeIsRequireFunction(formalParameters, [val]),
};
traverse(
t.file(t.program([t.expressionStatement(t.functionExpression(null, formalParameters, code))])),
ClosureRefVisitor,
null,
state
);
traverse.cache.clear();
this.functionInfos.set(code, functionInfo);
if (val.isResidual && functionInfo.unbound.size) {
if (!val.isUnsafeResidual) {
this.logger.logError(
val,
`residual function ${describeLocation(this.realm, val, undefined, code.loc) ||
"(unknown)"} refers to the following identifiers defined outside of the local scope: ${Object.keys(
functionInfo.unbound
).join(", ")}`
);
}
}
}
let additionalFunctionEffects = this.additionalFunctionValuesAndEffects.get(val);