Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor: enable strict mode for typescript #562

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

Merged
merged 1 commit into from
Aug 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/document-symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function collectDocumentSymbolsInRange(parent: tsp.NavigationTree, symbols: lsp.
continue;
}

const children = [];
const children: lsp.DocumentSymbol[] = [];
if (parent.childItems) {
for (const child of parent.childItems) {
if (child.spans.some(childSpan => !!Range.intersection(spanRange, asRange(childSpan)))) {
Expand Down Expand Up @@ -60,7 +60,7 @@ export function collectSymbolInformation(uri: string, current: tsp.NavigationTre
const name = current.text;
for (const span of current.spans) {
const range = asRange(span);
const children = [];
const children: lsp.SymbolInformation[] = [];
if (current.childItems) {
for (const child of current.childItems) {
if (child.spans.some(span => !!Range.intersection(range, asRange(span)))) {
Expand Down
2 changes: 1 addition & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class ConsoleLogger implements Logger {
}
}

private toStrings(...arg): string[] {
private toStrings(...arg: any[]): string[] {
return arg.map(a => JSON.stringify(a, null, 2));
}

Expand Down
18 changes: 3 additions & 15 deletions src/lsp-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,7 @@ describe('document symbol', () => {
server.didOpenTextDocument({
textDocument: doc
});
const symbols = await server.documentSymbol({
textDocument: doc,
position: lsp.Position.create(1, 1)
});

const symbols = await server.documentSymbol({ textDocument: doc });
assert.equal(`
Foo
foo
Expand All @@ -581,11 +577,7 @@ interface Box {
server.didOpenTextDocument({
textDocument: doc
});
const symbols = await server.documentSymbol({
textDocument: doc,
position: lsp.Position.create(1, 1)
});

const symbols = await server.documentSymbol({ textDocument: doc });
assert.equal(`
Box
height
Expand Down Expand Up @@ -616,11 +608,7 @@ Box
server.didOpenTextDocument({
textDocument: doc
});
const symbols = await server.documentSymbol({
textDocument: doc,
position: lsp.Position.create(1, 1)
}) as lsp.DocumentSymbol[];

const symbols = await server.documentSymbol({ textDocument: doc }) as lsp.DocumentSymbol[];
const expectation = `
Foo
foo
Expand Down
40 changes: 20 additions & 20 deletions src/lsp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,14 @@ class ServerInitializingIndicator {
}

export class LspServer {
private _tspClient: TspClient | null;
private _loadingIndicator: ServerInitializingIndicator | null;
private initializeParams: TypeScriptInitializeParams;
private initializeResult: TypeScriptInitializeResult;
private _tspClient: TspClient | null = null;
private _loadingIndicator: ServerInitializingIndicator | null = null;
private initializeParams: TypeScriptInitializeParams | null = null;
private diagnosticQueue?: DiagnosticEventQueue;
private logger: Logger;
private workspaceConfiguration: TypeScriptWorkspaceSettings;
private workspaceRoot: string | undefined;
private typeScriptAutoFixProvider: TypeScriptAutoFixProvider;
private typeScriptAutoFixProvider: TypeScriptAutoFixProvider | null = null;
private features: SupportedFeatures = {};

private readonly documents = new LspDocuments();
Expand Down Expand Up @@ -304,7 +303,7 @@ export class LspServer {
]);

const logFileUri = logFile && pathToUri(logFile, undefined);
this.initializeResult = {
const initializeResult: TypeScriptInitializeResult = {
capabilities: {
textDocumentSync: lsp.TextDocumentSyncKind.Incremental,
completionProvider: {
Expand Down Expand Up @@ -378,9 +377,9 @@ export class LspServer {
},
logFileUri
};
(this.initializeResult.capabilities as lspcalls.CallsServerCapabilities).callsProvider = true;
this.logger.log('onInitialize result', this.initializeResult);
return this.initializeResult;
(initializeResult.capabilities as lspcalls.CallsServerCapabilities).callsProvider = true;
this.logger.log('onInitialize result', initializeResult);
return initializeResult;
}
protected getLogFile(logVerbosity: string | undefined): string | undefined {
if (logVerbosity === undefined || logVerbosity === 'off') {
Expand Down Expand Up @@ -567,7 +566,7 @@ export class LspServer {
this.requestDiagnostics();
}

didSaveTextDocument(_params: lsp.DidChangeTextDocumentParams): void {
didSaveTextDocument(_params: lsp.DidSaveTextDocumentParams): void {
// do nothing
}

Expand Down Expand Up @@ -611,7 +610,7 @@ export class LspServer {
return result.body ? result.body.map(fileSpan => toLocation(fileSpan, this.documents)) : [];
}

async documentSymbol(params: lsp.TextDocumentPositionParams): Promise<lsp.DocumentSymbol[] | lsp.SymbolInformation[]> {
async documentSymbol(params: lsp.DocumentSymbolParams): Promise<lsp.DocumentSymbol[] | lsp.SymbolInformation[]> {
const file = uriToPath(params.textDocument.uri);
this.logger.log('symbol', params, file);
if (!file) {
Expand Down Expand Up @@ -639,7 +638,7 @@ export class LspServer {
return symbols;
}
protected get supportHierarchicalDocumentSymbol(): boolean {
const textDocument = this.initializeParams.capabilities.textDocument;
const textDocument = this.initializeParams?.capabilities.textDocument;
const documentSymbol = textDocument && textDocument.documentSymbol;
return !!documentSymbol && !!documentSymbol.hierarchicalDocumentSymbolSupport;
}
Expand Down Expand Up @@ -682,7 +681,7 @@ export class LspServer {
}
return lsp.CompletionList.create(completions, body?.isIncomplete);
} catch (error) {
if (error.message === 'No content available.') {
if ((error as Error).message === 'No content available.') {
this.logger.info('No content was available for completion request');
return null;
} else {
Expand Down Expand Up @@ -756,13 +755,14 @@ export class LspServer {
if (!result.body || !result.body.info.canRename || result.body.locs.length === 0) {
return undefined;
}
const workspaceEdit = {
changes: {}
};
const workspaceEdit: lsp.WorkspaceEdit = {};
result.body.locs
.forEach((spanGroup) => {
const uri = pathToUri(spanGroup.file, this.documents),
textEdits = workspaceEdit.changes[uri] || (workspaceEdit.changes[uri] = []);
const uri = pathToUri(spanGroup.file, this.documents);
if (!workspaceEdit.changes) {
workspaceEdit.changes = {};
}
const textEdits = workspaceEdit.changes[uri] || (workspaceEdit.changes[uri] = []);

spanGroup.locs.forEach((textSpan) => {
textEdits.push({
Expand Down Expand Up @@ -948,7 +948,7 @@ export class LspServer {
if (kinds && !this.pendingDebouncedRequest) {
const diagnostics = this.diagnosticQueue?.getDiagnosticsForFile(file) || [];
if (diagnostics.length) {
actions.push(...await this.typeScriptAutoFixProvider.provideCodeActions(kinds, file, diagnostics, this.documents));
actions.push(...await this.typeScriptAutoFixProvider!.provideCodeActions(kinds, file, diagnostics, this.documents));
}
}

Expand Down Expand Up @@ -1300,7 +1300,7 @@ export class LspServer {

private getInlayHintsOptions(file: string): lspinlayHints.InlayHintsOptions & tsp.UserPreferences {
const workspacePreference = this.getWorkspacePreferencesForDocument(file);
const userPreferences = this.initializeParams.initializationOptions?.preferences || {};
const userPreferences = this.initializeParams?.initializationOptions?.preferences || {};
return {
...userPreferences,
...workspacePreference.inlayHints ?? {}
Expand Down
2 changes: 1 addition & 1 deletion src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function toPlatformEOL(text: string): string {
}

class TestLspClient implements LspClient {
private workspaceEditsListener: (args: lsp.ApplyWorkspaceEditParams) => void | undefined;
private workspaceEditsListener: ((args: lsp.ApplyWorkspaceEditParams) => void) | null = null;

constructor(protected options: TestLspServerOptions, protected logger: ConsoleLogger) {}

Expand Down
4 changes: 2 additions & 2 deletions src/tsp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ interface TypeScriptRequestTypes {

export class TspClient {
public apiVersion: API;
private tsserverProc: cp.ChildProcess | null;
private readlineInterface: readline.ReadLine;
private tsserverProc: cp.ChildProcess | null = null;
private readlineInterface: readline.ReadLine | null = null;
private seq = 0;
private readonly deferreds: { [seq: number]: Deferred<any>; } = {};
private logger: Logger;
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

export class Deferred<T> {
resolve: (value: T) => void;
reject: (err?: unknown) => void;
resolve: (value: T) => void = (_) => {};
reject: (err?: unknown) => void = (_?) => {};

promise = new Promise<T>((resolve, reject) => {
this.resolve = resolve;
Expand Down
10 changes: 3 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"strict": true,
"composite": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"target": "es6",
"target": "es2020",
"module": "Node16",
"moduleResolution": "Node16",
"resolveJsonModule": true,
"esModuleInterop": true,
"lib": [
"es6"
],
"alwaysStrict": true,
"strictBindCallApply": true,
"strictNullChecks": true,
"lib": ["es2020"],
"skipLibCheck": true,
}
}