Skip to content

Commit

Permalink
fix(unused): Support flatMap.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Nov 27, 2017
1 parent d6a4b9a commit 40169e8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
5 changes: 5 additions & 0 deletions fixtures/flat-map/fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/of";
import "rxjs/add/operator/mergeMap";

const ob = Observable.of(1).flatMap((value) => [value + 1]);
13 changes: 13 additions & 0 deletions fixtures/flat-map/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": ["adds.ts", "fixture.ts"]
}
10 changes: 10 additions & 0 deletions source/fixtures-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ describe("fixtures", function (): void {
});
});

describe("flat-map", () => {

it("should effect no errors", () => {

const result = lint("flat-map");

expect(result).to.have.property("errorCount", 0);
});
});

describe("import-wholesale", () => {

it("should effect 'rxjs-no-wholesale' errors", () => {
Expand Down
43 changes: 25 additions & 18 deletions source/support/knowns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,43 @@ import * as path from "path";
import * as resolve from "resolve";
import * as semver from "semver";

function read(dir: string): { [key: string]: boolean } {

let entry;
try {
entry = resolve.sync("rxjs");
} catch (error) {
/*tslint:disable-next-line:no-console*/
console.warn("Cannot find rxjs in node_modules; some rxjs-tslint-rules will be ineffectual.");
let aliasOperators: { [key: string]: string } = { flatMap: "mergeMap" };
let dist: string | undefined = undefined;
let prototypeMethods: { [key: string]: boolean } = {};

try {

const entry = resolve.sync("rxjs");
dist = path.dirname(entry);

const root = dist.replace(/node_modules[\/\\]rxjs[\/\\](.*)$/, (match) => match);
const { version } = require(path.join(root, "package.json"));
prototypeMethods = semver.satisfies(version, "<5.5.0-beta.5") ? {} : { pipe: true, toPromise: true };

} catch (error) {
/*tslint:disable-next-line:no-console*/
console.warn("Cannot find rxjs in node_modules; some rxjs-tslint-rules will be ineffectual.");
}

function read(dir: string): { [key: string]: string } {

if (!dist) {
return {};
}

const dist = path.dirname(entry);

// In RxJS 5.5.0-beta.5, toPromise was moved into the prototype.
// However, to avoid breakage, the file in the add/operator directory was
// left in-place.

const root = dist.replace(/node_modules[\/\\]rxjs[\/\\](.*)$/, (match) => match);
const { version } = require(path.join(root, "package.json"));
const ignored = semver.satisfies(version, "<5.5.0-beta.5") ? {} : { "toPromise": true };

const names = fs.readdirSync(path.join(dist, dir));
return names
.filter((name) => /^[a-z]\w+\.js$/.test(name))
.map((name) => name.replace(/\.js/, ""))
.filter((name) => !ignored[name])
.reduce((acc, name) => ({ ...acc, [name]: true }), {});
.filter((name) => !prototypeMethods[name])
.reduce((acc, name) => ({ ...acc, [name]: name }), {});
}

export const knownObservables = read("add/observable");
export const knownOperators = read("add/operator");
export const knownPrototypeMethods = { pipe: true, toPromise: true };
export const knownOperators = { ...read("add/operator"), ...aliasOperators };
export const knownPrototypeMethods = { forEach: true, ...prototypeMethods };
export const knownStaticMethods = { create: true };
6 changes: 4 additions & 2 deletions source/support/used-walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ export class UsedWalker extends AddedWalker {

if (isReferenceType(type)) {
if (knownOperators.hasOwnProperty(name) && couldBeType(type.target, "Observable")) {
UsedWalker.add(this.usedOperators, name, propertyAccessExpression.name);
const actual = knownOperators[name];
UsedWalker.add(this.usedOperators, actual, propertyAccessExpression.name);
} else if (knownPrototypeMethods.hasOwnProperty(name) && couldBeType(type.target, "Observable")) {
UsedWalker.add(this.usedPrototypeMethods, name, propertyAccessExpression.name);
}
} else {
if (knownObservables.hasOwnProperty(name) && couldBeType(type, "Observable")) {
UsedWalker.add(this.usedObservables, name, propertyAccessExpression.name);
const actual = knownObservables[name];
UsedWalker.add(this.usedObservables, actual, propertyAccessExpression.name);
} else if (knownStaticMethods.hasOwnProperty(name) && couldBeType(type, "Observable")) {
UsedWalker.add(this.usedStaticMethods, name, propertyAccessExpression.name);
}
Expand Down

0 comments on commit 40169e8

Please sign in to comment.