Skip to content

Commit a558fcd

Browse files
authored
Merge pull request #74 from arethetypeswrong/definitely-typed
Add support for DefinitelyTyped
2 parents ad9491a + 894d0f3 commit a558fcd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+510
-94
lines changed

.changeset/good-glasses-wait.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@arethetypeswrong/core": minor
3+
---
4+
5+
Add support for DefinitelyTyped analysis.
6+
7+
- `createPackageFromNpm` now takes an options parameter that can control DefinitelyTyped inclusion.
8+
- The `Package` type is now a class, with new properties and methods:
9+
- `typesPackage` contains the package name and version for the included DefinitelyTyped package, if any.
10+
- `mergedWithTypes(typesPackage: Package)` returns a new `Package` instance with all files from both packages and the `typesPackage` instance property metadata filled in.
11+
- `createPackageFromTarballData` is no longer asynchronous.

.changeset/loud-hairs-relate.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@arethetypeswrong/cli": minor
3+
---
4+
5+
Add support for DefinitelyTyped analysis.
6+
7+
- `@types` packages will be fetched by default for implementation packages that do not contain any TypeScript files.
8+
- `--definitely-typed` can be used to set the version of the `@types` package fetched. By default, the version is inferred from the implementation package version.
9+
- `--no-definitely-typed` can be used to prevent `@types` package inclusion.

packages/cli/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ attw --pack .
7575

7676
#### From NPM
7777

78-
Specify the name (and, optionally, version range) of a package from the NPM registry instead of a local tarball filename.
78+
Specify the name (and, optionally, version or SemVer range) of a package from the NPM registry instead of a local tarball filename.
7979

8080
In the CLI: `--from-npm`, `-p`
8181

@@ -85,6 +85,17 @@ attw --from-npm <package-name>
8585

8686
In the config file, `fromNpm` can be a boolean value.
8787

88+
#### DefinitelyTyped
89+
90+
When a package does not contain types, specifies the version or SemVer range of the DefinitelyTyped `@types` package to use. Defaults to inferring the best version match from the implementation package version.
91+
92+
In the CLI: `--definitely-typed`, `--no-definitely-typed`
93+
94+
```shell
95+
attw -p <package-name> --definitely-typed <version>
96+
attw -p <package-name> --no-definitely-typed
97+
```
98+
8899
#### Format
89100

90101
The format to print the output in. Defaults to `auto`.

