Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
no-angle-bracket-type-assertion: check only for binary expression and…
Browse files Browse the repository at this point in the history
… add test (#4823)
  • Loading branch information
tanmoyopenroot authored and Josh Goldberg committed Aug 10, 2019
1 parent c5074f4 commit f772a15
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 10 deletions.
11 changes: 1 addition & 10 deletions src/rules/noAngleBracketTypeAssertionRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function walk(ctx: Lint.WalkContext) {
if (isTypeAssertion(node)) {
let { expression } = node;
const start = node.getStart(ctx.sourceFile);
const addParens = needsParens(node);
const addParens = isBinaryExpression(node.parent);
let replaceText = ` as ${node.type.getText(ctx.sourceFile)}${addParens ? ")" : ""}`;
while (isTypeAssertion(expression)) {
replaceText = ` as ${expression.type.getText(ctx.sourceFile)}${replaceText}`;
Expand All @@ -70,12 +70,3 @@ function walk(ctx: Lint.WalkContext) {
return ts.forEachChild(node, cb);
});
}

function needsParens(node: ts.TypeAssertion): boolean {
const parent = node.parent;
return (
isBinaryExpression(parent) &&
(parent.operatorToken.kind === ts.SyntaxKind.AmpersandToken ||
parent.operatorToken.kind === ts.SyntaxKind.BarToken)
);
}
35 changes: 35 additions & 0 deletions test/rules/no-angle-bracket-type-assertion/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,38 @@ let g = a as any as A;

let h = a as any as AsyncIterableIterator;

interface Action {
payload: number | false;
}

const action: Action = {
payload: 5,
}

const booleanAction: Action = {
payload: false,
}

const a = 5 + (action.payload as number);

const a = 5 - (action.payload as number);

const a = 5 * (action.payload as number);

const a = 5 / (action.payload as number);

const a = 5 % (action.payload as number);

const a = 5 && (action.payload as number);

const a = 5 || (action.payload as number);

const a = 5 % (action.payload as any as number);

const a = 5 && (action.payload as any as number);

const a = 5 || (action.payload as any as number);

const a = true || (booleanAction.payload as boolean);

const a = false && (booleanAction.payload as boolean);
47 changes: 47 additions & 0 deletions test/rules/no-angle-bracket-type-assertion/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,51 @@ let g = <A><any>a;
let h = <AsyncIterableIterator><any>a;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

interface Action {
payload: number | false;
}

const action: Action = {
payload: 5,
}

const booleanAction: Action = {
payload: false,
}

const a = 5 + <number>action.payload;
~~~~~~~~~~~~~~~~~~~~~~ [0]

const a = 5 - <number>action.payload;
~~~~~~~~~~~~~~~~~~~~~~ [0]

const a = 5 * <number>action.payload;
~~~~~~~~~~~~~~~~~~~~~~ [0]

const a = 5 / <number>action.payload;
~~~~~~~~~~~~~~~~~~~~~~ [0]

const a = 5 % <number>action.payload;
~~~~~~~~~~~~~~~~~~~~~~ [0]

const a = 5 && <number>action.payload;
~~~~~~~~~~~~~~~~~~~~~~ [0]

const a = 5 || <number>action.payload;
~~~~~~~~~~~~~~~~~~~~~~ [0]

const a = 5 % <number><any>action.payload;
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

const a = 5 && <number><any>action.payload;
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

const a = 5 || <number><any>action.payload;
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

const a = true || <boolean>booleanAction.payload;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

const a = false && <boolean>booleanAction.payload;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
[0]: Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.

0 comments on commit f772a15

Please sign in to comment.