@codeque/cli
Version:
Multiline code search for every language. Structural code search for JavaScript, TypeScript, HTML and CSS
60 lines (59 loc) • 2.59 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCodeFrame = exports.printVersionNumber = exports.print = exports.prepareHintText = exports.textEllipsis = void 0;
const path_1 = __importDefault(require("path"));
const code_frame_1 = require("@babel/code-frame");
const prettier_1 = require("prettier");
const textEllipsis = (text, maxLength) => {
const charsToReplace = Math.max(text.length - maxLength, 0);
const ellipsis = '...';
const shortenedRoot = charsToReplace > 0
? text.replace(new RegExp(`^(.){${charsToReplace + ellipsis.length}}`), ellipsis)
: text;
return shortenedRoot;
};
exports.textEllipsis = textEllipsis;
const prepareHintText = (hint) => {
return hint.tokens
.map(({ content, type }) => (type === 'code' ? `\`${content}\`` : content))
.join(' ');
};
exports.prepareHintText = prepareHintText;
exports.print = console.log;
const printVersionNumber = () => {
const versionNumber = require(path_1.default.join(__dirname, '../package.json')).version;
(0, exports.print)();
(0, exports.print)(versionNumber);
(0, exports.print)();
};
exports.printVersionNumber = printVersionNumber;
const getCodeFrame = (code, startLine, formatting = false, errorPos) => {
var _a;
const formatted = formatting ? (0, prettier_1.format)(code, { parser: 'babel-ts' }) : code;
const linesCount = (((_a = formatted.match(/\n/g)) === null || _a === void 0 ? void 0 : _a.length) || 0) + (!formatting ? 1 : 0);
const maxLineLength = (startLine + linesCount).toString().length;
const indicateError = errorPos !== undefined;
const linesBelow = indicateError ? linesCount - errorPos.line : linesCount;
const linesAbove = indicateError ? errorPos.line : undefined;
const errorLocation = indicateError
? {
start: errorPos,
end: errorPos,
}
: { start: { line: 0 } };
let codeFrame = (0, code_frame_1.codeFrameColumns)(formatted, errorLocation, {
highlightCode: true,
linesBelow,
linesAbove,
});
for (let i = linesCount; i > 0; i--) {
const frameLineNum = i;
const originalPaddingLen = frameLineNum.toString().length;
codeFrame = codeFrame.replace(` ${frameLineNum} |`, ` ${startLine + i - 1} |`.padStart(maxLineLength + 2 + originalPaddingLen, ' '));
}
return codeFrame;
};
exports.getCodeFrame = getCodeFrame;
;