Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [4.0.4] - 2023-04-08

- Fixed unwanted removal of empty rules

## [4.0.3] - 2023-03-04

- Fixed a bug releated to overriden properties
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function postcssRTLCSS (options: PluginOptions = {}): Plugin {
clean(css);
}
});
};
}

postcssRTLCSS.postcss = true;

Expand Down
4 changes: 3 additions & 1 deletion src/utilities/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export const clean = (css: Container): void => {
!!(node as Container).nodes
) {
if (!ruleHasChildren(node as Container)) {
node.remove();
if (mode === Mode.diff) {
node.remove();
}
} else {
const prev = node.prev();
if (prev) {
Expand Down
62 changes: 62 additions & 0 deletions tests/__snapshots__/empty-rules.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`[[Mode: combined]] Empty Rules Tests: Basic 1`] = `
".test1 {}

.test2, .test3 {}

[dir="ltr"] .test4 {
text-align: left;
}

[dir="rtl"] .test4 {
text-align: right;
}

.test5 {
color: red;
}

[dir="ltr"] .test5 {
padding-left: 20px;
}

[dir="rtl"] .test5 {
padding-right: 20px;
}"
`;

exports[`[[Mode: diff]] Empty Rules Tests: Basic 1`] = `
".test4 {
text-align: right;
}

.test5 {
padding-left: 0;
padding-right: 20px;
}"
`;

exports[`[[Mode: override]] Empty Rules Tests: Basic 1`] = `
".test1 {}

.test2, .test3 {}

.test4 {
text-align: left;
}

[dir="rtl"] .test4 {
text-align: right;
}

.test5 {
color: red;
padding-left: 20px;
}

[dir="rtl"] .test5 {
padding-left: 0;
padding-right: 20px;
}"
`;
12 changes: 12 additions & 0 deletions tests/css/empty-rules.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.test1 {}

.test2, .test3 {}

.test4 {
text-align: left;
}

.test5 {
color: red;
padding-left: 20px;
}
21 changes: 21 additions & 0 deletions tests/empty-rules.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import postcss from 'postcss';
import postcssRTLCSS from '../src';
import { PluginOptions } from '../src/@types';
import { readCSSFile, runTests } from './utils';

runTests({}, (pluginOptions: PluginOptions): void => {
describe(`[[Mode: ${pluginOptions.mode}]] Empty Rules Tests: `, () => {
let input = '';

beforeEach(async (): Promise<void> => {
input = input || await readCSSFile('empty-rules.css');
});

it('Basic', (): void => {
const options: PluginOptions = { ...pluginOptions };
const output = postcss([postcssRTLCSS(options)]).process(input);
expect(output.css).toMatchSnapshot();
expect(output.warnings()).toHaveLength(0);
});
});
});