Skip to content

Commit 08fbcd8

Browse files
author
Andy
authored
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
1 parent fe3a05e commit 08fbcd8

17 files changed

+140
-148
lines changed

src/compiler/binder.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -806,43 +806,26 @@ namespace ts {
806806
return antecedent;
807807
}
808808
setFlowNodeReferenced(antecedent);
809-
return <FlowCondition>{
810-
flags,
811-
expression,
812-
antecedent
813-
};
809+
return { flags, expression, antecedent };
814810
}
815811

816812
function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode {
817813
if (!isNarrowingExpression(switchStatement.expression)) {
818814
return antecedent;
819815
}
820816
setFlowNodeReferenced(antecedent);
821-
return <FlowSwitchClause>{
822-
flags: FlowFlags.SwitchClause,
823-
switchStatement,
824-
clauseStart,
825-
clauseEnd,
826-
antecedent
827-
};
817+
return { flags: FlowFlags.SwitchClause, switchStatement, clauseStart, clauseEnd, antecedent };
828818
}
829819

830820
function createFlowAssignment(antecedent: FlowNode, node: Expression | VariableDeclaration | BindingElement): FlowNode {
831821
setFlowNodeReferenced(antecedent);
832-
return <FlowAssignment>{
833-
flags: FlowFlags.Assignment,
834-
antecedent,
835-
node
836-
};
822+
return { flags: FlowFlags.Assignment, antecedent, node };
837823
}
838824

839825
function createFlowArrayMutation(antecedent: FlowNode, node: CallExpression | BinaryExpression): FlowNode {
840826
setFlowNodeReferenced(antecedent);
841-
return <FlowArrayMutation>{
842-
flags: FlowFlags.ArrayMutation,
843-
antecedent,
844-
node
845-
};
827+
const res: FlowArrayMutation = { flags: FlowFlags.ArrayMutation, antecedent, node };
828+
return res;
846829
}
847830

