Skip to content

Commit

Permalink
fix: Resolve css parser issue caused by interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
youthfulhps committed Sep 21, 2023
1 parent 648c3d2 commit 9ee5bb5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/helpers/extractor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { AST } from 'prettier';
import { generateJSXOpeningElementClassNameAttribute } from '~/helpers/generator';
import {
generateConcatenatedCSSTemplateLiteral,
generateJSXOpeningElementClassNameAttribute,
} from '~/helpers/generator';
import { convertStyles } from '~/helpers/converter';
import { parseSass } from '~/helpers/css-parser';

Expand Down Expand Up @@ -95,12 +98,16 @@ export function getVariableDeclarationThroughStyledRecursively(ast: AST) {
'value' in node.init.quasi.quasis[0] &&
isObject(node.init.quasi.quasis[0].value)
) {
const parsedScss = parseSass(node.init.quasi.quasis[0].value.raw);
const sassScript = generateConcatenatedCSSTemplateLiteral(
node.init.quasi.quasis,
);

const parsedCSS = parseSass(sassScript);

const componentDeclaration: ComponentDeclaration = {
name: node.id.name,
tag: node.init.tag.property.name,
styles: parsedScss,
styles: parsedCSS,
};

componentDeclarations.push(componentDeclaration);
Expand Down
14 changes: 14 additions & 0 deletions src/helpers/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,17 @@ export function generateJSXOpeningElementClassNameAttribute(

return attributes;
}

export function generateConcatenatedCSSTemplateLiteral(quasis: any[]) {
return quasis.reduce((sum, curr) => {
if (!curr.tail) {
const replaced = curr.value.raw.replace(/\n|\s/g, '');
if (replaced[replaced.length - 1] === ':') {
return sum + `${curr.value.raw} unset`;
}

return sum + curr.value.raw;
}
return sum + curr.value.raw;
}, '');
}

0 comments on commit 9ee5bb5

Please sign in to comment.