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

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+
ajv v8.12.0
8+
79
No problems found 🌟
810
911

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+
astring v1.8.6
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

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+
axios v1.4.0
8+
79
❓ Wildcard subpaths cannot yet be analyzed by this tool. https://github.com/arethetypeswrong/arethetypeswrong.github.io/issues/40
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# [email protected] --definitely-typed /Users/andrew/Developer/arethetypeswrong.github.io/packages/core/test/fixtures/@[email protected]
2+
3+
```
4+
$ attw [email protected] --definitely-typed /Users/andrew/Developer/arethetypeswrong.github.io/packages/core/test/fixtures/@[email protected]
5+
6+
7+
big.js v6.2.1
8+
@types/big.js v6.2.0
9+
10+
🎭 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
11+
12+
❌ Import resolved to JavaScript files, but no type declarations were found. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/UntypedResolution.md
13+
14+
⚠️ 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
15+
16+
17+
┌───────────────────┬────────────────────────┬──────────────────────────────┬─────────────────┬───────────────────────┐
18+
│ │ "big.js" │ "big.js/big.mjs" │ "big.js/big.js" │ "big.js/package.json" │
19+
├───────────────────┼────────────────────────┼──────────────────────────────┼─────────────────┼───────────────────────┤
20+
│ node10 │ 🟢 │ ❌ No types │ ❌ No types │ 🟢 (JSON) │
21+
├───────────────────┼────────────────────────┼──────────────────────────────┼─────────────────┼───────────────────────┤
22+
│ node16 (from CJS) │ 🟢 (CJS) │ ❌ No types │ ❌ No types │ 🟢 (JSON) │
23+
│ │ │ ⚠️ ESM (dynamic import only) │ │ │
24+
├───────────────────┼────────────────────────┼──────────────────────────────┼─────────────────┼───────────────────────┤
25+
│ node16 (from ESM) │ 🎭 Masquerading as CJS │ ❌ No types │ ❌ No types │ 🟢 (JSON) │
26+
├───────────────────┼────────────────────────┼──────────────────────────────┼─────────────────┼───────────────────────┤
27+
│ bundler │ 🟢 │ ❌ No types │ ❌ No types │ 🟢 (JSON) │
28+
└───────────────────┴────────────────────────┴──────────────────────────────┴─────────────────┴───────────────────────┘
29+
30+
31+
```
32+
33+
Exit code: 1

packages/cli/test/snapshots/[email protected] -f table.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
$ attw [email protected] -f table
55
66
7+
commander v10.0.1
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 resolved to JavaScript files, but no type declarations were found. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/UntypedResolution.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+
hexoid v1.0.0
8+
79
❗️ The resolved types use export default where the JavaScript file appears to use module.exports =. This will cause TypeScript under the node16 module mode to think an extra .default property access is required, but that will likely fail at runtime. These types should use export = instead of export default. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseExportDefault.md
810
911

packages/cli/test/snapshots/[email protected] -f ascii.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
$ attw [email protected] -f ascii
55
66
7+
klona v2.0.6
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

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+
node-html-parser v6.1.5
8+
79
🤨 CommonJS module simulates a default export with exports.default and exports.__esModule, but does not also set module.exports for compatibility with Node. Node, and some bundlers under certain conditions (https://andrewbranch.github.io/interop-test/#synthesizing-default-exports-for-cjs-modules), do not respect the __esModule marker, so accessing the intended default export will require a .default property access on the default import. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/CJSOnlyExportsDefault.md
810
911

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+
postcss v8.4.21
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 resolved to types through a conditional package.json export, but only after failing to resolve through an earlier condition. This behavior is a TypeScript bug (https://github.com/microsoft/TypeScript/issues/50762). It may misrepresent the runtime behavior of this import and should not be relied upon. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FallbackCondition.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+
react-chartjs-2 v5.2.0
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [email protected] --definitely-typed /Users/andrew/Developer/arethetypeswrong.github.io/packages/core/test/fixtures/@[email protected]
2+
3+
```
4+
$ attw [email protected] --definitely-typed /Users/andrew/Developer/arethetypeswrong.github.io/packages/core/test/fixtures/@[email protected]
5+
6+
7+
react v18.2.0
8+
@types/react v18.2.21
9+
10+
No problems found 🌟
11+
12+
13+
┌─────────────────────────┬───────────┬───────────────────┬───────────────────┬───────────┐
14+
│ │ node10 │ node16 (from CJS) │ node16 (from ESM) │ bundler │
15+
├─────────────────────────┼───────────┼───────────────────┼───────────────────┼───────────┤
16+
│ "react" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
17+
├─────────────────────────┼───────────┼───────────────────┼───────────────────┼───────────┤
18+
│ "react/package.json" │ 🟢 (JSON) │ 🟢 (JSON) │ 🟢 (JSON) │ 🟢 (JSON) │
19+
├─────────────────────────┼───────────┼───────────────────┼───────────────────┼───────────┤
20+
│ "react/jsx-runtime" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
21+
├─────────────────────────┼───────────┼───────────────────┼───────────────────┼───────────┤
22+
│ "react/jsx-dev-runtime" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
23+
├─────────────────────────┼───────────┼───────────────────┼───────────────────┼───────────┤
24+
│ "react/canary" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
25+
├─────────────────────────┼───────────┼───────────────────┼───────────────────┼───────────┤
26+
│ "react/experimental" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
27+
└─────────────────────────┴───────────┴───────────────────┴───────────────────┴───────────┘
28+
29+
30+
```
31+
32+
Exit code: 0

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+
rfdc v1.3.0
8+
79
❌ Import resolved to JavaScript files, but no type declarations were found. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/UntypedResolution.md
810
911

0 commit comments

Comments
 (0)