Support Flow as expressions in ESLint rules (#27590)

Support Flow `as` expressions in ESLint rules, e.g. `<expr> as <type>`.
This is the same syntax as TypeScript as expressions. I just looked for
any place referencing `TSAsExpression` (the TS node) or
`TypeCastExpression` (the previous Flow syntax) and added a case for
`AsExpression` as well.
This commit is contained in:
George Zahariev 2023-11-01 12:24:06 -07:00 committed by GitHub
parent 3eaa0c3871
commit 6bfc0e032a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -177,7 +177,7 @@ export default {
if (init == null) { if (init == null) {
return false; return false;
} }
while (init.type === 'TSAsExpression') { while (init.type === 'TSAsExpression' || init.type === 'AsExpression') {
init = init.expression; init = init.expression;
} }
// Detect primitive constants // Detect primitive constants
@ -1525,7 +1525,7 @@ function getConstructionExpressionType(node) {
} }
return null; return null;
case 'TypeCastExpression': case 'TypeCastExpression':
return getConstructionExpressionType(node.expression); case 'AsExpression':
case 'TSAsExpression': case 'TSAsExpression':
return getConstructionExpressionType(node.expression); return getConstructionExpressionType(node.expression);
} }

View File

@ -291,7 +291,11 @@ function checkBinaryExpression(context, node) {
(isEmptyLiteral(node.left) || isEmptyLiteral(node.right)) (isEmptyLiteral(node.left) || isEmptyLiteral(node.right))
) { ) {
let valueToTest = isEmptyLiteral(node.left) ? node.right : node.left; let valueToTest = isEmptyLiteral(node.left) ? node.right : node.left;
if (valueToTest.type === 'TypeCastExpression' && valueToTest.expression) { if (
(valueToTest.type === 'TypeCastExpression' ||
valueToTest.type === 'AsExpression') &&
valueToTest.expression
) {
valueToTest = valueToTest.expression; valueToTest = valueToTest.expression;
} }