Utilities for ESLint rule fixers and suggestions. ๐งโ๐ง
npm install eslint-fix-utils
Utilities for ESLint rule fixers and suggestions.
๐งโ๐ง
If you're working on an ESLint plugin, install this as a dependency:
``shell`
pnpm add eslint-fix-utils
You'll then be able to use any of its exported utilities in your rules.
Version of addObjectProperty that can be passed directly as a fix property.
`ts
import { fixAddObjectProperty } from "eslint-fix-utils";
// ...
export function report(
node: ESTree.ObjectExpression,
propertyKey: string,
propertyValue: string,
) {
context.report({
fix: fixAddObjectProperty(context, node, propertyKey, propertyValue),
messageId,
node,
});
}
`
Version of removeArrayElement that can be passed directly as a fix property.
`ts
import { fixRemoveArrayElement } from "eslint-fix-utils";
// ...
export function report(index: number, node: ESTree.ArrayExpression) {
context.report({
fix: fixRemoveArrayElement(context, index, node.elements),
messageId,
node: node.elements[index],
});
}
`
Version of removeObjectProperty that can be passed directly as a fix property.
`ts
import { fixRemoveObjectProperty } from "eslint-fix-utils";
// ...
export function report(index: number, node: ESTree.ObjectExpression) {
context.report({
fix: fixRemoveObjectProperty(context, node.properties[index]),
messageId,
node: node.properties[index],
});
}
`
#### addObjectProperty
Adds a new property to an object expression, along with any necessary commas.
Parameters:
1. context: Rule.Contextfixer: Rule.RuleFixer
2. objectExpression: ESTree.ObjectExpression
3. : the object nodepropertyKey: string
4. : the value for the new property's keypropertyValue: unknown
5. : the value for the new property's value
`ts
import { addObjectProperty } from "eslint-fix-utils";
// ...
export function report(index: number, node: ESTree.ObjectExpression) {
context.report({
fix: (fixer) {
// Adds a new property to the end of an Object:
return addObjectProperty(context, fixer, node, "type", "module");
},
messageId,
node,
});
}
`
`diff`
{
name: "my-package",
- version: "1.2.3"
+ version: "1.2.3",
+ type: "module"
}
Trailing commas are omitted so that the fixed code will work regardless of whether the language allows them.
#### removeArrayElement
Removes an element from an array expression, along with any commas that are no longer necessary.
Parameters:
1. contextfixer
2. elementOrIndex
3. : the child expression, spread element, or a numeric index of the childparentOrElements
4. : the array expression node, or its .elements array
`ts
import { removeArrayElement } from "eslint-fix-utils";
// ...
export function report(index: number, node: ESTree.ArrayExpression) {
context.report({
fix(fixer) {
// Removes the last element of the array:
return removeArrayElement(context, fixer, index, node.elements);
},
messageId,
node: node.elements[index],
});
}
`
`diff`
[
'a',
- 'b',
- 'c'
+ 'b'
]
Trailing commas are removed so that the fixed code will work regardless of whether the language and location allows them.
#### removeObjectProperty
Removes a property from an object expression, along with any commas that are no longer necessary.
Parameters:
1. contextfixer
2. property
3. : the property node
`ts
import { removeObjectProperty } from "eslint-fix-utils";
// ...
export function report(index: number, node: ESTree.ObjectExpression) {
context.report({
fix(fixer) {
// Removes the last property of the object:
return removeObjectProperty(context, fixer, node.properties[index]);
},
messageId,
node: node.properties[index],
});
}
`
`diff`
{
a: 1,
- b: 2,
- c: 3,
+ b: 2
}
Trailing commas are removed so that the fixed code will work regardless of whether the language and location allows them.
See .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md.
Thanks! ๐ง
Azat S. ๐ค | Josh Goldberg โจ ๐ป ๐ ๐ ๐ค ๐ ๐ง ๐ โ ๏ธ ๐ง ๐ | michael faith ๐ป ๐ ๐ ๐ค ๐ ๐ง ๐ โ ๏ธ ๐ง |
> ๐ This package was templated with create-typescript-app` using the Bingo engine.