Skip to content

Feature Request: Auto-convert syntax when switching between SCSS and Sass tabs in the playground #1505

@amaralazizy

Description

@amaralazizy

Problem

Currently, when switching between SCSS and Sass syntax tabs in the playground, the editor only changes syntax highlighting but doesn't convert the user's code. This means:

  1. If a user writes SCSS code and switches to the Sass tab, the SCSS syntax remains but with Sass syntax highlighting, causing syntax errors
  2. Users must manually rewrite their code in the new syntax to see it work
  3. The conversion only happens for the default example content, not for custom code

Proposed Solution

Automatically convert the user's code between SCSS and Sass syntaxes when switching tabs. This would:

  • Improve learning experience: Users can write in one syntax and immediately see the equivalent in the other
  • Enable syntax comparison: Learners can compare both syntaxes side-by-side while writing their own code
  • Prevent confusion: No more syntax errors when switching tabs
  • Match user expectations: Similar to other code playgrounds (TypeScript, Babel REPL) that show transpiled output

Current Behavior

// In playground.ts, lines 56-64
if (prop === 'inputFormat') {
  let newValue: string | undefined = undefined;
  // Show the default content in the new syntax if the editor still has
  // the default content in the old syntax.
  if (
    playgroundState.inputValue === defaultContents[previousInputFormat]
  ) {
    newValue = defaultContents[state.inputFormat];
  }
  changeSyntax(editor, state.inputFormat === 'indented', newValue);
}

Only converts when the editor contains the default example content.

Proposed Implementation

Add syntax conversion for all user code:

if (prop === 'inputFormat') {
  let newValue: string | undefined = undefined;
  
  if (
    playgroundState.inputValue === defaultContents[previousInputFormat]
  ) {
    // Show default content in new syntax
    newValue = defaultContents[state.inputFormat];
  } else {
    // Convert user's code to the new syntax
    try {
      newValue = convertSyntax(
        playgroundState.inputValue,
        previousInputFormat,
        state.inputFormat
      );
    } catch (error) {
      // If conversion fails, keep existing code and show warning
      console.warn('Failed to convert syntax:', error);
    }
  }
  
  changeSyntax(editor, state.inputFormat === 'indented', newValue);
}

Technical Challenges

  1. Bidirectional conversion: The Sass compiler only converts SCSS/Sass → CSS. We need to:

    • Compile to CSS first using the previous syntax
    • Parse the CSS and format it in the target syntax
    • Or find/create a dedicated SCSS ↔ Sass converter
  2. Preserving semantics: Need to ensure:

    • Comments are preserved
    • Variable names and structure remain intact
    • Formatting is readable
  3. Error handling: Handle cases where conversion fails gracefully

Alternative Approaches

  1. Opt-in toggle: Add a checkbox "Auto-convert syntax when switching" to preserve current behavior for users who prefer it
  2. Two-way binding: Show both syntaxes simultaneously in split view
  3. Use external library: Leverage existing tools like sass-convert

Questions for Maintainers

  1. Is this feature aligned with the playground's goals?
  2. Should conversion be always-on or opt-in?

Additional Context

This feature would particularly benefit:

  • Beginners learning Sass who want to understand both syntaxes
  • Teams migrating between SCSS and Sass
  • Educators demonstrating syntax differences in real-time

I'm willing to implement this feature and would appreciate guidance on the preferred approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions