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
Simplify the control directive detection logic
  • Loading branch information
elchininet committed Apr 23, 2020
commit c8349b3b6144ad5e4e1cf89992082d7780c0fcc2
6 changes: 0 additions & 6 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ export const RTL_COMMENT_REGEXP = /rtl:/;
export const RTL_CONTROL_DIRECTIVE_REG_EXP = /^\/\*!? *rtl:?(\w+)?:(\w+):?([^*]*?) *\*\/$/;
export const FLIP_PROPERTY_REGEXP = /(right|left)/i;

export enum IGNORE_MODE {
DISABLED,
NEXT_NODE,
BLOCK_MODE
}

export enum CONTROL_DIRECTIVE {
IGNORE = 'ignore'
}
Expand Down
43 changes: 19 additions & 24 deletions src/utilities/containers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Container, Node, Comment } from 'postcss';
import { COMMENT_TYPE, IGNORE_MODE, CONTROL_DIRECTIVE, CONTROL_DIRECTIVE_BLOCK } from '@constants';
import { ControlDirective } from '@types';
import { COMMENT_TYPE, CONTROL_DIRECTIVE, CONTROL_DIRECTIVE_BLOCK } from '@constants';
import { getControlDirective } from '@utilities/comments';
import { cleanRuleRawsBefore } from '@utilities/rules';

Expand All @@ -12,7 +13,7 @@ export const walkContainer = (
callback: WalkContainerCallback
): void => {

let ignoreMode = IGNORE_MODE.DISABLED;
let controlDirective: ControlDirective;

container.each((node: Node): undefined | false => {

Expand All @@ -21,41 +22,35 @@ export const walkContainer = (
if (node.type === COMMENT_TYPE) {

const comment = node as Comment;
const controlDirective = getControlDirective(comment);
controlDirective = getControlDirective(comment);

if (controlDirective) {

if (removeComments) {
cleanRuleRawsBefore(comment.next());
comment.remove();
}

switch (controlDirective.directive) {
case CONTROL_DIRECTIVE.IGNORE:
switch(controlDirective.block) {
case CONTROL_DIRECTIVE_BLOCK.BEGIN:
ignoreMode = IGNORE_MODE.BLOCK_MODE;
break;
case CONTROL_DIRECTIVE_BLOCK.END:
ignoreMode = IGNORE_MODE.DISABLED;
break;
default:
ignoreMode = IGNORE_MODE.NEXT_NODE;
}
break;
}

return;
}

} else {

if (ignoreMode === IGNORE_MODE.NEXT_NODE) {
ignoreMode = IGNORE_MODE.DISABLED;
return;
}
if (controlDirective) {

const { directive, block } = controlDirective;

if (block !== CONTROL_DIRECTIVE_BLOCK.BEGIN) {
controlDirective = null;
}

if (
directive === CONTROL_DIRECTIVE.IGNORE &&
block !== CONTROL_DIRECTIVE_BLOCK.END
) {
return;
}

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

callback(node);
Expand Down
40 changes: 40 additions & 0 deletions tests/__snapshots__/combined-autorename.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ exports[`Combined Tests Autorename Combined Autorename: flexible 1`] = `
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -481,6 +489,14 @@ exports[`Combined Tests Autorename Combined Autorename: flexible with custom str
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -812,6 +828,14 @@ exports[`Combined Tests Autorename Combined Autorename: flexible, greedy: true 1
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -1143,6 +1167,14 @@ exports[`Combined Tests Autorename Combined Autorename: strict 1`] = `
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -1474,6 +1506,14 @@ exports[`Combined Tests Autorename Combined Autorename: strict, greedy: true 1`]
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down
48 changes: 48 additions & 0 deletions tests/__snapshots__/combined-basic-options.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ exports[`Combined Tests Basic Options Combined {processKeyFrames: true} 1`] = `
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -519,6 +527,14 @@ exports[`Combined Tests Basic Options Combined {processUrls: true} 1`] = `
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -850,6 +866,14 @@ exports[`Combined Tests Basic Options Combined {source: rtl, processKeyFrames: t
padding-right: 10px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-right: 20px;
}

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

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -1215,6 +1239,14 @@ exports[`Combined Tests Basic Options Combined {source: rtl} 1`] = `
padding-right: 10px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-right: 20px;
}

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

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -1548,6 +1580,14 @@ exports[`Combined Tests Basic Options Combined {useCalc: true} 1`] = `
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -1879,6 +1919,14 @@ exports[`Combined Tests Basic Options Combined Basic 1`] = `
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down
24 changes: 24 additions & 0 deletions tests/__snapshots__/combined-prefixes.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ exports[`Combined Tests Prefixes Combined custom ltrPrefix and rtlPrefix 1`] = `
padding-right: 10px;
}

.ltr .test16:hover {
padding-right: 20px;
}

.rtl .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -491,6 +499,14 @@ exports[`Combined Tests Prefixes Combined custom ltrPrefix and rtlPrefix propert
padding-right: 10px;
}

.ltr .test16:hover, .left-to-right .test16:hover {
padding-right: 20px;
}

.rtl .test16:hover, .right-to-left .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -836,6 +852,14 @@ exports[`Combined Tests Prefixes Combined custom ltrPrefix, rtlPrefix, and bothP
padding-right: 10px;
}

.ltr .test16:hover, .left-to-right .test16:hover {
padding-right: 20px;
}

.rtl .test16:hover, .right-to-left .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down
32 changes: 32 additions & 0 deletions tests/__snapshots__/combined-string-map.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ exports[`Combined Tests String Map Combined custom no-valid string map and proce
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -489,6 +497,14 @@ exports[`Combined Tests String Map Combined custom no-valid string map and proce
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -824,6 +840,14 @@ exports[`Combined Tests String Map Combined custom string map and processUrls: t
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down Expand Up @@ -1159,6 +1183,14 @@ exports[`Combined Tests String Map Combined custom string map without names and
padding-right: 10px;
}

[dir=\\"ltr\\"] .test16:hover {
padding-right: 20px;
}

[dir=\\"rtl\\"] .test16:hover {
padding-left: 20px;
}

.test17 {
cursor: pointer;
padding: 10px 20px 40px 10px;
Expand Down
Loading