Frontend School

Real-time Linting Code Editor Component

Machine Coding
hard

Real-time Linting Code Editor Component

Hard
React
UI
Code Editor
Syntax Highlighting
Linting
Front-end

Overview

Gupshup's internal developer tools require a lightweight, performant code editor for quick script authoring and testing. This problem focuses on building the core editing experience, providing immediate visual feedback to the user through syntax highlighting and basic real-time linting. This component is crucial for improving developer efficiency and reducing common syntax errors in small code snippets.

What you need to do / Task

Your task is to build a CodeEditor React component that offers a basic yet functional editing experience with visual feedback.

  1. Editor Structure: Create a resizable or fixed-size editor area that displays code. It should consist of two main synchronized parts: line numbers on the left and the editable code content on the right.
  2. Line Numbers: Dynamically display line numbers that correspond to the code lines in the editor. These numbers must scroll synchronously with the code content.
  3. Basic Syntax Highlighting: Implement real-time syntax highlighting for common JavaScript-like constructs:
    • Keywords: const, let, function, return, if, else, for, while, class
    • Strings: Text enclosed in single (') or double (") quotes.
    • Comments: Single-line comments starting with //.
  4. Real-time Linting: As the user types, identify and highlight lines that are missing a semicolon (;) at the end. A line is considered to be missing a semicolon if it is not empty, does not end with { or }, and does not end with // (start of a comment).
  5. Error Indicators: For each line identified with a linting error, display a small, visually distinct indicator (e.g., a red dot or icon) next to its corresponding line number.
  6. Synchronization: Ensure smooth and accurate synchronization of scrolling, line height, and content display between the editable textarea, the line numbers, and the syntax-highlighted overlay.

UI/UX expectations

  • The editor should present a clean, minimalist interface.
  • Line numbers should be visually distinct (e.g., a lighter grey color) from the code content.
  • Syntax highlighting should use at least three distinct colors: one for keywords, one for strings, and one for comments. (Feel free to choose your own palette).
  • Linting error indicators should be clearly visible and positioned next to the line numbers.
  • The editor should use a monospace font for consistent character and line spacing.
  • The entire editor component (line numbers + code area) should have a reasonable fixed height with vertical scrolling for overflow.

Acceptance criteria

  • The editor component renders successfully with initial code.
  • Line numbers are correctly displayed for all lines of code and update as lines are added/removed.
  • Scrolling the code area also scrolls the line numbers and highlighted content synchronously.
  • Keywords (const, function), strings ("hello", 'world'), and comments (// a comment) are highlighted with distinct colors.
  • Typing let x = 1 immediately shows a linting error indicator next to that line's number.
  • Modifying let x = 1 to let x = 1; immediately removes the linting error indicator.
  • Lines ending with { (e.g., function foo() {), } (e.g., }), or consisting solely of a comment (e.g., // This is fine) do not show a linting error.
  • Empty lines do not show a linting error.
  • A line like const x = 1 // some comment does show a linting error, as the statement const x = 1 is missing a semicolon.

Constraints

Constraints & assumptions

  • The editor should be implemented using React and plain JavaScript/TypeScript. No external code editor libraries (e.g., Monaco Editor, CodeMirror) are allowed.
  • Focus on functional correctness and a reasonable level of performance for typical script snippets (e.g., up to ~100 lines of code). Extreme optimization for very large files is not required within the time limit.
  • You can assume a monospace font is used for consistent line height and character width.
  • The browser's default copy/paste, undo/redo functionality for the textarea is sufficient.
  • No need to implement advanced features like autocomplete, multi-cursor editing, or complex error reporting (e.g., tooltips with error messages).

Out of scope

  • Full language parsing beyond the specified linting rule.
  • Advanced IDE features (e.g., code folding, refactoring).
  • Theming or extensive customization options.
  • Handling non-ASCII characters or complex internationalization for syntax/linting.

What interviewers evaluate

  • Architectural Design: Clarity and organization of the component structure, state management, and separation of concerns.
  • React Proficiency: Effective use of React hooks, props, and rendering mechanisms for a dynamic UI.
  • Algorithm & Logic: Correct and efficient implementation of syntax highlighting and the real-time linting rule, especially comment handling.
  • UI/UX Fidelity: Accurate synchronization of visual elements (line numbers, highlighting, errors) with user input and scrolling.
  • Code Quality: Readability, maintainability, and defensive programming practices.

Loading editor...