Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace builtin-modules package with node's isBuiltin check #2176

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"dependencies": {
"@phenomnomnominal/tsquery": "6.1.3",
"automutate": "0.9.0",
"builtin-modules": "4.0.0",
"chalk": "5.4.1",
"commander": "13.1.0",
"enquirer": "2.4.1",
Expand Down Expand Up @@ -89,7 +88,7 @@
},
"packageManager": "[email protected]",
"engines": {
"node": ">=18"
"node": ">=18.6"
},
"publishConfig": {
"provenance": true
Expand Down
9 changes: 0 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions src/runtime/providers/createInstallMissingTypesProvider.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import builtinModules from "builtin-modules";

import { setSubtract } from "../../shared/sets.js";
import { createFileNamesAndServices } from "../createFileNamesAndServices.js";
import { createSingleUseProvider } from "../createSingleUseProvider.js";
import { collectExistingTypingPackages } from "./missingTypes/collectExistingTypingPackages.js";
import { collectPackageManagerRunner } from "./missingTypes/collectPackageManagerRunner.js";
import { collectReferencedPackageNames } from "./missingTypes/collectReferencedPackageNames.js";
import { filterTypedPackageNames } from "./missingTypes/filterTypedPackageNames.js";

const uniqueBuiltinModules = new Set(builtinModules);

/**
* Creates a mutations provider that installs missing types modules.
* @returns Provider to install missing types modules, if needed.
Expand All @@ -36,14 +31,10 @@

// Collect every package name referenced by every file in the project
const { services } = createFileNamesAndServices(options);
const referencedPackageNames =
collectReferencedPackageNames(services);

// Ignore package names already referenced in package.json or that don't exist in DefinitelyTyped
const missingPackageNames = setSubtract(
referencedPackageNames,
const missingPackageNames = collectReferencedPackageNames(
services,

Check warning on line 36 in src/runtime/providers/createInstallMissingTypesProvider.ts

View check run for this annotation

Codecov / codecov/patch

src/runtime/providers/createInstallMissingTypesProvider.ts#L35-L36

Added lines #L35 - L36 were not covered by tests
new Set(existingPackageNames),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, should we give existingPackageNames as a parameter for collectReferencedPackageNames and we could do the check already there, so we didn't need to go through the list again here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that change in second commit. Instead of using setSubsctract, we never add existingPackageNames to the list in the first place.
It's not exactly what this task / PR was about, so that commit could be removed and I can submit it separately. On the other hand, I think it's pretty simple change.

uniqueBuiltinModules,
);
const missingTypedPackageNames = await filterTypedPackageNames(
Array.from(missingPackageNames),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, expect, it } from "vitest";

import {
TypeStatCompilerOptions,
TypeStatOptions,
} from "../../../options/types.js";
import { createLanguageServices } from "../../../services/language.js";
import { collectReferencedPackageNames } from "./collectReferencedPackageNames.js";

describe("collectReferencedPackageNames", () => {
it.skip("should return package names", () => {
const options: Partial<TypeStatOptions> = {
compilerOptions: {} as Readonly<TypeStatCompilerOptions>,
package: {
directory: process.cwd(),
file: "package.json",
missingTypes: true,
},
projectPath: "tsconfig.json",
};
const services = createLanguageServices(options as TypeStatOptions);

const packageNames = collectReferencedPackageNames(
services,
new Set<string>(),
);

expect(Array.from(packageNames)).toStrictEqual(["node", "automutate"]);
});

it("should ignore defined package names", () => {
const options: Partial<TypeStatOptions> = {
compilerOptions: {} as Readonly<TypeStatCompilerOptions>,
package: {
directory: process.cwd(),
file: "package.json",
missingTypes: true,
},
projectPath: "tsconfig.json",
};
const services = createLanguageServices(options as TypeStatOptions);

const packageNames = collectReferencedPackageNames(
services,
new Set<string>(["automutate"]),
);

expect(Array.from(packageNames)).toStrictEqual(["node"]);
});
});
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
import { isBuiltin } from "node:module";
import ts from "typescript";

import { LanguageServices } from "../../../services/language.js";

export const collectReferencedPackageNames = (services: LanguageServices) => {
export const collectReferencedPackageNames = (
services: LanguageServices,
ignoredPackages: Set<string>,
) => {
const packageNames = new Set<string>();

for (const sourceFile of services.program.getSourceFiles()) {
for (const packageName of collectFileReferencedPackageNames(sourceFile)) {
const collected = collectFileReferencedPackageNames(
sourceFile,
ignoredPackages,
);
for (const packageName of collected) {
packageNames.add(packageName);
}
}

return packageNames;
};

const collectFileReferencedPackageNames = (sourceFile: ts.SourceFile) => {
const collectFileReferencedPackageNames = (
sourceFile: ts.SourceFile,
ignoredPackages: Set<string>,
) => {
const packageNames = new Set<string>();

const visitNode = (node: ts.Node) => {
const packageName = parsePackageNameFromNode(node);

if (packageName !== undefined) {
if (
packageName !== undefined &&
!isBuiltin(packageName) &&
!ignoredPackages.has(packageName)
) {
packageNames.add(packageName);
}

Expand Down