From 92891a3096281a7e4857bdbfe704b6925f772a03 Mon Sep 17 00:00:00 2001 From: Qiming Zhao Date: Fri, 17 Apr 2020 16:00:08 +0800 Subject: [PATCH] support @ts-expect-error directive on tsserver v390 --- .../features/directiveCommentCompletions.ts | 15 ++- src/server/utils/api.ts | 105 +++++++++--------- 2 files changed, 67 insertions(+), 53 deletions(-) diff --git a/src/server/features/directiveCommentCompletions.ts b/src/server/features/directiveCommentCompletions.ts index 25f3b50..74a8577 100644 --- a/src/server/features/directiveCommentCompletions.ts +++ b/src/server/features/directiveCommentCompletions.ts @@ -2,18 +2,18 @@ * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ - import { CancellationToken, CompletionContext, CompletionItem, CompletionItemKind, CompletionList, Position, Range } from 'vscode-languageserver-protocol' import { TextDocument } from 'vscode-languageserver-textdocument' import { workspace } from 'coc.nvim' import { ITypeScriptServiceClient } from '../typescriptService' +import API from '../utils/api' interface Directive { readonly value: string readonly description: string } -const directives: Directive[] = [ +const tsDirectives: Directive[] = [ { value: '@ts-check', description: 'Enables semantic checking in a JavaScript file. Must be at the top of a file.' @@ -28,6 +28,14 @@ const directives: Directive[] = [ } ] +const tsDirectives390: Directive[] = [ + ...tsDirectives, + { + value: '@ts-expect-error', + description: 'Suppresses @ts-check errors on the next line of a file, expecting at least one to exist.' + } +] + export default class DirectiveCommentCompletionProvider { constructor(private readonly client: ITypeScriptServiceClient) { } @@ -50,6 +58,9 @@ export default class DirectiveCommentCompletionProvider { const prefix = line.slice(0, position.character) const match = prefix.match(/^\s*\/\/+\s?(@[a-zA-Z\-]*)?$/) if (match) { + const directives = this.client.apiVersion.gte(API.v390) + ? tsDirectives390 + : tsDirectives let items = directives.map(directive => { const item = CompletionItem.create(directive.value) item.kind = CompletionItemKind.Snippet diff --git a/src/server/utils/api.ts b/src/server/utils/api.ts index 8c097c8..2c3f29e 100644 --- a/src/server/utils/api.ts +++ b/src/server/utils/api.ts @@ -5,61 +5,64 @@ import * as semver from 'semver' export default class API { - private static fromSimpleString(value: string): API { - return new API(value, value) - } + private static fromSimpleString(value: string): API { + return new API(value, value) + } - public static readonly defaultVersion = API.fromSimpleString('1.0.0') - public static readonly v203 = API.fromSimpleString('2.0.3') - public static readonly v206 = API.fromSimpleString('2.0.6') - public static readonly v208 = API.fromSimpleString('2.0.8') - public static readonly v213 = API.fromSimpleString('2.1.3') - public static readonly v220 = API.fromSimpleString('2.2.0') - public static readonly v222 = API.fromSimpleString('2.2.2') - public static readonly v230 = API.fromSimpleString('2.3.0') - public static readonly v234 = API.fromSimpleString('2.3.4') - public static readonly v240 = API.fromSimpleString('2.4.0') - public static readonly v250 = API.fromSimpleString('2.5.0') - public static readonly v260 = API.fromSimpleString('2.6.0') - public static readonly v270 = API.fromSimpleString('2.7.0') - public static readonly v280 = API.fromSimpleString('2.8.0') - public static readonly v290 = API.fromSimpleString('2.9.0') - public static readonly v291 = API.fromSimpleString('2.9.1') - public static readonly v292 = API.fromSimpleString('2.9.2') - public static readonly v300 = API.fromSimpleString('3.0.0') - public static readonly v310 = API.fromSimpleString('3.1.0') - public static readonly v314 = API.fromSimpleString('3.1.4') - public static readonly v320 = API.fromSimpleString('3.2.0') - public static readonly v330 = API.fromSimpleString('3.3.0') - public static readonly v333 = API.fromSimpleString('3.3.3') - public static readonly v340 = API.fromSimpleString('3.4.0') - public static readonly v345 = API.fromSimpleString('3.4.5') - public static readonly v350 = API.fromSimpleString('3.5.0') + public static readonly defaultVersion = API.fromSimpleString('1.0.0') + public static readonly v203 = API.fromSimpleString('2.0.3') + public static readonly v206 = API.fromSimpleString('2.0.6') + public static readonly v208 = API.fromSimpleString('2.0.8') + public static readonly v213 = API.fromSimpleString('2.1.3') + public static readonly v220 = API.fromSimpleString('2.2.0') + public static readonly v222 = API.fromSimpleString('2.2.2') + public static readonly v230 = API.fromSimpleString('2.3.0') + public static readonly v234 = API.fromSimpleString('2.3.4') + public static readonly v240 = API.fromSimpleString('2.4.0') + public static readonly v250 = API.fromSimpleString('2.5.0') + public static readonly v260 = API.fromSimpleString('2.6.0') + public static readonly v270 = API.fromSimpleString('2.7.0') + public static readonly v280 = API.fromSimpleString('2.8.0') + public static readonly v290 = API.fromSimpleString('2.9.0') + public static readonly v291 = API.fromSimpleString('2.9.1') + public static readonly v292 = API.fromSimpleString('2.9.2') + public static readonly v300 = API.fromSimpleString('3.0.0') + public static readonly v310 = API.fromSimpleString('3.1.0') + public static readonly v314 = API.fromSimpleString('3.1.4') + public static readonly v320 = API.fromSimpleString('3.2.0') + public static readonly v330 = API.fromSimpleString('3.3.0') + public static readonly v333 = API.fromSimpleString('3.3.3') + public static readonly v340 = API.fromSimpleString('3.4.0') + public static readonly v345 = API.fromSimpleString('3.4.5') + public static readonly v350 = API.fromSimpleString('3.5.0') + public static readonly v380 = API.fromSimpleString('3.8.0') + public static readonly v381 = API.fromSimpleString('3.8.1') + public static readonly v390 = API.fromSimpleString('3.9.0') - public static fromVersionString(versionString: string): API { - let version = semver.valid(versionString) - if (!version) { - return new API('invalid version', '1.0.0') - } + public static fromVersionString(versionString: string): API { + let version = semver.valid(versionString) + if (!version) { + return new API('invalid version', '1.0.0') + } - // Cut off any prerelease tag since we sometimes consume those on purpose. - const index = versionString.indexOf('-') - if (index >= 0) { - version = version.substr(0, index) - } - return new API(versionString, version) - } + // Cut off any prerelease tag since we sometimes consume those on purpose. + const index = versionString.indexOf('-') + if (index >= 0) { + version = version.substr(0, index) + } + return new API(versionString, version) + } - private constructor( - public readonly versionString: string, - private readonly version: string - ) { } + private constructor( + public readonly versionString: string, + private readonly version: string + ) { } - public gte(other: API): boolean { - return semver.gte(this.version, other.version) - } + public gte(other: API): boolean { + return semver.gte(this.version, other.version) + } - public lt(other: API): boolean { - return !this.gte(other) - } + public lt(other: API): boolean { + return !this.gte(other) + } }