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
Multiple changes:
- Improve the output formatting
- Refactored multiple utility methods to make them simpler
- Created a CHANGELOG
  • Loading branch information
elchininet committed Apr 12, 2020
commit 9f3065e79a74012ea6c58ef5165bef139ab89a84
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

## [1.0.2] - 2020-04-12

- Improve the output formatting
- Refactored multiple utility methods to make them simpler
- Created a CHANGELOG

## [1.0.1] - 2020-04-11

- Refactored the exported module to be a named exported instead of a default one

## [1.0.0] - 2020-04-11

- Created the postcss-rtlcss package
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,9 @@ In the next block only the `left` property will be ignored:
<details><summary>Expand</summary>
<p>

These directives should be used together, they will provide the beginning and the end for ignoring rules or declarations:
These directives should be used together, they will provide the beginning and the end for ignoring rules or declarations.

>**Note:** The directives inserted between these blocks will be ignored and maintained in the final output.

Ignoring multiple rules:

Expand Down
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import postcss, { Root, Transformer } from 'postcss';
import { PluginOptions, RulesObject, Mode } from '@types';
import { parseOptions } from '@utilities/options';
import { parseRules } from '@rules/parser';
import { parseRule } from '@utilities/rules';
import { cleanRules } from '@utilities/rules';
export { PluginOptions, Mode, Source, PluginStringMap, } from '@types';

const transformer = (options: PluginOptions = {}): Transformer => (
Expand All @@ -12,16 +12,14 @@ const transformer = (options: PluginOptions = {}): Transformer => (
appends.forEach(({rule, ruleLTR, ruleRTL}): void => {
if (optionsParsed.mode === Mode.combined) {
rule.after(ruleRTL);
parseRule(ruleRTL);
rule.after(ruleLTR);
parseRule(ruleLTR);
rule.after(ruleLTR);
} else {
rule.after(ruleLTR || ruleRTL);
parseRule(ruleLTR || ruleRTL);
rule.after(ruleLTR || ruleRTL);
}
if (rule.nodes.length === 0) {
rule.remove();
}
cleanRules(rule, ruleLTR, ruleRTL);
});
}
);
Expand Down
17 changes: 7 additions & 10 deletions src/rules/parser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Root, Comment, Node, Rule } from 'postcss';
import { RulesObject, PluginOptionsParsed, Mode } from '@types';
import { COMMENT_TYPE, IGNORE_MODE, IGNORE_BEGIN, IGNORE_END, RULE_TYPE } from '@constants';
import { getIgnoreComment, removeRTLComments } from '@utilities/comments';
import { parseRule } from '@utilities/rules';
import { getIgnoreComment } from '@utilities/comments';
import { cleanRuleBefore } from '@utilities/rules';
import { insertCombinedRules } from './rules-combined';
import { insertOverrideRules } from './rules-override';

Expand All @@ -21,7 +21,8 @@ export const parseRules = (css: Root, options: PluginOptionsParsed): RulesObject
const ignore = getIgnoreComment(comment);

if (ignore) {
comment.remove();
cleanRuleBefore(comment.next());
comment.remove();
switch (ignore) {
case IGNORE_BEGIN:
ignoreMode = IGNORE_MODE.BLOCK_MODE;
Expand All @@ -40,27 +41,23 @@ export const parseRules = (css: Root, options: PluginOptionsParsed): RulesObject

if (node.type === RULE_TYPE) {

parseRule(node);

if (ignoreMode === IGNORE_MODE.NEXT_RULE) {
ignoreMode = IGNORE_MODE.DISABLED;
removeRTLComments(node);
return;
}

if (ignoreMode === IGNORE_MODE.BLOCK_MODE) {
removeRTLComments(node);
return;
}

const rule = node as Rule;

if (options.mode === Mode.override) {
insertOverrideRules(appends, rule, options);
}
if (options.mode == Mode.combined) {
insertCombinedRules(appends, rule, options);
}
if (options.mode === Mode.override) {
insertOverrideRules(appends, rule, options);
}

}

Expand Down
7 changes: 1 addition & 6 deletions src/rules/rules-combined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import postcss, { Rule, Declaration } from 'postcss';
import rtlcss from 'rtlcss';
import { RulesObject, PluginOptionsParsed, Source } from '@types';
import { DECLARATION_TYPE, COMMENT_TYPE } from '@constants';
import { removeRTLComments } from '@utilities/comments';
import { addSelectorPrefixes } from '@utilities/selectors';
import { getRTLCSSStringMap } from '@utilities/options';

Expand All @@ -19,7 +18,6 @@ export const insertCombinedRules = (appends: RulesObject[], rule: Rule, options:
});

if (ruleStr === ruleFlippedtring) {
removeRTLComments(rule);
return;
}

Expand Down Expand Up @@ -57,10 +55,7 @@ export const insertCombinedRules = (appends: RulesObject[], rule: Rule, options:
});

