-
Notifications
You must be signed in to change notification settings - Fork 246
Open
Labels
enhancementjavascriptPull requests that update Javascript codePull requests that update Javascript code
Description
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:
- If a user writes SCSS code and switches to the Sass tab, the SCSS syntax remains but with Sass syntax highlighting, causing syntax errors
- Users must manually rewrite their code in the new syntax to see it work
- 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
-
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
-
Preserving semantics: Need to ensure:
- Comments are preserved
- Variable names and structure remain intact
- Formatting is readable
-
Error handling: Handle cases where conversion fails gracefully
Alternative Approaches
- Opt-in toggle: Add a checkbox "Auto-convert syntax when switching" to preserve current behavior for users who prefer it
- Two-way binding: Show both syntaxes simultaneously in split view
- Use external library: Leverage existing tools like
sass-convert
Questions for Maintainers
- Is this feature aligned with the playground's goals?
- 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
Labels
enhancementjavascriptPull requests that update Javascript codePull requests that update Javascript code