unicorn/prefer-dispose
eslint-plugin-unicorn
Suggestion
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-dispose": "error",
},
},
];Prefer using using/await using over manual try/finally resource disposal.
const file = open();
try {
read(file);
} finally {
file.close();
}const reader = openReader();
const writer = openWriter();
try {
pipe(reader, writer);
} finally {
writer.close();
reader.close();
}async function run() {
const connection = await connect();
try {
await query(connection);
} finally {
await connection.close();
}
}{
using file = open();
read(file);
}{
using reader = openReader();
using writer = openWriter();
pipe(reader, writer);
}async function run() {
await using connection = await connect();
await query(connection);
}Examples from the ESLint documentation.
No tracked preset enables this rule, so it has no reach yet.
None of the tracked presets enable this rule. Turn it on with the config below.
unicorn/prefer-dispose
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-dispose": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Prefer Iterator.concat(…) over temporary spread arrays.
Prefer JSON imports over reading and parsing JSON files.