addSelectorPrefixes(ruleFlipped, source === Source.ltr ? ltrPrefix : rtlPrefix);
addSelectorPrefixes(ruleFlippedSecond, source === Source.rtl ? ltrPrefix : rtlPrefix);
removeRTLComments(rule);
removeRTLComments(ruleFlipped);
removeRTLComments(ruleFlippedSecond);
addSelectorPrefixes(ruleFlippedSecond, source === Source.rtl ? ltrPrefix : rtlPrefix);

appends.push({
rule,
Expand Down
4 changes: 0 additions & 4 deletions src/rules/rules-override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import postcss, { Rule, Declaration } from 'postcss';
import rtlcss from 'rtlcss';
import { RulesObject, PluginOptionsParsed, Source } from '@types';
import { DECLARATION_TYPE, FLIP_PROPERTY_REGEXP, COMMENT_TYPE } from '@constants';
import { removeRTLComments } from '@utilities/comments';
import { addSelectorPrefixes } from '@utilities/selectors';
import { getRTLCSSStringMap } from '@utilities/options';

Expand All @@ -20,7 +19,6 @@ export const insertOverrideRules = (appends: RulesObject[], rule: Rule, options:
});

if (ruleStr === ruleFlippedtring) {
removeRTLComments(rule);
return;
}

Expand Down Expand Up @@ -50,8 +48,6 @@ export const insertOverrideRules = (appends: RulesObject[], rule: Rule, options:
});

addSelectorPrefixes(ruleFlipped, prefixes);
removeRTLComments(rule);
removeRTLComments(ruleFlipped);

appends.push({
rule,
Expand Down
25 changes: 2 additions & 23 deletions src/utilities/comments.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Comment, Rule, Node } from 'postcss';
import { Comment } from 'postcss';
import {
COMMENT_TYPE,
DECLARATION_TYPE,
RTL_COMMENT_REGEXP,
RTL_COMMENT_IGNORE_REGEXP,
IGNORE_NEXT,
IGNORE_BEGIN,
IGNORE_END,
IGNORE_END
} from '@constants';

export const getIgnoreComment = (comment: Comment): string | null => {
Expand All @@ -20,22 +17,4 @@ export const getIgnoreComment = (comment: Comment): string | null => {
}
}
return null;
};

export const removeRTLComments = (rule: Rule): void => {
rule.walk((node: Node): void => {
if (node.type === COMMENT_TYPE) {
if (RTL_COMMENT_REGEXP.test(node.toString())) {
node.remove();
}
}
if (node.type === DECLARATION_TYPE) {
// @ts-ignore
if (node.raws && node.raws.value && RTL_COMMENT_REGEXP.test(node.raws.value.raw)) {
// @ts-ignore
delete node.raws.value;
node.value = node.value.trim();
}
}
});
};
37 changes: 31 additions & 6 deletions src/utilities/rules.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import { Rule } from 'postcss';
import { Rule, Node } from 'postcss';
import { COMMENT_TYPE, RTL_COMMENT_REGEXP, DECLARATION_TYPE, RULE_TYPE } from '@constants';

