Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add full diagnostics to tserror #1706

Merged

Conversation

paulbrimicombe
Copy link
Contributor

  • This PR addresses Add error locations to TSError #1705
  • I've been through a few iterations and I think the nicest API is to have a mapping of the error locations for each diagnostic code.
    • This allows clients to look up the location of each error code and also iterate through the object values depending on their requirements.
  • I've added tests for the errors that are thrown when using ts-node as a library.
    • I opted to keep the tests separate for each of the assertions (error message, diagnostic codes, etc.) to make each different feature clear. I'm happy to change this.

@cspotcode
Copy link
Collaborator

Thanks for this, always happy to support consumers of our API doing cool things.

I'm inclined to expose the diagnostics directly, in exactly the form that typescript provides. That way, consumers will have everything they need no matter what they're trying to accomplish. Is that compatible with your use-case?

@cspotcode cspotcode added this to the 10.8.0 milestone Apr 1, 2022
@paulbrimicombe
Copy link
Contributor Author

Thanks for this, always happy to support consumers of our API doing cool things.

I'm inclined to expose the diagnostics directly, in exactly the form that typescript provides. That way, consumers will have everything they need no matter what they're trying to accomplish. Is that compatible with your use-case?

This is a very good plan. I've updated this PR to return the full diagnostics in the TSError. I could have moved things like the diagnostic code extraction into the TSError constructor but opted not to because that would be a breaking change strictly. Let me know what you think @cspotcode !

@paulbrimicombe paulbrimicombe changed the title Add error locations to tserror Add full diagnostics to tserror Apr 4, 2022
Copy link
Collaborator

@cspotcode cspotcode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good thanks again. I have one question and one note for myself.

I can do that work when I merge; I am trying to re-organize the tests slowly over time. Years ago, they were all in index.spec.ts. I'm trying to break that file apart a little bit at a time.

