Skip to content

Commit f391a37

Browse files
committed
fix: wrong class number issue
1 parent 9ca51f3 commit f391a37

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/twcss-to-sass.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ function getStyleContents(element: IHtmlNode): IHtmlNode {
110110
* Filter IHtmlNode array by node type and tagName
111111
*
112112
* @param {string} htmlJson
113+
* @param {number} deepth
113114
*
114-
* @returns Object
115+
* @returns IHtmlNode
115116
*/
116117
function filterHtmlData(nodeTree: IHtmlNode[], deepth = 0): IHtmlNode[] {
117118
if (nodeTree.length > 0) {
@@ -332,30 +333,25 @@ function groupUtilityToSass(
332333
* Extract SASS tree from HTML JSON tree
333334
*
334335
* @param {IHtmlNode} nodeTree
335-
* @param {int} deepth
336336
*
337337
* @returns string
338338
*/
339-
function getSassTree(nodeTree: IHtmlNode[], deepth = 0) {
339+
function getSassTree(nodeTree: IHtmlNode[]) {
340340
return nodeTree
341341
.map((node: IHtmlNode) => {
342342
let treeString = '',
343343
subTreeString = ''
344344

345345
if (Array.isArray(node.children) && node.children.length) {
346-
++deepth
347-
348-
subTreeString = getSassTree(node.children, deepth)
346+
subTreeString = getSassTree(node.children)
349347
}
350348

351349
if (node.filterAttributes) {
352350
// print tailwind class names
353351
if (node.filterAttributes.class) {
354-
let tailwindClassList = node.filterAttributes.class
352+
treeString += node.filterAttributes.class
355353
? `@apply ${node.filterAttributes.class};`
356354
: ''
357-
358-
treeString += tailwindClassList
359355
}
360356

361357
// inline style printing
@@ -373,17 +369,18 @@ function getSassTree(nodeTree: IHtmlNode[], deepth = 0) {
373369

374370
if (treeString.length || subTreeString.length) {
375371
const classComment = _defaultOptions.printComments
376-
? `/* ${node.comment ? node.comment : node.tagName} -> ${
377-
node.order
372+
? `/* ${node.comment ? node.comment : node.tagName}${
373+
node.order ? ' -> ' + node.order : ''
378374
} */`
379375
: ''
380376

381-
const className = getClassName(node, deepth)
377+
const className = getClassName(node, node.order)
382378

383379
let groupUtilityTree = ''
384380

381+
// convert group utilities
385382
if (node.filterAttributes?.class?.match(/ (group)(?!-)/gm)) {
386-
groupUtilityTree = groupUtilityToSass(node.children, deepth)
383+
groupUtilityTree = groupUtilityToSass(node.children, node.order)
387384

388385
if (groupUtilityTree !== '') {
389386
treeString += groupUtilityTree
@@ -408,24 +405,21 @@ function getSassTree(nodeTree: IHtmlNode[], deepth = 0) {
408405
return null
409406
})
410407
.join('')
411-
412-
return ''
413408
}
414409

415410
/**
416411
* Get ready to use HTML tree
417412
*
418413
* @param {IHtmlNode} nodeTree
419-
* @param {int} deepth
420414
*
421415
* @returns string
422416
*/
423-
function getHtmlTree(nodeTree: IHtmlNode[], deepth = 0): string {
417+
function getHtmlTree(nodeTree: IHtmlNode[]): string {
424418
if (nodeTree) {
425419
let htmlTree = ''
426420

427421
nodeTree.forEach(function (node: IHtmlNode, index) {
428-
const className = getClassName(node, deepth)
422+
const className = getClassName(node, node.order)
429423

430424
if (node.type == 'element' && node.tagName != 'style') {
431425
if (_defaultOptions.printComments) {
@@ -479,7 +473,7 @@ function getHtmlTree(nodeTree: IHtmlNode[], deepth = 0): string {
479473

480474
// sub tree
481475
if (hasSubElement) {
482-
htmlTree += getHtmlTree(node.children, deepth + 1)
476+
htmlTree += getHtmlTree(node.children)
483477
}
484478

485479
// prevent new line for siblings

0 commit comments

Comments
 (0)