Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support no-undef-class with :global() #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/core/traversalUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);
}
});
}
}
});
Expand Down
68 changes: 66 additions & 2 deletions test/lib/core/traversalUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('eliminateGlobals()', () => {

it('should remove classes wrapped in :global()', () => {
const content = `
.bar {}
.bar {}

:global(.bar.foo) {}`;

Expand All @@ -129,9 +129,73 @@ 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 {}`
);
});

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 {}`
);
});
});