eslint-plugin-safeguard
Version:
A custom ESLint plugin that provides multiple rules to enforce best practices and code safety.
74 lines (51 loc) • 1.59 kB
Markdown
- Prevents **self-assignment**, e.g., `obj.prop = obj.prop`.
- Detects **self-spreading in arrays**, e.g., `array.push(...array)`.
- Helps catch logical mistakes and improve code quality.
1. Install the plugin using `pnpm`, `npm`, or `yarn`:
```sh
pnpm add -D eslint-plugin-safeguard
npm install --save-dev eslint-plugin-safeguard
yarn add -D eslint-plugin-safeguard
```
2. Add the rule to your ESLint configuration (`eslint.config.js`):
```js
import safeguard from 'eslint-plugin-safeguard';
```
```js
{
plugins: {
safeguard,
},
"rules": {
"safeguard/no-self-assignments": "error"
}
}
```
```ts
const obj = { a: '' };
obj.a = obj.a; // ❌ Self-assignment does nothing
const arr = [];
arr.push(...arr); // ❌ Spreading itself into push is redundant
```
```ts
const obj = { a: '' };
obj.a = 'new value'; // ✅ Assigns a meaningful value
const arr = [1, 2, 3];
arr.push(4); // ✅ Pushes a new element
```
- **Checks assignments (`=`)**: If `obj.prop = obj.prop` is detected, it reports an error.
- **Checks method calls (`.push()` with self-spreading)**: If `array.push(...array)` is detected, it reports an error.
- **Ignores computed properties (`obj[key] = obj[key]`)** to avoid false positives.
## 📝 License
This project is licensed under the MIT License - see the [LICENSE](../../LICENSE) file for details.
---
**Author:** Estarlin R ([estarlincito.com](https://estarlincito.com))