From 124e05fd68ffe662fca5bc5fa9ee91dd9b52fdcb Mon Sep 17 00:00:00 2001 From: Paul van Brenk Date: Fri, 5 Aug 2016 16:44:43 -0700 Subject: [PATCH] PR Feedback --- src/compiler/core.ts | 24 +++++++++++++---------- src/compiler/diagnosticMessages.json | 14 ++++++------- src/compiler/types.ts | 1 - src/harness/fourslash.ts | 6 +++--- src/services/codefixes/codeFixProvider.ts | 7 ++----- src/services/codefixes/references.ts | 3 --- src/services/codefixes/superFixes.ts | 8 +++----- src/services/services.ts | 4 ++-- 8 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index b07c21fa4cb..835a8be3167 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1,4 +1,4 @@ -/// +/// /// @@ -115,15 +115,6 @@ namespace ts { return -1; } - export function firstOrUndefined(array: T[], predicate: (x: T) => boolean): T { - for (let i = 0, len = array.length; i < len; i++) { - if (predicate(array[i])) { - return array[i]; - } - } - return undefined; - } - export function indexOfAnyCharCode(text: string, charCodes: number[], start?: number): number { for (let i = start || 0, len = text.length; i < len; i++) { if (contains(charCodes, text.charCodeAt(i))) { @@ -229,6 +220,19 @@ namespace ts { return true; } + /** + * Returns the first element that matches the predicate, or undefined if none + * could be found. + */ + export function find(array: T[], predicate: (item: T) => boolean): T { + for (let i = 0, len = array.length; i < len; i++) { + if (predicate(array[i])) { + return array[i]; + } + } + return undefined; + } + /** * Returns the last element of an array if non-empty, undefined otherwise. */ diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 1dffc35c74c..34f5d481075 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3033,31 +3033,31 @@ "code": 17010 }, "Add missing 'super()' call.": { - "category": "CodeFix", + "category": "Message", "code": 90001 }, "Make 'super()' call the first statement in the constructor.": { - "category": "CodeFix", + "category": "Message", "code": 90002 }, "Change 'extends' to 'implements'": { - "category": "CodeFix", + "category": "Message", "code": 90003 }, "Remove unused identifiers": { - "category": "CodeFix", + "category": "Message", "code": 90004 }, "Implement interface on reference": { - "category": "CodeFix", + "category": "Message", "code": 90005 }, "Implement interface on class": { - "category": "CodeFix", + "category": "Message", "code": 90006 }, "Implement inherited abstract class": { - "category": "CodeFix", + "category": "Message", "code": 90007 } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index e31a4b0ace1..28ededebb0d 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2547,7 +2547,6 @@ namespace ts { Warning, Error, Message, - CodeFix, } export enum ModuleResolutionKind { diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 91597d7f593..19fa9ba5b4a 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -1,4 +1,4 @@ -// +// // Copyright (c) Microsoft Corporation. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -1879,11 +1879,11 @@ namespace FourSlash { this.raiseError("Errors expected."); } - if (diagnostics.length > 1 && !errorCode) { + if (diagnostics.length > 1 && errorCode !== undefined) { this.raiseError("When there's more than one error, you must specify the errror to fix."); } - const diagnostic = !errorCode ? diagnostics[0] : ts.firstOrUndefined(diagnostics, d => d.code == errorCode); + const diagnostic = !errorCode ? diagnostics[0] : ts.find(diagnostics, d => d.code == errorCode); const actual = this.languageService.getCodeFixesAtPosition(fileName, diagnostic.start, diagnostic.length, [`TS${diagnostic.code}`]); diff --git a/src/services/codefixes/codeFixProvider.ts b/src/services/codefixes/codeFixProvider.ts index ca877aef46a..71199af88fa 100644 --- a/src/services/codefixes/codeFixProvider.ts +++ b/src/services/codefixes/codeFixProvider.ts @@ -1,7 +1,6 @@ -/* @internal */ +/* @internal */ namespace ts { export interface CodeFix { - name: string; errorCodes: string[]; getCodeActions(context: CodeFixContext): CodeAction[]; } @@ -14,7 +13,7 @@ namespace ts { newLineCharacter: string; } - export namespace codeFix { + export namespace codefix { const codeFixes: Map = {}; export function registerCodeFix(action: CodeFix) { @@ -37,8 +36,6 @@ namespace ts { const fixes = codeFixes[context.errorCode]; let allActions: CodeAction[] = []; - Debug.assert(fixes && fixes.length > 0, "No fixes found for error: '${errorCode}'."); - forEach(fixes, f => { const actions = f.getCodeActions(context); if (actions && actions.length > 0) { diff --git a/src/services/codefixes/references.ts b/src/services/codefixes/references.ts index 3675d626678..a9ab7a98b33 100644 --- a/src/services/codefixes/references.ts +++ b/src/services/codefixes/references.ts @@ -1,6 +1,3 @@ /// /// /// -/// -/// -/// diff --git a/src/services/codefixes/superFixes.ts b/src/services/codefixes/superFixes.ts index 2c6a67de513..d06ba9df955 100644 --- a/src/services/codefixes/superFixes.ts +++ b/src/services/codefixes/superFixes.ts @@ -1,13 +1,12 @@ /* @internal */ -namespace ts.codeFix { +namespace ts.codefix { function getOpenBraceEnd(constructor: ConstructorDeclaration, sourceFile: SourceFile) { // First token is the open curly, this is where we want to put the 'super' call. return constructor.body.getFirstToken(sourceFile).getEnd(); } registerCodeFix({ - name: "AddMissingSuperCallFix", - errorCodes: ["TS2377"], + errorCodes: [`TS${Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call.code}`], getCodeActions: (context: CodeFixContext) => { const sourceFile = context.sourceFile; const token = getTokenAtPosition(sourceFile, context.span.start); @@ -22,8 +21,7 @@ namespace ts.codeFix { }); registerCodeFix({ - name: "MakeSuperCallTheFirstStatementInTheConstructor", - errorCodes: ["TS17009"], + errorCodes: [`TS${Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class.code}`], getCodeActions: (context: CodeFixContext) => { const sourceFile = context.sourceFile; diff --git a/src/services/services.ts b/src/services/services.ts index b189a6375b7..839fc4c298e 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1902,7 +1902,7 @@ namespace ts { } export function getSupportedCodeFixes() { - return codeFix.CodeFixProvider.getSupportedErrorCodes(); + return codefix.CodeFixProvider.getSupportedErrorCodes(); } // Cache host information about script Should be refreshed @@ -3041,7 +3041,7 @@ namespace ts { documentRegistry: DocumentRegistry = createDocumentRegistry(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames(), host.getCurrentDirectory())): LanguageService { const syntaxTreeCache: SyntaxTreeCache = new SyntaxTreeCache(host); - const codeFixProvider: codeFix.CodeFixProvider = new codeFix.CodeFixProvider(); + const codeFixProvider: codefix.CodeFixProvider = new codefix.CodeFixProvider(); let ruleProvider: formatting.RulesProvider; let program: Program; let lastProjectVersion: string;