export const parseRule = (rule: Rule): void => {
// @ts-ignore
rule.cleanRaws();
if (rule.root().index(rule) > 0) {
rule.raws.before = '\n\n';
export const cleanRuleBefore = (node: Node | void): void => {
if (node && node.type === RULE_TYPE) {
node.raws.before = '\n\n';
}
};

export const cleanRules = (...rules: (Rule | undefined | null)[]): void => {
rules.forEach((rule: Rule | undefined | null): void | undefined => {
if (rule) {
const prev = rule.prev();
if (prev && prev.type !== COMMENT_TYPE) {
cleanRuleBefore(rule);
}
rule.walk((node: Node): void => {
if (node.type === COMMENT_TYPE) {
if (RTL_COMMENT_REGEXP.test(node.toString())) {
node.remove();
}
}
if (node.type === DECLARATION_TYPE) {
// @ts-ignore
if (node.raws && node.raws.value && RTL_COMMENT_REGEXP.test(node.raws.value.raw)) {
// @ts-ignore
delete node.raws.value;
node.value = node.value.trim();
}
}
});
}
});
};
21 changes: 0 additions & 21 deletions tests/__snapshots__/combined.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ exports[`Combined Tests Combined {processUrls: true} 1`] = `
}

/* Comment not related to rtl */

.test3 {
margin: 1px 2px 3px;
padding: 10px 20px;

/* Property comment not related to rtl */
text-align: center;
}
Expand Down Expand Up @@ -88,7 +86,6 @@ exports[`Combined Tests Combined {processUrls: true} 1`] = `
}

/* rtl:novalid:ignore */

.test8 {
display: flex;
padding-left: 10%;
Expand Down Expand Up @@ -194,11 +191,9 @@ exports[`Combined Tests Combined {source: rtl} 1`] = `
}

/* Comment not related to rtl */

.test3 {
margin: 1px 2px 3px;
padding: 10px 20px;

/* Property comment not related to rtl */
text-align: center;
}
Expand Down Expand Up @@ -257,7 +252,6 @@ exports[`Combined Tests Combined {source: rtl} 1`] = `
}

/* rtl:novalid:ignore */

.test8 {
display: flex;
padding-left: 10%;
Expand Down Expand Up @@ -364,11 +358,9 @@ exports[`Combined Tests Combined {useCalc: true} 1`] = `
}

/* Comment not related to rtl */

.test3 {
margin: 1px 2px 3px;
padding: 10px 20px;

/* Property comment not related to rtl */
text-align: center;
}
Expand Down Expand Up @@ -428,7 +420,6 @@ exports[`Combined Tests Combined {useCalc: true} 1`] = `
}

/* rtl:novalid:ignore */

.test8 {
display: flex;
padding-left: 10%;
Expand Down Expand Up @@ -534,11 +525,9 @@ exports[`Combined Tests Combined Basic 1`] = `
}

/* Comment not related to rtl */

.test3 {
margin: 1px 2px 3px;
padding: 10px 20px;

/* Property comment not related to rtl */
text-align: center;
}
Expand Down Expand Up @@ -597,7 +586,6 @@ exports[`Combined Tests Combined Basic 1`] = `
}

/* rtl:novalid:ignore */

.test8 {
display: flex;
padding-left: 10%;
Expand Down Expand Up @@ -703,11 +691,9 @@ exports[`Combined Tests Combined custom ltrPrefix and rtlPrefix properties 1`] =
}

/* Comment not related to rtl */

.test3 {
margin: 1px 2px 3px;
padding: 10px 20px;

/* Property comment not related to rtl */
text-align: center;
}
Expand Down Expand Up @@ -766,7 +752,6 @@ exports[`Combined Tests Combined custom ltrPrefix and rtlPrefix properties 1`] =
}

/* rtl:novalid:ignore */

.test8 {
display: flex;
padding-left: 10%;
Expand Down Expand Up @@ -872,11 +857,9 @@ exports[`Combined Tests Combined custom ltrPrefix and rtlPrefix properties as ar
}

/* Comment not related to rtl */

.test3 {
margin: 1px 2px 3px;
padding: 10px 20px;

/* Property comment not related to rtl */
text-align: center;
}
Expand Down Expand Up @@ -941,7 +924,6 @@ exports[`Combined Tests Combined custom ltrPrefix and rtlPrefix properties as ar
}

/* rtl:novalid:ignore */

.test8 {
display: flex;
padding-left: 10%;
Expand Down Expand Up @@ -1051,11 +1033,9 @@ exports[`Combined Tests Combined custom string map 1`] = `
}

/* Comment not related to rtl */

.test3 {
margin: 1px 2px 3px;
padding: 10px 20px;

/* Property comment not related to rtl */
text-align: center;
}
Expand Down Expand Up @@ -1114,7 +1094,6 @@ exports[`Combined Tests Combined custom string map 1`] = `
}

/* rtl:novalid:ignore */

.test8 {
display: flex;
padding-left: 10%;
Expand Down
Loading