-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Rename Angular async pipe rule.
- Loading branch information
Showing
7 changed files
with
68 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @license Use of this source code is governed by an MIT-style license that | ||
* can be found in the LICENSE file at https://github.com/cartant/rxjs-tslint-rules | ||
*/ | ||
|
||
import { tsquery } from "@phenomnomnominal/tsquery"; | ||
import * as Lint from "tslint"; | ||
import * as ts from "typescript"; | ||
import { couldBeType } from "../support/util"; | ||
|
||
export class Rule extends Lint.Rules.TypedRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
description: | ||
"Disallows the calling of `subscribe` within an Angular component.", | ||
options: null, | ||
optionsDescription: "Not configurable.", | ||
requiresTypeInfo: true, | ||
ruleName: "rxjs-prefer-angular-async-pipe", | ||
type: "style", | ||
typescriptOnly: true | ||
}; | ||
|
||
public static FAILURE_STRING = "Prefer async pipe over subscribe"; | ||
|
||
public applyWithProgram( | ||
sourceFile: ts.SourceFile, | ||
program: ts.Program | ||
): Lint.RuleFailure[] { | ||
const failures: Lint.RuleFailure[] = []; | ||
const typeChecker = program.getTypeChecker(); | ||
|
||
const classDeclarations = tsquery( | ||
sourceFile, | ||
`ClassDeclaration:has(Decorator[expression.expression.name="Component"])` | ||
); | ||
classDeclarations.forEach(classDeclaration => { | ||
const propertyAccessExpressions = tsquery( | ||
classDeclaration, | ||
`CallExpression PropertyAccessExpression[name.name="subscribe"]` | ||
); | ||
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 | ||
) | ||
); | ||
} | ||
}); | ||
}); | ||
return failures; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters