Skip to content

Commit abdb3b1

Browse files
ramithachriseppstein
authored andcommitted
feat: Removing common prefixes from states, like, is.
1 parent ed51f58 commit abdb3b1

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

Diff for: packages/@css-blocks/bem-to-blocks/src/index.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type BlockToBemSelectorMap = Map<string, ElementToBemSelectorMap>;
1313
type BemToBlockClassMap = WeakMap<BemSelector, BlockClassSelector>;
1414

1515
const EMPTY_ELEMENT_PLACEHOLDER = "EMPTY-ELEMENT-PLACEHOLDER";
16+
const COMMON_PREFIXES_FOR_MODIFIERS = ["is"];
1617

1718
export function convertBemToBlocks(files: Array<string>): Promise<void>[] {
1819
let promises: Promise<void>[] = [];
@@ -22,7 +23,7 @@ export function convertBemToBlocks(files: Array<string>): Promise<void>[] {
2223
.process(css, { from: file });
2324
// rewrite the file with the processed output
2425
const parsedFilePath = path.parse(file);
25-
const blockFilePath = Object.assign(parsedFilePath, {ext: `.block${parsedFilePath.ext}`, base: parsedFilePath.base.replace(parsedFilePath.ext, `.block${parsedFilePath.ext}`)} );
26+
const blockFilePath = Object.assign(parsedFilePath, {ext: `.block${parsedFilePath.ext}`, base: undefined} );
2627
promises.push(fs.writeFile(path.format(blockFilePath), output.toString()));
2728
});
2829
});
@@ -109,8 +110,14 @@ export function constructBlocksMap(bemSelectorCache: BemSelectorMap): BemToBlock
109110
let blockClass = resultMap.get(sel);
110111
let lcs = blockClass && blockClass.state && lcsMap[blockClass.state];
111112
if (blockClass && blockClass.state && lcs) {
112-
blockClass.subState = blockClass.state.replace(`${lcs}-`, "");
113-
blockClass.state = lcs.replace(/-$/, "");
113+
if (COMMON_PREFIXES_FOR_MODIFIERS.indexOf(lcs) > -1) {
114+
// if we find that the state contains a common prefix, we strip
115+
// it of that prefix
116+
blockClass.state = blockClass.state.replace(`${lcs}-`, "");;
117+
} else {
118+
blockClass.subState = blockClass.state.replace(`${lcs}-`, "");
119+
blockClass.state = lcs.replace(/-$/, "");
120+
}
114121
}
115122
});
116123
}
@@ -184,6 +191,9 @@ export const bemToBlocksPlugin: postcss.Plugin<PostcssAny> = postcss.plugin("bem
184191
quoteMark: blockClassName.subState ? '"' : undefined,
185192
operator: blockClassName.subState ? "=" : undefined,
186193
value: blockClassName.subState,
194+
// the API for postcss-selector-parser mentions raws to be
195+
// deprecated when used with quoteMark but that didn't work as
196+
// expected. Keeping quoteMark and raws until it is fixed.
187197
raws: {value: blockClassName.subState ? `"${blockClassName.subState}"` : undefined},
188198
});
189199

Diff for: packages/@css-blocks/bem-to-blocks/src/utils.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ export const R_BEM_REGEX = /^.(?:((?:[a-z0-9]+-)*[a-z0-9]+)(__(?:[a-z0-9]+-)*[a-
55
// regex that matches block--modifier__element pattern
66
export const R_BME_REGEX = /^.(?:((?:[a-z0-9]+-)*[a-z0-9]+)(--(?:[a-z0-9]+-)*[a-z0-9]+)?(__(?:[a-z0-9]+-)*[a-z0-9]+)?)$/;
77

8-
const COMMON_PREFIXES_FOR_MODIFIERS = ["is"];
9-
108
/**
119
* function to find the LCS (longest common substring) from a string array
1210
*
@@ -28,11 +26,10 @@ export function findLcsMap(arr: string[]): {[key: string]: string} {
2826
wordMap[word[0]] = new Array(word.join("-"));
2927
}
3028
});
31-
// return only those values who have a count of greater than 1 and whose key
32-
// is not present in COMMON_PREFIXES_FOR_MODIFIERS
29+
// return only those values who have a count of greater than 1
3330
let reducedWordMap = {};
3431
for (let [key, value] of Object.entries(wordMap)) {
35-
if (value.length > 1 && COMMON_PREFIXES_FOR_MODIFIERS.indexOf(key) < 0) {
32+
if (value.length > 1) {
3633
value.forEach(item => {
3734
// create a reverser mapping of the string to the key
3835
reducedWordMap[item] = key;

Diff for: packages/@css-blocks/bem-to-blocks/test/construct-blocks-map-test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,11 @@ describe("construct-blocks", () => {
114114
let mockMap = new Map(Object.entries({
115115
".myblock__myelement--is-disabled": sel1,
116116
".myblock__myelement--is-animating": sel2,
117-
118117
}));
119118

120119
let result = constructBlocksMap(mockMap);
121-
assert.deepEqual(result.get(sel1), new BlockClassSelector({ class: "myelement", state: "is-disabled" }));
122-
assert.deepEqual(result.get(sel2), new BlockClassSelector({ class: "myelement", state: "is-animating" }));
120+
assert.deepEqual(result.get(sel1), new BlockClassSelector({ class: "myelement", state: "disabled" }));
121+
assert.deepEqual(result.get(sel2), new BlockClassSelector({ class: "myelement", state: "animating" }));
123122
});
124123

125124
});

0 commit comments

Comments
 (0)