Frontend School

Code Snippet Editor with Live Diagnostics

Machine Coding
hard

Code Snippet Editor with Live Diagnostics

Hard
Frontend
React
TypeScript
Editor
diagnostics
Syntax Highlighting

Overview

Freshworks products often empower users with scripting capabilities, enabling custom logic for automation, integrations, or data manipulation. A robust and user-friendly code editor component is crucial for these developers and administrators to write and debug their code snippets effectively. This problem challenges you to build a core part of such an editor, focusing on real-time feedback and a smooth user experience, which directly impacts developer productivity within our platforms.

What you need to do / Task

Your goal is to implement an interactive code editor component that provides real-time syntax highlighting and basic linting for a simplified JavaScript-like language.

  1. Code Editor Foundation: Set up a basic multi-line code input area that functions as a single component.
  2. Real-time Syntax Highlighting: Implement syntax highlighting for a predefined set of language constructs (keywords, strings, single-line comments).
  3. Basic Linter Integration: Develop a simple 'linter' that detects common syntax issues (e.g., unclosed brackets, missing semicolons, unknown keywords/identifiers) in real-time.
  4. Inline Diagnostics Display: Visually present the detected diagnostics (errors/warnings) directly within the editor's text area (e.g., using wavy underlines, custom tooltips on hover).
  5. Performance & Responsiveness: Ensure that the editor remains responsive during typing, with highlighting and diagnostics updating efficiently.

UI/UX expectations

  • The editor should provide a seamless typing experience, with minimal lag for updates.
  • Syntax highlighting should be visually distinct (e.g., different colors for keywords, strings, comments) and applied to the text as the user types.
  • Diagnostics should be non-intrusive but clearly visible. A common pattern is to show a wavy underline beneath the problematic code segment, with a tooltip appearing on hover to display the diagnostic message.
  • The editor should have a reasonable default size and be visually integrated with a diagnostics panel displaying a list of all current issues.
  • Scrolling the editor content should also scroll the highlighted/diagnosed overlay to maintain alignment.

Acceptance criteria

  • Basic text input and editing functionality works correctly within the editor.
  • Keywords (e.g., function, if, return, var, const) are highlighted with a distinct color.
  • Strings (enclosed in single or double quotes) are highlighted with a distinct color.
  • Single-line comments (//) are highlighted with a distinct color and style (e.g., italic).
  • Unclosed parentheses () and curly braces {} are detected and marked as 'error' diagnostics.
  • Missing semicolons at the end of relevant lines (e.g., statements not ending with } or //) are detected as 'warning' diagnostics.
  • Simple unknown identifiers (words not matching keywords, strings, or comments) are detected as 'warning' diagnostics.
  • Diagnostics are displayed visually within the editor (e.g., wavy underlines).
  • Hovering over an underlined diagnostic in the editor displays its message in a tooltip.
  • All syntax highlighting and diagnostic feedback updates within approximately 300ms of user input.
  • The editor is visually presentable and scrolls correctly.

Constraints

Constraints & assumptions

  • Focus on a simplified subset of JavaScript-like syntax for highlighting and linting. No need for full JS grammar support.
  • For highlighting and basic linting, regular expressions and simple string parsing are sufficient. Complex Abstract Syntax Tree (AST) parsing is not required.
  • The 'language' will include keywords, single/double-quoted strings, and single-line comments.
  • Performance should be optimized for typical code snippet lengths (e.g., up to ~100 lines).
  • The solution should be implemented using React and TypeScript.

Out of scope

  • Full-fledged IDE features like autocomplete, refactoring, multi-cursor editing, or complex language services.
  • Advanced language parsing (e.g., scope analysis, type checking, syntax error recovery beyond basic checks).
  • Support for all JavaScript syntax features (e.g., template literals, multi-line comments).
  • Multi-file projects or module resolution.
  • Persistence of code content.

What interviewers evaluate

  • Technical Proficiency: Correct and robust implementation of syntax highlighting and diagnostic logic, including handling edge cases.
  • Architecture & Design: Effective state management, component structure, and clear separation of concerns within the editor component.
  • Performance & Responsiveness: Ensuring a smooth user experience with efficient, real-time updates through debouncing and optimized rendering.
  • Code Quality: Readability, maintainability, modularity, and appropriate use of data structures and algorithms.

Loading editor...