Skip to content

Commit 31f4930

Browse files
author
Andy Hanson
committed
Fix many no-object-literal-type-assertion lint errors
1 parent 08a57d8 commit 31f4930

17 files changed

+92
-103
lines changed

src/compiler/binder.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -801,43 +801,26 @@ namespace ts {
801801
return antecedent;
802802
}
803803
setFlowNodeReferenced(antecedent);
804-
return <FlowCondition>{
805-
flags,
806-
expression,
807-
antecedent
808-
};
804+
return id<FlowCondition>({ flags, expression, antecedent });
809805
}
810806

811807
function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode {
812808
if (!isNarrowingExpression(switchStatement.expression)) {
813809
return antecedent;
814810
}
815811
setFlowNodeReferenced(antecedent);
816-
return <FlowSwitchClause>{
817-
flags: FlowFlags.SwitchClause,
818-
switchStatement,
819-
clauseStart,
820-
clauseEnd,
821-
antecedent
822-
};
812+
return id<FlowSwitchClause>({ flags: FlowFlags.SwitchClause, switchStatement, clauseStart, clauseEnd, antecedent });
823813
}
824814

825815
function createFlowAssignment(antecedent: FlowNode, node: Expression | VariableDeclaration | BindingElement): FlowNode {
826816
setFlowNodeReferenced(antecedent);
827-
return <FlowAssignment>{
828-
flags: FlowFlags.Assignment,
829-
antecedent,
830-
node
831-
};
817+
return id<FlowAssignment>({ flags: FlowFlags.Assignment, antecedent, node });
832818
}
833819

834820
function createFlowArrayMutation(antecedent: FlowNode, node: CallExpression | BinaryExpression): FlowNode {
835821
setFlowNodeReferenced(antecedent);
836-
return <FlowArrayMutation>{
837-
flags: FlowFlags.ArrayMutation,
838-
antecedent,
839-
node
840-
};
822+
const res: FlowArrayMutation = { flags: FlowFlags.ArrayMutation, antecedent, node };
823+
return res;
841824
}
842825

