What is @eslint-community/eslint-utils?
@eslint-community/eslint-utils is a utility library for working with ESLint. It provides a set of tools to help with creating, testing, and debugging ESLint rules and plugins.
What are @eslint-community/eslint-utils's main functionalities?
getStaticValue
The `getStaticValue` function evaluates the static value of a given AST node. This is useful for determining the value of constants and literals in the code.
const { getStaticValue } = require('@eslint-community/eslint-utils');
const espree = require('espree');
const ast = espree.parse('const foo = 42;', { ecmaVersion: 2020 });
const node = ast.body[0].declarations[0].init;
const value = getStaticValue(node);
console.log(value); // { value: 42 }
ReferenceTracker
The `ReferenceTracker` class helps track references to global variables and properties. This is useful for identifying where and how certain variables or properties are used in the code.
const { ReferenceTracker } = require('@eslint-community/eslint-utils');
const espree = require('espree');
const eslintScope = require('eslint-scope');
const ast = espree.parse('const foo = { bar: 42 }; foo.bar;', { ecmaVersion: 2020 });
const scopeManager = eslintScope.analyze(ast);
const tracker = new ReferenceTracker(scopeManager);
for (const { node } of tracker.iterateGlobalReferences({ foo: { bar: true } })) {
console.log(node); // Node representing `foo.bar`
}
getFunctionHeadLocation
The `getFunctionHeadLocation` function returns the location of the function head, which includes the function keyword, name, and parameters. This is useful for reporting issues related to function definitions.
const { getFunctionHeadLocation } = require('@eslint-community/eslint-utils');
const espree = require('espree');
const ast = espree.parse('function foo() {}', { ecmaVersion: 2020 });
const node = ast.body[0];
const location = getFunctionHeadLocation(node, { sourceCode: { text: 'function foo() {}' } });
console.log(location); // { start: { line: 1, column: 0 }, end: { line: 1, column: 13 } }
Other packages similar to @eslint-community/eslint-utils
eslint
ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code. It is highly configurable and can be extended with custom rules and plugins. While @eslint-community/eslint-utils provides utilities for working with ESLint, ESLint itself is the core tool for linting JavaScript code.
eslint-plugin-import
eslint-plugin-import is a plugin that provides linting rules for validating proper imports. It helps ensure that import statements are valid and follow best practices. While @eslint-community/eslint-utils provides general utilities for ESLint, eslint-plugin-import focuses specifically on import/export syntax.
eslint-plugin-react
eslint-plugin-react is a plugin that provides linting rules for React applications. It helps enforce best practices and catch common issues in React code. While @eslint-community/eslint-utils provides general utilities for ESLint, eslint-plugin-react focuses specifically on React.