Skip to content

Commit

Permalink
feat(prefer-async-pipe): Add rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Oct 6, 2018
1 parent 8944209 commit b25af74
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"test": "yarn run lint && yarn run test:build && yarn run test:mocha && yarn run test:tslint-v5 && yarn run test:tslint-v6 && yarn run test:tslint-v6-compat",
"test:build": "yarn run test:clean && tsc -p tsconfig.json",
"test:clean": "rimraf build",
"test:debug": "tslint --test ./test/v6/fixtures/prefer-observer/**/tslint.json",
"test:debug": "tslint --test ./test/v6/fixtures/prefer-async-pipe/**/tslint.json",
"test:issues": "yarn run test:clean && tsc -p tsconfig.json && tslint --test ./test/v6/fixtures/issues/**/tslint.json",
"test:mocha": "mocha build/**/*-spec.js",
"test:tslint-v5": "yarn --cwd ./test/v5 install && yarn --cwd ./test/v5 upgrade && tslint --test ./test/v5/fixtures/**/tslint.json",
Expand Down
59 changes: 59 additions & 0 deletions source/rules/rxjsPreferAsyncPipeRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @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 * as Lint from "tslint";
import * as ts from "typescript";
import * as tsutils from "tsutils";
import { tsquery } from "@phenomnomnominal/tsquery";
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-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 componentIdentifiers = tsquery(
sourceFile,
`ClassDeclaration Identifier[name=/Component$/]`
);
componentIdentifiers.forEach(componentIdentifier => {
const { parent: classDeclaration } = componentIdentifier;
const propertyAccessExpressions = tsquery(
classDeclaration,
`CallExpression PropertyAccessExpression[name.name="subscribe"]`
);
propertyAccessExpressions.forEach(propertyAccessExpression => {
if (tsutils.isPropertyAccessExpression(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;
}
}
15 changes: 15 additions & 0 deletions test/v6/fixtures/prefer-async-pipe/default/fixture.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Component OnInit } from "@angular/core";
import { of } from "rxjs";

@Component({
selector: "some-component",
template: "<span>something</span>"
})
export class SomeComponent implements OnInit {
ngOnInit() {
of("foo").subscribe();
~~~~~~~~~ [prefer-async-pipe]
}
}

[prefer-async-pipe]: Prefer async pipe over subscribe
13 changes: 13 additions & 0 deletions test/v6/fixtures/prefer-async-pipe/default/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"baseUrl": ".",
"lib": ["es2015"],
"noEmit": true,
"paths": {
"rxjs": ["../../node_modules/rxjs"]
},
"skipLibCheck": true,
"target": "es5"
},
"include": ["fixture.ts"]
}
8 changes: 8 additions & 0 deletions test/v6/fixtures/prefer-async-pipe/default/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"defaultSeverity": "error",
"jsRules": {},
"rules": {
"rxjs-prefer-async-pipe": { "severity": "error" }
},
"rulesDirectory": "../../../../../build/rules"
}

0 comments on commit b25af74

Please sign in to comment.