-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# API References | ||
|
||
## `createTwoSlasher` | ||
|
||
TwoSlash runs a TypeScript language server to get the information, which could be a heavy operation to load and parse all the files it needs. In repetitive usages, you may not want to initialize the language server every simple time. TwoSlash provides a `createTwoSlasher` factory function allows you to cache the language servers and reuse the already initialized files. | ||
|
||
```ts | ||
import { createTwoSlasher } from 'twoslash' | ||
|
||
const twoslasher = createTwoSlasher({ | ||
// you can have some default options here | ||
}) | ||
|
||
const result1 = twoslasher('import { ref } from "vue"', 'ts') | ||
// the second time will be much faster as the types from `vue` is already | ||
const result2 = twoslasher('import { computed } from "vue"', 'ts') | ||
``` | ||
|
||
This would result in a [5-20 times faster](#benchmark) performance in repetitive usage. | ||
|
||
To avoid getting interference across runs, it will reuse the language server with the same `compilerOptions`. Internally it holds a map of hashed `compilerOptions` to the language server instances. | ||
|
||
You can retrieve the cached map and clear it when necessary, to avoid memory leaks: | ||
|
||
```ts | ||
import { createTwoSlasher } from 'twoslash' | ||
|
||
const twoslasher = createTwoSlasher() | ||
|
||
// do something | ||
|
||
// Clear the cached language servers, free the memory | ||
twoslasher.getCacheMap()?.clear() | ||
``` | ||
|
||
## `twoslasher` | ||
|
||
// TODO: | ||
|
||
## `twoslasherLegacy` | ||
|
||
// TODO: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,19 @@ | ||
# Options References | ||
|
||
// TODO: | ||
// TODO: add more fields | ||
|
||
## Compiler Options | ||
|
||
## Handbook Options | ||
|
||
### `keepNotations` | ||
|
||
Tell TwoSlash to not remove any notations, and keep the original code untouched. The `nodes` will have the position information of the original code. Useful for better source mapping combing with `meta.removals`. | ||
|
||
Default: `false` | ||
|
||
### `noErrorsCutted` | ||
|
||
Ignore errors that occurred in the cutted code. | ||
|
||
Default: `false` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,106 @@ | ||
--- | ||
outline: deep | ||
--- | ||
|
||
# Result References | ||
|
||
// TODO: | ||
// TODO: add more fields | ||
|
||
## Information Nodes | ||
|
||
TwoSlash returns all types of information in the `nodes` array. | ||
|
||
### Properties | ||
|
||
Nodes provide the following common properties: | ||
|
||
- `type`: the type of the node. Can be `hover`, `query`, `error`, `tag`, `highlight` or `completion` | ||
- was `kind` in `@typescript/twoslash` for some entries | ||
- `start`: the 0-indexed start position of the node in the output code | ||
- `line`: a 0-indexed line number of the node in the output code | ||
- `character`: a 0-indexed character number of the node in the output code | ||
- was `offset` in `@typescript/twoslash` for some entries | ||
- `length`: length of the node | ||
|
||
For different types of nodes, they have some extra properties: | ||
|
||
#### Type `hover` | ||
|
||
- `text`: the text of the hover, usually the type information of the given node | ||
- `docs`: the jsdoc of the given node, can be `undefined` | ||
|
||
#### Type `query` | ||
|
||
Same as `hover` | ||
|
||
#### Type `highlight` | ||
|
||
- `text`: the extra annotation text of the highlight, can be `undefined` | ||
|
||
#### Type `completion` | ||
|
||
- `completion`: the completion entries | ||
- `completionPrefix`: the prefix of the completion | ||
|
||
#### Type `error` | ||
|
||
- `text`: the error message | ||
- was `renderedMessage` in `@typescript/twoslash` | ||
- `level`: the error level | ||
- was `category` in `@typescript/twoslash` | ||
- `code`: TypeScript error code | ||
- `id`: a generated based on the code and position of the error | ||
|
||
#### Type `tag` | ||
|
||
- `text`: the text of the tag | ||
- was `annotation` in `@typescript/twoslash` | ||
|
||
### Getters | ||
|
||
To make it easier to access, we also provide some getters shortcuts to each type of the nodes: | ||
|
||
```ts | ||
export interface TwoSlashReturn { | ||
/** The output code */ | ||
code: string | ||
|
||
/** | ||
* Nodes containing various bits of information about the code | ||
*/ | ||
nodes: TwoSlashNode[] | ||
|
||
/** Getters */ | ||
get hovers(): NodeHover[] // was `staticQuickInfos` | ||
get queries(): NodeQuery[] // was `queries` with `kind: 'query'` | ||
get completions(): NodeCompletion[] // was `queries` with `kind: 'completion'` | ||
get errors(): NodeError[] | ||
get highlights(): NodeHighlight[] | ||
get tags(): NodeTag[] | ||
|
||
/** | ||
* The meta information | ||
*/ | ||
meta: TwoSlashReturnMeta | ||
} | ||
``` | ||
|
||
## Meta Information | ||
|
||
An additional `meta` property is returned providing additional information about the result. | ||
|
||
### `meta.flagNotations` | ||
|
||
The list of options flag notation that is detected from the code. | ||
|
||
### `meta.removals` | ||
|
||
A list of the index ranges of the code removed by TwoSlash from the original code, useful for better source mapping. | ||
|
||
### `meta.compilerOptions` | ||
|
||
The final resolved `compilerOptions` | ||
|
||
### `meta.handbookOptions` | ||
|
||
The final resolved `handbookOptions` |