Skip to content

Commit

Permalink
chore: Rename Angular async pipe rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Nov 6, 2019
1 parent c6df4b3 commit 33fdd80
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ The package includes the following rules (none of which are enabled by default):
| `rxjs-no-unsafe-takeuntil` | Disallows the application of operators after `takeUntil`. Operators placed after `takeUntil` can effect [subscription leaks](https://medium.com/@cartant/rxjs-avoiding-takeuntil-leaks-fb5182d047ef). | [See below](#rxjs-no-unsafe-takeuntil) |
| `rxjs-no-unused-add` | Disallows the importation of patched observables or operators that are not used in the module. | None |
| `rxjs-no-wholesale` | Disallows the wholesale importation of `rxjs` or `rxjs/Rx`. | None |
| `rxjs-prefer-async-pipe` | Disallows the calling of `subscribe` within an Angular component. | None |
| `rxjs-prefer-angular-async-pipe` | Disallows the calling of `subscribe` within an Angular component. | None |
| `rxjs-prefer-observer` | Enforces the passing of observers to `subscribe` and `tap`. See [this RxJS issue](https://github.com/ReactiveX/rxjs/issues/4159). | [See below](#rxjs-prefer-observer) |
| `rxjs-suffix-subjects` | Disalllows subjects that don't end with the specified `suffix` option. | [See below](#rxjs-suffix-subjects) |
| `rxjs-throw-error` | Enforces the passing of `Error` values to `error` notifications. | None |
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The package includes the following rules (none of which are enabled by default):
| `rxjs-no-unsafe-takeuntil` | Disallows the application of operators after `takeUntil`. Operators placed after `takeUntil` can effect [subscription leaks](https://medium.com/@cartant/rxjs-avoiding-takeuntil-leaks-fb5182d047ef). | [See below](#rxjs-no-unsafe-takeuntil) |
| `rxjs-no-unused-add` | Disallows the importation of patched observables or operators that are not used in the module. | None |
| `rxjs-no-wholesale` | Disallows the wholesale importation of `rxjs` or `rxjs/Rx`. | None |
| `rxjs-prefer-async-pipe` | Disallows the calling of `subscribe` within an Angular component. | None |
| `rxjs-prefer-angular-async-pipe` | Disallows the calling of `subscribe` within an Angular component. | None |
| `rxjs-prefer-observer` | Enforces the passing of observers to `subscribe` and `tap`. See [this RxJS issue](https://github.com/ReactiveX/rxjs/issues/4159). | [See below](#rxjs-prefer-observer) |
| `rxjs-suffix-subjects` | Disalllows subjects that don't end with the specified `suffix` option. | [See below](#rxjs-suffix-subjects) |
| `rxjs-throw-error` | Enforces the passing of `Error` values to `error` notifications. | None |
Expand Down
62 changes: 62 additions & 0 deletions source/rules/rxjsPreferAngularAsyncPipeRule.ts
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;
}
}
47 changes: 3 additions & 44 deletions source/rules/rxjsPreferAsyncPipeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
* 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";
import { Rule as PreferAngularAsyncPipeRule } from "./rxjsPreferAngularAsyncPipeRule";

export class Rule extends Lint.Rules.TypedRule {
export class Rule extends PreferAngularAsyncPipeRule {
public static metadata: Lint.IRuleMetadata = {
deprecationMessage: "Use the rxjs-prefer-angular-async-pipe rule instead.",
description:
"Disallows the calling of `subscribe` within an Angular component.",
options: null,
Expand All @@ -19,44 +18,4 @@ export class Rule extends Lint.Rules.TypedRule {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"defaultSeverity": "error",
"jsRules": {},
"rules": {
"rxjs-prefer-async-pipe": { "severity": "error" }
"rxjs-prefer-angular-async-pipe": { "severity": "error" }
},
"rulesDirectory": "../../../../../build/rules"
}

0 comments on commit 33fdd80

Please sign in to comment.