848831
function finishFlowLabel(flow: FlowLabel): FlowNode {

src/compiler/checker.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,7 @@ namespace ts {
21722172
if (accessibleSymbolChain) {
21732173
const hasAccessibleDeclarations = hasVisibleDeclarations(accessibleSymbolChain[0], shouldComputeAliasesToMakeVisible);
21742174
if (!hasAccessibleDeclarations) {
2175-
return <SymbolAccessibilityResult>{
2175+
return {
21762176
accessibility: SymbolAccessibility.NotAccessible,
21772177
errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning),
21782178
errorModuleName: symbol !== initialSymbol ? symbolToString(symbol, enclosingDeclaration, SymbolFlags.Namespace) : undefined,
@@ -2294,7 +2294,7 @@ namespace ts {
22942294
const symbol = resolveName(enclosingDeclaration, firstIdentifier.escapedText, meaning, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined);
22952295

22962296
// Verify if the symbol is accessible
2297-
return (symbol && hasVisibleDeclarations(symbol, /*shouldComputeAliasToMakeVisible*/ true)) || <SymbolVisibilityResult>{
2297+
return (symbol && hasVisibleDeclarations(symbol, /*shouldComputeAliasToMakeVisible*/ true)) || {
22982298
accessibility: SymbolAccessibility.NotAccessible,
22992299
errorSymbolName: getTextOfNode(firstIdentifier),
23002300
errorNode: firstIdentifier
@@ -6300,7 +6300,7 @@ namespace ts {
63006300
return {
63016301
kind: TypePredicateKind.This,
63026302
type: getTypeFromTypeNode(node.type)
6303-
} as ThisTypePredicate;
6303+
};
63046304
}
63056305
}
63066306

@@ -8109,13 +8109,13 @@ namespace ts {
81098109
parameterName: predicate.parameterName,
81108110
parameterIndex: predicate.parameterIndex,
81118111
type: instantiateType(predicate.type, mapper)
8112-
} as IdentifierTypePredicate;
8112+
};
81138113
}
81148114
else {
81158115
return {
81168116
kind: TypePredicateKind.This,
81178117
type: instantiateType(predicate.type, mapper)
8118-
} as ThisTypePredicate;
8118+
};
81198119
}
81208120
}
81218121

src/compiler/emitter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,9 @@ 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 = createToken(SyntaxKind.DotToken);
1199+
dotToken.pos = dotRangeStart;
1200+
dotToken.end = dotRangeEnd;
11991201
indentBeforeDot = needsIndentation(node, node.expression, dotToken);
12001202
indentAfterDot = needsIndentation(node, dotToken, node.name);
12011203
}

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(node, append<SynthesizedComment>(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(node, append<SynthesizedComment>(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
@@ -6169,7 +6169,7 @@ namespace ts {
61696169

61706170
export function parseIsolatedJSDocComment(content: string, start: number, length: number): { jsDoc: JSDoc, diagnostics: Diagnostic[] } | undefined {
61716171
initializeState(content, ScriptTarget.Latest, /*_syntaxCursor:*/ undefined, ScriptKind.JS);
6172-
sourceFile = <SourceFile>{ languageVariant: LanguageVariant.Standard, text: content };
6172+
sourceFile = <SourceFile>{ languageVariant: LanguageVariant.Standard, text: content }; // tslint:disable-line no-object-literal-type-assertion
61736173
const jsDoc = parseJSDocCommentWorker(start, length);
61746174
const diagnostics = parseDiagnostics;
61756175
clearState();

src/compiler/transformers/generators.ts

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,11 @@ namespace ts {
164164
}
165165

166166
// A generated code block
167-
interface CodeBlock {
168-
kind: CodeBlockKind;
169-
}
167+
type CodeBlock = | ExceptionBlock | LabeledBlock | SwitchBlock | LoopBlock | WithBlock;
170168

171169
// a generated exception block, used for 'try' statements
172-
interface ExceptionBlock extends CodeBlock {
170+
interface ExceptionBlock {
171+
kind: CodeBlockKind.Exception;
173172
state: ExceptionBlockState;
174173
startLabel: Label;
175174
catchVariable?: Identifier;
@@ -179,27 +178,31 @@ namespace ts {
179178
}
180179

181180
// A generated code that tracks the target for 'break' statements in a LabeledStatement.
182-
interface LabeledBlock extends CodeBlock {
181+
interface LabeledBlock {
182+
kind: CodeBlockKind.Labeled;
183183
labelText: string;
184184
isScript: boolean;
185185
breakLabel: Label;
186186
}
187187

188188
// a generated block that tracks the target for 'break' statements in a 'switch' statement
189-
interface SwitchBlock extends CodeBlock {
189+
interface SwitchBlock {
190+
kind: CodeBlockKind.Switch;
190191
isScript: boolean;
191192
breakLabel: Label;
192193
}
193194

194195
// a generated block that tracks the targets for 'break' and 'continue' statements, used for iteration statements
195-
interface LoopBlock extends CodeBlock {
196+
interface LoopBlock {
197+
kind: CodeBlockKind.Loop;
196198
continueLabel: Label;
197199
isScript: boolean;
198200
breakLabel: Label;
199201
}
200202

201203
// a generated block associated with a 'with' statement
202-
interface WithBlock extends CodeBlock {
204+
interface WithBlock {
205+
kind: CodeBlockKind.With;
203206
expression: Identifier;
204207
startLabel: Label;
205208
endLabel: Label;
@@ -2070,7 +2073,7 @@ namespace ts {
20702073
const startLabel = defineLabel();
20712074
const endLabel = defineLabel();
20722075
markLabel(startLabel);
2073-
beginBlock(<WithBlock>{
2076+
beginBlock({
20742077
kind: CodeBlockKind.With,
20752078
expression,
20762079
startLabel,
@@ -2087,18 +2090,14 @@ namespace ts {
20872090
markLabel(block.endLabel);
20882091
}
20892092

2090-
function isWithBlock(block: CodeBlock): block is WithBlock {
2091-
return block.kind === CodeBlockKind.With;
2092-
}
2093-
20942093
/**
20952094
* Begins a code block for a generated `try` statement.
20962095
*/
20972096
function beginExceptionBlock(): Label {
20982097
const startLabel = defineLabel();
20992098
const endLabel = defineLabel();
21002099
markLabel(startLabel);
2101-
beginBlock(<ExceptionBlock>{
2100+
beginBlock({
21022101
kind: CodeBlockKind.Exception,
21032102
state: ExceptionBlockState.Try,
21042103
startLabel,
@@ -2188,18 +2187,14 @@ namespace ts {
21882187
exception.state = ExceptionBlockState.Done;
21892188
}
21902189

2191-
function isExceptionBlock(block: CodeBlock): block is ExceptionBlock {
2192-
return block.kind === CodeBlockKind.Exception;
2193-
}
2194-
21952190
/**
21962191
* Begins a code block that supports `break` or `continue` statements that are defined in
21972192
* the source tree and not from generated code.
21982193
*
21992194
* @param labelText Names from containing labeled statements.
22002195
*/
22012196
function beginScriptLoopBlock(): void {
2202-
beginBlock(<LoopBlock>{
2197+
beginBlock({
22032198
kind: CodeBlockKind.Loop,
22042199
isScript: true,
22052200
breakLabel: -1,
@@ -2217,7 +2212,7 @@ namespace ts {
22172212
*/
22182213
function beginLoopBlock(continueLabel: Label): Label {
22192214
const breakLabel = defineLabel();
2220-
beginBlock(<LoopBlock>{
2215+
beginBlock({
22212216
kind: CodeBlockKind.Loop,
22222217
isScript: false,
22232218
breakLabel,
@@ -2245,7 +2240,7 @@ namespace ts {
22452240
*
22462241
*/
22472242
function beginScriptSwitchBlock(): void {
2248-
beginBlock(<SwitchBlock>{
2243+
beginBlock({
22492244
kind: CodeBlockKind.Switch,
22502245
isScript: true,
22512246
breakLabel: -1
@@ -2259,7 +2254,7 @@ namespace ts {
22592254
*/
22602255
function beginSwitchBlock(): Label {
22612256
const breakLabel = defineLabel();
2262-
beginBlock(<SwitchBlock>{
2257+
beginBlock({
22632258
kind: CodeBlockKind.Switch,
22642259
isScript: false,
22652260
breakLabel,
@@ -2280,7 +2275,7 @@ namespace ts {
22802275
}
22812276

22822277
function beginScriptLabeledBlock(labelText: string) {
2283-
beginBlock(<LabeledBlock>{
2278+
beginBlock({
22842279
kind: CodeBlockKind.Labeled,
22852280
isScript: true,
22862281
labelText,
@@ -2290,7 +2285,7 @@ namespace ts {
22902285

22912286
function beginLabeledBlock(labelText: string) {
22922287
const breakLabel = defineLabel();
2293-
beginBlock(<LabeledBlock>{
2288+
beginBlock({
22942289
kind: CodeBlockKind.Labeled,
22952290
isScript: false,
22962291
labelText,
@@ -2878,34 +2873,37 @@ namespace ts {
28782873
for (; blockIndex < blockActions.length && blockOffsets[blockIndex] <= operationIndex; blockIndex++) {
28792874
const block = blocks[blockIndex];
28802875
const blockAction = blockActions[blockIndex];
2881-
if (isExceptionBlock(block)) {
2882-
if (blockAction === BlockAction.Open) {
2883-
if (!exceptionBlockStack) {
2884-
exceptionBlockStack = [];
2885-
}
2876+
switch (block.kind) {
2877+
case CodeBlockKind.Exception:
2878+
if (blockAction === BlockAction.Open) {
2879+
if (!exceptionBlockStack) {
2880+
exceptionBlockStack = [];
2881+
}
28862882

2887-
if (!statements) {
2888-
statements = [];
2889-
}
2883+
if (!statements) {
2884+
statements = [];
2885+
}
28902886

2891-
exceptionBlockStack.push(currentExceptionBlock);
2892-
currentExceptionBlock = block;
2893-
}
2894-
else if (blockAction === BlockAction.Close) {
2895-
currentExceptionBlock = exceptionBlockStack.pop();
2896-
}
2897-
}
2898-
else if (isWithBlock(block)) {
2899-
if (blockAction === BlockAction.Open) {
2900-
if (!withBlockStack) {
2901-
withBlockStack = [];
2887+
exceptionBlockStack.push(currentExceptionBlock);
2888+
currentExceptionBlock = block;
29022889
}
2890+
else if (blockAction === BlockAction.Close) {
2891+
currentExceptionBlock = exceptionBlockStack.pop();
2892+
}
2893+
break;
2894+
case CodeBlockKind.With:
2895+
if (blockAction === BlockAction.Open) {
2896+
if (!withBlockStack) {
2897+
withBlockStack = [];
2898+
}
29032899

2904-
withBlockStack.push(block);
2905-
}
2906-
else if (blockAction === BlockAction.Close) {
2907-
withBlockStack.pop();
2908-
}
2900+
withBlockStack.push(block);
2901+
}
2902+
else if (blockAction === BlockAction.Close) {
2903+
withBlockStack.pop();
2904+
}
2905+
break;
2906+
// default: do nothing
29092907
}
29102908
}
29112909
}

0 commit comments

Comments
 (0)