Skip to content

Fix bug with variable composition #27

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 23 additions & 4 deletions source/rules/prefer-composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
isCallExpression,
isIdentifier,
isMemberExpression,
isVariableDeclarator,
isVariableDeclarator
} from "eslint-etc";
import { ruleCreator } from "../utils";

Expand Down Expand Up @@ -141,6 +141,18 @@ const rule = ruleCreator({
return entry;
}

function getArgumentName(callExpression: es.CallExpression) {
const { arguments: args } = callExpression;
if (args.length === 0) {
return undefined;
}
const [arg] = args;
if (isIdentifier(arg)) {
return arg.name;
}
return undefined;
}

function getMethodCalleeName(callExpression: es.CallExpression) {
const { callee } = callExpression;
if (isMemberExpression(callee)) {
Expand Down Expand Up @@ -227,7 +239,7 @@ const rule = ruleCreator({
const { name } = identifier;
const { addCallExpressions, subscriptions } = entry;
const addCallExpression = addCallExpressions.find(
(callExpression) => getMethodCalleeName(callExpression) === name
(callExpression) => getArgumentName(callExpression) === name
);
if (!addCallExpression) {
return false;
Expand All @@ -236,8 +248,15 @@ const rule = ruleCreator({
if (!object || !couldBeSubscription(object)) {
return false;
}
subscriptions.add(name);
return true;
if (isMemberExpression(object) && isIdentifier(object.property)) {
subscriptions.add(object.property.name);
return true;
}
if (isIdentifier(object)) {
subscriptions.add(object.name);
return true;
}
return false;
}

return {
Expand Down
10 changes: 5 additions & 5 deletions tests/rules/prefer-composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import { stripIndent } from "common-tags";
import { fromFixture } from "eslint-etc";
import rule = require("../../source/rules/prefer-composition");
import { ruleTester } from "../utils";
import rule = require("../../source/rules/prefer-composition");

ruleTester({ types: true }).run("prefer-composition", rule, {
valid: [
Expand Down Expand Up @@ -46,10 +46,10 @@ ruleTester({ types: true }).run("prefer-composition", rule, {
value: string;
private subscription = new Subscription();
ngOnInit() {
let subscription = of("foo").subscribe(value => this.value = value);
this.subscription.add(subscription);1
subscription = of("bar").subscribe(value => this.value = value);
this.subscription.add(subscription);
let foo = of("foo").subscribe(value => this.value = value);
this.subscription.add(foo);
foo = of("bar").subscribe(value => this.value = value);
this.subscription.add(foo);
}
ngOnDestroy() {
this.subscription.unsubscribe();
Expand Down