Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@parcel/scope-hoisting

Package Overview
Dependencies
Maintainers
1
Versions
293
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@parcel/scope-hoisting - npm Package Compare versions

Comparing version 2.0.0-nightly.253 to 2.0.0-nightly.257

6

lib/concat.js

@@ -82,3 +82,3 @@ "use strict";

enter(asset, context) {
if (shouldExcludeAsset(bundleGraph, asset, usedExports)) {
if (shouldSkipAsset(bundleGraph, asset, usedExports)) {
return context;

@@ -94,3 +94,3 @@ }

exit(asset, context) {
if (!context || shouldExcludeAsset(bundleGraph, asset, usedExports)) {
if (!context || shouldSkipAsset(bundleGraph, asset, usedExports)) {
return;

@@ -238,3 +238,3 @@ }

function shouldExcludeAsset(bundleGraph, asset, usedExports) {
function shouldSkipAsset(bundleGraph, asset, usedExports) {
return asset.sideEffects === false && !asset.meta.isCommonJS && (!usedExports.has(asset.id) || (0, _nullthrows.default)(usedExports.get(asset.id)).size === 0) && !bundleGraph.getIncomingDependencies(asset).find(d => // Don't exclude assets that was imported as a wildcard

@@ -241,0 +241,0 @@ d.symbols.hasExportSymbol('*'));

@@ -313,2 +313,3 @@ "use strict";

for (let {
exportAs,
exportSymbol,

@@ -319,41 +320,45 @@ symbol,

} of bundleGraph.getExportedSymbols(entry)) {
if (!symbol) {
// Reexport that couldn't be resolved
let relativePath = (0, _path.relative)(options.projectRoot, asset.filePath);
throw (0, _utils2.getThrowableDiagnosticForNode)(`${relativePath} does not export '${exportSymbol}'`, entry.filePath, loc);
}
if (symbol != null) {
let hasReplacement = replacements.get(symbol);
symbol = hasReplacement !== null && hasReplacement !== void 0 ? hasReplacement : symbol; // If there is an existing binding with the exported name (e.g. an import),
// rename it so we can use the name for the export instead.
let hasReplacement = replacements.get(symbol);
symbol = hasReplacement !== null && hasReplacement !== void 0 ? hasReplacement : symbol; // If there is an existing binding with the exported name (e.g. an import),
// rename it so we can use the name for the export instead.
if (path.scope.hasBinding(exportAs) && exportAs !== symbol) {
(0, _renamer.default)(path.scope, exportAs, path.scope.generateUid(exportAs));
}
if (path.scope.hasBinding(exportSymbol) && exportSymbol !== symbol) {
(0, _renamer.default)(path.scope, exportSymbol, path.scope.generateUid(exportSymbol));
}
let binding = (0, _nullthrows.default)(path.scope.getBinding(symbol));
let binding = (0, _nullthrows.default)(path.scope.getBinding(symbol));
if (!hasReplacement) {
let id = !t.isValidIdentifier(exportAs) ? path.scope.generateUid(exportAs) : exportAs; // rename only once, avoid having to update `replacements` transitively
if (!hasReplacement) {
let id = !t.isValidIdentifier(exportSymbol) ? path.scope.generateUid(exportSymbol) : exportSymbol; // rename only once, avoid having to update `replacements` transitively
(0, _renamer.default)(path.scope, symbol, id);
replacements.set(symbol, id);
symbol = id;
}
(0, _renamer.default)(path.scope, symbol, id);
replacements.set(symbol, id);
symbol = id;
}
let [stmt] = binding.path.getStatementParent().insertAfter(EXPORT_TEMPLATE({
NAME: t.identifier(exportAs),
IDENTIFIER: t.identifier(symbol)
}));
binding.reference(stmt.get('expression.right')); // Exports other than the default export are live bindings. Insert an assignment
// after each constant violation so this remains true.
let [stmt] = binding.path.getStatementParent().insertAfter(EXPORT_TEMPLATE({
NAME: t.identifier(exportSymbol),
IDENTIFIER: t.identifier(symbol)
}));
binding.reference(stmt.get('expression.right')); // Exports other than the default export are live bindings. Insert an assignment
// after each constant violation so this remains true.
if (exportSymbol !== 'default') {
for (let path of binding.constantViolations) {
let [stmt] = path.insertAfter(EXPORT_TEMPLATE({
NAME: t.identifier(exportSymbol),
IDENTIFIER: t.identifier(symbol)
}));
binding.reference(stmt.get('expression.right'));
if (exportAs !== 'default') {
for (let path of binding.constantViolations) {
let [stmt] = path.insertAfter(EXPORT_TEMPLATE({
NAME: t.identifier(exportAs),
IDENTIFIER: t.identifier(symbol)
}));
binding.reference(stmt.get('expression.right'));
}
}
} else if (symbol === null) {
// TODO `meta.exportsIdentifier[exportSymbol]` should be exported
let relativePath = (0, _path.relative)(options.projectRoot, asset.filePath);
throw (0, _utils2.getThrowableDiagnosticForNode)(`${relativePath} couldn't be statically analyzed when importing '${exportSymbol}'`, entry.filePath, loc);
} else {
// Reexport that couldn't be resolved
let relativePath = (0, _path.relative)(options.projectRoot, asset.filePath);
throw (0, _utils2.getThrowableDiagnosticForNode)(`${relativePath} does not export '${exportSymbol}'`, entry.filePath, loc);
}

@@ -360,0 +365,0 @@ }

@@ -103,2 +103,3 @@ "use strict";

for (let {
exportAs,
exportSymbol,

@@ -109,21 +110,24 @@ symbol,

} of bundleGraph.getExportedSymbols(entry)) {
if (symbol == null) {
// Reexport that couldn't be resolved
let relativePath = (0, _path.relative)(options.projectRoot, asset.filePath);
throw (0, _utils2.getThrowableDiagnosticForNode)(`${relativePath} does not export '${exportSymbol}'`, entry.filePath, loc);
}
if (symbol != null) {
symbol = replacements.get(symbol) || symbol; // Map CommonJS module.exports assignments to default ESM exports for interop
symbol = replacements.get(symbol) || symbol; // Map CommonJS module.exports assignments to default ESM exports for interop
if (exportAs === '*') {
exportAs = 'default';
} // If there is an existing binding with the exported name (e.g. an import),
// rename it so we can use the name for the export instead.
if (exportSymbol === '*') {
exportSymbol = 'default';
} // If there is an existing binding with the exported name (e.g. an import),
// rename it so we can use the name for the export instead.
if (programPath.scope.hasBinding(exportAs) && exportAs !== symbol) {
(0, _renamer.default)(programPath.scope, exportAs, programPath.scope.generateUid(exportAs));
}
if (programPath.scope.hasBinding(exportSymbol) && exportSymbol !== symbol) {
(0, _renamer.default)(programPath.scope, exportSymbol, programPath.scope.generateUid(exportSymbol));
exportedIdentifiers.set(exportAs, symbol);
} else if (symbol === null) {
// TODO `meta.exportsIdentifier[exportSymbol]` should be exported
let relativePath = (0, _path.relative)(options.projectRoot, asset.filePath);
throw (0, _utils2.getThrowableDiagnosticForNode)(`${relativePath} couldn't be statically analyzed when importing '${exportSymbol}'`, entry.filePath, loc);
} else {
let relativePath = (0, _path.relative)(options.projectRoot, asset.filePath);
throw (0, _utils2.getThrowableDiagnosticForNode)(`${relativePath} does not export '${exportSymbol}'`, entry.filePath, loc);
}
exportedIdentifiers.set(exportSymbol, symbol);
}

@@ -130,0 +134,0 @@ }

@@ -110,3 +110,3 @@ "use strict";

symbol: exportSymbol,
identifier: undefined,
identifier: null,
loc

@@ -141,9 +141,4 @@ };

} = resolveSymbol(originalModule, originalName, bundle);
let node;
let node = identifier ? findSymbol(path, identifier) : identifier; // If the module is not in this bundle, create a `require` call for it.
if (identifier) {
node = findSymbol(path, identifier);
} // If the module is not in this bundle, create a `require` call for it.
if (!node && (!mod.meta.id || !assets.has((0, _utils.assertString)(mod.meta.id)))) {

@@ -155,3 +150,3 @@ node = addBundleImport(mod, path);

if (!node && !mod.meta.isCommonJS && mod.meta.isES6Module) {
if (node === undefined && !mod.meta.isCommonJS && mod.meta.isES6Module) {
var _depLoc$filePath, _path$node$loc;

@@ -161,6 +156,6 @@

throw (0, _utils.getThrowableDiagnosticForNode)(`${relativePath} does not export '${symbol}'`, (_depLoc$filePath = depLoc === null || depLoc === void 0 ? void 0 : depLoc.filePath) !== null && _depLoc$filePath !== void 0 ? _depLoc$filePath : (_path$node$loc = path.node.loc) === null || _path$node$loc === void 0 ? void 0 : _path$node$loc.filename, depLoc);
} // If it is CommonJS, look for an exports object.
} // Look for an exports object if we bailed out.
if (!node && mod.meta.isCommonJS) {
if (node === undefined && mod.meta.isCommonJS || node === null) {
node = findSymbol(path, (0, _utils.assertString)(mod.meta.exportsIdentifier));

@@ -172,3 +167,4 @@

return interop(mod, symbol, path, node);
node = interop(mod, symbol, path, node);
return node;
}

@@ -198,15 +194,8 @@

if (!path.scope.getBinding(name)) {
// Hoist to the nearest path with the same scope as the exports is declared in
let binding = path.scope.getBinding((0, _utils.assertString)(mod.meta.exportsIdentifier));
let parent;
let binding = (0, _nullthrows.default)(path.scope.getBinding(bundle.hasAsset(mod) ? (0, _utils.assertString)(mod.meta.exportsIdentifier) : // If this bundle doesn't have the asset, use the binding for
// the `parcelRequire`d init function.
(0, _utils.getName)(mod, 'init')));
(0, _assert.default)(binding.path.getStatementParent().parentPath.isProgram(), "Expected binding declaration's parent to be the program"); // Hoist to the nearest path with the same scope as the exports is declared in.
if (binding) {
(0, _assert.default)(binding.path.getStatementParent().parentPath.isProgram(), "Expected binding declaration's parent to be the program");
parent = path.findParent(p => t.isProgram(p.parent));
}
if (!parent) {
parent = path.getStatementParent();
}
let parent = (0, _nullthrows.default)(path.findParent(p => t.isProgram(p.parent)));
let [decl] = parent.insertBefore(DEFAULT_INTEROP_TEMPLATE({

@@ -216,7 +205,3 @@ NAME: t.identifier(name),

}));
if (binding) {
binding.reference(decl.get('declarations.0.init'));
}
binding.reference(decl.get('declarations.0.init'));
getScopeBefore(parent).registerDeclaration(decl);

@@ -223,0 +208,0 @@ }

{
"name": "@parcel/scope-hoisting",
"version": "2.0.0-nightly.253+44a44081",
"version": "2.0.0-nightly.257+5175a66b",
"description": "Blazing fast, zero configuration web application bundler",

@@ -24,9 +24,9 @@ "license": "MIT",

"@babel/types": "^7.3.3",
"@parcel/babylon-walk": "2.0.0-nightly.1875+44a44081",
"@parcel/diagnostic": "2.0.0-nightly.253+44a44081",
"@parcel/babylon-walk": "2.0.0-nightly.1879+5175a66b",
"@parcel/diagnostic": "2.0.0-nightly.257+5175a66b",
"@parcel/source-map": "2.0.0-alpha.4.9",
"@parcel/utils": "2.0.0-nightly.253+44a44081",
"@parcel/utils": "2.0.0-nightly.257+5175a66b",
"nullthrows": "^1.1.1"
},
"gitHead": "44a44081bc44503da7233484b318cf5724aa156f"
"gitHead": "5175a66b28cd2a6090b247204e550cc0264aabbe"
}

@@ -106,3 +106,3 @@ // @flow

enter(asset, context) {
if (shouldExcludeAsset(bundleGraph, asset, usedExports)) {
if (shouldSkipAsset(bundleGraph, asset, usedExports)) {
return context;

@@ -117,3 +117,3 @@ }

exit(asset, context) {
if (!context || shouldExcludeAsset(bundleGraph, asset, usedExports)) {
if (!context || shouldSkipAsset(bundleGraph, asset, usedExports)) {
return;

@@ -267,3 +267,3 @@ }

function shouldExcludeAsset(
function shouldSkipAsset(
bundleGraph: BundleGraph,

@@ -270,0 +270,0 @@ asset: Asset,

@@ -425,2 +425,3 @@ // @flow

for (let {
exportAs,
exportSymbol,

@@ -431,3 +432,55 @@ symbol,

} of bundleGraph.getExportedSymbols(entry)) {
if (!symbol) {
if (symbol != null) {
let hasReplacement = replacements.get(symbol);
symbol = hasReplacement ?? symbol;
// If there is an existing binding with the exported name (e.g. an import),
// rename it so we can use the name for the export instead.
if (path.scope.hasBinding(exportAs) && exportAs !== symbol) {
rename(path.scope, exportAs, path.scope.generateUid(exportAs));
}
let binding = nullthrows(path.scope.getBinding(symbol));
if (!hasReplacement) {
let id = !t.isValidIdentifier(exportAs)
? path.scope.generateUid(exportAs)
: exportAs;
// rename only once, avoid having to update `replacements` transitively
rename(path.scope, symbol, id);
replacements.set(symbol, id);
symbol = id;
}
let [stmt] = binding.path.getStatementParent().insertAfter(
EXPORT_TEMPLATE({
NAME: t.identifier(exportAs),
IDENTIFIER: t.identifier(symbol),
}),
);
binding.reference(stmt.get<NodePath<Identifier>>('expression.right'));
// Exports other than the default export are live bindings. Insert an assignment
// after each constant violation so this remains true.
if (exportAs !== 'default') {
for (let path of binding.constantViolations) {
let [stmt] = path.insertAfter(
EXPORT_TEMPLATE({
NAME: t.identifier(exportAs),
IDENTIFIER: t.identifier(symbol),
}),
);
binding.reference(
stmt.get<NodePath<Identifier>>('expression.right'),
);
}
}
} else if (symbol === null) {
// TODO `meta.exportsIdentifier[exportSymbol]` should be exported
let relativePath = relative(options.projectRoot, asset.filePath);
throw getThrowableDiagnosticForNode(
`${relativePath} couldn't be statically analyzed when importing '${exportSymbol}'`,
entry.filePath,
loc,
);
} else {
// Reexport that couldn't be resolved

@@ -441,50 +494,2 @@ let relativePath = relative(options.projectRoot, asset.filePath);

}
let hasReplacement = replacements.get(symbol);
symbol = hasReplacement ?? symbol;
// If there is an existing binding with the exported name (e.g. an import),
// rename it so we can use the name for the export instead.
if (path.scope.hasBinding(exportSymbol) && exportSymbol !== symbol) {
rename(
path.scope,
exportSymbol,
path.scope.generateUid(exportSymbol),
);
}
let binding = nullthrows(path.scope.getBinding(symbol));
if (!hasReplacement) {
let id = !t.isValidIdentifier(exportSymbol)
? path.scope.generateUid(exportSymbol)
: exportSymbol;
// rename only once, avoid having to update `replacements` transitively
rename(path.scope, symbol, id);
replacements.set(symbol, id);
symbol = id;
}
let [stmt] = binding.path.getStatementParent().insertAfter(
EXPORT_TEMPLATE({
NAME: t.identifier(exportSymbol),
IDENTIFIER: t.identifier(symbol),
}),
);
binding.reference(stmt.get<NodePath<Identifier>>('expression.right'));
// Exports other than the default export are live bindings. Insert an assignment
// after each constant violation so this remains true.
if (exportSymbol !== 'default') {
for (let path of binding.constantViolations) {
let [stmt] = path.insertAfter(
EXPORT_TEMPLATE({
NAME: t.identifier(exportSymbol),
IDENTIFIER: t.identifier(symbol),
}),
);
binding.reference(
stmt.get<NodePath<Identifier>>('expression.right'),
);
}
}
}

@@ -491,0 +496,0 @@ }

@@ -135,2 +135,3 @@ // @flow strict-local

for (let {
exportAs,
exportSymbol,

@@ -141,6 +142,32 @@ symbol,

} of bundleGraph.getExportedSymbols(entry)) {
if (symbol == null) {
// Reexport that couldn't be resolved
if (symbol != null) {
symbol = replacements.get(symbol) || symbol;
// Map CommonJS module.exports assignments to default ESM exports for interop
if (exportAs === '*') {
exportAs = 'default';
}
// If there is an existing binding with the exported name (e.g. an import),
// rename it so we can use the name for the export instead.
if (programPath.scope.hasBinding(exportAs) && exportAs !== symbol) {
rename(
programPath.scope,
exportAs,
programPath.scope.generateUid(exportAs),
);
}
exportedIdentifiers.set(exportAs, symbol);
} else if (symbol === null) {
// TODO `meta.exportsIdentifier[exportSymbol]` should be exported
let relativePath = relative(options.projectRoot, asset.filePath);
throw getThrowableDiagnosticForNode(
`${relativePath} couldn't be statically analyzed when importing '${exportSymbol}'`,
entry.filePath,
loc,
);
} else {
let relativePath = relative(options.projectRoot, asset.filePath);
throw getThrowableDiagnosticForNode(
`${relativePath} does not export '${exportSymbol}'`,

@@ -151,24 +178,2 @@ entry.filePath,

}
symbol = replacements.get(symbol) || symbol;
// Map CommonJS module.exports assignments to default ESM exports for interop
if (exportSymbol === '*') {
exportSymbol = 'default';
}
// If there is an existing binding with the exported name (e.g. an import),
// rename it so we can use the name for the export instead.
if (
programPath.scope.hasBinding(exportSymbol) &&
exportSymbol !== symbol
) {
rename(
programPath.scope,
exportSymbol,
programPath.scope.generateUid(exportSymbol),
);
}
exportedIdentifiers.set(exportSymbol, symbol);
}

@@ -175,0 +180,0 @@ }

@@ -143,3 +143,3 @@ // @flow

symbol: exportSymbol,
identifier: undefined,
identifier: null,
loc,

@@ -171,6 +171,3 @@ };

let node;
if (identifier) {
node = findSymbol(path, identifier);
}
let node = identifier ? findSymbol(path, identifier) : identifier;

@@ -184,3 +181,3 @@ // If the module is not in this bundle, create a `require` call for it.

// If this is an ES6 module, throw an error if we cannot resolve the module
if (!node && !mod.meta.isCommonJS && mod.meta.isES6Module) {
if (node === undefined && !mod.meta.isCommonJS && mod.meta.isES6Module) {
let relativePath = relative(options.projectRoot, mod.filePath);

@@ -194,4 +191,4 @@ throw getThrowableDiagnosticForNode(

// If it is CommonJS, look for an exports object.
if (!node && mod.meta.isCommonJS) {
// Look for an exports object if we bailed out.
if ((node === undefined && mod.meta.isCommonJS) || node === null) {
node = findSymbol(path, assertString(mod.meta.exportsIdentifier));

@@ -202,3 +199,4 @@ if (!node) {

return interop(mod, symbol, path, node);
node = interop(mod, symbol, path, node);
return node;
}

@@ -227,19 +225,19 @@

if (!path.scope.getBinding(name)) {
// Hoist to the nearest path with the same scope as the exports is declared in
let binding = path.scope.getBinding(
assertString(mod.meta.exportsIdentifier),
let binding = nullthrows(
path.scope.getBinding(
bundle.hasAsset(mod)
? assertString(mod.meta.exportsIdentifier)
: // If this bundle doesn't have the asset, use the binding for
// the `parcelRequire`d init function.
getName(mod, 'init'),
),
);
let parent;
if (binding) {
invariant(
binding.path.getStatementParent().parentPath.isProgram(),
"Expected binding declaration's parent to be the program",
);
parent = path.findParent(p => t.isProgram(p.parent));
}
if (!parent) {
parent = path.getStatementParent();
}
invariant(
binding.path.getStatementParent().parentPath.isProgram(),
"Expected binding declaration's parent to be the program",
);
// Hoist to the nearest path with the same scope as the exports is declared in.
let parent = nullthrows(path.findParent(p => t.isProgram(p.parent)));
let [decl] = parent.insertBefore(

@@ -252,7 +250,5 @@ DEFAULT_INTEROP_TEMPLATE({

if (binding) {
binding.reference(
decl.get<NodePath<Identifier>>('declarations.0.init'),
);
}
binding.reference(
decl.get<NodePath<Identifier>>('declarations.0.init'),
);

@@ -259,0 +255,0 @@ getScopeBefore(parent).registerDeclaration(decl);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc