From a57f9386a2b7a8f425793940821d194d8654005d Mon Sep 17 00:00:00 2001 From: Nicholas Jamieson Date: Sat, 20 Apr 2019 12:50:06 +1000 Subject: [PATCH] fix(nested): Fix false positive. --- source/rules/rxjsNoNestedSubscribeRule.ts | 36 ++++++++++++----------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/source/rules/rxjsNoNestedSubscribeRule.ts b/source/rules/rxjsNoNestedSubscribeRule.ts index ec318c0f..421dc8bb 100644 --- a/source/rules/rxjsNoNestedSubscribeRule.ts +++ b/source/rules/rxjsNoNestedSubscribeRule.ts @@ -30,7 +30,7 @@ export class Rule extends Lint.Rules.TypedRule { const failures: Lint.RuleFailure[] = []; const typeChecker = program.getTypeChecker(); - const subscribeQuery = `CallExpression PropertyAccessExpression[name.name="subscribe"]`; + const subscribeQuery = `CallExpression > PropertyAccessExpression[name.name="subscribe"]`; const propertyAccessExpressions = tsquery(sourceFile, subscribeQuery); propertyAccessExpressions.forEach(node => { const propertyAccessExpression = node as ts.PropertyAccessExpression; @@ -41,23 +41,25 @@ export class Rule extends Lint.Rules.TypedRule { ); if (couldBeType(type, "Observable")) { callExpression.arguments.forEach(arg => { - const propertyAccessExpressions = tsquery(arg, subscribeQuery); - propertyAccessExpressions.forEach(node => { - const propertyAccessExpression = node as ts.PropertyAccessExpression; - const type = typeChecker.getTypeAtLocation( - propertyAccessExpression.expression - ); - if (couldBeType(type, "Observable")) { - const { name } = propertyAccessExpression; - failures.push( - new Lint.RuleFailure( - sourceFile, - name.getStart(), - name.getStart() + name.getWidth(), - Rule.FAILURE_STRING, - this.ruleName - ) + const innerPropertyAccessExpressions = tsquery(arg, subscribeQuery); + innerPropertyAccessExpressions.forEach(node => { + const innerPropertyAccessExpression = node as ts.PropertyAccessExpression; + if (innerPropertyAccessExpression !== propertyAccessExpression) { + const type = typeChecker.getTypeAtLocation( + innerPropertyAccessExpression.expression ); + if (couldBeType(type, "Observable")) { + const { name } = innerPropertyAccessExpression; + failures.push( + new Lint.RuleFailure( + sourceFile, + name.getStart(), + name.getStart() + name.getWidth(), + Rule.FAILURE_STRING, + this.ruleName + ) + ); + } } }); });