Member called on a local assigned from a method call is reported unused
What happened?
Follow-up to #4311. local.rotate(angle).translate(...) is now credited, but the same call through a local is not. Both of these still report Point.translate:
const rotated = angle ? local.rotate(angle) : local;(our real code)const rotated = local.rotate(angle);
The receiver kind does not matter: a parameter typed Point and a const local = new Point(0, 0) local both fail. A type annotation on the local (const rotated: Point = ...) makes it pass.
Reproduction
mkdir repro30 && cd repro30
echo '{ "name": "repro", "private": true, "type": "module", "main": "src/index.ts" }' > package.json
mkdir node_modules src
cat > src/point.ts <<'EOF'
export class Point {
constructor(
readonly x: number,
readonly y: number,
) {}
rotate(angle: number): Point {
return new Point(this.x * Math.cos(angle), this.y * Math.sin(angle));
}
translate(dx: number, dy: number): Point {
return new Point(this.x + dx, this.y + dy);
}
}
EOF
cat > src/index.ts <<'EOF'
import type { Point } from "./point.js";
export function toWorld(local: Point, center: Point, angle: number): Point {
const rotated = angle ? local.rotate(angle) : local;
return rotated.translate(center.x, center.y);
}
EOF
fallow dead-code
1 entry point detected (1 package.json)
── Unused Code ─────────────────────────────────────
● Unused class members (1)
src/point.ts
:11 Point.translate
Class methods or properties never referenced outside their class
https://fallow.tools/docs/explanations/dead-code/#unused-class-members
✗ 1 class member (0.01s)
Replacing the ternary with const rotated = local.rotate(angle); gives the same output.
Expected behavior
Point.translate is credited through the return type of rotate, and through the ternary whose branches are both Point.
Fallow version
3.42.0
Operating system
Linux
Configuration
None.
Released in v3.43.1 (npm install [email protected]).
fallow dead-code now credits a member on a local that holds a method call result. const rotated = local.rotate(angle); rotated.translate(1, 1); credits Point.translate, the same as the chained call.
Thanks for the report, @Smrtnyk.