constructor(
diagnosticText: string,
public diagnosticCodes: number[],
diagnostics: ReadonlyArray<_ts.Diagnostic> = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this argument optional to avoid a breaking change to our API surface, since TSError is part of our API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this argument optional to avoid a breaking change to our API surface, since TSError is part of our API?

Yes, that's exactly why it's optional — I didn't want to make this a breaking change.

"Argument of type '123' " +
"is not assignable to parameter of type 'string | undefined'.",
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: consider moving these tests into a new spec file, perhaps tserror.spec.ts or throws-diagnostics.spec.ts Consider also combining it with pre-existing tests for diagnostics.

@codecov
Copy link

codecov bot commented Apr 16, 2022

Codecov Report

Merging #1706 (b6e01c8) into main (511e0e7) will increase coverage by 0.06%.
The diff coverage is 75.00%.

Impacted Files Coverage Δ
src/index.ts 79.09% <75.00%> (-0.14%) ⬇️
src/esm.ts 84.95% <0.00%> (-0.23%) ⬇️
dist-raw/node-errors.js 100.00% <0.00%> (ø)
dist-raw/node-cjs-helpers.js
dist-raw/node-esm-default-get-format.js
dist-raw/node-package-json-reader.js
dist-raw/node-cjs-loader-utils.js
dist-raw/node-repl-await.js
dist-raw/node-internal-errors.js 94.11% <0.00%> (ø)
... and 6 more

@cspotcode cspotcode merged commit deef947 into TypeStrong:main Apr 17, 2022
@cspotcode cspotcode linked an issue Apr 17, 2022 that may be closed by this pull request
@paulbrimicombe
Copy link
Contributor Author

Thanks very much for merging @cspotcode (sorry for the slow response — I've been on vacation).

@paulbrimicombe paulbrimicombe deleted the add-error-locations-to-tserror branch April 26, 2022 08:04
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this pull request May 30, 2022
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [ts-node](https://typestrong.org/ts-node) ([source](https://github.com/TypeStrong/ts-node)) | devDependencies | minor | [`10.7.0` -> `10.8.0`](https://renovatebot.com/diffs/npm/ts-node/10.7.0/10.8.0) |

---

### Release Notes

<details>
<summary>TypeStrong/ts-node</summary>

### [`v10.8.0`](https://github.com/TypeStrong/ts-node/releases/tag/v10.8.0)

[Compare Source](TypeStrong/ts-node@v10.7.0...v10.8.0)

Questions about this release? Ask in the official discussion thread: [#&#8203;1767](TypeStrong/ts-node#1767)

**Added**

-   Added support for `module=NodeNext`, `module=Node16`, `.mts`, `.cts`, `.mjs`, and `.cjs` file extensions ([#&#8203;1414](TypeStrong/ts-node#1414), [#&#8203;1694](TypeStrong/ts-node#1694), [#&#8203;1744](TypeStrong/ts-node#1744), [#&#8203;1745](TypeStrong/ts-node#1745), [#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1717](TypeStrong/ts-node#1717), [#&#8203;1753](TypeStrong/ts-node#1753), [#&#8203;1757](TypeStrong/ts-node#1757)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   For best results, enable `experimentalResolver` ([docs](https://typestrong.org/ts-node/docs/options#experimentalresolver))
    -   See TypeScript's official documentation: https://www.typescriptlang.org/docs/handbook/esm-node.html
    -   enables mixed-mode projects with both ESM and CommonJS
    -   enables all supported file extensions in TypeScript 4.7
    -   Obeys package.json "type"
-   Added ability to include file extensions in CommonJS imports ([#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1753](TypeStrong/ts-node#1753)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   Enables consistency with ESM, where file extensions are often mandatory
-   Resolves from emitted to source file extensions ([#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1753](TypeStrong/ts-node#1753)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   Must enable `experimentalResolver`, will be enabled by default in a future version ([docs](https://typestrong.org/ts-node/docs/options#experimentalresolver))
    -   Typechecker requires importing the *emitted* file extension; ts-node resolves correctly to the *source* file.  E.g. `import "./foo.js"` will execute `foo.ts` See also: [TypeScript issue #&#8203;37582](microsoft/TypeScript#37582)
    -   If typechecking is disabled, you can also use *source* file extensions.  E.g. `import "./foo.ts"`
-   Added `experimentalSpecifierResolution` ([#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1753](TypeStrong/ts-node#1753)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   the same as Node's `--experimental-specifier-resolution` ([Node docs](https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#customizing-esm-specifier-resolution-algorithm))
    -   can also be specified in `tsconfig.json` for convenience, to avoid the CLI flag
    -   allows omitting file extensions in ESM imports, plus a few other CommonJS-style conveniences
-   Adds `diagnostics` property to `TSError`, with array of TypeScript diagnostic objects from the compiler ([API docs](https://typestrong.org/ts-node/api/classes/TSError.html)) ([#&#8203;1705](TypeStrong/ts-node#1705), [#&#8203;1706](TypeStrong/ts-node#1706)) [@&#8203;paulbrimicombe](https://github.com/paulbrimicombe)

**Changed**

-   Renames option `experimentalResolverFeatures` to `experimentalResolver` ([docs](https://typestrong.org/ts-node/docs/options#experimentalresolver)) ([#&#8203;1727](TypeStrong/ts-node#1727)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Internal change to ESM loader for compatibility with forthcoming node versions: returns `shortCircuit: true` ([#&#8203;1714](TypeStrong/ts-node#1714), [#&#8203;1715](TypeStrong/ts-node#1715)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Performance: Optimize filesystem stat calls in ESM loader and new CommonJS resolver ([#&#8203;1758](TypeStrong/ts-node#1758), [#&#8203;1759](TypeStrong/ts-node#1759)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Performance, maintenance: Upgrade source-mapper dependency "[@&#8203;cspotcode/source-map-support](https://github.com/cspotcode/source-map-support)"
    -   Switches to "trace-mapping" for underlying source-map parsing ([#&#8203;1729](TypeStrong/ts-node#1729)) [@&#8203;cspotcode](https://github.com/cspotcode)

**Fixed**

-   Fixed bug where REPL `.type` command was not showing any type information when using TypeScript nightly builds ([#&#8203;1761](TypeStrong/ts-node#1761), [#&#8203;1762](TypeStrong/ts-node#1762)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Correctly suppress "Custom ESM Loaders" warning on newer node versions where the warning's prose changed ([#&#8203;1701](TypeStrong/ts-node#1701)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed REPL bug where function signatures could not be entered across multiple lines ([#&#8203;1667](TypeStrong/ts-node#1667), [#&#8203;1677](TypeStrong/ts-node#1677)) [@&#8203;d9k](https://github.com/d9k)
-   REPL treats unparenthesized object literals as objects, instead of as block scopes ([#&#8203;1697](TypeStrong/ts-node#1697), [#&#8203;1699](TypeStrong/ts-node#1699)) [@&#8203;jhmaster2000](https://github.com/jhmaster2000)
-   Fixed bug where `preferTsExts` combined with third-party transpiler hooks could disrupt `nyc` code coverage ([#&#8203;1755](TypeStrong/ts-node#1755)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed bug where `file://` URLs in stack traces did not always use percent-encoding ([#&#8203;1738](TypeStrong/ts-node#1738), [#&#8203;1726](TypeStrong/ts-node#1726), [#&#8203;1729](TypeStrong/ts-node#1729)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed bug where v8-compile-cache-lib did not correctly unhook itself ([#&#8203;1717](TypeStrong/ts-node#1717), [#&#8203;1718](TypeStrong/ts-node#1718), [#&#8203;1719](TypeStrong/ts-node#1719)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   This internal dependency is used to speed up loading the TypeScript compiler

**Docs**

-   Many docs improvements ([#&#8203;1682](TypeStrong/ts-node#1682)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Options page: each option its own linkable header w/usage example ([#&#8203;1606](TypeStrong/ts-node#1606)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Categorize APIs in typedoc, make entrypoints more prominent ([#&#8203;1456](TypeStrong/ts-node#1456)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Clarify that the shorthand for `--project` is `-P`, not `-p` ([#&#8203;1731](TypeStrong/ts-node#1731), [#&#8203;1734](TypeStrong/ts-node#1734)) [@&#8203;lobsterkatie](https://github.com/lobsterkatie)
-   Add common ESM errors to Troubleshooting page ([#&#8203;1607](TypeStrong/ts-node#1607)) [@&#8203;cspotcode](https://github.com/cspotcode)

https://github.com/TypeStrong/ts-node/milestone/12

</details>

---

### Configuration

📅 **Schedule**: At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1367
Reviewed-by: crapStone <crapstone@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add error locations to TSError
2 participants