843826
function finishFlowLabel(flow: FlowLabel): FlowNode {

src/compiler/checker.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,7 +2144,7 @@ namespace ts {
21442144
if (accessibleSymbolChain) {
21452145
const hasAccessibleDeclarations = hasVisibleDeclarations(accessibleSymbolChain[0], shouldComputeAliasesToMakeVisible);
21462146
if (!hasAccessibleDeclarations) {
2147-
return <SymbolAccessibilityResult>{
2147+
return {
21482148
accessibility: SymbolAccessibility.NotAccessible,
21492149
errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning),
21502150
errorModuleName: symbol !== initialSymbol ? symbolToString(symbol, enclosingDeclaration, SymbolFlags.Namespace) : undefined,
@@ -2266,7 +2266,7 @@ namespace ts {
22662266
const symbol = resolveName(enclosingDeclaration, (<Identifier>firstIdentifier).text, meaning, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined);
22672267

22682268
// Verify if the symbol is accessible
2269-
return (symbol && hasVisibleDeclarations(symbol, /*shouldComputeAliasToMakeVisible*/ true)) || <SymbolVisibilityResult>{
2269+
return (symbol && hasVisibleDeclarations(symbol, /*shouldComputeAliasToMakeVisible*/ true)) || {
22702270
accessibility: SymbolAccessibility.NotAccessible,
22712271
errorSymbolName: getTextOfNode(firstIdentifier),
22722272
errorNode: firstIdentifier
@@ -6254,16 +6254,16 @@ namespace ts {
62546254
const parameterName = node.parameterName as Identifier;
62556255
return {
62566256
kind: TypePredicateKind.Identifier,
6257-
parameterName: parameterName ? parameterName.text : undefined,
6257+
parameterName: parameterName ? <string>parameterName.text : undefined,
62586258
parameterIndex: parameterName ? getTypePredicateParameterIndex((node.parent as SignatureDeclaration).parameters, parameterName) : undefined,
62596259
type: getTypeFromTypeNode(node.type)
6260-
} as IdentifierTypePredicate;
6260+
};
62616261
}
62626262
else {
62636263
return {
62646264
kind: TypePredicateKind.This,
62656265
type: getTypeFromTypeNode(node.type)
6266-
} as ThisTypePredicate;
6266+
};
62676267
}
62686268
}
62696269

@@ -8015,13 +8015,13 @@ namespace ts {
80158015
parameterName: predicate.parameterName,
80168016
parameterIndex: predicate.parameterIndex,
80178017
type: instantiateType(predicate.type, mapper)
8018-
} as IdentifierTypePredicate;
8018+
};
80198019
}
80208020
else {
80218021
return {
80228022
kind: TypePredicateKind.This,
80238023
type: instantiateType(predicate.type, mapper)
8024-
} as ThisTypePredicate;
8024+
};
80258025
}
80268026
}
80278027

src/compiler/core.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,4 +2543,12 @@ namespace ts {
25432543
export function isCheckJsEnabledForFile(sourceFile: SourceFile, compilerOptions: CompilerOptions) {
25442544
return sourceFile.checkJsDirective ? sourceFile.checkJsDirective.enabled : compilerOptions.checkJs;
25452545
}
2546+
2547+
/**
2548+
* The identity function.
2549+
* Also useful to provide a contextual type for an object literal.
2550+
*/
2551+
export function id<T>(value: T) {
2552+
return value;
2553+
}
25462554
}

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ namespace ts {
11951195
if (!(getEmitFlags(node) & EmitFlags.NoIndentation)) {
11961196
const dotRangeStart = node.expression.end;
11971197
const dotRangeEnd = skipTrivia(currentSourceFile.text, node.expression.end) + 1;
1198-
const dotToken = <Node>{ kind: SyntaxKind.DotToken, pos: dotRangeStart, end: dotRangeEnd };
1198+
const dotToken = <Node>{ kind: SyntaxKind.DotToken, pos: dotRangeStart, end: dotRangeEnd }; // tslint:disable-line no-object-literal-type-assertion
11991199
indentBeforeDot = needsIndentation(node, node.expression, dotToken);
12001200
indentAfterDot = needsIndentation(node, dotToken, node.name);
12011201
}

src/compiler/factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,7 +2586,7 @@ namespace ts {
25862586
}
25872587

25882588
export function addSyntheticLeadingComment<T extends Node>(node: T, kind: SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia, text: string, hasTrailingNewLine?: boolean) {
2589-
return setSyntheticLeadingComments(node, append(getSyntheticLeadingComments(node), <SynthesizedComment>{ kind, pos: -1, end: -1, hasTrailingNewLine, text }));
2589+
return setSyntheticLeadingComments<T>(node, append(getSyntheticLeadingComments(node), { kind, pos: -1, end: -1, hasTrailingNewLine, text }));
25902590
}
25912591

25922592
export function getSyntheticTrailingComments(node: Node): SynthesizedComment[] | undefined {
@@ -2600,7 +2600,7 @@ namespace ts {
26002600
}
26012601

26022602
export function addSyntheticTrailingComment<T extends Node>(node: T, kind: SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia, text: string, hasTrailingNewLine?: boolean) {
2603-
return setSyntheticTrailingComments(node, append(getSyntheticTrailingComments(node), <SynthesizedComment>{ kind, pos: -1, end: -1, hasTrailingNewLine, text }));
2603+
return setSyntheticTrailingComments<T>(node, append(getSyntheticTrailingComments(node), { kind, pos: -1, end: -1, hasTrailingNewLine, text }));
26042604
}
26052605

26062606
/**

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6118,7 +6118,7 @@ namespace ts {
61186118

61196119
export function parseIsolatedJSDocComment(content: string, start: number, length: number) {
61206120
initializeState(content, ScriptTarget.Latest, /*_syntaxCursor:*/ undefined, ScriptKind.JS);
6121-
sourceFile = <SourceFile>{ languageVariant: LanguageVariant.Standard, text: content };
6121+
sourceFile = <SourceFile>{ languageVariant: LanguageVariant.Standard, text: content }; // tslint:disable-line no-object-literal-type-assertion
61226122
const jsDoc = parseJSDocCommentWorker(start, length);
61236123
const diagnostics = parseDiagnostics;
61246124
clearState();

src/compiler/transformers/generators.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,7 @@ namespace ts {
20152015
*
20162016
* @param block Information about the block.
20172017
*/
2018-
function beginBlock(block: CodeBlock): number {
2018+
function beginBlock<T extends CodeBlock>(block: T): number {
20192019
if (!blocks) {
20202020
blocks = [];
20212021
blockActions = [];
@@ -2070,7 +2070,7 @@ namespace ts {
20702070
const startLabel = defineLabel();
20712071
const endLabel = defineLabel();
20722072
markLabel(startLabel);
2073-
beginBlock(<WithBlock>{
2073+
beginBlock<WithBlock>({
20742074
kind: CodeBlockKind.With,
20752075
expression,
20762076
startLabel,
@@ -2098,7 +2098,7 @@ namespace ts {
20982098
const startLabel = defineLabel();
20992099
const endLabel = defineLabel();
21002100
markLabel(startLabel);
2101-
beginBlock(<ExceptionBlock>{
2101+
beginBlock<ExceptionBlock>({
21022102
kind: CodeBlockKind.Exception,
21032103
state: ExceptionBlockState.Try,
21042104
startLabel,
@@ -2199,7 +2199,7 @@ namespace ts {
21992199
* @param labelText Names from containing labeled statements.
22002200
*/
22012201
function beginScriptLoopBlock(): void {
2202-
beginBlock(<LoopBlock>{
2202+
beginBlock<LoopBlock>({
22032203
kind: CodeBlockKind.Loop,
22042204
isScript: true,
22052205
breakLabel: -1,
@@ -2217,7 +2217,7 @@ namespace ts {
22172217
*/
22182218
function beginLoopBlock(continueLabel: Label): Label {
22192219
const breakLabel = defineLabel();
2220-
beginBlock(<LoopBlock>{
2220+
beginBlock<LoopBlock>({
22212221
kind: CodeBlockKind.Loop,
22222222
isScript: false,
22232223
breakLabel,
@@ -2245,7 +2245,7 @@ namespace ts {
22452245
*
22462246
*/
22472247
function beginScriptSwitchBlock(): void {
2248-
beginBlock(<SwitchBlock>{
2248+
beginBlock<SwitchBlock>({
22492249
kind: CodeBlockKind.Switch,
22502250
isScript: true,
22512251
breakLabel: -1
@@ -2259,7 +2259,7 @@ namespace ts {
22592259
*/
22602260
function beginSwitchBlock(): Label {
22612261
const breakLabel = defineLabel();
2262-
beginBlock(<SwitchBlock>{
2262+
beginBlock<SwitchBlock>({
22632263
kind: CodeBlockKind.Switch,
22642264
isScript: false,
22652265
breakLabel,
@@ -2280,7 +2280,7 @@ namespace ts {
22802280
}
22812281

22822282
function beginScriptLabeledBlock(labelText: string) {
2283-
beginBlock(<LabeledBlock>{
2283+
beginBlock<LabeledBlock>({
22842284
kind: CodeBlockKind.Labeled,
22852285
isScript: true,
22862286
labelText,
@@ -2290,7 +2290,7 @@ namespace ts {
22902290

22912291
function beginLabeledBlock(labelText: string) {
22922292
const breakLabel = defineLabel();
2293-
beginBlock(<LabeledBlock>{
2293+
beginBlock<LabeledBlock>({
22942294
kind: CodeBlockKind.Labeled,
22952295
isScript: false,
22962296
labelText,

src/harness/harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ namespace Harness {
564564
}
565565

566566
export let listFiles: typeof IO.listFiles = (path, spec?, options?) => {
567-
options = options || <{ recursive?: boolean; }>{};
567+
options = options || {};
568568

569569
function filesInFolder(folder: string): string[] {
570570
let paths: string[] = [];

src/harness/projectsRunner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,12 @@ class ProjectRunner extends RunnerBase {
426426
compilerResult.program ?
427427
ts.filter(compilerResult.program.getSourceFiles(), sourceFile => !Harness.isDefaultLibraryFile(sourceFile.fileName)) :
428428
[]),
429-
sourceFile => <Harness.Compiler.TestFile>{
429+
sourceFile => ts.id<Harness.Compiler.TestFile>({
430430
unitName: ts.isRootedDiskPath(sourceFile.fileName) ?
431431
RunnerBase.removeFullPaths(sourceFile.fileName) :
432432
sourceFile.fileName,
433433
content: sourceFile.text
434-
});
434+
}));
435435

436436
return Harness.Compiler.getErrorBaseline(inputFiles, compilerResult.errors);
437437
}

src/harness/unittests/compileOnSave.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,18 +518,18 @@ namespace ts.projectSystem {
518518
};
519519
const host = createServerHost([f], { newLine });
520520
const session = createSession(host);
521-
session.executeCommand(<server.protocol.OpenRequest>{
521+
session.executeCommand(id<server.protocol.OpenRequest>({
522522
seq: 1,
523523
type: "request",
524-
command: "open",
524+
command: server.protocol.CommandTypes.Open,
525525
arguments: { file: f.path }
526-
});
527-
session.executeCommand(<server.protocol.CompileOnSaveEmitFileRequest>{
526+
}));
527+
session.executeCommand(id<server.protocol.CompileOnSaveEmitFileRequest>({
528528
seq: 2,
529529
type: "request",
530-
command: "compileOnSaveEmitFile",
530+
command: server.protocol.CommandTypes.CompileOnSaveEmitFile,
531531
arguments: { file: f.path }
532-
});
532+
}));
533533
const emitOutput = host.readFile(path + ts.Extension.Js);
534534
assert.equal(emitOutput, f.content + newLine, "content of emit output should be identical with the input + newline");
535535
}

0 commit comments

Comments
 (0)