Skip to content

Commit 0063077

Browse files
committed
Improve declaration overriding logic
1 parent 0720fd0 commit 0063077

21 files changed

+234
-443
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ coverage/
1010
/index.d.ts
1111
/index.js
1212
/options.d.ts
13-
/options.js
13+
/options.js
14+
.yalc/
15+
yalc.lock

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22

3-
## [1.8.0] - 2021-05-23
3+
## [1.8.1] - 2022-07-24
4+
5+
- Improve the overriding logic to avoid edge cases with multiple overrides
6+
7+
## [1.8.0] - 2022-07-23
48

59
- Add an option to have more control over the selector prefixing logic (`prefixSelectorTransformer`)
610

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-rtlcss",
3-
"version": "1.8.0",
3+
"version": "1.8.1",
44
"description": "PostCSS plugin to build Cascading Style Sheets (CSS) with Left-To-Right (LTR) and Right-To-Left (RTL) rules",
55
"keywords": [
66
"postcss",

src/@types/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { Rule, AtRule } from 'postcss';
1+
import {
2+
Rule,
3+
AtRule,
4+
Declaration
5+
} from 'postcss';
26

37
export interface ObjectWithProps<T> {
48
[key: string]: T;
@@ -103,4 +107,6 @@ export interface ControlDirective {
103107
block?: string;
104108
directive: string;
105109
option?: string;
106-
}
110+
}
111+
112+
export type DeclarationHashMapProp = Record<string, Record<string, { decl: Declaration; value: string; }>>;

src/parsers/declarations.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
import postcss, { Rule, Node, Declaration, Comment, vendor } from 'postcss';
1+
import postcss, {
2+
Rule,
3+
Node,
4+
Declaration,
5+
Comment,
6+
vendor
7+
} from 'postcss';
28
import rtlcss from 'rtlcss';
3-
import { Mode, Autorename, ControlDirective } from '@types';
9+
import {
10+
Mode,
11+
Autorename,
12+
ControlDirective,
13+
DeclarationHashMapProp
14+
} from '@types';
415
import {
516
DECLARATION_TYPE,
617
FLIP_PROPERTY_REGEXP,
@@ -19,7 +30,9 @@ import {
1930
allDeclarations,
2031
initialValues,
2132
appendDeclarationToRule,
22-
hasIgnoreDirectiveInRaws
33+
hasIgnoreDirectiveInRaws,
34+
hasSameUpcomingDeclaration,
35+
hasMirrorDeclaration
2336
} from '@utilities/declarations';
2437
import { walkContainer } from '@utilities/containers';
2538
import {
@@ -52,10 +65,15 @@ export const parseDeclarations = (
5265
const ruleBoth = ruleFlipped.clone();
5366
const ruleSafe = ruleFlipped.clone();
5467

55-
const declarationHashMap = Array.prototype.reduce.call(rule.nodes, (obj: Record<string, string>, node: Node): Record<string, string> => {
68+
const declarationHashMap = Array.prototype.reduce.call(rule.nodes, (obj: DeclarationHashMapProp, node: Node): DeclarationHashMapProp => {
5669
if (node.type === DECLARATION_TYPE) {
5770
const decl = node as Declaration;
58-
obj[decl.prop] = decl.value.trim();
71+
const index = rule.index(decl);
72+
obj[decl.prop] = obj[decl.prop] || {};
73+
obj[decl.prop][index] = {
74+
decl,
75+
value: decl.value.trim()
76+
};
5977
}
6078
return obj;
6179
}, {});
@@ -168,6 +186,10 @@ export const parseDeclarations = (
168186
) {
169187
return;
170188
}
189+
190+
if (hasSameUpcomingDeclaration(rule, decl, declarationHashMap)) {
191+
return;
192+
}
171193

172194
if (isAnimation) {
173195

@@ -237,7 +259,7 @@ export const parseDeclarations = (
237259
deleteDeclarations.push(decl);
238260
}
239261
return;
240-
} else if (declarationHashMap[declFlipped.prop] === declFlippedValue) {
262+
} else if (hasMirrorDeclaration(rule, declFlipped, declarationHashMap)) {
241263
simetricRules = true;
242264
if (isConflictedDeclaration && !hasIgnoreDirectiveInRaws(decl)) {
243265
appendDeclarationToRule(decl, ruleSafe);

src/utilities/declarations.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { Rule, Declaration } from 'postcss';
2-
import { ObjectWithProps, DeclarationsData } from '@types';
2+
import {
3+
ObjectWithProps,
4+
DeclarationsData,
5+
DeclarationHashMapProp
6+
} from '@types';
37
import { COMMENT_TYPE, RTL_COMMENT_IGNORE_REGEXP } from '@constants';
48
import shorthandDeclarationsJson from '@data/shorthand-declarations.json';
59
import notShorthandDeclarationsJson from '@data/not-shorthand-declarations.json';
@@ -58,10 +62,42 @@ const hasIgnoreDirectiveInRaws = (decl: Declaration): boolean => {
5862
return false;
5963
};
6064

65+
const hasSameUpcomingDeclaration = (
66+
rule: Rule,
67+
decl: Declaration,
68+
declarationHashMap: DeclarationHashMapProp
69+
): boolean => {
70+
const index = rule.index(decl);
71+
const indexes = Object.keys(declarationHashMap[decl.prop]).map(Number);
72+
if (indexes.length === 1) {
73+
return false;
74+
}
75+
return indexes.some((i: number) => i > index);
76+
};
77+
78+
const hasMirrorDeclaration = (
79+
rule: Rule,
80+
declFlipped: Declaration,
81+
declarationHashMap: DeclarationHashMapProp
82+
): boolean => {
83+
if (declarationHashMap[declFlipped.prop]) {
84+
const entries = Object.entries(declarationHashMap[declFlipped.prop]);
85+
return entries.some((entry) => {
86+
return (
87+
entry[1].value === declFlipped.value.trim() &&
88+
!hasSameUpcomingDeclaration(rule, entry[1].decl, declarationHashMap)
89+
);
90+
});
91+
}
92+
return false;
93+
};
94+
6195
export {
6296
declarations,
6397
allDeclarations,
6498
initialValues,
6599
appendDeclarationToRule,
66-
hasIgnoreDirectiveInRaws
100+
hasIgnoreDirectiveInRaws,
101+
hasSameUpcomingDeclaration,
102+
hasMirrorDeclaration
67103
};

tests/__snapshots__/combined-autorename.test.ts.snap

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,7 @@ exports[`Combined Tests Autorename Combined Autorename: flexible 1`] = `
2525
2626
.test2 {
2727
color: red;
28-
}
29-
30-
[dir=\\"ltr\\"] .test2 {
3128
text-align: left;
32-
}
33-
34-
[dir=\\"rtl\\"] .test2 {
35-
text-align: right;
36-
}
37-
38-
[dir] .test2 {
3929
text-align: center;
4030
}
4131
@@ -624,17 +614,7 @@ exports[`Combined Tests Autorename Combined Autorename: flexible with custom str
624614
625615
.test2 {
626616
color: red;
627-
}
628-
629-
[dir=\\"ltr\\"] .test2 {
630617
text-align: left;
631-
}
632-
633-
[dir=\\"rtl\\"] .test2 {
634-
text-align: right;
635-
}
636-
637-
[dir] .test2 {
638618
text-align: center;
639619
}
640620
@@ -1223,17 +1203,7 @@ exports[`Combined Tests Autorename Combined Autorename: flexible, greedy: true 1
12231203
12241204
.test2 {
12251205
color: red;
1226-
}
1227-
1228-
[dir=\\"ltr\\"] .test2 {
12291206
text-align: left;
1230-
}
1231-
1232-
[dir=\\"rtl\\"] .test2 {
1233-
text-align: right;
1234-
}
1235-
1236-
[dir] .test2 {
12371207
text-align: center;
12381208
}
12391209
@@ -1822,17 +1792,7 @@ exports[`Combined Tests Autorename Combined Autorename: only control directives
18221792
18231793
.test2 {
18241794
color: red;
1825-
}
1826-
1827-
[dir=\\"ltr\\"] .test2 {
18281795
text-align: left;
1829-
}
1830-
1831-
[dir=\\"rtl\\"] .test2 {
1832-
text-align: right;
1833-
}
1834-
1835-
[dir] .test2 {
18361796
text-align: center;
18371797
}
18381798
@@ -2421,17 +2381,7 @@ exports[`Combined Tests Autorename Combined Autorename: only control directives,
24212381
24222382
.test2 {
24232383
color: red;
2424-
}
2425-
2426-
[dir=\\"ltr\\"] .test2 {
24272384
text-align: left;
2428-
}
2429-
2430-
[dir=\\"rtl\\"] .test2 {
2431-
text-align: right;
2432-
}
2433-
2434-
[dir] .test2 {
24352385
text-align: center;
24362386
}
24372387
@@ -3020,17 +2970,7 @@ exports[`Combined Tests Autorename Combined Autorename: strict 1`] = `
30202970
30212971
.test2 {
30222972
color: red;
3023-
}
3024-
3025-
[dir=\\"ltr\\"] .test2 {
30262973
text-align: left;
3027-
}
3028-
3029-
[dir=\\"rtl\\"] .test2 {
3030-
text-align: right;
3031-
}
3032-
3033-
[dir] .test2 {
30342974
text-align: center;
30352975
}
30362976
@@ -3619,17 +3559,7 @@ exports[`Combined Tests Autorename Combined Autorename: strict, greedy: true 1`]
36193559
36203560
.test2 {
36213561
color: red;
3622-
}
3623-
3624-
[dir=\\"ltr\\"] .test2 {
36253562
text-align: left;
3626-
}
3627-
3628-
[dir=\\"rtl\\"] .test2 {
3629-
text-align: right;
3630-
}
3631-
3632-
[dir] .test2 {
36333563
text-align: center;
36343564
}
36353565

0 commit comments

Comments
 (0)