123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
-
- "use strict";
-
-
-
-
-
- const astUtils = require("../util/ast-utils");
-
-
-
-
-
-
- class FixTracker {
-
-
-
- constructor(fixer, sourceCode) {
- this.fixer = fixer;
- this.sourceCode = sourceCode;
- this.retainedRange = null;
- }
-
-
-
- retainRange(range) {
- this.retainedRange = range;
- return this;
- }
-
-
-
- retainEnclosingFunction(node) {
- const functionNode = astUtils.getUpperFunction(node);
-
- return this.retainRange(functionNode ? functionNode.range : this.sourceCode.ast.range);
- }
-
-
-
- retainSurroundingTokens(nodeOrToken) {
- const tokenBefore = this.sourceCode.getTokenBefore(nodeOrToken) || nodeOrToken;
- const tokenAfter = this.sourceCode.getTokenAfter(nodeOrToken) || nodeOrToken;
-
- return this.retainRange([tokenBefore.range[0], tokenAfter.range[1]]);
- }
-
-
-
- replaceTextRange(range, text) {
- let actualRange;
-
- if (this.retainedRange) {
- actualRange = [
- Math.min(this.retainedRange[0], range[0]),
- Math.max(this.retainedRange[1], range[1])
- ];
- } else {
- actualRange = range;
- }
-
- return this.fixer.replaceTextRange(
- actualRange,
- this.sourceCode.text.slice(actualRange[0], range[0]) +
- text +
- this.sourceCode.text.slice(range[1], actualRange[1])
- );
- }
-
-
-
- remove(nodeOrToken) {
- return this.replaceTextRange(nodeOrToken.range, "");
- }
- }
-
- module.exports = FixTracker;
|