Skip to content

Commit 33e39b8

Browse files
authored
Add support for errors being mapped to unknown types (#79)
* WIP - Error/unknown support * Change files * Added tests * PR Feedback: Tweaked change log
1 parent 77acc98 commit 33e39b8

File tree

7 files changed

+59
-1
lines changed

7 files changed

+59
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ All packages are validated via ESLint with a consistent set of rules as well as
4040

4141
All packages support testing via `vitest`. Tests are authored with the `.test.ts` suffix and tests can be run with `npm run test`.
4242

43+
Snapshot tests will fail if the number of files generated or the contents of the files change. If snapshot changes are expected, you can update them by running: `npm run test:update`
44+
4345
## Contributing
4446

4547
See [Contributing.md](./CONTRIBUTING.md) for details on contributions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Added support for Error types as function arguments to the TypeScript generator. They will be typed as 'unknown'",
4+
"packageName": "@minecraft/api-docs-generator",
5+
"email": "[email protected]",
6+
"dependentChangeType": "none"
7+
}

tools/api-docs-generator-test-snapshots/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@
33
This package contains vitest snapshots for various @minecraft/api-docs-generator scenarios, processing different configurations of Minecraft API metadata with different combinations of markup generator outputs.
44

55
It exists as a separate package in order to import both @minecraft/api-docs-generator and the @minecraft/markup-generators-plugin packages and test them together.
6+
7+
## Testing
8+
9+
This package is validated with a series of vitest snapshot tests that validate generated markup for specific scenarios. When making changes to the generator, please add new scenarios or update the existing scenarios.
10+
11+
Snapshot tests will fail if the number of files generated or the contents of the files change. If snapshot changes are expected, you can update them by running: `npm run test:update` at the root of the repository.
12+
13+
It is possible to run specific tests using `npm run test -- -- --test <name>`, where `<name>` is a pattern matching `.test.ts`/`.spec.ts`file names. Running this in the JavaScript Debug Terminal allows for debugging specific tests (see above).
14+

tools/api-docs-generator-test-snapshots/test/errors/__snapshots__/errors.spec.ts.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,25 @@ Notes:
9191
- Throws [*ExampleError*](ExampleError.md)
9292
9393
## Methods
94+
- [exampleMethodThatTakesErrorParam](#examplemethodthattakeserrorparam)
9495
- [exampleMethodThatThrows](#examplemethodthatthrows)
9596
- [exampleMethodThatThrowsCustomError](#examplemethodthatthrowscustomerror)
9697
- [exampleMethodThatThrowsTwoErrors](#examplemethodthatthrowstwoerrors)
9798
- [exampleMethodThatThrowsWithReadOnlyPrivilege](#examplemethodthatthrowswithreadonlyprivilege)
9899
100+
### **exampleMethodThatTakesErrorParam**
101+
\`
102+
exampleMethodThatTakesErrorParam(errorParam: unknown): string
103+
\`
104+
105+
#### **Parameters**
106+
- **errorParam**: *unknown*
107+
108+
**Returns** *string*
109+
110+
Notes:
111+
- This function can't be called in read-only mode.
112+
99113
### **exampleMethodThatThrows**
100114
\`
101115
exampleMethodThatThrows(): string
@@ -432,6 +446,12 @@ export class ExampleClass {
432446
* {@link ExampleError}
433447
*/
434448
examplePropertyWithCrossModuleReturnAndError: minecraftexamplemodule2.CrossModuleReturn;
449+
/**
450+
* @remarks
451+
* This function can't be called in read-only mode.
452+
*
453+
*/
454+
exampleMethodThatTakesErrorParam(errorParam: unknown): string;
435455
/**
436456
* @remarks
437457
* This function can't be called in read-only mode.

tools/api-docs-generator-test-snapshots/test/errors/input/test-script-module.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
"base_types": [],
55
"constants": [],
66
"functions": [
7+
{
8+
"arguments": [
9+
{
10+
"name": "errorParam",
11+
"type": {
12+
"is_bind_type": false,
13+
"is_errorable": false,
14+
"name": "error"
15+
}
16+
}
17+
],
18+
"is_constructor": false,
19+
"name": "exampleMethodThatTakesErrorParam",
20+
"return_type": {
21+
"is_bind_type": false,
22+
"is_errorable": false,
23+
"name": "string"
24+
}
25+
},
726
{
827
"arguments": [],
928
"is_constructor": false,

tools/api-docs-generator/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ We recommend using Visual Studio Code's [JavaScript Debug Terminal](https://code
8686

8787
This package is validated with a series of vitest snapshot tests that validate generated markup for specific scenarios. When making changes to the generator, please add new scenarios or update the existing scenarios.
8888

89-
Snapshot tests will fail if the number of files generated or the contents of the files change. If snapshot changes are expected, you can update them by running: `npm run test:update`
89+
Snapshot tests will fail if the number of files generated or the contents of the files change. If snapshot changes are expected, you can update them by running: `npm run test:update` at the root of the repository.
9090

9191
It is possible to run specific tests using `npm run test -- -- --test <name>`, where `<name>` is a pattern matching `.test.ts`/`.spec.ts`file names. Running this in the JavaScript Debug Terminal allows for debugging specific tests (see above).
9292

tools/api-docs-generator/src/filters/TypeScriptFilters.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const typescriptTypeMappings: Record<string, string> = {
2222
uint64: 'number',
2323
float: 'number',
2424
double: 'number',
25+
error: 'unknown', // You can throw anything in JavaScript, so the type is unknown
2526
};
2627

2728
/**

0 commit comments

Comments
 (0)