Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function addAutoImports(path, state) {
const imports = getImportNames(path, state);
if (isModule(path)) {
// import {jsx} from "react";
// import {createElement} from "react";
const importMap = {};
imports.forEach(importName => {
if (!importMap[importName]) {
importMap[importName] = addNamed(
path,
importName,
getSource(state.source, importName),
{
importedInterop: "uncompiled",
ensureLiveReference: true,
},
).name;
}
this.addDefaultImport = (source, nameHint, blockHoist) => {
// If something on the page adds a helper when the file is an ES6
// file, we can't reused the cached helper name after things have been
// transformed because it has almost certainly been renamed.
const cacheKey = isModule(file.path);
const key = `${source}:${nameHint}:${cacheKey || ''}`;
let cached = cache.get(key);
if (cached) {
cached = t.cloneDeep(cached);
} else {
cached = addDefault(file.path, source, {
importedInterop: 'uncompiled',
nameHint,
blockHoist,
});
cache.set(key, cached);
}
cached[Symbol.for('helper')] = true;
file.set('helperGenerator', name => {
const isInteropHelper = HEADER_HELPERS.includes(name);
// Explicitly set the CommonJS interop helpers to their reserve
// blockHoist of 4 so they are guaranteed to exist
// when other things used them to import.
const blockHoist = isInteropHelper && !isModule(file.path) ? 4 : undefined;
return this.addDefaultImport(`${moduleName}/helpers/${name}`, name, blockHoist);
});
// out and let Babel either insert it directly, or throw an error
// so that plugins can handle that case properly.
if (
file.availableHelper &&
!file.availableHelper(name, runtimeVersion)
) {
return;
}
const isInteropHelper = HEADER_HELPERS.indexOf(name) !== -1;
// Explicitly set the CommonJS interop helpers to their reserve
// blockHoist of 4 so they are guaranteed to exist
// when other things used them to import.
const blockHoist =
isInteropHelper && !isModule(file.path) ? 4 : undefined;
const helpersDir =
esModules && file.path.node.sourceType === "module"
? "helpers/esm"
: "helpers";
return this.addDefaultImport(
`${modulePath}/${helpersDir}/${name}`,
name,
blockHoist,
);
});
}
this.addDefaultImport = (source, nameHint, blockHoist) => {
// If something on the page adds a helper when the file is an ES6
// file, we can't reused the cached helper name after things have been
// transformed because it has almost certainly been renamed.
const cacheKey = isModule(file.path);
const key = `${source}:${nameHint}:${cacheKey || ""}`;
let cached = cache.get(key);
if (cached) {
cached = t.cloneNode(cached);
} else {
cached = addDefault(file.path, source, {
importedInterop: "uncompiled",
nameHint,
blockHoist,
});
cache.set(key, cached);
}
return cached;
};
function createIdentifierName(path, name, importName) {
if (isModule(path)) {
const identifierName = `${importName[name]}`;
return identifierName;
} else {
return `${importName[name]}.${name}`;
}
}
export function rewriteModuleStatementsAndPrepareHeader(
path: NodePath,
{
exportName,
strict,
allowTopLevelThis,
strictMode,
loose,
noInterop,
lazy,
esNamespaceOnly,
},
) {
assert(isModule(path), "Cannot process module statements in a script");
path.node.sourceType = "script";
const meta = normalizeAndLoadModuleMetadata(path, exportName, {
noInterop,
loose,
lazy,
esNamespaceOnly,
});
if (!allowTopLevelThis) {
rewriteThis(path);
}
rewriteLiveReferences(path, meta);
if (strictMode !== false) {