From a06805dbcd51324b0c280cef790bfbcdf3474d3a Mon Sep 17 00:00:00 2001 From: Brian C Boles Date: Fri, 18 Oct 2019 15:39:59 -0700 Subject: [PATCH 1/2] Update eliminateGlobals to support css declarations that have both global and new classes. --- lib/core/traversalUtils.js | 29 ++++++++++++++++- test/lib/core/traversalUtils.test.js | 47 ++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/lib/core/traversalUtils.js b/lib/core/traversalUtils.js index 9ae4bdf..7b07890 100644 --- a/lib/core/traversalUtils.js +++ b/lib/core/traversalUtils.js @@ -206,6 +206,7 @@ export const getParentSelectorClassesMap = (ast: gASTNode): classMapType => { /* mutates ast by removing instances of :global + If global psuedoClass and class are present only remove psuedoclass. */ export const eliminateGlobals = (ast: gASTNode) => { ast.traverse((node, index, parent) => { @@ -221,7 +222,33 @@ export const eliminateGlobals = (ast: gASTNode) => { fp.get('content'), )(node) ) { - parent.removeChild(index); + const selectorNodes = fp.compose( + fp.filter({ type: 'selector' }), + fp.get('content'), + )(node); + + selectorNodes.forEach(selectorNode => { + if (fp.compose( + fp.negate(fp.isEmpty), + fp.find({ type: 'arguments' }), + fp.get('content'), + fp.find({ type: 'pseudoClass' }), + fp.get('content'), + )(selectorNode) && + fp.compose( + fp.negate(fp.isEmpty), + fp.find({ type: 'class' }), + fp.get('content'), + )(selectorNode)) { + selectorNode.traverse((subNode, subNodeIndex, subNodeParent) => { + if (subNode.type === 'pseudoClass') { + subNodeParent.removeChild(subNodeIndex); + } + }); + } else { + parent.removeChild(index); + } + }); } } }); diff --git a/test/lib/core/traversalUtils.test.js b/test/lib/core/traversalUtils.test.js index 9d302ff..2f44bb4 100644 --- a/test/lib/core/traversalUtils.test.js +++ b/test/lib/core/traversalUtils.test.js @@ -116,7 +116,7 @@ describe('eliminateGlobals()', () => { it('should remove classes wrapped in :global()', () => { const content = ` -.bar {} + .bar {} :global(.bar.foo) {}`; @@ -129,9 +129,52 @@ describe('eliminateGlobals()', () => { expect(ast.toString()).to.be.equal( ` -.bar {} + .bar {} ` ); }); + + it('should remove only classes wrapped in :global() - global first', () => { + const content = ` +.bar {} + +:global(.bar.foo) .someother {}`; + + const ast = gonzales.parse( + content, + { syntax: 'scss' } + ); + + eliminateGlobals(ast); + + expect(ast.toString()).to.be.equal( + ` +.bar {} + + .someother {}` + ); + }); + + it('should remove only classes wrapped in :global() - global second', () => { + const content = ` +.bar {} + +.someother :global(.bar.foo) {}`; + + const ast = gonzales.parse( + content, + { syntax: 'scss' } + ); + + eliminateGlobals(ast); + + expect(ast.toString()).to.be.equal( + ` +.bar {} + +.someother {}` + ); + }); }); + From d07bed626b043bc3db2db41b2633d648c3990a2b Mon Sep 17 00:00:00 2001 From: Brian C Boles Date: Fri, 18 Oct 2019 15:47:57 -0700 Subject: [PATCH 2/2] add additional global test --- test/lib/core/traversalUtils.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/lib/core/traversalUtils.test.js b/test/lib/core/traversalUtils.test.js index 2f44bb4..8c9bc0d 100644 --- a/test/lib/core/traversalUtils.test.js +++ b/test/lib/core/traversalUtils.test.js @@ -176,5 +176,26 @@ describe('eliminateGlobals()', () => { .someother {}` ); }); + + it('should remove only classes wrapped in :global() - global middle', () => { + const content = ` +.bar {} + +.someother :global(.bar.foo) .somethingelse {}`; + + const ast = gonzales.parse( + content, + { syntax: 'scss' } + ); + + eliminateGlobals(ast); + + expect(ast.toString()).to.be.equal( + ` +.bar {} + +.someother .somethingelse {}` + ); + }); });