ts-code-frame is a visual terminal code frame pointer generator.
pnpm add @se-oss/code-frameInstall using your favorite package manager
npm
npm install @se-oss/code-frameyarn
yarn add @se-oss/code-frameUse createCodeFrame to generate a pointer at a specific string index.
import { createCodeFrame } from '@se-oss/code-frame';
const code = 'const a = 1;\nconst b = 2;\nconst c = 3;';
const result = createCodeFrame(code, 15);
console.log(result);Use codeFrameColumns to specify exact line and column ranges.
import { codeFrameColumns } from '@se-oss/code-frame';
const code = 'const a = 1;\nconst b = 2;\nconst c = 3;';
const result = codeFrameColumns(code, {
start: { line: 2, column: 7 },
end: { line: 2, column: 8 },
});
console.log(result);Add custom messages pointing to the error location.
const result = codeFrameColumns(
code,
{ start: { line: 2, column: 7 } },
{ message: 'Unexpected token' }
);Enable colored output using native node utility styling.
const result = codeFrameColumns(
code,
{ start: { line: 2, column: 7 } },
{ highlightCode: true }
);Control context windows above and below the error.
const result = codeFrameColumns(
code,
{ start: { line: 2, column: 7 } },
{
linesAbove: 1,
linesBelow: 1,
}
);Manually syntax highlight code snippets.
import { highlight } from '@se-oss/code-frame';
const colored = highlight('const x = 42;');For all configuration options, please see the API docs.
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.
Thanks again for your support, it is much appreciated! ๐
MIT ยฉ Shahrad Elahi and contributors.