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

babel-plugin-descend-imports

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-descend-imports - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

src/matchImports.js

2

package.json
{
"name": "babel-plugin-descend-imports",
"version": "1.0.0",
"version": "1.0.1",
"description": "Babel plugin to move imports down",

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

@@ -1,12 +0,13 @@

const types = require('@babel/types');
const { matchImports } = require('./matchImports');
const importsVisitor = {
ImportDeclaration(path, state) {
if (path.node === state.targetedImport) {
path.remove();
ImportDeclaration(path, { lastImport, targetedImports }) {
if (path.node === lastImport) {
path.insertAfter(targetedImports);
return;
}
if (path.node === state.lastImport) {
path.insertAfter(state.targetedImport);
// TODO targetedImports.has(node);
if (targetedImports.find(node => node === path.node)) {
path.remove();
}

@@ -19,42 +20,14 @@ },

Program(path, state) {
const foundImportNodes = [];
let index = 0;
const { targetedImports, lastImportIndex } = matchImports({
nodes: path.node.body,
pattern: state.opts.pattern,
});
for (; index < path.node.body.length; index++) {
const node = path.node.body[index];
if (!types.isImportDeclaration(node)) {
break;
}
const importStatement = node.source.value;
if (state.opts.pattern.test(importStatement)) {
foundImportNodes.push({ node, index });
}
}
// no matched imported
if (!foundImportNodes.length) {
if (!targetedImports.length) {
return;
}
// more than one matched imported
if (foundImportNodes.length > 1) {
// TBD support more than one matched import
return;
}
// the matched import is the last in the imports block
if (foundImportNodes[0].index === index - 1) {
return;
}
// the matched import is not the last in the imports block
if (foundImportNodes[0].index < index - 1) {
path.traverse(importsVisitor, {
targetedImport: foundImportNodes[0].node,
lastImport: path.node.body[index - 1],
});
}
const lastImport = path.node.body[lastImportIndex];
path.traverse(importsVisitor, { targetedImports, lastImport });
},

@@ -61,0 +34,0 @@ };

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