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

Replace typescript/lib/protocol with typescript/lib/tsserverlibrary #311

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/compiler-tsx/src/template/scope/Scope.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: Migrate to typescript.
import { parse, parseExpression } from '@babel/parser'
import {
Expression,
Expand Down
2 changes: 1 addition & 1 deletion packages/transforms/src/tsTransformScriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export function transformScriptSetup(
): TypeScript.ExportKeyword | null {
if (!ts.canHaveModifiers(node)) return null
const modifier = node.modifiers?.find(
(modifier): modifier is ts.ExportKeyword =>
(modifier): modifier is TypeScript.ExportKeyword =>
modifier.kind === ts.SyntaxKind.ExportKeyword,
)
if (modifier == null) return null
Expand Down
92 changes: 51 additions & 41 deletions packages/typecheck/src/TypeScriptServerHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Path from 'path'
import { createInterface, Interface } from 'readline'
import resolveFrom from 'resolve-from'
import type { Readable, Writable } from 'stream'
import type * as Proto from 'typescript/lib/protocol'
import type * as TS from 'typescript/lib/tsserverlibrary'

function resolve(moduleId: string, directory: string): string {
try {
Expand All @@ -21,10 +21,10 @@ function debug(...args: any[]): void {
}

export class TypeScriptServerHost {
private readonly voidCommands: Proto.CommandTypes[] = [
'open' as Proto.CommandTypes.Open,
'geterr' as Proto.CommandTypes.Geterr,
'geterrForProject' as Proto.CommandTypes.GeterrForProject,
private readonly voidCommands: TS.server.protocol.CommandTypes[] = [
'open' as TS.server.protocol.CommandTypes.Open,
'geterr' as TS.server.protocol.CommandTypes.Geterr,
'geterrForProject' as TS.server.protocol.CommandTypes.GeterrForProject,
]

public readonly serverPath = resolve('typescript/lib/tsserver', process.cwd())
Expand All @@ -45,7 +45,7 @@ export class TypeScriptServerHost {

private readonly responseHandlers = new Map<
number,
(response: Proto.Response) => void
(response: TS.server.protocol.Response) => void
>()

private _messageId = 0
Expand Down Expand Up @@ -103,8 +103,10 @@ export class TypeScriptServerHost {

this.readline.on('line', (line) => {
if (line.startsWith('{')) {
const payload: Proto.Request | Proto.Response | Proto.Event =
JSON.parse(line)
const payload:
| TS.server.protocol.Request
| TS.server.protocol.Response
| TS.server.protocol.Event = JSON.parse(line)

debug('RECV:', payload)
if (payload.type === 'response') {
Expand All @@ -125,11 +127,11 @@ export class TypeScriptServerHost {

private eventHandlers: Record<string, Function[]> = {}

private onEvent(payload: Proto.Event): void {
private onEvent(payload: TS.server.protocol.Event): void {
this.eventHandlers[payload.event]?.forEach((fn) => fn(payload.body))
}

private send(message: Omit<Proto.Message, 'seq'>): number {
private send(message: Omit<TS.server.protocol.Message, 'seq'>): number {
if (this.isClosed) {
throw new Error('Cannot send messages to a closed server connection.')
}
Expand All @@ -143,36 +145,36 @@ export class TypeScriptServerHost {
}

public on(
event: Proto.DiagnosticEventKind,
event: TS.server.protocol.DiagnosticEventKind,
fn: (
event: Required<Proto.DiagnosticEvent>['body'],
event: Required<TS.server.protocol.DiagnosticEvent>['body'],
) => void | Promise<void>,
): () => void
public on(
event: Proto.RequestCompletedEventName,
event: TS.server.protocol.RequestCompletedEventName,
fn: (
event: Required<Proto.RequestCompletedEvent>['body'],
event: Required<TS.server.protocol.RequestCompletedEvent>['body'],
) => void | Promise<void>,
): () => void
public on(
event: Proto.ProjectLoadingFinishEventName,
event: TS.server.protocol.ProjectLoadingFinishEventName,
fn: (
event: Required<Proto.ProjectLoadingFinishEvent>['body'],
event: Required<TS.server.protocol.ProjectLoadingFinishEvent>['body'],
) => void | Promise<void>,
): () => void
public on(
event: Proto.ProjectsUpdatedInBackgroundEventName,
event: TS.server.protocol.ProjectsUpdatedInBackgroundEventName,
fn: (
event: Required<Proto.ProjectsUpdatedInBackgroundEvent>['body'],
event: Required<TS.server.protocol.ProjectsUpdatedInBackgroundEvent>['body'],
) => void | Promise<void>,
): () => void

public on(
event:
| Proto.DiagnosticEventKind
| Proto.RequestCompletedEventName
| Proto.ProjectLoadingFinishEventName
| Proto.ProjectsUpdatedInBackgroundEventName,
| TS.server.protocol.DiagnosticEventKind
| TS.server.protocol.RequestCompletedEventName
| TS.server.protocol.ProjectLoadingFinishEventName
| TS.server.protocol.ProjectsUpdatedInBackgroundEventName,
fn: Function,
): () => void {
const handlers =
Expand All @@ -189,11 +191,15 @@ export class TypeScriptServerHost {
}

public async sendRequest(
request: Omit<Proto.Request, 'seq' | 'type'>,
): Promise<Proto.Response | number | undefined> {
request: Omit<TS.server.protocol.Request, 'seq' | 'type'>,
): Promise<TS.server.protocol.Response | number | undefined> {
const id = this.send({ type: 'request', ...request })

if (!this.voidCommands.includes(request.command as Proto.CommandTypes)) {
if (
!this.voidCommands.includes(
request.command as TS.server.protocol.CommandTypes,
)
) {
this.pendingResponses += 1
return await new Promise((resolve) => {
this.responseHandlers.set(id, (response) => resolve(response))
Expand All @@ -203,7 +209,9 @@ export class TypeScriptServerHost {
}
}

public sendEvent(event: Omit<Proto.Request, 'seq' | 'type'>): void {
public sendEvent(
event: Omit<TS.server.protocol.Request, 'seq' | 'type'>,
): void {
this.send({ type: 'event', ...event })
}

Expand All @@ -224,33 +232,35 @@ export class TypeScriptServerHost {
}

public async sendCommand(
command: 'configure' | Proto.CommandTypes.Configure,
args: Proto.ConfigureRequest['arguments'],
): Promise<Proto.ConfigureResponse>
command: 'configure' | TS.server.protocol.CommandTypes.Configure,
args: TS.server.protocol.ConfigureRequest['arguments'],
): Promise<TS.server.protocol.ConfigureResponse>
public async sendCommand(
command: 'projectInfo' | Proto.CommandTypes.ProjectInfo,
args: Proto.ProjectInfoRequest['arguments'],
): Promise<Proto.ProjectInfoResponse>
command: 'projectInfo' | TS.server.protocol.CommandTypes.ProjectInfo,
args: TS.server.protocol.ProjectInfoRequest['arguments'],
): Promise<TS.server.protocol.ProjectInfoResponse>

public async sendCommand(
command:
| 'compilerOptionsForInferredProjects'
| Proto.CommandTypes.CompilerOptionsForInferredProjects,
args: Proto.SetCompilerOptionsForInferredProjectsRequest['arguments'],
): Promise<Proto.SetCompilerOptionsForInferredProjectsResponse>
| TS.server.protocol.CommandTypes.CompilerOptionsForInferredProjects,
args: TS.server.protocol.SetCompilerOptionsForInferredProjectsRequest['arguments'],
): Promise<TS.server.protocol.SetCompilerOptionsForInferredProjectsResponse>

public async sendCommand(
command: 'updateOpen' | Proto.CommandTypes.UpdateOpen,
args: Proto.UpdateOpenRequest['arguments'],
command: 'updateOpen' | TS.server.protocol.CommandTypes.UpdateOpen,
args: TS.server.protocol.UpdateOpenRequest['arguments'],
): Promise<number>

public async sendCommand(
command: 'geterr' | Proto.CommandTypes.Geterr,
args: Proto.GeterrRequest['arguments'],
command: 'geterr' | TS.server.protocol.CommandTypes.Geterr,
args: TS.server.protocol.GeterrRequest['arguments'],
): Promise<number>
public async sendCommand(
command: 'geterrForProject' | Proto.CommandTypes.GeterrForProject,
args: Proto.GeterrForProjectRequest['arguments'],
command:
| 'geterrForProject'
| TS.server.protocol.CommandTypes.GeterrForProject,
args: TS.server.protocol.GeterrForProjectRequest['arguments'],
): Promise<number>

public async sendCommand(command: string, args: any): Promise<any> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export class PluginManager {
overrideMethod(
serverHost,
'watchFile',
(watchFile) => (fileName, callback) => {
(watchFile) => (fileName, callback, pollingInterval, options) => {
if (fs.isGeneratedVueFile(fileName)) {
return watchFile(
fs.getRealFileNameIfAny(fileName),
Expand All @@ -401,14 +401,16 @@ export class PluginManager {
)
callback(fileName, eventKind)
},
pollingInterval,
options,
)
}

if (fs.isProjectRuntimeFile(fileName)) {
return { close: () => {} }
}

return watchFile(fileName, callback)
return watchFile(fileName, callback, pollingInterval, options)
},
)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vue-languageservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"vscode-languageserver-types": "^3.17.2",
"vscode-languageserver-textdocument": "^1.0.7",
"vscode-css-languageservice": "^5.1.13",
"vscode-html-languageservice": "^4.2.2"
"vscode-html-languageservice": "^4.2.2",
"vscode-uri": "^2.1.2"
},
"devDependencies": {}
}