From 08fbcd8b80a63d7ad59bdeff8fdf47dcf0838d1c Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 10 Aug 2017 12:52:15 -0700 Subject: [PATCH] Fix many no-object-literal-type-assertion lint errors (#17278) * Fix many no-object-literal-type-assertion lint errors * Simple fixes * Use a union for FlowNode * PR feedback and remove remaining `id()` uses * Use a union for CodeBlock * Discriminate CodeBlock by CodeBlockKind --- src/compiler/binder.ts | 27 +----- src/compiler/checker.ts | 10 +- src/compiler/emitter.ts | 4 +- src/compiler/factory.ts | 4 +- src/compiler/parser.ts | 2 +- src/compiler/transformers/generators.ts | 94 +++++++++---------- src/compiler/types.ts | 20 ++-- src/harness/harness.ts | 2 +- src/harness/projectsRunner.ts | 4 +- src/harness/unittests/compileOnSave.ts | 14 +-- .../convertCompilerOptionsFromJson.ts | 42 ++++----- src/harness/unittests/matchFiles.ts | 20 ++-- src/harness/unittests/session.ts | 5 +- src/harness/unittests/typingsInstaller.ts | 14 +-- src/server/editorServices.ts | 15 +-- src/server/session.ts | 6 +- .../typingsInstaller/typingsInstaller.ts | 5 +- 17 files changed, 140 insertions(+), 148 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 45880f855fd..6ca2f402ac2 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -806,11 +806,7 @@ namespace ts { return antecedent; } setFlowNodeReferenced(antecedent); - return { - flags, - expression, - antecedent - }; + return { flags, expression, antecedent }; } function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode { @@ -818,31 +814,18 @@ namespace ts { return antecedent; } setFlowNodeReferenced(antecedent); - return { - flags: FlowFlags.SwitchClause, - switchStatement, - clauseStart, - clauseEnd, - antecedent - }; + return { flags: FlowFlags.SwitchClause, switchStatement, clauseStart, clauseEnd, antecedent }; } function createFlowAssignment(antecedent: FlowNode, node: Expression | VariableDeclaration | BindingElement): FlowNode { setFlowNodeReferenced(antecedent); - return { - flags: FlowFlags.Assignment, - antecedent, - node - }; + return { flags: FlowFlags.Assignment, antecedent, node }; } function createFlowArrayMutation(antecedent: FlowNode, node: CallExpression | BinaryExpression): FlowNode { setFlowNodeReferenced(antecedent); - return { - flags: FlowFlags.ArrayMutation, - antecedent, - node - }; + const res: FlowArrayMutation = { flags: FlowFlags.ArrayMutation, antecedent, node }; + return res; } function finishFlowLabel(flow: FlowLabel): FlowNode { diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a6736ac7c38..07c94bd746e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2172,7 +2172,7 @@ namespace ts { if (accessibleSymbolChain) { const hasAccessibleDeclarations = hasVisibleDeclarations(accessibleSymbolChain[0], shouldComputeAliasesToMakeVisible); if (!hasAccessibleDeclarations) { - return { + return { accessibility: SymbolAccessibility.NotAccessible, errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), errorModuleName: symbol !== initialSymbol ? symbolToString(symbol, enclosingDeclaration, SymbolFlags.Namespace) : undefined, @@ -2294,7 +2294,7 @@ namespace ts { const symbol = resolveName(enclosingDeclaration, firstIdentifier.escapedText, meaning, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); // Verify if the symbol is accessible - return (symbol && hasVisibleDeclarations(symbol, /*shouldComputeAliasToMakeVisible*/ true)) || { + return (symbol && hasVisibleDeclarations(symbol, /*shouldComputeAliasToMakeVisible*/ true)) || { accessibility: SymbolAccessibility.NotAccessible, errorSymbolName: getTextOfNode(firstIdentifier), errorNode: firstIdentifier @@ -6300,7 +6300,7 @@ namespace ts { return { kind: TypePredicateKind.This, type: getTypeFromTypeNode(node.type) - } as ThisTypePredicate; + }; } } @@ -8109,13 +8109,13 @@ namespace ts { parameterName: predicate.parameterName, parameterIndex: predicate.parameterIndex, type: instantiateType(predicate.type, mapper) - } as IdentifierTypePredicate; + }; } else { return { kind: TypePredicateKind.This, type: instantiateType(predicate.type, mapper) - } as ThisTypePredicate; + }; } } diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index d51f7ada3de..5444c618353 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1195,7 +1195,9 @@ namespace ts { if (!(getEmitFlags(node) & EmitFlags.NoIndentation)) { const dotRangeStart = node.expression.end; const dotRangeEnd = skipTrivia(currentSourceFile.text, node.expression.end) + 1; - const dotToken = { kind: SyntaxKind.DotToken, pos: dotRangeStart, end: dotRangeEnd }; + const dotToken = createToken(SyntaxKind.DotToken); + dotToken.pos = dotRangeStart; + dotToken.end = dotRangeEnd; indentBeforeDot = needsIndentation(node, node.expression, dotToken); indentAfterDot = needsIndentation(node, dotToken, node.name); } diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 47df0a9bdc2..daec1bce1e8 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -2586,7 +2586,7 @@ namespace ts { } export function addSyntheticLeadingComment(node: T, kind: SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia, text: string, hasTrailingNewLine?: boolean) { - return setSyntheticLeadingComments(node, append(getSyntheticLeadingComments(node), { kind, pos: -1, end: -1, hasTrailingNewLine, text })); + return setSyntheticLeadingComments(node, append(getSyntheticLeadingComments(node), { kind, pos: -1, end: -1, hasTrailingNewLine, text })); } export function getSyntheticTrailingComments(node: Node): SynthesizedComment[] | undefined { @@ -2600,7 +2600,7 @@ namespace ts { } export function addSyntheticTrailingComment(node: T, kind: SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia, text: string, hasTrailingNewLine?: boolean) { - return setSyntheticTrailingComments(node, append(getSyntheticTrailingComments(node), { kind, pos: -1, end: -1, hasTrailingNewLine, text })); + return setSyntheticTrailingComments(node, append(getSyntheticTrailingComments(node), { kind, pos: -1, end: -1, hasTrailingNewLine, text })); } /** diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index ee47771d619..3ec652b215e 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -6169,7 +6169,7 @@ namespace ts { export function parseIsolatedJSDocComment(content: string, start: number, length: number): { jsDoc: JSDoc, diagnostics: Diagnostic[] } | undefined { initializeState(content, ScriptTarget.Latest, /*_syntaxCursor:*/ undefined, ScriptKind.JS); - sourceFile = { languageVariant: LanguageVariant.Standard, text: content }; + sourceFile = { languageVariant: LanguageVariant.Standard, text: content }; // tslint:disable-line no-object-literal-type-assertion const jsDoc = parseJSDocCommentWorker(start, length); const diagnostics = parseDiagnostics; clearState(); diff --git a/src/compiler/transformers/generators.ts b/src/compiler/transformers/generators.ts index 5e2016ab590..f45147b526c 100644 --- a/src/compiler/transformers/generators.ts +++ b/src/compiler/transformers/generators.ts @@ -164,12 +164,11 @@ namespace ts { } // A generated code block - interface CodeBlock { - kind: CodeBlockKind; - } + type CodeBlock = | ExceptionBlock | LabeledBlock | SwitchBlock | LoopBlock | WithBlock; // a generated exception block, used for 'try' statements - interface ExceptionBlock extends CodeBlock { + interface ExceptionBlock { + kind: CodeBlockKind.Exception; state: ExceptionBlockState; startLabel: Label; catchVariable?: Identifier; @@ -179,27 +178,31 @@ namespace ts { } // A generated code that tracks the target for 'break' statements in a LabeledStatement. - interface LabeledBlock extends CodeBlock { + interface LabeledBlock { + kind: CodeBlockKind.Labeled; labelText: string; isScript: boolean; breakLabel: Label; } // a generated block that tracks the target for 'break' statements in a 'switch' statement - interface SwitchBlock extends CodeBlock { + interface SwitchBlock { + kind: CodeBlockKind.Switch; isScript: boolean; breakLabel: Label; } // a generated block that tracks the targets for 'break' and 'continue' statements, used for iteration statements - interface LoopBlock extends CodeBlock { + interface LoopBlock { + kind: CodeBlockKind.Loop; continueLabel: Label; isScript: boolean; breakLabel: Label; } // a generated block associated with a 'with' statement - interface WithBlock extends CodeBlock { + interface WithBlock { + kind: CodeBlockKind.With; expression: Identifier; startLabel: Label; endLabel: Label; @@ -2070,7 +2073,7 @@ namespace ts { const startLabel = defineLabel(); const endLabel = defineLabel(); markLabel(startLabel); - beginBlock({ + beginBlock({ kind: CodeBlockKind.With, expression, startLabel, @@ -2087,10 +2090,6 @@ namespace ts { markLabel(block.endLabel); } - function isWithBlock(block: CodeBlock): block is WithBlock { - return block.kind === CodeBlockKind.With; - } - /** * Begins a code block for a generated `try` statement. */ @@ -2098,7 +2097,7 @@ namespace ts { const startLabel = defineLabel(); const endLabel = defineLabel(); markLabel(startLabel); - beginBlock({ + beginBlock({ kind: CodeBlockKind.Exception, state: ExceptionBlockState.Try, startLabel, @@ -2188,10 +2187,6 @@ namespace ts { exception.state = ExceptionBlockState.Done; } - function isExceptionBlock(block: CodeBlock): block is ExceptionBlock { - return block.kind === CodeBlockKind.Exception; - } - /** * Begins a code block that supports `break` or `continue` statements that are defined in * the source tree and not from generated code. @@ -2199,7 +2194,7 @@ namespace ts { * @param labelText Names from containing labeled statements. */ function beginScriptLoopBlock(): void { - beginBlock({ + beginBlock({ kind: CodeBlockKind.Loop, isScript: true, breakLabel: -1, @@ -2217,7 +2212,7 @@ namespace ts { */ function beginLoopBlock(continueLabel: Label): Label { const breakLabel = defineLabel(); - beginBlock({ + beginBlock({ kind: CodeBlockKind.Loop, isScript: false, breakLabel, @@ -2245,7 +2240,7 @@ namespace ts { * */ function beginScriptSwitchBlock(): void { - beginBlock({ + beginBlock({ kind: CodeBlockKind.Switch, isScript: true, breakLabel: -1 @@ -2259,7 +2254,7 @@ namespace ts { */ function beginSwitchBlock(): Label { const breakLabel = defineLabel(); - beginBlock({ + beginBlock({ kind: CodeBlockKind.Switch, isScript: false, breakLabel, @@ -2280,7 +2275,7 @@ namespace ts { } function beginScriptLabeledBlock(labelText: string) { - beginBlock({ + beginBlock({ kind: CodeBlockKind.Labeled, isScript: true, labelText, @@ -2290,7 +2285,7 @@ namespace ts { function beginLabeledBlock(labelText: string) { const breakLabel = defineLabel(); - beginBlock({ + beginBlock({ kind: CodeBlockKind.Labeled, isScript: false, labelText, @@ -2878,34 +2873,37 @@ namespace ts { for (; blockIndex < blockActions.length && blockOffsets[blockIndex] <= operationIndex; blockIndex++) { const block = blocks[blockIndex]; const blockAction = blockActions[blockIndex]; - if (isExceptionBlock(block)) { - if (blockAction === BlockAction.Open) { - if (!exceptionBlockStack) { - exceptionBlockStack = []; - } + switch (block.kind) { + case CodeBlockKind.Exception: + if (blockAction === BlockAction.Open) { + if (!exceptionBlockStack) { + exceptionBlockStack = []; + } - if (!statements) { - statements = []; - } + if (!statements) { + statements = []; + } - exceptionBlockStack.push(currentExceptionBlock); - currentExceptionBlock = block; - } - else if (blockAction === BlockAction.Close) { - currentExceptionBlock = exceptionBlockStack.pop(); - } - } - else if (isWithBlock(block)) { - if (blockAction === BlockAction.Open) { - if (!withBlockStack) { - withBlockStack = []; + exceptionBlockStack.push(currentExceptionBlock); + currentExceptionBlock = block; } + else if (blockAction === BlockAction.Close) { + currentExceptionBlock = exceptionBlockStack.pop(); + } + break; + case CodeBlockKind.With: + if (blockAction === BlockAction.Open) { + if (!withBlockStack) { + withBlockStack = []; + } - withBlockStack.push(block); - } - else if (blockAction === BlockAction.Close) { - withBlockStack.pop(); - } + withBlockStack.push(block); + } + else if (blockAction === BlockAction.Close) { + withBlockStack.pop(); + } + break; + // default: do nothing } } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 76c0b640f45..8989f5784b8 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2176,16 +2176,18 @@ namespace ts { locked?: boolean; } - export interface AfterFinallyFlow extends FlowNode, FlowLock { + export interface AfterFinallyFlow extends FlowNodeBase, FlowLock { antecedent: FlowNode; } - export interface PreFinallyFlow extends FlowNode { + export interface PreFinallyFlow extends FlowNodeBase { antecedent: FlowNode; lock: FlowLock; } - export interface FlowNode { + export type FlowNode = + | AfterFinallyFlow | PreFinallyFlow | FlowStart | FlowLabel | FlowAssignment | FlowCondition | FlowSwitchClause | FlowArrayMutation; + export interface FlowNodeBase { flags: FlowFlags; id?: number; // Node id used by flow type cache in checker } @@ -2193,30 +2195,30 @@ namespace ts { // FlowStart represents the start of a control flow. For a function expression or arrow // function, the container property references the function (which in turn has a flowNode // property for the containing control flow). - export interface FlowStart extends FlowNode { + export interface FlowStart extends FlowNodeBase { container?: FunctionExpression | ArrowFunction | MethodDeclaration; } // FlowLabel represents a junction with multiple possible preceding control flows. - export interface FlowLabel extends FlowNode { + export interface FlowLabel extends FlowNodeBase { antecedents: FlowNode[]; } // FlowAssignment represents a node that assigns a value to a narrowable reference, // i.e. an identifier or a dotted name that starts with an identifier or 'this'. - export interface FlowAssignment extends FlowNode { + export interface FlowAssignment extends FlowNodeBase { node: Expression | VariableDeclaration | BindingElement; antecedent: FlowNode; } // FlowCondition represents a condition that is known to be true or false at the // node's location in the control flow. - export interface FlowCondition extends FlowNode { + export interface FlowCondition extends FlowNodeBase { expression: Expression; antecedent: FlowNode; } - export interface FlowSwitchClause extends FlowNode { + export interface FlowSwitchClause extends FlowNodeBase { switchStatement: SwitchStatement; clauseStart: number; // Start index of case/default clause range clauseEnd: number; // End index of case/default clause range @@ -2225,7 +2227,7 @@ namespace ts { // FlowArrayMutation represents a node potentially mutates an array, i.e. an // operation of the form 'x.push(value)', 'x.unshift(value)' or 'x[n] = value'. - export interface FlowArrayMutation extends FlowNode { + export interface FlowArrayMutation extends FlowNodeBase { node: CallExpression | BinaryExpression; antecedent: FlowNode; } diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 4e42dc41fd8..6c5b3ba4778 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -564,7 +564,7 @@ namespace Harness { } export let listFiles: typeof IO.listFiles = (path, spec?, options?) => { - options = options || <{ recursive?: boolean; }>{}; + options = options || {}; function filesInFolder(folder: string): string[] { let paths: string[] = []; diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index 7344c432b97..752767d9706 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -426,12 +426,12 @@ class ProjectRunner extends RunnerBase { compilerResult.program ? ts.filter(compilerResult.program.getSourceFiles(), sourceFile => !Harness.isDefaultLibraryFile(sourceFile.fileName)) : []), - sourceFile => { + (sourceFile): Harness.Compiler.TestFile => ({ unitName: ts.isRootedDiskPath(sourceFile.fileName) ? RunnerBase.removeFullPaths(sourceFile.fileName) : sourceFile.fileName, content: sourceFile.text - }); + })); return Harness.Compiler.getErrorBaseline(inputFiles, compilerResult.errors); } diff --git a/src/harness/unittests/compileOnSave.ts b/src/harness/unittests/compileOnSave.ts index 79e1f921dcb..31a882b19d6 100644 --- a/src/harness/unittests/compileOnSave.ts +++ b/src/harness/unittests/compileOnSave.ts @@ -518,18 +518,20 @@ namespace ts.projectSystem { }; const host = createServerHost([f], { newLine }); const session = createSession(host); - session.executeCommand({ + const openRequest: server.protocol.OpenRequest = { seq: 1, type: "request", - command: "open", + command: server.protocol.CommandTypes.Open, arguments: { file: f.path } - }); - session.executeCommand({ + }; + session.executeCommand(openRequest); + const emitFileRequest: server.protocol.CompileOnSaveEmitFileRequest = { seq: 2, type: "request", - command: "compileOnSaveEmitFile", + command: server.protocol.CommandTypes.CompileOnSaveEmitFile, arguments: { file: f.path } - }); + }; + session.executeCommand(emitFileRequest); const emitOutput = host.readFile(path + ts.Extension.Js); assert.equal(emitOutput, f.content + newLine, "content of emit output should be identical with the input + newline"); } diff --git a/src/harness/unittests/convertCompilerOptionsFromJson.ts b/src/harness/unittests/convertCompilerOptionsFromJson.ts index 4b2fad32d7b..14c0cb7347d 100644 --- a/src/harness/unittests/convertCompilerOptionsFromJson.ts +++ b/src/harness/unittests/convertCompilerOptionsFromJson.ts @@ -67,14 +67,14 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { module: ModuleKind.CommonJS, target: ScriptTarget.ES5, noImplicitAny: false, sourceMap: false, lib: ["lib.es5.d.ts", "lib.es2015.core.d.ts", "lib.es2015.symbol.d.ts"] }, - errors: [] + errors: [] } ); }); @@ -92,7 +92,7 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { module: ModuleKind.CommonJS, target: ScriptTarget.ES5, noImplicitAny: false, @@ -100,7 +100,7 @@ namespace ts { allowJs: false, lib: ["lib.es5.d.ts", "lib.es2015.core.d.ts", "lib.es2015.symbol.d.ts"] }, - errors: [] + errors: [] } ); }); @@ -117,7 +117,7 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { module: ModuleKind.CommonJS, target: ScriptTarget.ES5, noImplicitAny: false, @@ -146,7 +146,7 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { target: ScriptTarget.ES5, noImplicitAny: false, sourceMap: false, @@ -174,7 +174,7 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { target: ScriptTarget.ES5, noImplicitAny: false, sourceMap: false, @@ -201,7 +201,7 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { noImplicitAny: false, sourceMap: false, }, @@ -227,7 +227,7 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { noImplicitAny: false, sourceMap: false, }, @@ -255,7 +255,7 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { module: ModuleKind.CommonJS, target: ScriptTarget.ES5, noImplicitAny: false, @@ -286,7 +286,7 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { module: ModuleKind.CommonJS, target: ScriptTarget.ES5, noImplicitAny: false, @@ -317,7 +317,7 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { module: ModuleKind.CommonJS, target: ScriptTarget.ES5, noImplicitAny: false, @@ -348,7 +348,7 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { module: ModuleKind.CommonJS, target: ScriptTarget.ES5, noImplicitAny: false, @@ -379,7 +379,7 @@ namespace ts { } }, "tsconfig.json", { - compilerOptions: { + compilerOptions: { module: ModuleKind.CommonJS, target: ScriptTarget.ES5, noImplicitAny: false, @@ -415,8 +415,8 @@ namespace ts { it("Convert default tsconfig.json to compiler-options ", () => { assertCompilerOptions({}, "tsconfig.json", { - compilerOptions: {} as CompilerOptions, - errors: [] + compilerOptions: {}, + errors: [] } ); }); @@ -434,7 +434,7 @@ namespace ts { } }, "jsconfig.json", { - compilerOptions: { + compilerOptions: { allowJs: true, maxNodeModuleJsDepth: 2, allowSyntheticDefaultImports: true, @@ -445,7 +445,7 @@ namespace ts { sourceMap: false, lib: ["lib.es5.d.ts", "lib.es2015.core.d.ts", "lib.es2015.symbol.d.ts"] }, - errors: [] + errors: [] } ); }); @@ -463,7 +463,7 @@ namespace ts { } }, "jsconfig.json", { - compilerOptions: { + compilerOptions: { allowJs: false, maxNodeModuleJsDepth: 2, allowSyntheticDefaultImports: true, @@ -474,7 +474,7 @@ namespace ts { sourceMap: false, lib: ["lib.es5.d.ts", "lib.es2015.core.d.ts", "lib.es2015.symbol.d.ts"] }, - errors: [] + errors: [] } ); }); @@ -516,7 +516,7 @@ namespace ts { allowSyntheticDefaultImports: true, skipLibCheck: true }, - errors: [] + errors: [] } ); }); diff --git a/src/harness/unittests/matchFiles.ts b/src/harness/unittests/matchFiles.ts index 9e2a5883754..f2c2369ef37 100644 --- a/src/harness/unittests/matchFiles.ts +++ b/src/harness/unittests/matchFiles.ts @@ -110,23 +110,21 @@ namespace ts { } { const actual = ts.parseJsonConfigFileContent(json, host, basePath, existingOptions, configFileName, resolutionStack); - expected.errors = map(expected.errors, error => { - return { - category: error.category, - code: error.code, - file: undefined, - length: undefined, - messageText: error.messageText, - start: undefined, - }; - }); + expected.errors = expected.errors.map(error => ({ + category: error.category, + code: error.code, + file: undefined, + length: undefined, + messageText: error.messageText, + start: undefined, + })); assertParsed(actual, expected); } } function createDiagnosticForConfigFile(json: any, start: number, length: number, diagnosticMessage: DiagnosticMessage, arg0: string) { const text = JSON.stringify(json); - const file = { + const file = { // tslint:disable-line no-object-literal-type-assertion fileName: caseInsensitiveTsconfigPath, kind: SyntaxKind.SourceFile, text diff --git a/src/harness/unittests/session.ts b/src/harness/unittests/session.ts index 1ce5792a81b..1f79213bee8 100644 --- a/src/harness/unittests/session.ts +++ b/src/harness/unittests/session.ts @@ -81,14 +81,15 @@ namespace ts.server { session.executeCommand(req); - expect(lastSent).to.deep.equal({ + const expected: protocol.Response = { command: CommandNames.Unknown, type: "response", seq: 0, message: "Unrecognized JSON command: foobar", request_seq: 0, success: false - }); + }; + expect(lastSent).to.deep.equal(expected); }); it("should return a tuple containing the response and if a response is required on success", () => { const req: protocol.ConfigureRequest = { diff --git a/src/harness/unittests/typingsInstaller.ts b/src/harness/unittests/typingsInstaller.ts index 92ecd5e31b8..6a6978254c1 100644 --- a/src/harness/unittests/typingsInstaller.ts +++ b/src/harness/unittests/typingsInstaller.ts @@ -935,25 +935,26 @@ namespace ts.projectSystem { import * as cmd from "commander ` }; - session.executeCommand({ + const openRequest: server.protocol.OpenRequest = { seq: 1, type: "request", - command: "open", + command: server.protocol.CommandTypes.Open, arguments: { file: f.path, fileContent: f.content } - }); + }; + session.executeCommand(openRequest); const projectService = session.getProjectService(); checkNumberOfProjects(projectService, { inferredProjects: 1 }); const proj = projectService.inferredProjects[0]; const version1 = proj.getCachedUnresolvedImportsPerFile_TestOnly().getVersion(); // make a change that should not affect the structure of the program - session.executeCommand({ + const changeRequest: server.protocol.ChangeRequest = { seq: 2, type: "request", - command: "change", + command: server.protocol.CommandTypes.Change, arguments: { file: f.path, insertString: "\nlet x = 1;", @@ -962,7 +963,8 @@ namespace ts.projectSystem { endLine: 2, endOffset: 0 } - }); + }; + session.executeCommand(changeRequest); host.checkTimeoutQueueLength(1); host.runQueuedTimeoutCallbacks(); const version2 = proj.getCachedUnresolvedImportsPerFile_TestOnly().getVersion(); diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index a17ce1d5787..b03959d12a3 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -441,10 +441,11 @@ namespace ts.server { if (!this.eventHandler) { return; } - this.eventHandler({ + const event: ProjectLanguageServiceStateEvent = { eventName: ProjectLanguageServiceStateEvent, data: { project, languageServiceEnabled } - }); + }; + this.eventHandler(event); } updateTypingsForProject(response: SetTypings | InvalidateCachedTypings): void { @@ -602,10 +603,11 @@ namespace ts.server { } for (const openFile of this.openFiles) { - this.eventHandler({ + const event: ContextEvent = { eventName: ContextEvent, data: { project: openFile.getDefaultProject(), fileName: openFile.fileName } - }); + }; + this.eventHandler(event); } } @@ -1105,10 +1107,11 @@ namespace ts.server { return; } - this.eventHandler({ + const event: ConfigFileDiagEvent = { eventName: ConfigFileDiagEvent, data: { configFileName, diagnostics: diagnostics || emptyArray, triggerFile } - }); + }; + this.eventHandler(event); } private createAndAddConfiguredProject(configFileName: NormalizedPath, projectOptions: ProjectOptions, configFileErrors: ReadonlyArray, clientFileName?: string) { diff --git a/src/server/session.ts b/src/server/session.ts index 0b746e4090a..a797ffc36da 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -540,7 +540,7 @@ namespace ts.server { } private convertToDiagnosticsWithLinePositionFromDiagnosticFile(diagnostics: ReadonlyArray): protocol.DiagnosticWithLinePosition[] { - return diagnostics.map(d => { + return diagnostics.map(d => ({ message: flattenDiagnosticMessageText(d.messageText, this.host.newLine), start: d.start, length: d.length, @@ -548,7 +548,7 @@ namespace ts.server { code: d.code, startLocation: d.file && convertToLocation(getLineAndCharacterOfPosition(d.file, d.start)), endLocation: d.file && convertToLocation(getLineAndCharacterOfPosition(d.file, d.start + d.length)) - }); + })); } private getCompilerOptionsDiagnostics(args: protocol.CompilerOptionsDiagnosticsRequestArgs) { @@ -829,7 +829,7 @@ namespace ts.server { return renameLocations.map(location => { const locationScriptInfo = project.getScriptInfo(location.fileName); - return { + return { file: location.fileName, start: locationScriptInfo.positionToLineOffset(location.textSpan.start), end: locationScriptInfo.positionToLineOffset(textSpanEnd(location.textSpan)), diff --git a/src/server/typingsInstaller/typingsInstaller.ts b/src/server/typingsInstaller/typingsInstaller.ts index 8a3840fd984..df2b6916e4b 100644 --- a/src/server/typingsInstaller/typingsInstaller.ts +++ b/src/server/typingsInstaller/typingsInstaller.ts @@ -356,14 +356,15 @@ namespace ts.server.typingsInstaller { this.sendResponse(this.createSetTypings(req, currentlyCachedTypings.concat(installedTypingFiles))); } finally { - this.sendResponse({ + const response: EndInstallTypes = { kind: EventEndInstallTypes, eventId: requestId, projectName: req.projectName, packagesToInstall: scopedTypings, installSuccess: ok, typingsInstallerVersion: ts.version // qualified explicitly to prevent occasional shadowing - }); + }; + this.sendResponse(response); } }); }