Skip to content

Commit

Permalink
Add support for peer deps (#1072)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey authored Oct 21, 2024
1 parent a735796 commit b98ee74
Show file tree
Hide file tree
Showing 23 changed files with 396 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/fresh-masks-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@definitelytyped/definitions-parser": patch
"@definitelytyped/eslint-plugin": patch
"@definitelytyped/header-parser": patch
---

Add support for peerDependencies
3 changes: 3 additions & 0 deletions packages/definitions-parser/src/lib/definition-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ async function getPackageJsonInfoForPackage(
const packageJson = fs.readJson(packageJsonName) as {
readonly license?: unknown;
readonly dependencies?: unknown;
readonly peerDependencies?: unknown;
readonly devDependencies?: unknown;
readonly imports?: unknown;
readonly exports?: unknown;
Expand All @@ -217,6 +218,7 @@ async function getPackageJsonInfoForPackage(
const header = Array.isArray(packageJsonResult) ? undefined : packageJsonResult;
const allowedDependencies = await getAllowedPackageJsonDependencies();
errors.push(...checkPackageJsonDependencies(packageJson.dependencies, packageJsonName, allowedDependencies));
errors.push(...checkPackageJsonDependencies(packageJson.peerDependencies, packageJsonName, allowedDependencies));
errors.push(
...checkPackageJsonDependencies(
packageJson.devDependencies,
Expand Down Expand Up @@ -248,6 +250,7 @@ async function getPackageJsonInfoForPackage(
license: license as License,
dependencies: packageJson.dependencies as Record<string, string>,
devDependencies: packageJson.devDependencies as Record<string, string>,
peerDependencies: packageJson.peerDependencies as Record<string, string> | undefined,
imports: imports as object | undefined,
exports: exports as string | object | undefined,
type: packageJsonType as "module" | undefined,
Expand Down
19 changes: 18 additions & 1 deletion packages/definitions-parser/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ console.log(jQuery);
}),
);

const allowedDep = dt.pkgDir("allowed-dep");
allowedDep.set("package.json", packageJson("allowed-dep", "1.0", { "prettier": "*" }));

const nonAllowedDep = dt.pkgDir("non-allowed-dep");
nonAllowedDep.set("package.json", packageJson("non-allowed-dep", "1.0", { "not-allowed": "*" }));

const allowedPeerDep = dt.pkgDir("allowed-peer-dep");
allowedPeerDep.set("package.json", packageJson("allowed-peer-dep", "1.0", {}, { "prettier": "*" }));

const nonAllowedPeerDep = dt.pkgDir("non-allowed-peer-dep");
nonAllowedPeerDep.set("package.json", packageJson("non-allowed-peer-dep", "1.0", {}, { "not-allowed-peer": "*" }));

return dt;
}

Expand Down Expand Up @@ -287,7 +299,7 @@ ${testNames.map((s) => " " + JSON.stringify(s)).join(",\n")}
}`;
}

function packageJson(packageName: string, version: string, dependencies: Record<string, string>) {
function packageJson(packageName: string, version: string, dependencies: Record<string, string>, peerDependencies?: Record<string, string>) {
return `{
"private": true,
"name": "@types/${packageName}",
Expand All @@ -302,6 +314,11 @@ function packageJson(packageName: string, version: string, dependencies: Record<
.map(([name, version]) => ` "${name}": "${version}"`)
.join(",\n")}
},
${peerDependencies ? `"peerDependencies": {
${Object.entries(peerDependencies)
.map(([name, version]) => ` "${name}": "${version}"`)
.join(",\n")}
},` : ""}
"devDependencies": {
"@types/${packageName}": "workspace:."
}
Expand Down
11 changes: 11 additions & 0 deletions packages/definitions-parser/src/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ export interface TypingsDataRaw {
*/
readonly devDependencies: PackageJsonDependencies;

/**
* Packages that this package peer depends on.
*/
readonly peerDependencies?: PackageJsonDependencies;

/**
* The [older] version of the library that this definition package refers to, as represented *on-disk*.
*
Expand Down Expand Up @@ -523,13 +528,19 @@ export class TypingsData extends PackageBase {
get devDependencies(): PackageJsonDependencies {
return this.data.devDependencies ?? {};
}
get peerDependencies(): PackageJsonDependencies {
return this.data.peerDependencies ?? {};
}
*allPackageJsonDependencies(): Iterable<[string, string]> {
for (const [name, version] of Object.entries(this.dependencies)) {
yield [name, version];
}
for (const [name, version] of Object.entries(this.devDependencies)) {
yield [name, version];
}
for (const [name, version] of Object.entries(this.peerDependencies)) {
yield [name, version];
}
}

private _contentHash: string | undefined;
Expand Down
34 changes: 34 additions & 0 deletions packages/definitions-parser/test/definition-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,38 @@ import route = require('@ember/routing/route');
const dt = createMockDT();
return expect(getTypingInfo("wordpress__plugins", dt.fs)).resolves.toBeDefined();
});

it("does not error on allowed dependencies", () => {
const dt = createMockDT();

return expect(getTypingInfo("allowed-dep", dt.fs)).resolves.not.toHaveProperty("errors");
});

it("errors on non-allowed dependencies", () => {
const dt = createMockDT();

return expect(getTypingInfo("non-allowed-dep", dt.fs)).resolves.toEqual({
errors: [
"In package.json: Dependency not-allowed not in the allowed dependencies list.\n" +
"Please make a pull request to microsoft/DefinitelyTyped-tools adding it to `packages/definitions-parser/allowedPackageJsonDependencies.txt`.",
],
});
});

it("does not error on allowed peer dependencies", () => {
const dt = createMockDT();

return expect(getTypingInfo("allowed-peer-dep", dt.fs)).resolves.not.toHaveProperty("errors");
});

it("errors on non-allowed peer dependencies", () => {
const dt = createMockDT();

return expect(getTypingInfo("non-allowed-peer-dep", dt.fs)).resolves.toEqual({
errors: [
"In package.json: Dependency not-allowed-peer not in the allowed dependencies list.\n" +
"Please make a pull request to microsoft/DefinitelyTyped-tools adding it to `packages/definitions-parser/allowedPackageJsonDependencies.txt`.",
],
});
});
});
6 changes: 6 additions & 0 deletions packages/definitions-parser/test/packages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ describe(TypingsData, () => {
{
"@types/known": "workspace:.",
},
{
"peer-dependency-1": "*",
}
);
data = new TypingsData(dt.fs, versions["1.0"], true);
});
Expand Down Expand Up @@ -164,6 +167,9 @@ describe(TypingsData, () => {
expect(data.devDependencies).toEqual({
"@types/known": "workspace:.",
});
expect(data.peerDependencies).toEqual({
"peer-dependency-1": "*",
});
expect(data.id).toEqual({
typesDirectoryName: "known",
version: {
Expand Down
2 changes: 2 additions & 0 deletions packages/definitions-parser/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function createTypingsVersionRaw(
libraryName: string,
dependencies: { readonly [name: string]: string },
devDependencies: { readonly [name: string]: string },
peerDependencies?: { readonly [name: string]: string },
): TypingsVersionsRaw {
return {
"1.0": {
Expand All @@ -29,6 +30,7 @@ export function createTypingsVersionRaw(
license: License.MIT,
dependencies,
devDependencies,
peerDependencies,
olderVersionDirectories: [],
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const rule = createRule({
}
return dep;
})
.filter((dep) => dep !== info.realName && packageJson.dependencies?.[dep] === undefined); // TODO(jakebailey): add test for this case from https://github.com/microsoft/DefinitelyTyped-tools/pull/773
.filter((dep) => dep !== info.realName && packageJson.dependencies?.[dep] === undefined && packageJson.peerDependencies?.[dep] === undefined); // TODO(jakebailey): add test for this case from https://github.com/microsoft/DefinitelyTyped-tools/pull/773

commentsMatching(context.sourceCode, /<reference\s+types\s*=\s*"(.+)"\s*\/>/, (ref, comment) => {
if (devDeps.includes(ref)) {
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface PackageJSON {
owners: string[];
dependencies?: Record<string, string | undefined>;
devDependencies?: Record<string, string | undefined>;
peerDependencies?: Record<string, string | undefined>;
tsconfigs?: string[];
}

Expand Down
1 change: 1 addition & 0 deletions packages/header-parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function validatePackageJson(
case "name":
case "version":
case "devDependencies":
case "peerDependencies":
case "projects":
case "minimumTypeScriptVersion":
case "owners":
Expand Down
1 change: 1 addition & 0 deletions packages/header-parser/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe("validatePackageJson", () => {
const header = { ...pkgJson, nonNpm: false, libraryMajorVersion: 18, libraryMinorVersion: 0 };
delete (header as any).dependencies;
delete (header as any).devDependencies;
delete (header as any).peerDependencies;
delete (header as any).private;
delete (header as any).version;
it("requires private: true", () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/mergebot/src/_tests/fixtures/70751/_downloads.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"express": 102459932
}
6 changes: 6 additions & 0 deletions packages/mergebot/src/_tests/fixtures/70751/_files.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"9856226df8d03bf135be670297bb5bf01f79625a:types/express/package.json": "{\n \"private\": true,\n \"name\": \"@types/express\",\n \"version\": \"5.0.9999\",\n \"projects\": [\n \"http://expressjs.com\"\n ],\n \"dependencies\": {\n \"@types/body-parser\": \"*\",\n \"@types/qs\": \"*\",\n \"@types/serve-static\": \"*\"\n },\n \"peerDependencies\": {\n \"@types/express-serve-static-core\": \"^5.0.0\"\n },\n \"devDependencies\": {\n \"@types/express\": \"workspace:.\",\n \"@types/node\": \"*\"\n },\n \"owners\": [\n {\n \"name\": \"Boris Yankov\",\n \"githubUsername\": \"borisyankov\"\n },\n {\n \"name\": \"China Medical University Hospital\",\n \"githubUsername\": \"CMUH\"\n },\n {\n \"name\": \"Puneet Arora\",\n \"githubUsername\": \"puneetar\"\n },\n {\n \"name\": \"Dylan Frankland\",\n \"githubUsername\": \"dfrankland\"\n }\n ]\n}\n",
"b6d1f2876fe51aed6de22ab5a293daf0e3b16ab2:types/express/package.json": "{\n \"private\": true,\n \"name\": \"@types/express\",\n \"version\": \"5.0.9999\",\n \"projects\": [\n \"http://expressjs.com\"\n ],\n \"dependencies\": {\n \"@types/body-parser\": \"*\",\n \"@types/express-serve-static-core\": \"^5.0.0\",\n \"@types/qs\": \"*\",\n \"@types/serve-static\": \"*\"\n },\n \"devDependencies\": {\n \"@types/express\": \"workspace:.\",\n \"@types/node\": \"*\"\n },\n \"owners\": [\n {\n \"name\": \"Boris Yankov\",\n \"githubUsername\": \"borisyankov\"\n },\n {\n \"name\": \"China Medical University Hospital\",\n \"githubUsername\": \"CMUH\"\n },\n {\n \"name\": \"Puneet Arora\",\n \"githubUsername\": \"puneetar\"\n },\n {\n \"name\": \"Dylan Frankland\",\n \"githubUsername\": \"dfrankland\"\n }\n ]\n}\n",
"9856226df8d03bf135be670297bb5bf01f79625a:types/express/v4/package.json": "{\n \"private\": true,\n \"name\": \"@types/express\",\n \"version\": \"4.17.9999\",\n \"projects\": [\n \"http://expressjs.com\"\n ],\n \"dependencies\": {\n \"@types/body-parser\": \"*\",\n \"@types/qs\": \"*\",\n \"@types/serve-static\": \"*\"\n },\n \"peerDependencies\": {\n \"@types/express-serve-static-core\": \"^4.17.33\"\n },\n \"devDependencies\": {\n \"@types/express\": \"workspace:.\",\n \"@types/node\": \"*\"\n },\n \"owners\": [\n {\n \"name\": \"Boris Yankov\",\n \"githubUsername\": \"borisyankov\"\n },\n {\n \"name\": \"China Medical University Hospital\",\n \"githubUsername\": \"CMUH\"\n },\n {\n \"name\": \"Puneet Arora\",\n \"githubUsername\": \"puneetar\"\n },\n {\n \"name\": \"Dylan Frankland\",\n \"githubUsername\": \"dfrankland\"\n }\n ]\n}\n",
"b6d1f2876fe51aed6de22ab5a293daf0e3b16ab2:types/express/v4/package.json": "{\n \"private\": true,\n \"name\": \"@types/express\",\n \"version\": \"4.17.9999\",\n \"projects\": [\n \"http://expressjs.com\"\n ],\n \"dependencies\": {\n \"@types/body-parser\": \"*\",\n \"@types/express-serve-static-core\": \"^4.17.33\",\n \"@types/qs\": \"*\",\n \"@types/serve-static\": \"*\"\n },\n \"devDependencies\": {\n \"@types/express\": \"workspace:.\",\n \"@types/node\": \"*\"\n },\n \"owners\": [\n {\n \"name\": \"Boris Yankov\",\n \"githubUsername\": \"borisyankov\"\n },\n {\n \"name\": \"China Medical University Hospital\",\n \"githubUsername\": \"CMUH\"\n },\n {\n \"name\": \"Puneet Arora\",\n \"githubUsername\": \"puneetar\"\n },\n {\n \"name\": \"Dylan Frankland\",\n \"githubUsername\": \"dfrankland\"\n }\n ]\n}\n"
}
149 changes: 149 additions & 0 deletions packages/mergebot/src/_tests/fixtures/70751/_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
"data": {
"repository": {
"id": "MDEwOlJlcG9zaXRvcnk2MDkzMzE2",
"pullRequest": {
"id": "PR_kwDOAFz6BM59K3Uu",
"title": "Use peer dep for express",
"createdAt": "2024-09-30T20:54:41Z",
"author": {
"login": "jakebailey",
"__typename": "User"
},
"authorAssociation": "MEMBER",
"baseRef": {
"name": "master",
"__typename": "Ref"
},
"labels": {
"nodes": [],
"__typename": "LabelConnection"
},
"isDraft": false,
"mergeable": "MERGEABLE",
"number": 70751,
"state": "OPEN",
"headRefOid": "9856226df8d03bf135be670297bb5bf01f79625a",
"changedFiles": 2,
"additions": 6,
"deletions": 2,
"commitIds": {
"nodes": [
{
"commit": {
"oid": "9856226df8d03bf135be670297bb5bf01f79625a",
"parents": {
"nodes": [
{
"oid": "b6d1f2876fe51aed6de22ab5a293daf0e3b16ab2",
"__typename": "Commit"
}
],
"__typename": "CommitConnection"
},
"__typename": "Commit"
},
"__typename": "PullRequestCommit"
}
],
"__typename": "PullRequestCommitConnection"
},
"timelineItems": {
"nodes": [],
"__typename": "PullRequestTimelineItemsConnection"
},
"reviews": {
"nodes": [],
"__typename": "PullRequestReviewConnection"
},
"commits": {
"totalCount": 1,
"nodes": [
{
"commit": {
"checkSuites": {
"nodes": [
{
"databaseId": 29037908492,
"app": {
"name": "GitHub Actions",
"__typename": "App"
},
"conclusion": null,
"resourcePath": "/DefinitelyTyped/DefinitelyTyped/commit/9856226df8d03bf135be670297bb5bf01f79625a/checks?check_suite_id=29037908492",
"status": "IN_PROGRESS",
"url": "https://github.com/DefinitelyTyped/DefinitelyTyped/commit/9856226df8d03bf135be670297bb5bf01f79625a/checks?check_suite_id=29037908492",
"checkRuns": {
"nodes": [
{
"title": null,
"__typename": "CheckRun"
}
],
"__typename": "CheckRunConnection"
},
"createdAt": "2024-09-30T20:54:45Z",
"workflowRun": {
"file": {
"path": ".github/workflows/CI.yml",
"__typename": "WorkflowRunFile"
},
"__typename": "WorkflowRun"
},
"__typename": "CheckSuite"
}
],
"__typename": "CheckSuiteConnection"
},
"status": null,
"authoredDate": "2024-09-30T20:54:04Z",
"committedDate": "2024-09-30T20:54:04Z",
"pushedDate": null,
"oid": "9856226df8d03bf135be670297bb5bf01f79625a",
"__typename": "Commit"
},
"__typename": "PullRequestCommit"
}
],
"__typename": "PullRequestCommitConnection"
},
"comments": {
"totalCount": 0,
"nodes": [],
"__typename": "IssueCommentConnection"
},
"files": {
"totalCount": 2,
"nodes": [
{
"path": "types/express/package.json",
"additions": 3,
"deletions": 1,
"__typename": "PullRequestChangedFile"
},
{
"path": "types/express/v4/package.json",
"additions": 3,
"deletions": 1,
"__typename": "PullRequestChangedFile"
}
],
"pageInfo": {
"hasNextPage": false,
"endCursor": "Mg",
"__typename": "PageInfo"
},
"__typename": "PullRequestChangedFileConnection"
},
"projectItems": {
"nodes": [],
"__typename": "ProjectV2ItemConnection"
},
"__typename": "PullRequest"
},
"__typename": "Repository"
}
},
"loading": false,
"networkStatus": 7
}
Loading

0 comments on commit b98ee74

Please sign in to comment.