Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/long-dots-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"debarrel": minor
---

Add metrics
14 changes: 13 additions & 1 deletion codemods/debarrel/scripts/codemod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Codemod, Edit, GetSelector } from "codemod:ast-grep";
import { useMetricAtom } from "codemod:metrics";
import path from "path";
import type { Language } from "./utils/language.ts";
import { getStringContent } from "./utils/ast.ts";
Expand All @@ -16,8 +17,11 @@ import {
type BarrelMockInfo,
} from "./utils/mocks.ts";

const codemod: Codemod<Language> = async (root) => {
const barrelImport = useMetricAtom("barrel_import");

const codemod: Codemod<Language> = async (root, options) => {
const rootNode = root.root();
const relativeFilename = root.relativeFilename();
const filename = root.filename();
const edits: Edit[] = [];
const barrelRewrites = new Map<string, BarrelMockInfo>();
Expand Down Expand Up @@ -77,6 +81,14 @@ const codemod: Codemod<Language> = async (root) => {

if (rewrites.length === 0) continue;

const firstRewrite = rewrites[0];
if (firstRewrite) {
barrelImport.increment({
filePath: firstRewrite.resolvedFilePath,
importer: relativeFilename,
});
}

recordBarrelRewrites(barrelRewrites, importPath, rewrites);

const byPath = groupByPath(rewrites);
Expand Down
7 changes: 5 additions & 2 deletions codemods/debarrel/scripts/utils/specifiers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SgNode } from "codemod:ast-grep";
import type { SgNode, SgRoot } from "codemod:ast-grep";
import type { Language } from "./language.ts";
import { getStringContent } from "./ast.ts";
import {
Expand All @@ -15,6 +15,7 @@ export interface SpecRewrite {
newImportPath: string;
localName: string;
importType: "default" | "named" | "namespace";
resolvedFilePath: string;
}

/**
Expand All @@ -24,7 +25,7 @@ export interface SpecRewrite {
export function resolveSpecifier(
localBinding: SgNode<Language>,
importPath: string,
def: { kind: string; root: { filename(): string }; node: SgNode<Language> },
def: { kind: string; root: SgRoot<Language>; node: SgNode<Language> },
): SpecRewrite | null {
if (def.kind !== "external") return null;

Expand All @@ -51,6 +52,7 @@ export function resolveSpecifier(
newImportPath: joinImportPaths(importPath, info.sourceFromBarrel),
localName: info.localName,
importType: info.importType,
resolvedFilePath: def.root.relativeFilename(),
};
}
// Import-then-reexport: definition landed on import_specifier in the barrel
Expand All @@ -72,6 +74,7 @@ export function resolveSpecifier(
newImportPath: joinImportPaths(importPath, impPath),
localName: originalName,
importType: "named",
resolvedFilePath: def.root.relativeFilename(),
};
}

Expand Down
11 changes: 11 additions & 0 deletions codemods/debarrel/tests/aliased-reexport/expected/src/metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
11 changes: 11 additions & 0 deletions codemods/debarrel/tests/aliased-reexport/input/src/metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
11 changes: 11 additions & 0 deletions codemods/debarrel/tests/aliased-reexport/metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/lib/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/lib/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
11 changes: 11 additions & 0 deletions codemods/debarrel/tests/barrel-with-own-declarations/metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/lib/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/components/index.ts",
"importer": "src/App.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/components/index.ts",
"importer": "src/App.ts"
},
"count": 1
}
]
}
11 changes: 11 additions & 0 deletions codemods/debarrel/tests/basic-named-reexport/metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/components/index.ts",
"importer": "src/App.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Modal from "./components/Modal";

console.log(Modal(), "second file");
18 changes: 18 additions & 0 deletions codemods/debarrel/tests/default-as-named/expected/src/metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/components/index.ts",
"importer": "src/App.ts"
},
"count": 1
},
{
"cardinality": {
"filePath": "src/components/index.ts",
"importer": "src/SecondApp.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Modal } from "./components";

console.log(Modal(), "second file");
18 changes: 18 additions & 0 deletions codemods/debarrel/tests/default-as-named/input/src/metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/components/index.ts",
"importer": "src/App.ts"
},
"count": 1
},
{
"cardinality": {
"filePath": "src/components/index.ts",
"importer": "src/SecondApp.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/utils/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/utils/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
11 changes: 11 additions & 0 deletions codemods/debarrel/tests/import-then-reexport/metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/utils/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/utils/index.ts",
"importer": "src/other.ts"
},
"count": 1
},
{
"cardinality": {
"filePath": "src/utils/index.ts",
"importer": "src/page.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/utils/index.ts",
"importer": "src/other.ts"
},
"count": 1
},
{
"cardinality": {
"filePath": "src/utils/index.ts",
"importer": "src/page.ts"
},
"count": 1
}
]
}
18 changes: 18 additions & 0 deletions codemods/debarrel/tests/multiple-named-reexports/metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/utils/index.ts",
"importer": "src/other.ts"
},
"count": 1
},
{
"cardinality": {
"filePath": "src/utils/index.ts",
"importer": "src/page.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/lib/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/lib/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
11 changes: 11 additions & 0 deletions codemods/debarrel/tests/partial-with-type-specifiers/metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/lib/index.ts",
"importer": "src/consumer.ts"
},
"count": 1
}
]
}
1 change: 1 addition & 0 deletions codemods/debarrel/tests/test.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
semantic_workspace: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"barrel_import": [
{
"cardinality": {
"filePath": "src/components/index.ts",
"importer": "src/App.ts"
},
"count": 1
}
]
}
Loading
Loading