Skip to content

Commit

Permalink
changed: pass exports.delete
Browse files Browse the repository at this point in the history
  • Loading branch information
binjospookie committed Dec 27, 2023
1 parent 6976deb commit 8a189af
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 33 deletions.
16 changes: 7 additions & 9 deletions bin/getUnusedExports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ const getRepoRoot = () =>

/**
* @param {{
* cmd: {function(_: string): void}
* config: {
* exclude: Set<string>,
* babelPlugins: Set<string>,
* exclude: Set<string>
* babelPlugins: Set<string>
* batch: {
* default: number
* },
* },
* pkg: {
* name: string,
* name: string
* path: string
* },
* exports: Set<string>
* }}
*
* @returns {Promise<Set.<string>>}
*/
const getUnusedExports = async ({ config, pkg, exports }) => {
const getUnusedExports = async ({ config, pkg, cmd }) => {
const tokens = [`from '${pkg.name}'`, `from "${pkg.name}"`]

const repoRoot = getRepoRoot()
Expand All @@ -41,16 +41,14 @@ const getUnusedExports = async ({ config, pkg, exports }) => {
batch.push(entry)

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

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

return exports
}

export { getUnusedExports }
8 changes: 4 additions & 4 deletions bin/getUnusedExports/processBatch/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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>,
* pkg: {
* name: string,
Expand All @@ -15,15 +15,15 @@ import { findImport } from './findImport.js'
* }}
*
*/
const processBatch = async ({ config, exports, files, pkg, tokens }) => {
const processBatch = async ({ config, cmd, files, pkg, tokens }) => {
const filesPromise = files.map(async file => {
const found = await findImport({ file, tokens })
return found ? file : null
})

const filterPromise = (await Promise.all(filesPromise))
.filter(x => x !== null)
.map(file => filterExports({ config, exports, file, pkg }))
.map(file => traverse({ config, cmd, file, pkg }))

await Promise.all(filterPromise)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { readFile } from '../../utils/index.js'

/**
* @param {{
* cmd: {function(_: string): void}
* file: string
* pkg: {
* name: string
* },
* config: {
* babelPlugins: Set<string>,
* babelPlugins: Set<string>
* },
* exports: Set<string>
* }}
*/
const filterExports = async ({ file, pkg, config, exports }) => {
const traverse = async ({ file, pkg, config, cmd }) => {
const code = await readFile(file)

const ast = parse(code, {
Expand All @@ -30,17 +30,17 @@ const filterExports = async ({ file, pkg, config, exports }) => {
const type = specifier.type

if (type === 'ImportSpecifier' || type === 'ImportDefaultSpecifier') {
exports.delete(specifier.imported?.name)
cmd(specifier.imported?.name)
} else if (type === 'ImportNamespaceSpecifier') {
exports.delete('* as ' + specifier.local.name)
cmd('* as ' + specifier.local.name)
}
}
}
},
ExportNamedDeclaration(node) {
if (node.source && node.source.value === pkg.name) {
for (const specifier of node.specifiers) {
exports.delete(specifier.exported.name)
cmd(specifier.exported.name)
}
}
}
Expand All @@ -49,4 +49,4 @@ const filterExports = async ({ file, pkg, config, exports }) => {
walk.ancestor(visitors)(ast)
}

export { filterExports }
export { traverse }
12 changes: 6 additions & 6 deletions bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ const main = async ({ config }) => {
const pkg = { name, path: process.cwd() }
const statusApi = createStatusAPI({ pkg })
const exports = await getExports({ config, pkg })
const exportsSize = exports.size
const originalExportsSize = exports.size

exports.onEmpty(statusApi.succeed)

if (exportsSize === 0) {
if (originalExportsSize === 0) {
statusApi.failed({
msg: `Nothing is exported from ${pkg.name}. Remove it.`
})
}

const unusedExports = await getUnusedExports({ config, pkg, exports })
await getUnusedExports({ config, pkg, cmd: exports.delete.bind(exports) })

if (unusedExports.size === 0) {
if (exports.size === 0) {
statusApi.succeed()
}

if (unusedExports.size === exportsSize) {
if (exports.size === originalExportsSize) {
statusApi.failed({
msg: `Nothing is imported from ${pkg.name}. Remove it.`
})
}

statusApi.failed({
msg: `Unused exports in ${pkg.name} package found`,
exports: unusedExports
exports
})
}

Expand Down
22 changes: 19 additions & 3 deletions monorepo/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,39 @@
exports[`run check-exports in package-a 1`] = `
"
> check-exports
> node ../../../../bin/index.js
> node ../../../bin/index.js
Failed Unused exports in package-a package found
[
"F",
"UnusedType"
]
"
`;

exports[`run check-exports in package-b 1`] = `
"
> check-exports
> node ../../../../bin/index.js -e index.tsx
> node ../../../bin/index.js -e index.tsx
"
`;

exports[`run check-exports in package-b 2`] = `
"✔ I'm checking exports from the package-b package
"
`;

exports[`run check-exports in package-c 1`] = `
"
> check-exports
> node ../../../../bin/index.js
> node ../../../bin/index.js
Failed Nothing is imported from package-c. Remove it.
"
`;
2 changes: 1 addition & 1 deletion monorepo/packages/package-a/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "package-a",
"scripts": {
"check-exports": "node ../../../../bin/index.js"
"check-exports": "node ../../../bin/index.js"
}
}
2 changes: 1 addition & 1 deletion monorepo/packages/package-b/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "package-b",
"scripts": {
"check-exports": "node ../../../../bin/index.js -e index.tsx"
"check-exports": "node ../../../bin/index.js -e index.tsx"
}
}
2 changes: 1 addition & 1 deletion monorepo/packages/package-c/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "package-c",
"scripts": {
"check-exports": "node ../../../../bin/index.js"
"check-exports": "node ../../../bin/index.js"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"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.",
"main": "./bin/index.js",
"bin": "./bin/index.js",
Expand Down

0 comments on commit 8a189af

Please sign in to comment.