Repository: eslint/eslint
File: lib/rules/utils/fix-tracker.js (JavaScript)

Code before:

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

/**
 * A helper class to combine fix options into a fix command. Currently, it
 * exposes some "retain" methods that extend the range of the text being
 * replaced so that other fixes won't touch that region in the same pass.
 */
class FixTracker {
	/**
	 * Create a new FixTracker.
	 * @param {ruleFixer} fixer A ruleFixer instance.
	 * @param {SourceCode} sourceCode A SourceCode object for the current code.
	 */
	constructor(fixer, sourceCode) {
		this.fixer = fixer;
		this.sourceCode = sourceCode;
		this.retainedRange = null;
	}

	/**
	 * Mark the given range as "retained", meaning that other fixes may not
	 * may not modify this region in the same pass.
	 * @param {number[]} range The range to retain.
	 * @returns {FixTracker} The same RuleFixer, for chained calls.
	 */
	retainRange(range) {
		this.retainedRange = range;
		return this;
	}

	/**
	 * Given a node, find the function containing it (or the entire program) and
	 * mark it as retained, meaning that other fixes may not modify it in this
	 * pass. This is useful for avoiding conflicts in fixes that modify control
	 * flow.
	 * @param {ASTNode} node The node to use as a starting point.
	 * @returns {FixTracker} The same RuleFixer, for chained calls.
	 */
	retainEnclosingFunction(node) {
		const functionNode = astUtils.getUpperFunction(node);

		return this.retainRange(
			functionNode ? functionNode.range : this.sourceCode.ast.range,
		);
	}

	/**
	 * Given a node or token, find the token before and afterward, and mark that

Review comment (suggestion):
```suggestion
	 * @param {SourceRange} range The range to retain.
```

Code after:
 * @import { SourceRange } from "@eslint/core";
 */

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("./ast-utils");

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

/**
 * A helper class to combine fix options into a fix command. Currently, it
 * exposes some "retain" methods that extend the range of the text being
 * replaced so that other fixes won't touch that region in the same pass.
 */
class FixTracker {
	/**
	 * Create a new FixTracker.
	 * @param {ruleFixer} fixer A ruleFixer instance.
	 * @param {SourceCode} sourceCode A SourceCode object for the current code.
	 */
	constructor(fixer, sourceCode) {
		this.fixer = fixer;
		this.sourceCode = sourceCode;
		this.retainedRange = null;
	}

	/**
	 * Mark the given range as "retained", meaning that other fixes may not
	 * may not modify this region in the same pass.
	 * @param {SourceRange} range The range to retain.
	 * @returns {FixTracker} The same RuleFixer, for chained calls.
	 */
	retainRange(range) {
		this.retainedRange = range;
		return this;
	}

	/**
	 * Given a node, find the function containing it (or the entire program) and
	 * mark it as retained, meaning that other fixes may not modify it in this
	 * pass. This is useful for avoiding conflicts in fixes that modify control
	 * flow.
	 * @param {ASTNode} node The node to use as a starting point.
	 * @returns {FixTracker} The same RuleFixer, for chained calls.
	 */
	retainEnclosingFunction(node) {
		const functionNode = astUtils.getUpperFunction(node);