@dev-build-deploy/diagnose-it
Version:
Expressive Diagnostics library
126 lines (125 loc) • 4.24 kB
JavaScript
;
/*
* SPDX-FileCopyrightText: 2023 Kevin de Jong <monkaii@hotmail.com>
* SPDX-License-Identifier: MIT
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FixItHint = exports.ModificationColorCodes = void 0;
const chalk_1 = __importDefault(require("chalk"));
/**
* Color codes for the modification types
* @member DEFAULT Red
* @member INSERT Green
* @member REMOVE Red
* @member REPLACE Yellow
*/
exports.ModificationColorCodes = {
DEFAULT: chalk_1.default.red,
INSERT: chalk_1.default.green,
REMOVE: chalk_1.default.red,
REPLACE: chalk_1.default.yellow,
};
/**
* Fix-it Hint
*
* @class FixItHint
* @member modification Modification type (DEFAULT, INSERT, REMOVE, REPLACE)
* @member range Range in the string
* @member text Text to insert, replace or remove
*
* @example Inserting character(s)
* ```typescript
* const hint = FixItHint.createInsertion(10, "Hello");
* ```
*
* @example Replacing character(s) with the provided hint
* ```typescript
* const hint = FixItHint.createReplacement({ index: 10, length: 5 }, "Hello");
* ```
*
* @example Removing character(s)
* ```typescript
* const hint = FixItHint.createRemoval({ index: 10, length: 5 });
* ```
*/
class FixItHint {
/**
* @param modification Modification type (DEFAULT, INSERT, REMOVE, REPLACE)
* @param range Range in the string
* @private
*/
constructor(modification, range, text) {
this.modification = modification;
this.range = range;
if (modification === "INSERT" || modification === "REPLACE") {
if (text === undefined)
throw new Error("Text is required for INSERT and REPLACE modifications");
if (modification === "INSERT" && range.length > 1)
throw new Error("Length must be 1 for INSERT modifications");
this.text = text;
}
else if (modification === "REMOVE" || modification === "DEFAULT") {
if (text !== undefined)
throw new Error("Text is not supported for REMOVE modifications");
}
}
/**
* Creates a FixIt Hint without any modification type
* @param range Range in the string
* @returns Fix-it Hint Object
*/
static create(range) {
return new FixItHint("DEFAULT", range);
}
/**
* Creates a modification hint for inserting character(s) at a specific index
* @param index Index for the insertion
* @param text Text to insert
* @returns Fix-it Hint Object
*/
static createInsertion(index, text) {
return new FixItHint("INSERT", { index, length: 1 }, text);
}
/**
* Creates a modification hint for replacing character(s) at a specific index
* @param range Range to replace
* @param text Text to replace with
* @returns Fix-it Hint Object
*/
static createReplacement(range, text) {
return new FixItHint("REPLACE", range, text);
}
/**
* Creates a modification hint for removing character(s) at a specific index
* @param range Range to remove
* @returns Fix-it Hint Object
*/
static createRemoval(range) {
return new FixItHint("REMOVE", range);
}
/**
* Applies the fix-it hint to the provided line
* @param line Line to apply the fix-it hint to
* @returns Line with the fix-it hint applied
*/
apply(line) {
let result = line;
switch (this.modification) {
case "INSERT":
result = result.slice(0, this.range.index - 1) + this.text + result.slice(this.range.index - 1);
break;
case "REMOVE":
result = result.slice(0, this.range.index - 1) + result.slice(this.range.index - 1 + this.range.length);
break;
case "REPLACE":
result =
result.slice(0, this.range.index - 1) + this.text + result.slice(this.range.index - 1 + this.range.length);
break;
}
return result;
}
}
exports.FixItHint = FixItHint;