Skip to content

Don't require Finnish notation or Subject suffixes for forkJoin or combineLatest properties #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/rules/finnish.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Examples of **correct** code for this rule:

```ts
const answer$ = of(42, 54);

// The static observable creators that accept a sources object are exempt from this rule.
combineLatest({ answer: of(42, 54) });
```

## Options
Expand Down
3 changes: 3 additions & 0 deletions docs/rules/suffix-subjects.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Examples of **correct** code for this rule:
```ts
const answersSubject = new Subject<number>();
const answersSubject$ = new Subject<number>();

// The static observable creators that accept a sources object are exempt from this rule.
combineLatest({ answers: new Subject<number>() });
```

## Options
Expand Down
9 changes: 9 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
export const defaultObservable = String.raw`[Aa]ction(s|s\$|\$)$`;

/**
* The names of static observable creators
* that accept a sources object as input.
*/
export const SOURCES_OBJECT_ACCEPTING_STATIC_OBSERVABLE_CREATORS = [
'combineLatest',
'forkJoin',
];

/**
* The names of operators that are safe to be used after
* operators like `takeUntil` that complete the observable.
Expand Down
7 changes: 4 additions & 3 deletions src/rules/finnish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { AST_NODE_TYPES, TSESTree as es, ESLintUtils } from '@typescript-eslint/
import {
findParent,
getLoc,
getTypeServices } from '../etc';
import { ruleCreator } from '../utils';
getTypeServices,
} from '../etc';
import { isSourcesObjectAcceptingStaticObservableCreator, ruleCreator } from '../utils';

const defaultOptions: readonly {
functions?: boolean;
Expand Down Expand Up @@ -228,7 +229,7 @@ export const finnishRule = ruleCreator({
) => {
if (validate.properties) {
const parent = node.parent as es.Property;
if (node === parent.key) {
if (node === parent.key && !isSourcesObjectAcceptingStaticObservableCreator(parent.parent.parent)) {
checkNode(node);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/rules/suffix-subjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getLoc,
getTypeServices,
} from '../etc';
import { escapeRegExp, ruleCreator } from '../utils';
import { escapeRegExp, isSourcesObjectAcceptingStaticObservableCreator, ruleCreator } from '../utils';

const defaultOptions: readonly {
parameters?: boolean;
Expand Down Expand Up @@ -161,7 +161,7 @@ export const suffixSubjectsRule = ruleCreator({
) => {
if (validate.properties) {
const parent = node.parent as es.Property;
if (node === parent.key) {
if (node === parent.key && !isSourcesObjectAcceptingStaticObservableCreator(parent.parent.parent)) {
checkNode(node);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './create-regexp-for-words';
export * from './escape-regexp';
export * from './find-is-last-operator-order-valid';
export * from './is-sources-object-accepting-static-observable-creator';
export * from './rule-creator';
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TSESTree as es } from '@typescript-eslint/utils';
import { SOURCES_OBJECT_ACCEPTING_STATIC_OBSERVABLE_CREATORS } from '../constants';
import { isCallExpression, isIdentifier } from '../etc/is';

/**
* Returns true if the given expression is a call to a
* sourcesObject-accepting static observable creator.
*
* @example
* ```ts
* combineLatest({ a$: of(), b$: of() });
* ```
*/
export function isSourcesObjectAcceptingStaticObservableCreator(expression: es.Node): boolean {
return isCallExpression(expression)
&& isIdentifier(expression.callee)
&& SOURCES_OBJECT_ACCEPTING_STATIC_OBSERVABLE_CREATORS.includes(expression.callee.name);
}
9 changes: 9 additions & 0 deletions tests/rules/finnish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ ruleTester({ types: true }).run('finnish', finnishRule, {
`,
options: [{ properties: false }],
},
{
code: stripIndent`
// Static observable creators that accept a sources object
import { of, combineLatest, forkJoin } from "rxjs";

combineLatest({ one: of(0), two: of('a') });
forkJoin({ one: of(0), two: of('a') });
`,
},
],
invalid: [
fromFixture(
Expand Down
9 changes: 9 additions & 0 deletions tests/rules/suffix-subjects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ ruleTester({ types: true }).run('suffix-subjects', suffixSubjectsRule, {
const mySubject = new MySubject<number>();
`,
},
{
code: stripIndent`
// Static observable creators that accept a sources object
import { Subject, BehaviorSubject, combineLatest, forkJoin } from "rxjs";

combineLatest({ one: new Subject<number>(), two: new BehaviorSubject('a') });
forkJoin({ one: new Subject<number>(), two: new BehaviorSubject('a') });
`,
},
],
invalid: [
fromFixture(
Expand Down