Skip to content

Rename all applicable Thenables to Promise #352

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 2 commits into from
Nov 13, 2020
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
20 changes: 3 additions & 17 deletions src/languageservice/services/schemaRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { IConnection } from 'vscode-languageserver';
import { xhr, XHRResponse, getErrorStatusDescription } from 'request-light';
import * as fs from 'fs';

import { VSCodeContentRequest, CustomSchemaContentRequest } from '../../requestTypes';
import { CustomSchemaContentRequest } from '../../requestTypes';
import { isRelativePath, relativeToAbsolutePath } from '../utils/paths';

/**
* Handles schema content requests given the schema URI
* @param uri can be a local file, vscode request, http(s) request or a custom request
*/
export const schemaRequestHandler = (connection: IConnection, uri: string): Thenable<string> => {
export const schemaRequestHandler = (connection: IConnection, uri: string): Promise<string> => {
if (!uri) {
return Promise.reject('No schema specified');
}
Expand Down Expand Up @@ -45,20 +45,6 @@ export const schemaRequestHandler = (connection: IConnection, uri: string): Then
});
}

// vscode schema content requests are forwarded to the client through LSP
// This is a non-standard LSP extension introduced by the JSON language server
// See https://github.com/microsoft/vscode/blob/master/extensions/json-language-features/server/README.md
if (scheme === 'vscode') {
return connection.sendRequest(VSCodeContentRequest.type, uri).then(
(responseText) => {
return responseText;
},
(error) => {
return error.message;
}
);
}

// HTTP(S) requests are sent and the response result is either the schema content or an error
if (scheme === 'http' || scheme === 'https') {
// Send the HTTP(S) schema content request and return the result
Expand All @@ -74,5 +60,5 @@ export const schemaRequestHandler = (connection: IConnection, uri: string): Then
}

