Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 12, 2024
1 parent 0e7ab3f commit 2d60e0d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ export interface TwoSlashReturn {
nodes: TwoSlashNode[]

/** Getters */
get queries(): NodeQuery[]
get completions(): NodeCompletion[]
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 hovers(): NodeHover[]
get tags(): NodeTag[]

/**
Expand All @@ -116,9 +116,11 @@ const result1 = twoslasher('import { ref } from "vue"', 'ts')
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.

To avoid memory leaks, you can retrieve the cached map and clear them when necessary:
You can retrieve the cached map and clear it when necessary, to avoid memory leaks:

```ts
import { createTwoSlasher } from 'twoslashes'
Expand All @@ -133,7 +135,27 @@ twoSlasher.getCacheMap()?.clear()

### Backward Compatibility Layer

// TODO:
To make it easier to migrate from `@typescript/twoslash`, TwoSlash<sup>es</sup> provides a backward compatibility layer that allows you to use the old interface with the new implementation.

```ts
import { twoslasherLegacy } from 'twoslashes'

const result = twoslasherLegacy('import { ref } from "vue"', 'ts')

console.log(result.staticQuickInfos) // the old interface
```

You can also compose it your own by only converting the return value:

```ts
import { convertLegacyReturn, twoslasher } from 'twoslashes'

const result = twoslasher('import { ref } from "vue"', 'ts') // new interface

const legacy = convertLegacyReturn(result) // <--

console.log(legacy.staticQuickInfos) // the old interface
```

## Benchmark

Expand Down
2 changes: 1 addition & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { CompilerOptions, JsxEmit } from 'typescript'
import { createFSBackedSystem, createSystem, createVirtualTypeScriptEnvironment } from '@typescript/vfs'
import { objectHash } from 'ohash'
import { TwoslashError } from './error'
import type { CompilerOptionDeclaration, CreateTwoSlashOptions, HandbookOptions, NodeError, NodeErrorWithoutPosition, NodeHover, NodeWithoutPosition, ParsedFlagNotation, Position, Range, TwoSlashExecuteOptions, TwoSlashInstance, TwoSlashOptions, TwoSlashReturn, TwoSlashReturnMeta } from './types'
import type { CompilerOptionDeclaration, CreateTwoSlashOptions, NodeError, NodeWithoutPosition, ParsedFlagNotation, Position, Range, TwoSlashExecuteOptions, TwoSlashInstance, TwoSlashOptions, TwoSlashReturn, TwoSlashReturnMeta } from './types'
import { areRangesIntersecting, createPositionConverter, getExtension, getIdentifierTextSpans, isInRange, isInRanges, parseFlag, removeCodeRanges, resolveNodePositions, splitFiles, typesToExtension } from './utils'
import { validateCodeForErrors } from './validation'
import { defaultCompilerOptions, defaultHandbookOptions } from './defaults'
Expand Down
2 changes: 1 addition & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ readdirSync(fixturesFolder).forEach(fixtureName => {
ts.ScriptTarget.ES2015,
)

const allIdentifiers = getIdentifierTextSpans(ts, file)
const allIdentifiers = getIdentifierTextSpans(ts, file, 0)
expect(allIdentifiers.length).toEqual(40)
})

0 comments on commit 2d60e0d

Please sign in to comment.