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

babel-plugin-fake-import-specifiers

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-fake-import-specifiers - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

2

package.json
{
"name": "babel-plugin-fake-import-specifiers",
"version": "1.0.0",
"version": "1.1.0",
"description": "Allow importing pieces while only exporting the whole object",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -0,0 +0,0 @@ # babel-plugin-fake-import-specifiers

'use strict';
function convertDasherizedToCamelized(str) {
return str.split('-').reduce(function(str, chunk) {
if (str === '') {
str = chunk;
} else {
str += chunk.substr(0, 1).toUpperCase() + chunk.substr(1);
}
return str;
}, '');
}
module.exports = function(babel) {

@@ -9,7 +20,19 @@ var Plugin = babel.Plugin;

var defaultSpecifierLookup;
var newIdentifierLookup;
var visitor = {
ImportDeclaration: function(node, parent, scope, file) {
if (sourcesToFake.indexOf(node.source.value) === -1) {
return;
let value = node.source.value;
let sourceMinusOne;
let dasherizedPackageName;
if (sourcesToFake.indexOf(value) === -1) {
// try to match a partial import
// ex: source = 'my-lib'
// import aPackage from 'my-lib/a-package';
let lastSlashIndex = value.lastIndexOf('/');
sourceMinusOne = value.substr(0, lastSlashIndex);
if (sourcesToFake.indexOf(sourceMinusOne) === -1) {
return;
}
dasherizedPackageName = value.substr(lastSlashIndex + 1);
}

@@ -30,22 +53,46 @@

defaultSpecifier = specifier;
break;
} else if (dasherizedPackageName) {
// ignore partial match imports with non default specifiers
// ex: source = 'my-lib'
// import { anotherPackage } from 'my-lib/a-package';
return;
}
}
function generateNewDefaultSpecifier() {
defaultSpecifierIndentifier = scope.generateUidIdentifier();
defaultSpecifier = t.importDefaultSpecifier(defaultSpecifierIndentifier);
}
var hasSpecifiers;
specifiers.forEach(function(specifier) {
if (!t.isImportSpecifier(specifier)) {
return;
}
function addSpecifierToLookup(name) {
defaultSpecifierLookup[name] = defaultSpecifierIndentifier;
hasSpecifiers = true;
}
if (!defaultSpecifierIndentifier) {
defaultSpecifierIndentifier = scope.generateUidIdentifier();
defaultSpecifier = t.importDefaultSpecifier(defaultSpecifierIndentifier);
}
if (dasherizedPackageName) {
node.source.value = sourceMinusOne;
defaultSpecifierLookup[specifier.local.name] = defaultSpecifierIndentifier;
hasSpecifiers = true;
});
let oldName = defaultSpecifierIndentifier.name;
let name = convertDasherizedToCamelized(dasherizedPackageName);
newIdentifierLookup[oldName] = name;
generateNewDefaultSpecifier();
addSpecifierToLookup(oldName);
} else {
specifiers.forEach(function(specifier) {
if (!t.isImportSpecifier(specifier)) {
return;
}
if (!defaultSpecifierIndentifier) {
generateNewDefaultSpecifier();
}
addSpecifierToLookup(specifier.local.name);
});
}
if (hasSpecifiers) {

@@ -67,2 +114,7 @@ node.specifiers = [defaultSpecifier];

var newIdentifier = newIdentifierLookup[name];
if (newIdentifier) {
name = newIdentifier;
}
return this.replaceWith(

@@ -81,2 +133,3 @@ t.callExpression(

defaultSpecifierLookup = {};
newIdentifierLookup = {};
}

@@ -83,0 +136,0 @@ };

Sorry, the diff of this file is not supported yet

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