From 08c42b31785b43e89f3d0c485a0f20f6becfbc83 Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Tue, 18 Jan 2022 17:35:05 +0000 Subject: [PATCH] [analysis_server] Fix some minor LSP codegen issues Change-Id: Ie6171416cca75ec623d600ddea2e1efefd8156ec Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/228645 Reviewed-by: Brian Wilkerson Commit-Queue: Brian Wilkerson --- .../lib/lsp_protocol/protocol_generated.dart | 58 ++++++++++--------- .../tool/lsp_spec/codegen_dart.dart | 2 + .../tool/lsp_spec/typescript_parser.dart | 19 +++--- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/pkg/analysis_server/lib/lsp_protocol/protocol_generated.dart b/pkg/analysis_server/lib/lsp_protocol/protocol_generated.dart index e8de391aa45..3222ce81011 100644 --- a/pkg/analysis_server/lib/lsp_protocol/protocol_generated.dart +++ b/pkg/analysis_server/lib/lsp_protocol/protocol_generated.dart @@ -628,7 +628,7 @@ class CallHierarchyItem implements ToJsonable { final kind = SymbolKind.fromJson(kindJson as int); final tagsJson = json['tags']; final tags = (tagsJson as List?) - ?.map((item) => SymbolTag.fromJson(item as num)) + ?.map((item) => SymbolTag.fromJson(item as int)) .toList(); final detailJson = json['detail']; final detail = detailJson as String?; @@ -5528,7 +5528,7 @@ class CompletionClientCapabilitiesInsertTextModeSupport implements ToJsonable { Map json) { final valueSetJson = json['valueSet']; final valueSet = (valueSetJson as List) - .map((item) => InsertTextMode.fromJson(item as num)) + .map((item) => InsertTextMode.fromJson(item as int)) .toList(); return CompletionClientCapabilitiesInsertTextModeSupport( valueSet: valueSet); @@ -5743,7 +5743,7 @@ class CompletionContext implements ToJsonable { CompletionContext({required this.triggerKind, this.triggerCharacter}); static CompletionContext fromJson(Map json) { final triggerKindJson = json['triggerKind']; - final triggerKind = CompletionTriggerKind.fromJson(triggerKindJson as num); + final triggerKind = CompletionTriggerKind.fromJson(triggerKindJson as int); final triggerCharacterJson = json['triggerCharacter']; final triggerCharacter = triggerCharacterJson as String?; return CompletionContext( @@ -5879,7 +5879,7 @@ class CompletionItem implements ToJsonable { : null; final insertTextModeJson = json['insertTextMode']; final insertTextMode = insertTextModeJson != null - ? InsertTextMode.fromJson(insertTextModeJson as num) + ? InsertTextMode.fromJson(insertTextModeJson as int) : null; final textEditJson = json['textEdit']; final textEdit = textEditJson == null @@ -7041,7 +7041,7 @@ class CompletionTriggerKind { const CompletionTriggerKind._(this._value); const CompletionTriggerKind.fromJson(this._value); - final num _value; + final int _value; static bool canParse(Object? obj, LspJsonReporter reporter) { switch (obj) { @@ -8606,7 +8606,7 @@ class Diagnostic implements ToJsonable { final range = Range.fromJson(rangeJson as Map); final severityJson = json['severity']; final severity = severityJson != null - ? DiagnosticSeverity.fromJson(severityJson as num) + ? DiagnosticSeverity.fromJson(severityJson as int) : null; final codeJson = json['code']; final code = codeJson as String?; @@ -8620,7 +8620,7 @@ class Diagnostic implements ToJsonable { final message = messageJson as String; final tagsJson = json['tags']; final tags = (tagsJson as List?) - ?.map((item) => DiagnosticTag.fromJson(item as num)) + ?.map((item) => DiagnosticTag.fromJson(item as int)) .toList(); final relatedInformationJson = json['relatedInformation']; final relatedInformation = (relatedInformationJson as List?) @@ -8953,10 +8953,10 @@ class DiagnosticSeverity { const DiagnosticSeverity(this._value); const DiagnosticSeverity.fromJson(this._value); - final num _value; + final int _value; static bool canParse(Object? obj, LspJsonReporter reporter) { - return obj is num; + return obj is int; } /// Reports an error. @@ -8988,10 +8988,10 @@ class DiagnosticTag { const DiagnosticTag(this._value); const DiagnosticTag.fromJson(this._value); - final num _value; + final int _value; static bool canParse(Object? obj, LspJsonReporter reporter) { - return obj is num; + return obj is int; } /// Unused or unnecessary code. @@ -12384,7 +12384,7 @@ class DocumentSymbol implements ToJsonable { final kind = SymbolKind.fromJson(kindJson as int); final tagsJson = json['tags']; final tags = (tagsJson as List?) - ?.map((item) => SymbolTag.fromJson(item as num)) + ?.map((item) => SymbolTag.fromJson(item as int)) .toList(); final deprecatedJson = json['deprecated']; final deprecated = deprecatedJson as bool?; @@ -12864,7 +12864,7 @@ class DocumentSymbolClientCapabilitiesTagSupport implements ToJsonable { Map json) { final valueSetJson = json['valueSet']; final valueSet = (valueSetJson as List) - .map((item) => SymbolTag.fromJson(item as num)) + .map((item) => SymbolTag.fromJson(item as int)) .toList(); return DocumentSymbolClientCapabilitiesTagSupport(valueSet: valueSet); } @@ -16348,7 +16348,8 @@ class InitializeParams implements WorkDoneProgressParams, ToJsonable { final trace = obj['trace']; if (trace != null && !((trace == 'off' || trace == 'message' || trace == 'verbose'))) { - reporter.reportError('must be of type String'); + reporter.reportError( + 'must be one of the literals \'off\', \'message\', \'verbose\''); return false; } } finally { @@ -16870,10 +16871,10 @@ class InsertTextMode { const InsertTextMode(this._value); const InsertTextMode.fromJson(this._value); - final num _value; + final int _value; static bool canParse(Object? obj, LspJsonReporter reporter) { - return obj is num; + return obj is int; } /// The insertion or replace strings is taken as it is. If the value is multi @@ -19567,10 +19568,10 @@ class PrepareSupportDefaultBehavior { const PrepareSupportDefaultBehavior(this._value); const PrepareSupportDefaultBehavior.fromJson(this._value); - final num _value; + final int _value; static bool canParse(Object? obj, LspJsonReporter reporter) { - return obj is num; + return obj is int; } /// The client's default behavior is to select the identifier according the to @@ -19834,7 +19835,7 @@ class PublishDiagnosticsClientCapabilitiesTagSupport implements ToJsonable { Map json) { final valueSetJson = json['valueSet']; final valueSet = (valueSetJson as List) - .map((item) => DiagnosticTag.fromJson(item as num)) + .map((item) => DiagnosticTag.fromJson(item as int)) .toList(); return PublishDiagnosticsClientCapabilitiesTagSupport(valueSet: valueSet); } @@ -20906,7 +20907,7 @@ class RenameClientCapabilities implements ToJsonable { final prepareSupportDefaultBehavior = prepareSupportDefaultBehaviorJson != null ? PrepareSupportDefaultBehavior.fromJson( - prepareSupportDefaultBehaviorJson as num) + prepareSupportDefaultBehaviorJson as int) : null; final honorsChangeAnnotationsJson = json['honorsChangeAnnotations']; final honorsChangeAnnotations = honorsChangeAnnotationsJson as bool?; @@ -26030,7 +26031,8 @@ class SetTraceParams implements ToJsonable { return false; } if (!((value == 'off' || value == 'message' || value == 'verbose'))) { - reporter.reportError('must be of type String'); + reporter.reportError( + 'must be one of the literals \'off\', \'message\', \'verbose\''); return false; } } finally { @@ -27116,7 +27118,7 @@ class SignatureHelpContext implements ToJsonable { static SignatureHelpContext fromJson(Map json) { final triggerKindJson = json['triggerKind']; final triggerKind = - SignatureHelpTriggerKind.fromJson(triggerKindJson as num); + SignatureHelpTriggerKind.fromJson(triggerKindJson as int); final triggerCharacterJson = json['triggerCharacter']; final triggerCharacter = triggerCharacterJson as String?; final isRetriggerJson = json['isRetrigger']; @@ -27683,10 +27685,10 @@ class SignatureHelpTriggerKind { const SignatureHelpTriggerKind(this._value); const SignatureHelpTriggerKind.fromJson(this._value); - final num _value; + final int _value; static bool canParse(Object? obj, LspJsonReporter reporter) { - return obj is num; + return obj is int; } /// Signature help was invoked manually by the user or by a command. @@ -27972,7 +27974,7 @@ class SymbolInformation implements ToJsonable { final kind = SymbolKind.fromJson(kindJson as int); final tagsJson = json['tags']; final tags = (tagsJson as List?) - ?.map((item) => SymbolTag.fromJson(item as num)) + ?.map((item) => SymbolTag.fromJson(item as int)) .toList(); final deprecatedJson = json['deprecated']; final deprecated = deprecatedJson as bool?; @@ -28209,10 +28211,10 @@ class SymbolTag { const SymbolTag(this._value); const SymbolTag.fromJson(this._value); - final num _value; + final int _value; static bool canParse(Object? obj, LspJsonReporter reporter) { - return obj is num; + return obj is int; } /// Render a symbol as obsolete, usually using a strike-out. @@ -32991,7 +32993,7 @@ class WorkspaceSymbolClientCapabilitiesTagSupport implements ToJsonable { Map json) { final valueSetJson = json['valueSet']; final valueSet = (valueSetJson as List) - .map((item) => SymbolTag.fromJson(item as num)) + .map((item) => SymbolTag.fromJson(item as int)) .toList(); return WorkspaceSymbolClientCapabilitiesTagSupport(valueSet: valueSet); } diff --git a/pkg/analysis_server/tool/lsp_spec/codegen_dart.dart b/pkg/analysis_server/tool/lsp_spec/codegen_dart.dart index 5383d50bece..ba060b9aeec 100644 --- a/pkg/analysis_server/tool/lsp_spec/codegen_dart.dart +++ b/pkg/analysis_server/tool/lsp_spec/codegen_dart.dart @@ -131,6 +131,8 @@ List _getSortedUnique(List items) { } String _getTypeCheckFailureMessage(TypeBase type) { + type = resolveTypeAlias(type); + if (type is LiteralType) { return 'must be the literal ${type.literal}'; } else if (type is LiteralUnionType) { diff --git a/pkg/analysis_server/tool/lsp_spec/typescript_parser.dart b/pkg/analysis_server/tool/lsp_spec/typescript_parser.dart index 28bf1c59d78..d897d0bbe9c 100644 --- a/pkg/analysis_server/tool/lsp_spec/typescript_parser.dart +++ b/pkg/analysis_server/tool/lsp_spec/typescript_parser.dart @@ -42,7 +42,8 @@ List parseString(String input) { return parser.parse(); } -TypeBase typeOfLiteral(TokenType tokenType) { +TypeBase typeOfLiteral(Token token) { + final tokenType = token.type; final typeName = tokenType == TokenType.STRING ? 'string' : tokenType == TokenType.NUMBER @@ -178,7 +179,7 @@ class Interface extends AstNode { } class LiteralType extends TypeBase { - final Type type; + final TypeBase type; final String literal; LiteralType(this.type, this.literal); @@ -278,7 +279,7 @@ class Parser { final value = _match([TokenType.EQUAL]) ? _advance() : null; if (type == null && value != null) { - type = typeOfLiteral(value.type); + type = typeOfLiteral(value); } _consume(TokenType.SEMI_COLON, 'Expected ;'); @@ -339,7 +340,7 @@ class Parser { final value = _match([TokenType.EQUAL]) ? _advance() : null; if (type == null && value != null) { - type = typeOfLiteral(value.type); + type = typeOfLiteral(value); } return Const(leadingComment, name, type!, value!); } @@ -601,17 +602,13 @@ class Parser { // Some types are in (parens), so we just parse the contents as a nested type. type = _type(containerName, fieldName); _consume(TokenType.RIGHT_PAREN, 'Expected )'); - } else if (_check(TokenType.STRING)) { + } else if (_check(TokenType.STRING) || _check(TokenType.NUMBER)) { final token = _advance(); - // In TS and the spec, literal strings can be types: + // In TS and the spec, literal values can be types: // export const PlainText: 'plaintext' = 'plaintext'; // trace?: 'off' | 'messages' | 'verbose'; - type = LiteralType(Type.identifier('string'), token.lexeme); - } else if (_check(TokenType.NUMBER)) { - final token = _advance(); - // In TS and the spec, literal numbers can be types: // export const Invoked: 1 = 1; - type = LiteralType(Type.identifier('number'), token.lexeme); + type = LiteralType(typeOfLiteral(token), token.lexeme); } else if (_match([TokenType.LEFT_BRACKET])) { // Tuples will just be converted to List/Array. final tupleElementTypes = [];