packages/cli/src/index.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Format = (typeof formats)[number];
2525
export interface Opts {
2626
pack?: boolean;
2727
fromNpm?: boolean;
28+
definitelyTyped?: boolean | string;
2829
summary?: boolean;
2930
emoji?: boolean;
3031
color?: boolean;
@@ -55,6 +56,8 @@ particularly ESM-related module resolution issues.`
5556
)
5657
.option("-P, --pack", "Run `npm pack` in the specified directory and delete the resulting .tgz file afterwards")
5758
.option("-p, --from-npm", "Read from the npm registry instead of a local file")
59+
.addOption(new Option("--definitely-typed [version]", "Specify the version range of @types to use").default(true))
60+
.option("--no-definitely-typed", "Don't include @types")
5861
.addOption(new Option("-f, --format <format>", "Specify the print format").choices(formats).default("auto"))
5962
.option("-q, --quiet", "Don't print anything to STDOUT (overrides all other options)")
6063
.option(
@@ -92,6 +95,13 @@ particularly ESM-related module resolution issues.`
9295

9396
let analysis: core.CheckResult;
9497
let deleteTgz;
98+
const dtIsPath =
99+
typeof opts.definitelyTyped === "string" &&
100+
(opts.definitelyTyped.includes("/") ||
101+
opts.definitelyTyped.includes("\\") ||
102+
opts.definitelyTyped.endsWith(".tgz") ||
103+
opts.definitelyTyped.endsWith(".tar.gz"));
104+
95105
if (opts.fromNpm) {
96106
if (opts.pack) {
97107
program.error("--pack and --from-npm cannot be used together");
@@ -101,14 +111,18 @@ particularly ESM-related module resolution issues.`
101111
if (result.status === "error") {
102112
program.error(result.error);
103113
} else {
104-
analysis = await core.checkPackage(
105-
await core.createPackageFromNpm(`${result.data.name}@${result.data.version}`),
106-
{
107-
entrypoints: opts.entrypoints,
108-
includeEntrypoints: opts.includeEntrypoints,
109-
excludeEntrypoints: opts.excludeEntrypoints,
110-
}
111-
);
114+
const pkg = dtIsPath
115+
? (await core.createPackageFromNpm(`${result.data.name}@${result.data.version}`)).mergedWithTypes(
116+
core.createPackageFromTarballData(new Uint8Array(await readFile(opts.definitelyTyped as string)))
117+
)
118+
: await core.createPackageFromNpm(`${result.data.name}@${result.data.version}`, {
119+
definitelyTyped: opts.definitelyTyped,
120+
});
121+
analysis = await core.checkPackage(pkg, {
122+
entrypoints: opts.entrypoints,
123+
includeEntrypoints: opts.includeEntrypoints,
124+
excludeEntrypoints: opts.excludeEntrypoints,
125+
});
112126
}
113127
} catch (error) {
114128
if (error instanceof FetchError) {
@@ -156,7 +170,14 @@ particularly ESM-related module resolution issues.`
156170
}
157171
const file = await readFile(fileName);
158172
const data = new Uint8Array(file);
159-
analysis = await core.checkPackage(await core.createPackageFromTarballData(data), {
173+
const pkg = dtIsPath
174+
? core
175+
.createPackageFromTarballData(data)
176+
.mergedWithTypes(
177+
core.createPackageFromTarballData(new Uint8Array(await readFile(opts.definitelyTyped as string)))
178+
)
179+
: core.createPackageFromTarballData(data);
180+
analysis = await core.checkPackage(pkg, {
160181
entrypoints: opts.entrypoints,
161182
includeEntrypoints: opts.includeEntrypoints,
162183
excludeEntrypoints: opts.excludeEntrypoints,

packages/cli/src/render/typed.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ export async function typed(analysis: core.Analysis, opts: Opts) {
3131
);
3232
}
3333

34+
console.log(`${analysis.packageName} v${analysis.packageVersion}`);
35+
if (analysis.types.kind === "@types") {
36+
console.log(`${analysis.types.packageName} v${analysis.types.packageVersion}`);
37+
}
38+
console.log();
39+
3440
if (opts.summary) {
3541
const defaultSummary = marked(!opts.emoji ? " No problems found" : " No problems found 🌟");
3642
const summaryTexts = Object.keys(grouped).map((kind) => {

packages/cli/test/snapshots.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ const tests = [
2828
["[email protected]", "--entrypoints . jsx-runtime"],
2929
["[email protected]", "--exclude-entrypoints macros -f ascii"],
3030
["[email protected]", "--include-entrypoints ./foo -f ascii"],
31+
32+
[
33+
34+
`--definitely-typed ${new URL("../../../core/test/fixtures/@[email protected]", import.meta.url).pathname}`,
35+
],
36+
[
37+
38+
`--definitely-typed ${new URL("../../../core/test/fixtures/@[email protected]", import.meta.url).pathname}`,
39+
],
3140
];
3241

3342
const defaultOpts = "-f table-flipped";

packages/cli/test/snapshots/@apollo__client-3.7.16.tgz.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
$ attw @apollo__client-3.7.16.tgz -f table-flipped
55
66
7+
@apollo/client v3.7.16
8+
79
👺 Import resolved to an ESM type declaration file, but a CommonJS JavaScript file. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseESM.md
810
911
⚠️ A require call resolved to an ESM JavaScript file, which is an error in Node and some bundlers. CommonJS consumers will need to use a dynamic import. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/CJSResolvesToESM.md

packages/cli/test/snapshots/@[email protected]

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
$ attw @[email protected] -f table-flipped
55
66
7+
@ice/app v3.2.6
8+
79
⚠️ A require call resolved to an ESM JavaScript file, which is an error in Node and some bundlers. CommonJS consumers will need to use a dynamic import. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/CJSResolvesToESM.md
810
911
💀 Import failed to resolve to type declarations or JavaScript files. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/NoResolution.md

packages/cli/test/snapshots/@[email protected]

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
$ attw @[email protected] -f table-flipped
55
66
7+
@reduxjs/toolkit v2.0.0-beta.0
8+
79
🎭 Import resolved to a CommonJS type declaration file, but an ESM JavaScript file. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md
810
911
🥴 Import found in a type declaration file failed to resolve. Either this indicates that runtime resolution errors will occur, or (more likely) the types misrepresent the contents of the JavaScript files. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/InternalResolutionError.md

packages/cli/test/snapshots/@[email protected]

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
$ attw @[email protected] -f table-flipped
55
66
7+
@vitejs/plugin-react v3.1.0
8+
79
🎭 Import resolved to a CommonJS type declaration file, but an ESM JavaScript file. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md
810
911

0 commit comments

Comments
 (0)