diff --git a/exporters/variables-scss/src/formatters/stylesFormatter.ts b/exporters/variables-scss/src/formatters/stylesFormatter.ts index c74da8c352..554ca7f1ec 100644 --- a/exporters/variables-scss/src/formatters/stylesFormatter.ts +++ b/exporters/variables-scss/src/formatters/stylesFormatter.ts @@ -9,27 +9,39 @@ export const formatLinesAtEndOfTheFile = (css: string): string => { return css.replace(/\n{2,}$/, '\n'); }; +const formattingConfig = { + js: { + indentation: ' ', + openingBracket: '{', + closingBracket: '}', + }, + scss: { + indentation: ' ', + openingBracket: '(', + closingBracket: ')', + }, +}; + export const indentAndFormat = (css: string, hasJsOutput: boolean): string => { - const INDENTATION = hasJsOutput ? JS_INDENTATION : SCSS_INDENTATION; + const fileType = hasJsOutput ? 'js' : 'scss'; + const { indentation, openingBracket, closingBracket } = formattingConfig[fileType]; let indentationLevel = 0; let formattedCSS = ''; const lines = css.split('\n'); - const openBracket = hasJsOutput ? '{' : '('; - const closeBracket = hasJsOutput ? '}' : ')'; for (const line of lines) { - // Check if both openBracket and closeBracket are on the same line - if (line.includes(openBracket) && line.includes(closeBracket)) { - formattedCSS += `${INDENTATION.repeat(indentationLevel)}${line}\n`; - } else if (line.includes(openBracket)) { - formattedCSS += `${INDENTATION.repeat(indentationLevel)}${line}\n`; + // Check if both openingBracket and closeBracket are on the same line + if (line.includes(openingBracket) && line.includes(closingBracket)) { + formattedCSS += `${indentation.repeat(indentationLevel)}${line}\n`; + } else if (line.includes(openingBracket)) { + formattedCSS += `${indentation.repeat(indentationLevel)}${line}\n`; indentationLevel += 1; - } else if (line.includes(closeBracket)) { + } else if (line.includes(closingBracket)) { indentationLevel -= 1; - formattedCSS += `${INDENTATION.repeat(indentationLevel)}${line}\n`; + formattedCSS += `${indentation.repeat(indentationLevel)}${line}\n`; } else { - formattedCSS += `${INDENTATION.repeat(indentationLevel)}${line}\n`; + formattedCSS += `${indentation.repeat(indentationLevel)}${line}\n`; } }