Skip to content

Commit

Permalink
test(scope): Add proper failing test for #100.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Jun 11, 2019
1 parent 340ca11 commit 9d637f2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
13 changes: 8 additions & 5 deletions source/rules/rxjsNoUnsafeScopeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ export class Rule extends Lint.Rules.TypedRule {
allowMethods: { type: "boolean" },
allowParameters: { type: "boolean" },
allowProperties: { type: "boolean" },
allowSubscribe: { type: "boolean" },
allowTap: { type: "boolean" }
},
type: "object"
},
optionsDescription: Lint.Utils.dedent`
An optional object with optional \`allowDo\`, \`allowParameters\` and \`allowTap\` properties all of which default to \`true\`.
If the \`allowDo\` and \`allowTap\` options are \`true\`, the rule is not applied within \`do\` and \`tap\` operators respectively.
If the \`allowParameters\` option is \`true\`, referencing function parameters from outer scopes is allowed.
If the \`allowMethods\` option is \`true\`, calling methods via \`this\` is allowed.
If the \`allowProperties\` option is \`true\`, accessing properties via \`this\` is allowed.`,
An optional object with optional \`allowDo\`, \`allowParameters\` and \`allowTap\` properties all of which default to \`true\`.
If the \`allowDo\` and \`allowTap\` options are \`true\`, the rule is not applied within \`do\` and \`tap\` operators respectively.
If the \`allowParameters\` option is \`true\`, referencing function parameters from outer scopes is allowed.
If the \`allowMethods\` option is \`true\`, calling methods via \`this\` is allowed.
If the \`allowProperties\` option is \`true\`, accessing properties via \`this\` is allowed.
If the \`allowSubscribe\` option is \`true\`, the rule is not applied within \`subscribe\` callbacks.`,
requiresTypeInfo: true,
ruleName: "rxjs-no-unsafe-scopes",
type: "functionality",
Expand All @@ -60,6 +62,7 @@ class Walker extends ScopeWalker {
private allowMethods = true;
private allowParameters = true;
private allowProperties = false;
private allowSubscribe = true;
private allowTap = true;

constructor(
Expand Down
2 changes: 2 additions & 0 deletions test/v6/fixtures/no-unsafe-scope/default/fixture.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ of(1).pipe(map(function (value) { return outer = value; })).subscribe();
~~~~~ [no-unsafe-scope]
of(1).pipe(map(value => value * HUNDRED * THOUSAND)).subscribe();
of(1).pipe(tap(value => outer = value)).subscribe();
of(1).subscribe(value => outer = value);

of(1).pipe(
map(() => Horizontal.Left),
Expand Down Expand Up @@ -54,6 +55,7 @@ class User {
~~~~ [no-unsafe-scope]
of("Hello").pipe(map(value => `${value}, ${this.foo()}.`)).subscribe();
of("Hello").pipe(map(value => `${value}, ${this.service.bar()}.`)).subscribe();
of("Hello").subscribe(value => this.name = value);
}
foo(): string { return "foo"; }
}
Expand Down
5 changes: 5 additions & 0 deletions test/v6/fixtures/no-unsafe-scope/disallow/fixture.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ let outer: any;

of(1).pipe(tap(value => outer = value)).subscribe();
~~~~~ [no-unsafe-scope]
of(1).subscribe(value => outer = value);
~~~~~ [no-unsafe-scope]

function piped(outer: number): Observable<number> {
return of(1).pipe(map(value => outer + value));
~~~~~ [no-unsafe-scope]
}

class User {
value: string;
constructor(private name: string, private service: { bar(): string }) {
of("Hello").pipe(map(value => `${value}, ${this.foo()}.`)).subscribe();
~~~~ [no-unsafe-scope]
of("Hello").pipe(map(value => `${value}, ${this.service.bar()}.`)).subscribe();
~~~~ [no-unsafe-scope]
of("Hello").subscribe(value => this.value = value);
~~~~ [no-unsafe-scope]
}
foo(): string { return "foo"; }
}
Expand Down
1 change: 1 addition & 0 deletions test/v6/fixtures/no-unsafe-scope/disallow/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"allowDo": false,
"allowMethods": false,
"allowParameters": false,
"allowSubscribe": false,
"allowTap": false
}],
"severity": "error"
Expand Down

0 comments on commit 9d637f2

Please sign in to comment.