Simple Code Frame

1.3.0 · active · verified Sun Apr 19

Simple Code Frame is a lightweight JavaScript utility designed for generating structured code frames, primarily used in error reporting or diagnostic tools. Its current stable version is 1.3.0. Unlike more comprehensive alternatives like `@babel/code-frame`, `simple-code-frame` distinguishes itself by operating directly on raw string input without requiring an AST parser. This focused design makes it exceptionally small and performant for tasks that only involve highlighting specific lines and columns within arbitrary text. The library ships with TypeScript types, providing a good developer experience for both JavaScript and TypeScript projects. Releases appear to follow a moderate cadence, adding features like `maxWidth` for line truncation and colored output in recent minor versions. It is suitable for environments where parsing overhead is undesirable, focusing solely on visual presentation of code context.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates generating a code frame for a specific line and column, including options for highlighting and message, and usage of `maxWidth` for truncation.

import { createCodeFrame } from 'simple-code-frame';

const sourceCode = `function greet(name) {
  console.log('Hello, ' + name + '!');
  throw new Error('Something went wrong');
}

greet('World');`;

// Simulate an error at line 3, column 9
const errorFrame = createCodeFrame(sourceCode, 3, 9, {
  highlightColor: 'red',
  message: 'Error here!'
});

console.log(errorFrame);

// Example with maxWidth and no message
const longLineCode = `const veryLongVariableName = 'This is a very long string that should be truncated by maxWidth.';
console.log(veryLongVariableName);`;
const truncatedFrame = createCodeFrame(longLineCode, 1, 1, { maxWidth: 50 });
console.log('\n--- Truncated Frame ---\n');
console.log(truncatedFrame);

view raw JSON →