Skip to content

Commit 28846ba

Browse files
committed
refactor: extract functions into util folder with minor fix
1 parent 7dfda15 commit 28846ba

File tree

3 files changed

+83
-64
lines changed

3 files changed

+83
-64
lines changed

packages/dtslint/src/index.ts

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
runAreTheTypesWrong,
1313
} from "./checks";
1414
import { TsVersion, lint } from "./lint";
15-
import { getCompilerOptions, packageDirectoryNameWithVersionFromPath, packageNameFromPath } from "./util";
15+
import { findDTRootAndPackageNameFrom, getCompilerOptions, packageDirectoryNameWithVersionFromPath } from "./util";
1616
import assert = require("assert");
1717

1818
async function main(): Promise<void> {
@@ -151,16 +151,8 @@ async function runTests(
151151
npmChecks: boolean | "only",
152152
tsLocal: string | undefined,
153153
): Promise<string> {
154-
// Assert that we're really on DefinitelyTyped.
155-
const dtRoot = findDTRoot(dirPath);
156-
const packageName = packageNameFromPath(dirPath);
154+
const { dtRoot } = await findDTRootAndPackageNameFrom(dirPath);
157155
const packageDirectoryNameWithVersion = packageDirectoryNameWithVersionFromPath(dirPath);
158-
assertPathIsInDefinitelyTyped(dirPath, dtRoot);
159-
assertPathIsNotBanned(packageName);
160-
assertPackageIsNotDeprecated(
161-
packageName,
162-
await fs.promises.readFile(joinPaths(dtRoot, "notNeededPackages.json"), "utf-8"),
163-
);
164156

165157
const typesVersions = getTypesVersions(dirPath);
166158
const packageJson = checkPackageJson(dirPath, typesVersions);
@@ -293,58 +285,6 @@ async function testTypesVersion(
293285
return { errors };
294286
}
295287

296-
function findDTRoot(dirPath: string) {
297-
let path = dirPath;
298-
while (basename(path) !== "types" && dirname(path) !== "." && dirname(path) !== "/") {
299-
path = dirname(path);
300-
}
301-
return dirname(path);
302-
}
303-
304-
function assertPathIsInDefinitelyTyped(dirPath: string, dtRoot: string): void {
305-
// TODO: It's not clear whether this assertion makes sense, and it's broken on Azure Pipelines (perhaps because DT isn't cloned into DefinitelyTyped)
306-
// Re-enable it later if it makes sense.
307-
// if (basename(dtRoot) !== "DefinitelyTyped")) {
308-
if (!fs.existsSync(joinPaths(dtRoot, "types"))) {
309-
throw new Error(
310-
"Since this type definition includes a header (a comment starting with `// Type definitions for`), " +
311-
"assumed this was a DefinitelyTyped package.\n" +
312-
"But it is not in a `DefinitelyTyped/types/xxx` directory: " +
313-
dirPath,
314-
);
315-
}
316-
}
317-
318-
/**
319-
* Starting at some point in time, npm has banned all new packages whose names
320-
* contain the word `download`. However, some older packages exist that still
321-
* contain this name.
322-
* @NOTE for contributors: The list of literal exceptions below should ONLY be
323-
* extended with packages for which there already exists a corresponding type
324-
* definition package in the `@types` scope. More information:
325-
* https://github.com/microsoft/DefinitelyTyped-tools/pull/381.
326-
*/
327-
function assertPathIsNotBanned(packageName: string) {
328-
if (
329-
/(^|\W)download($|\W)/.test(packageName) &&
330-
packageName !== "download" &&
331-
packageName !== "downloadjs" &&
332-
packageName !== "s3-download-stream"
333-
) {
334-
// Since npm won't release their banned-words list, we'll have to manually add to this list.
335-
throw new Error(`${packageName}: Contains the word 'download', which is banned by npm.`);
336-
}
337-
}
338-
339-
export function assertPackageIsNotDeprecated(packageName: string, notNeededPackages: string) {
340-
const unneeded = JSON.parse(notNeededPackages).packages;
341-
if (Object.keys(unneeded).includes(packageName)) {
342-
throw new Error(`${packageName}: notNeededPackages.json has an entry for ${packageName}.
343-
That means ${packageName} ships its own types, and @types/${packageName} was deprecated and removed from Definitely Typed.
344-
If you want to re-add @types/${packageName}, please remove its entry from notNeededPackages.json.`);
345-
}
346-
}
347-
348288
if (require.main === module) {
349289
main().catch((err) => {
350290
console.error(err.stack);

packages/dtslint/src/util.ts

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createGitHubStringSetGetter, joinPaths } from "@definitelytyped/utils";
1+
import { createGitHubStringSetGetter, joinPaths, readFile } from "@definitelytyped/utils";
22
import fs from "fs";
33
import { basename, dirname } from "path";
44
import stripJsonComments = require("strip-json-comments");
@@ -42,3 +42,82 @@ export const getExpectedNpmVersionFailures = createGitHubStringSetGetter(
4242
"packages/dtslint/expectedNpmVersionFailures.txt",
4343
joinPaths(root, "expectedNpmVersionFailures.txt"),
4444
);
45+
46+
export async function findDTRootAndPackageNameFrom(dirPath: string): Promise<{ dtRoot: string; packageName: string }> {
47+
const dtRoot = findDTRoot(dirPath);
48+
assertPathIsInDefinitelyTyped(dirPath, dtRoot);
49+
50+
const packageName = packageNameFromPath(dirPath);
51+
assertPathIsNotBanned(packageName);
52+
53+
const notNeededPackagesJsonPath = joinPaths(dtRoot, "notNeededPackages.json");
54+
const notNeededPackagesJson = await readFile(notNeededPackagesJsonPath);
55+
assertPackageIsNotDeprecated(packageName, notNeededPackagesJson);
56+
57+
return { dtRoot, packageName };
58+
}
59+
60+
export function findDTRoot(dirPath: string) {
61+
let path = dirPath;
62+
while (basename(path) !== "types" && dirname(path) !== "." && dirname(path) !== "/") {
63+
path = dirname(path);
64+
}
65+
return dirname(path);
66+
}
67+
68+
export function assertPathIsInDefinitelyTyped(dirPath: string, dtRoot: string): void {
69+
// TODO: It's not clear whether this assertion makes sense, and it's broken on Azure Pipelines (perhaps because DT isn't cloned into DefinitelyTyped)
70+
// Re-enable it later if it makes sense.
71+
// if (basename(dtRoot) !== "DefinitelyTyped")) {
72+
if (!fs.existsSync(joinPaths(dtRoot, dirPath))) {
73+
throw new Error(
74+
[
75+
`The type definition for "${dirPath}" is expected to be a DefinitelyTyped package`,
76+
`located in \`DefinitelyTyped/${dirPath}\` directory`,
77+
]
78+
.map((sentence) => sentence.trim())
79+
.join(" "),
80+
);
81+
}
82+
}
83+
84+
/**
85+
* Starting at some point in time, npm has banned all new packages whose names
86+
* contain the word `download`. However, some older packages exist that still
87+
* contain this name.
88+
* @NOTE for contributors: The list of literal exceptions below should ONLY be
89+
* extended with packages for which there already exists a corresponding type
90+
* definition package in the `@types` scope. More information:
91+
* https://github.com/microsoft/DefinitelyTyped-tools/pull/381.
92+
*/
93+
export function assertPathIsNotBanned(packageName: string) {
94+
if (
95+
/(^|\W)download($|\W)/.test(packageName) &&
96+
packageName !== "download" &&
97+
packageName !== "downloadjs" &&
98+
packageName !== "s3-download-stream"
99+
) {
100+
// Since npm won't release their banned-words list, we'll have to manually add to this list.
101+
throw new Error(`${packageName}: Contains the word 'download', which is banned by npm.`);
102+
}
103+
}
104+
105+
export function assertPackageIsNotDeprecated(packageName: string, notNeededPackages: string) {
106+
const unneeded = JSON.parse(notNeededPackages).packages;
107+
if (Object.keys(unneeded).includes(packageName)) {
108+
throw new Error(
109+
[
110+
`${packageName}:`,
111+
`notNeededPackages.json has an entry for ${packageName}.`,
112+
113+
`That means ${packageName} ships its own types,`,
114+
`and @types/${packageName} was deprecated and removed from Definitely Typed.`,
115+
116+
`If you want to re-add @types/${packageName}, `,
117+
`please remove its entry from notNeededPackages.json.`,
118+
]
119+
.map((sentence) => sentence.trim())
120+
.join(" "),
121+
);
122+
}
123+
}

packages/dtslint/test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="jest" />
22
import { CompilerOptionsRaw, checkTsconfig } from "../src/checks";
3-
import { assertPackageIsNotDeprecated } from "../src/index";
3+
import { assertPackageIsNotDeprecated } from "../src/util";
44

55
describe("dtslint", () => {
66
const base: CompilerOptionsRaw = {

0 commit comments

Comments
 (0)