Socket
Socket
Sign inDemoInstall

pure-index

Package Overview
Dependencies
Maintainers
1
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pure-index - npm Package Compare versions

Comparing version 0.0.18 to 0.0.19

bin/getUnusedExports/processBatch/traverse.js

56

bin/getExports.js
import { join } from 'node:path'
import parser from '@babel/parser'
import { ancestor } from 'babel-walk'
import walk from 'babel-walk'
import { readFile, ObservableSet } from './utils/index.js'
// const traverse = _traverse.default
/**

@@ -31,41 +29,18 @@ * @param {{

// const visitors = {
// ExportNamedDeclaration(node) {
// if (node.declaration) {
// const declaration = node.declaration
const visitors = {
ExportNamedDeclaration(node) {
if (node.declaration) {
const declaration = node.declaration
// if (declaration.declarations) {
// // constants and fns
// for (const decl of declaration.declarations) {
// result.add(decl.id.name)
// }
// }
// } else if (node.specifiers) {
// // `export { name }`
// for (const specifier of node.specifiers) {
// result.add(specifier.exported.name)
// }
// }
// }
// }
const traverse = () => {
for (const node of ast.program.body) {
if (node.type === 'ExportNamedDeclaration') {
if (node.declaration) {
const declaration = node.declaration
if (declaration.declarations) {
// constants and fns
for (const decl of declaration.declarations) {
result.add(decl.id.name)
}
if (declaration.declarations) {
// constants and fns
for (const decl of declaration.declarations) {
result.add(decl.id.name)
}
} else if (node.specifiers) {
// `export { name }`
for (const specifier of node.specifiers) {
result.add(specifier.exported.name)
}
}
continue
} else if (node.specifiers) {
// `export { name }`
for (const specifier of node.specifiers) {
result.add(specifier.exported.name)
}
}

@@ -75,4 +50,3 @@ }

// ancestor(visitors)(ast)
traverse(ast)
walk.ancestor(visitors)(ast)

@@ -79,0 +53,0 @@ return result

@@ -12,5 +12,6 @@ import { execSync } from 'node:child_process'

* @param {{
* cmd: {function(_: string): void}
* config: {
* exclude: Set<string>,
* babelPlugins: Set<string>,
* exclude: Set<string>
* babelPlugins: Set<string>
* batch: {

@@ -21,6 +22,5 @@ * default: number

* pkg: {
* name: string,
* name: string
* path: string
* },
* exports: Set<string>
* }}

@@ -30,3 +30,3 @@ *

*/
const getUnusedExports = async ({ config, pkg, exports }) => {
const getUnusedExports = async ({ config, pkg, cmd }) => {
const tokens = [`from '${pkg.name}'`, `from "${pkg.name}"`]

@@ -47,3 +47,3 @@

if (batch.length >= config.batch.default) {
await processBatch({ config, exports, files: batch, pkg, tokens })
await processBatch({ config, cmd, files: batch, pkg, tokens })
batch = []

@@ -54,8 +54,6 @@ }

if (batch.length > 0) {
await processBatch({ config, exports, files: batch, pkg, tokens })
await processBatch({ config, cmd, files: batch, pkg, tokens })
}
return exports
}
export { getUnusedExports }

@@ -1,10 +0,10 @@

import { filterExports } from './filter.js'
import { findImport } from './findImport.js'
import { traverse } from './traverse.js'
/**
* @param {{
* cmd: {function(_: string): void}
* config: {
* babelPlugins: Set<string>,
* },
* exports: Set<string>
* files: Array<string>,

@@ -18,3 +18,3 @@ * pkg: {

*/
const processBatch = async ({ config, exports, files, pkg, tokens }) => {
const processBatch = async ({ config, cmd, files, pkg, tokens }) => {
const filesPromise = files.map(async file => {

@@ -27,3 +27,3 @@ const found = await findImport({ file, tokens })

.filter(x => x !== null)
.map(file => filterExports({ config, exports, file, pkg }))
.map(file => traverse({ config, cmd, file, pkg }))

@@ -30,0 +30,0 @@ await Promise.all(filterPromise)

#!/usr/bin/env node
import { getConfig } from './getConfig.js'
import { getExports } from './getExports.js'
import { getUnusedExports } from './getUnusedExports/index.js'
import { createStatusAPI, readJSON } from './utils/index.js'
import { main } from './main.js'

@@ -14,31 +12,2 @@ const config = await getConfig()

const { name } = await readJSON('package.json')
const pkg = { name, path: process.cwd() }
const statusApi = createStatusAPI({ pkg })
const exports = await getExports({ config, pkg })
const exportsSize = exports.size
exports.onEmpty(statusApi.succeed)
if (exportsSize === 0) {
statusApi.failed({
msg: `Nothing is exported from ${pkg.name}. Remove it.`
})
}
const unusedExports = await getUnusedExports({ config, pkg, exports })
if (unusedExports.size === 0) {
statusApi.succeed()
}
if (unusedExports.size === exportsSize) {
statusApi.failed({
msg: `Nothing is imported from ${pkg.name}. Remove it.`
})
}
statusApi.failed({
msg: `Unused exports in ${pkg.name} package found`,
exports: unusedExports
})
await main({ config })
{
"name": "pure-index",
"type": "module",
"version": "0.0.18",
"version": "0.0.19",
"description": "Utility for monorepos. It helps to find unused exports from packages.",

@@ -15,3 +15,3 @@ "main": "./bin/index.js",

"@babel/parser": "7.23.6",
"babel-walk": "^3.0.0",
"babel-walk": "3.0.0",
"fast-glob": "3.3.2",

@@ -18,0 +18,0 @@ "lilconfig": "3.0.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