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
This commit is contained in:
Andy
2017-08-10 12:52:15 -07:00
committed by GitHub
parent fe3a05e89a
commit 08fbcd8b80
17 changed files with 140 additions and 148 deletions
+5 -22
View File
@@ -806,11 +806,7 @@ namespace ts {
return antecedent;
}
setFlowNodeReferenced(antecedent);
return <FlowCondition>{
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 <FlowSwitchClause>{
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 <FlowAssignment>{
flags: FlowFlags.Assignment,
antecedent,
node
};
return { flags: FlowFlags.Assignment, antecedent, node };
}
function createFlowArrayMutation(antecedent: FlowNode, node: CallExpression | BinaryExpression): FlowNode {
setFlowNodeReferenced(antecedent);
return <FlowArrayMutation>{
flags: FlowFlags.ArrayMutation,
antecedent,
node
};
const res: FlowArrayMutation = { flags: FlowFlags.ArrayMutation, antecedent, node };
return res;
}
function finishFlowLabel(flow: FlowLabel): FlowNode {
+5 -5
View File
@@ -2172,7 +2172,7 @@ namespace ts {
if (accessibleSymbolChain) {
const hasAccessibleDeclarations = hasVisibleDeclarations(accessibleSymbolChain[0], shouldComputeAliasesToMakeVisible);
if (!hasAccessibleDeclarations) {
return <SymbolAccessibilityResult>{
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)) || <SymbolVisibilityResult>{
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;
};
}
}
+3 -1
View File
@@ -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 = <Node>{ 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);
}
+2 -2
View File
@@ -2586,7 +2586,7 @@ namespace ts {
}
export function addSyntheticLeadingComment<T extends Node>(node: T, kind: SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia, text: string, hasTrailingNewLine?: boolean) {
return setSyntheticLeadingComments(node, append(getSyntheticLeadingComments(node), <SynthesizedComment>{ kind, pos: -1, end: -1, hasTrailingNewLine, text }));
return setSyntheticLeadingComments(node, append<SynthesizedComment>(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<T extends Node>(node: T, kind: SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia, text: string, hasTrailingNewLine?: boolean) {
return setSyntheticTrailingComments(node, append(getSyntheticTrailingComments(node), <SynthesizedComment>{ kind, pos: -1, end: -1, hasTrailingNewLine, text }));
return setSyntheticTrailingComments(node, append<SynthesizedComment>(getSyntheticTrailingComments(node), { kind, pos: -1, end: -1, hasTrailingNewLine, text }));
}
/**
+1 -1
View File
@@ -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 = <SourceFile>{ languageVariant: LanguageVariant.Standard, text: content };
sourceFile = <SourceFile>{ languageVariant: LanguageVariant.Standard, text: content }; // tslint:disable-line no-object-literal-type-assertion
const jsDoc = parseJSDocCommentWorker(start, length);
const diagnostics = parseDiagnostics;
clearState();
+46 -48
View File
@@ -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(<WithBlock>{
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(<ExceptionBlock>{
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(<LoopBlock>{
beginBlock({
kind: CodeBlockKind.Loop,
isScript: true,
breakLabel: -1,
@@ -2217,7 +2212,7 @@ namespace ts {
*/
function beginLoopBlock(continueLabel: Label): Label {
const breakLabel = defineLabel();
beginBlock(<LoopBlock>{
beginBlock({
kind: CodeBlockKind.Loop,
isScript: false,
breakLabel,
@@ -2245,7 +2240,7 @@ namespace ts {
*
*/
function beginScriptSwitchBlock(): void {
beginBlock(<SwitchBlock>{
beginBlock({
kind: CodeBlockKind.Switch,
isScript: true,
breakLabel: -1
@@ -2259,7 +2254,7 @@ namespace ts {
*/
function beginSwitchBlock(): Label {
const breakLabel = defineLabel();
beginBlock(<SwitchBlock>{
beginBlock({
kind: CodeBlockKind.Switch,
isScript: false,
breakLabel,
@@ -2280,7 +2275,7 @@ namespace ts {
}
function beginScriptLabeledBlock(labelText: string) {
beginBlock(<LabeledBlock>{
beginBlock({
kind: CodeBlockKind.Labeled,
isScript: true,
labelText,
@@ -2290,7 +2285,7 @@ namespace ts {
function beginLabeledBlock(labelText: string) {
const breakLabel = defineLabel();
beginBlock(<LabeledBlock>{
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
}
}
}
+11 -9
View File
@@ -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;
}