babel-plugin-descend-imports
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"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 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3942
4
62