@mconst/eslint-plugin-must-use
v0.0.3
Published
A plugin for ESLint that emulates the `#must_use` attribute in Rust
Readme
ESLint plugin: must use
This is a plugin for Typescript/ESLint that enables you to enforce the usage of values of a certain type. It works much like the #must_use attribute in Rust.
For example, let's say you have a Result<A, E> type, that represents a success (A) or a failure (E) in your code. You want every instance of Result to be used within your code, so even when the successful value of a result is void, ESLint will force developers to use the result, if only to check that it was successful.
In this case, you would pass Result as a type to the plugin in your ESLint config file:
import { defineConfig } from "eslint/config";
import mustUse from "@mconst/eslint-plugin-must-use";
export default defineConfig([
{
// ...
plugins: {
// ...
"@mustUse": mustUse,
// ...
},
// ...
rules: {
"@mustUse/mustUse": ["error", { types: ["Result"] }],
},
}
]);After this, your code will be valid if every instance of Result is used:
declare function computeResult(): Result<void, Error>;
// the result is stored in a constant -> valid code
const result = computeResult();
// the result is ignored -> invalid code
computeResult();