// Neither local file nor vscode, nor HTTP(S) schema request, so send it off as a custom request
return connection.sendRequest(CustomSchemaContentRequest.type, uri) as Thenable<string>;
return connection.sendRequest(CustomSchemaContentRequest.type, uri) as Promise<string>;
};
6 changes: 3 additions & 3 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ASTNode, ObjectASTNode, PropertyASTNode } from '../jsonASTTypes';
import { parse as parseYAML } from '../parser/yamlParser07';
import { YAMLSchemaService } from './yamlSchemaService';
import { JSONSchema, JSONSchemaRef } from '../jsonSchema';
import { Thenable, CompletionsCollector } from 'vscode-json-languageservice';
import { CompletionsCollector } from 'vscode-json-languageservice';
import {
CompletionItem,
CompletionItemKind,
Expand Down Expand Up @@ -54,7 +54,7 @@ export class YAMLCompletion extends JSONCompletion {
this.configuredIndentation = languageSettings.indentation;
}

public doComplete(document: TextDocument, position: Position, isKubernetes = false): Thenable<CompletionList> {
public doComplete(document: TextDocument, position: Position, isKubernetes = false): Promise<CompletionList> {
const result: CompletionList = {
items: [],
isIncomplete: false,
Expand Down Expand Up @@ -157,7 +157,7 @@ export class YAMLCompletion extends JSONCompletion {
const newSchema = schema;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const collectionPromises: Thenable<any>[] = [];
const collectionPromises: Promise<any>[] = [];

let addValue = true;

Expand Down
3 changes: 1 addition & 2 deletions src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

import { Thenable } from 'vscode-json-languageservice';
import { Hover, TextDocument, Position } from 'vscode-languageserver-types';
import { matchOffsetToDocument } from '../utils/arrUtils';
import { LanguageSettings } from '../yamlLanguageService';
Expand All @@ -29,7 +28,7 @@ export class YAMLHover {
}
}

public doHover(document: TextDocument, position: Position, isKubernetes = false): Thenable<Hover> {
public doHover(document: TextDocument, position: Position, isKubernetes = false): Promise<Hover> {
if (!this.shouldHover || !document) {
return Promise.resolve(undefined);
}
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/services/yamlLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { parse as parseYAML } from '../parser/yamlParser07';
import { findLinks as JSONFindLinks } from 'vscode-json-languageservice/lib/umd/services/jsonLinks';
import { DocumentLink } from 'vscode-languageserver';

export function findLinks(document: TextDocument): Thenable<DocumentLink[]> {
export function findLinks(document: TextDocument): Promise<DocumentLink[]> {
const doc = parseYAML(document.getText());
// Find links across all YAML Documents then report them back once finished
const linkPromises = [];
Expand Down
18 changes: 9 additions & 9 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

import { JSONSchema, JSONSchemaMap, JSONSchemaRef } from '../jsonSchema';
import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, Thenable } from '../yamlLanguageService';
import { SchemaRequestService, WorkspaceContextService } from '../yamlLanguageService';
import {
UnresolvedSchema,
ResolvedSchema,
Expand All @@ -26,7 +26,7 @@ import * as yaml from 'js-yaml';

const localize = nls.loadMessageBundle();

export declare type CustomSchemaProvider = (uri: string) => Thenable<string | string[]>;
export declare type CustomSchemaProvider = (uri: string) => Promise<string | string[]>;

export enum MODIFICATION_ACTIONS {
'delete',
Expand Down Expand Up @@ -105,7 +105,7 @@ export class YAMLSchemaService extends JSONSchemaService {
schemaToResolve: UnresolvedSchema,
schemaURL: string,
dependencies: SchemaDependencies
): Thenable<ResolvedSchema> {
): Promise<ResolvedSchema> {
const resolveErrors: string[] = schemaToResolve.errors.slice(0);
const schema = schemaToResolve.schema;
const contextService = this.contextService;
Expand Down Expand Up @@ -147,7 +147,7 @@ export class YAMLSchemaService extends JSONSchemaService {
parentSchemaURL: string,
parentSchemaDependencies: SchemaDependencies
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Thenable<any> => {
): Promise<any> => {
if (contextService && !/^\w+:\/\/.*/.test(uri)) {
uri = contextService.resolveRelativePath(uri, parentSchemaURL);
}
Expand All @@ -174,7 +174,7 @@ export class YAMLSchemaService extends JSONSchemaService {
parentSchemaURL: string,
parentSchemaDependencies: SchemaDependencies
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Thenable<any> => {
): Promise<any> => {
if (!node || typeof node !== 'object') {
return Promise.resolve(null);
}
Expand All @@ -183,7 +183,7 @@ export class YAMLSchemaService extends JSONSchemaService {
const seen: JSONSchema[] = [];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const openPromises: Thenable<any>[] = [];
const openPromises: Promise<any>[] = [];

const collectEntries = (...entries: JSONSchemaRef[]): void => {
for (const entry of entries) {
Expand Down Expand Up @@ -262,7 +262,7 @@ export class YAMLSchemaService extends JSONSchemaService {
});
}

public getSchemaForResource(resource: string, doc: JSONDocument): Thenable<ResolvedSchema> {
public getSchemaForResource(resource: string, doc: JSONDocument): Promise<ResolvedSchema> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const resolveSchema = (): any => {
const seen: { [schemaId: string]: boolean } = Object.create(null);
Expand Down Expand Up @@ -507,7 +507,7 @@ export class YAMLSchemaService extends JSONSchemaService {
return super.getOrAddSchemaHandle(id, unresolvedSchemaContent);
}

loadSchema(schemaUri: string): Thenable<UnresolvedSchema> {
loadSchema(schemaUri: string): Promise<UnresolvedSchema> {
const requestService = this.requestService;
return super.loadSchema(schemaUri).then((unresolvedJsonSchema: UnresolvedSchema) => {
// If json-language-server failed to parse the schema, attempt to parse it as YAML instead.
Expand Down Expand Up @@ -570,7 +570,7 @@ export class YAMLSchemaService extends JSONSchemaService {
return super.getRegisteredSchemaIds(filter);
}

getResolvedSchema(schemaId: string): Thenable<ResolvedSchema> {
getResolvedSchema(schemaId: string): Promise<ResolvedSchema> {
return super.getResolvedSchema(schemaId);
}

Expand Down
64 changes: 7 additions & 57 deletions src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { YAMLHover } from './services/yamlHover';
import { YAMLValidation } from './services/yamlValidation';
import { YAMLFormatter } from './services/yamlFormatter';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { JSONWorkerContribution, JSONDocument, DefinitionLink } from 'vscode-json-languageservice';
import { JSONDocument, DefinitionLink } from 'vscode-json-languageservice';
import { findLinks } from './services/yamlLinks';

export interface LanguageSettings {
Expand All @@ -41,56 +41,6 @@ export interface LanguageSettings {
indentation?: string;
}

export interface PromiseConstructor {
/**
* Creates a new Promise.
* @param executor A callback used to initialize the promise. This callback is passed two arguments:
* a resolve callback used resolve the promise with a value or the result of another promise,
* and a reject callback used to reject the promise with a provided reason or error.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new <T>(executor: (resolve: (value?: T | Thenable<T>) => void, reject: (reason?: any) => void) => void): Thenable<T>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T>(values: Array<T | Thenable<T>>): Thenable<T[]>;
/**
* Creates a new rejected promise for the provided reason.
* @param reason The reason the promise was rejected.
* @returns A new rejected Promise.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reject<T>(reason: any): Thenable<T>;

/**
* Creates a new resolved promise for the provided value.
* @param value A promise.
* @returns A promise whose internal state matches the provided promise.
*/
resolve<T>(value: T | Thenable<T>): Thenable<T>;
}

export interface Thenable<R> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
then<TResult>(
onfulfilled?: (value: R) => TResult | Thenable<TResult>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onrejected?: (reason: any) => TResult | Thenable<TResult>
): Thenable<TResult>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
then<TResult>(onfulfilled?: (value: R) => TResult | Thenable<TResult>, onrejected?: (reason: any) => void): Thenable<TResult>;
}

export interface WorkspaceContextService {
resolveRelativePath(relativePath: string, resource: string): string;
}
Expand All @@ -99,7 +49,7 @@ export interface WorkspaceContextService {
* in case of an error, a displayable error string
*/
export interface SchemaRequestService {
(uri: string): Thenable<string>;
(uri: string): Promise<string>;
}

export interface SchemaConfiguration {
Expand Down Expand Up @@ -129,13 +79,13 @@ export interface CustomFormatterOptions {
export interface LanguageService {
configure(settings: LanguageSettings): void;
registerCustomSchemaProvider(schemaProvider: CustomSchemaProvider): void;
doComplete(document: TextDocument, position: Position, isKubernetes: boolean): Thenable<CompletionList>;
doValidation(document: TextDocument, isKubernetes: boolean): Thenable<Diagnostic[]>;
doHover(document: TextDocument, position: Position): Thenable<Hover | null>;
doComplete(document: TextDocument, position: Position, isKubernetes: boolean): Promise<CompletionList>;
doValidation(document: TextDocument, isKubernetes: boolean): Promise<Diagnostic[]>;
doHover(document: TextDocument, position: Position): Promise<Hover | null>;
findDocumentSymbols(document: TextDocument): SymbolInformation[];
findDocumentSymbols2(document: TextDocument): DocumentSymbol[];
findDefinition(document: TextDocument, position: Position, doc: JSONDocument): Thenable<DefinitionLink[]>;
findLinks(document: TextDocument): Thenable<DocumentLink[]>;
findDefinition(document: TextDocument, position: Position, doc: JSONDocument): Promise<DefinitionLink[]>;
findLinks(document: TextDocument): Promise<DocumentLink[]>;
resetSchema(uri: string): boolean;
doFormat(document: TextDocument, options: CustomFormatterOptions): TextEdit[];
addSchema(schemaID: string, schema: JSONSchema): void;
Expand Down
2 changes: 1 addition & 1 deletion test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const languageSettingsSetup = new ServiceSetup().withCompletion();
const languageService = configureLanguageService(languageSettingsSetup.languageSettings);

suite('Auto Completion Tests', () => {
function parseSetup(content: string, position): Thenable<CompletionList> {
function parseSetup(content: string, position): Promise<CompletionList> {
const testTextDocument = setupSchemaIDTextDocument(content);
return languageService.doComplete(testTextDocument, testTextDocument.positionAt(position), false);
}
Expand Down
2 changes: 1 addition & 1 deletion test/customTags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let languageService = configureLanguageService(languageSettingsSetup.languageSet

// Defines a Mocha test suite to group tests of similar kind together
suite('Custom Tag tests Tests', () => {
function parseSetup(content: string, customTags: string[]): Thenable<Diagnostic[]> {
function parseSetup(content: string, customTags: string[]): Promise<Diagnostic[]> {
const testTextDocument = setupTextDocument(content);
languageSettingsSetup.languageSettings.customTags = customTags;
languageService = configureLanguageService(languageSettingsSetup.languageSettings);
Expand Down
2 changes: 1 addition & 1 deletion test/defaultSnippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const languageService = configureLanguageService(languageSettingsSetup.languageS

suite('Default Snippet Tests', () => {
describe('Snippet Tests', function () {
function parseSetup(content: string, position: number): Thenable<CompletionList> {
function parseSetup(content: string, position: number): Promise<CompletionList> {
const testTextDocument = setupTextDocument(content);
return languageService.doComplete(testTextDocument, testTextDocument.positionAt(position), false);
}
Expand Down
2 changes: 1 addition & 1 deletion test/findLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const languageService = configureLanguageService(new ServiceSetup().languageSett

suite('FindDefintion Tests', () => {
describe('Jump to defintion', function () {
function findLinks(content: string): Thenable<DocumentLink[]> {
function findLinks(content: string): Promise<DocumentLink[]> {
const testTextDocument = setupTextDocument(content);
return languageService.findLinks(testTextDocument);
}
Expand Down
2 changes: 1 addition & 1 deletion test/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ suite('Hover Tests', () => {
});

describe('Hover', function () {
function parseSetup(content: string, position): Thenable<Hover> {
function parseSetup(content: string, position): Promise<Hover> {
const testTextDocument = setupSchemaIDTextDocument(content);
return languageService.doHover(testTextDocument, testTextDocument.positionAt(position));
}
Expand Down
6 changes: 3 additions & 3 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ languageService.configure(languageSettings);
suite('Kubernetes Integration Tests', () => {
// Tests for validator
describe('Yaml Validation with kubernetes', function () {
function parseSetup(content: string): Thenable<Diagnostic[]> {
function parseSetup(content: string): Promise<Diagnostic[]> {
const testTextDocument = setupTextDocument(content);
return languageService.doValidation(testTextDocument, true);
}
Expand Down Expand Up @@ -193,7 +193,7 @@ suite('Kubernetes Integration Tests', () => {

describe('yamlCompletion with kubernetes', function () {
describe('doComplete', function () {
function parseSetup(content: string, position): Thenable<CompletionList> {
function parseSetup(content: string, position): Promise<CompletionList> {
const testTextDocument = setupTextDocument(content);
return languageService.doComplete(testTextDocument, testTextDocument.positionAt(position), true);
}
Expand Down Expand Up @@ -283,7 +283,7 @@ suite('Kubernetes Integration Tests', () => {
});

describe('yamlHover with kubernetes', function () {
function parseSetup(content: string, offset: number): Thenable<Hover> {
function parseSetup(content: string, offset: number): Promise<Hover> {
const testTextDocument = setupTextDocument(content);
return languageService.doHover(testTextDocument, testTextDocument.positionAt(offset));
}
Expand Down
4 changes: 2 additions & 2 deletions test/mulipleDocuments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ const languageSettingsSetup = new ServiceSetup()
suite('Multiple Documents Validation Tests', () => {
// Tests for validator
describe('Multiple Documents Validation', function () {
function validatorSetup(content: string): Thenable<Diagnostic[]> {
function validatorSetup(content: string): Promise<Diagnostic[]> {
const testTextDocument = setupTextDocument(content);
const languageService = configureLanguageService(languageSettingsSetup.languageSettings);
return languageService.doValidation(testTextDocument, false);
}

function hoverSetup(content: string, position): Thenable<Hover> {
function hoverSetup(content: string, position): Promise<Hover> {
const testTextDocument = setupTextDocument(content);
const languageService = configureLanguageService(languageSettingsSetup.languageSettings);
return languageService.doHover(testTextDocument, testTextDocument.positionAt(position));
Expand Down
2 changes: 1 addition & 1 deletion test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const workspaceContext = {
},
};

const schemaRequestServiceForURL = (uri: string): Thenable<string> => {
const schemaRequestServiceForURL = (uri: string): Promise<string> => {
const headers = { 'Accept-Encoding': 'gzip, deflate' };
return xhr({ url: uri, followRedirects: 5, headers }).then(
(response) => {
Expand Down
2 changes: 1 addition & 1 deletion test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const languageService = configureLanguageService(languageSettingsSetup.languageS

// Defines a Mocha test suite to group tests of similar kind together
suite('Validation Tests', () => {
function parseSetup(content: string, isKubernetes = false): Thenable<Diagnostic[]> {
function parseSetup(content: string, isKubernetes = false): Promise<Diagnostic[]> {
const testTextDocument = setupSchemaIDTextDocument(content);
return languageService.doValidation(testTextDocument, isKubernetes);
}
Expand Down
Loading