agiledigital-labs / agiledigital-labs/eslint-plugin-total-functions

New rule: detect premature logging in fp-ts code

未关闭
#734 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
fp-ts
主要语言
TypeScript
星标
92
派生
5
PR 合并指标
30 天内没有已合并 PR

描述

Some codebases will have embraced TaskEither and friends but will not have adopted a hard-line stance against impure logging.

In these codebases, imperative logging is tolerated. Fine you might say, but it does open us up to the antipattern of premature logging -- logging that happens during program construction when it should actually happen during program execution.

Here's an example:

```ts
const launch = (numberToLaunch: number): TaskEither => {
// This logging is premature. This is the line that would be flagged by the proposed rule.
// The author believed they were logging during program execution but they are actually logging during program construction.
console.info(`Launching ${numberToLaunch} nukes!`);
// TE.of is just for illustration here. It could be an HTTP call or any other impure operation
// and the effect type doesn't have to be TE (it could be IO, RTE, etc).
return TE.of(`${numberToLaunch} nukes launched`);
};

// This returns an effect but doesn't interpret (execute) it.
const launchSevenNukes = launch(7);

// At this point, the logs say "Launching 7 nukes" but no such operation has actually started!

// It is only after we interpret the effect that the actual nuke launching operation has started.
const launchNukesPromise = launchSevenNukes();

// Here is the first moment we should actually see logs.

// And here is the actual result of launching.
const result = await launchNukesPromise;
```

If we construct the effect and then:
* defer execution to much later in the program
* decide not to actually execute it
* execute it multiple times (e.g. to implement retries)
... then the logs are going to be misleading to one degree or another.

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。