Skip to content

Latest commit

Β 

History

681 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ESLint Fix Utils

Utilities for ESLint rule fixers and suggestions. πŸ§‘β€πŸ”§

πŸ‘ͺ All Contributors: 3 🀝 Code of Conduct: Kept πŸ§ͺ Coverage πŸ“ License: MIT πŸ“¦ npm version πŸ’ͺ TypeScript: Strict

Usage

If you're working on an ESLint plugin, install this as a dependency:

pnpm add eslint-fix-utils

You'll then be able to use any of its exported utilities in your rules.

Fixer APIs

fixAddObjectProperty

Version of addObjectProperty that can be passed directly as a fix property.

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,
  });
}

fixRemoveArrayElement

Version of removeArrayElement that can be passed directly as a fix property.

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],
  });
}

fixRemoveObjectProperty

Version of removeObjectProperty that can be passed directly as a fix property.

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],
  });
}

Full APIs

addObjectProperty

Adds a new property to an object expression, along with any necessary commas.

Parameters:

  1. context: Rule.Context
  2. fixer: Rule.RuleFixer
  3. objectExpression: ESTree.ObjectExpression: the object node
  4. propertyKey: string: the value for the new property's key
  5. propertyValue: unknown: the value for the new property's value
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,
	});
}
{
 	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. context
  2. fixer
  3. elementOrIndex: the child expression, spread element, or a numeric index of the child
  4. parentOrElements: the array expression node, or its .elements array
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],
  });
}
[
 	'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. context
  2. fixer
  3. property: the property node
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],
  });
}
{
 	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.

Development

See .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md. Thanks! πŸ”§

Contributors

Azat S.
Azat S.

πŸ€”
Josh Goldberg ✨
Josh Goldberg ✨

πŸ’» πŸ–‹ πŸ“– πŸ€” πŸš‡ 🚧 πŸ“† ⚠️ πŸ”§ πŸ›
michael faith
michael faith

πŸ’» πŸ–‹ πŸ“– πŸ€” πŸš‡ 🚧 πŸ“† ⚠️ πŸ”§

About

Utilities for ESLint rule fixers and suggestions. πŸ§‘β€πŸ”§

Resources

Code of conduct

Contributing

Security policy

Stars

17 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages