Skip to content

Commit 51361f6

Browse files
committed
Fix infinite processing problems
1 parent 2dfb4c6 commit 51361f6

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sector-labs/postcss-inline-class",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Inline the declarations of other CSS classes in your CSS classes.",
55
"main": "src/index.js",
66
"repository": "https://github.com/sectorlabs/postcss-inline-class",

src/findNestedRules.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ const findNestedRules = (root, targetSelector) => {
44
const nestedMatches = [];
55

66
root.walkRules((rule) => {
7-
if (rule.selectors.includes(targetSelector) && rule.parent.type === 'root') {
7+
if (rule.parent.type !== 'root') {
88
return;
99
}
1010

11-
const isNestedRule =
12-
rule.selectors.find((selector) => selector.includes(targetSelector)) &&
13-
rule.parent.type === 'root';
11+
if (rule.selectors.includes(targetSelector)) {
12+
return;
13+
}
14+
15+
const isNestedRule = rule.selectors.find((selector) => selector.includes(targetSelector));
1416

1517
if (isNestedRule) {
16-
nestedMatches.push(rule);
18+
nestedMatches.push(rule.clone());
1719
}
1820
});
1921

src/index.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ const processAtRule = (onError, atRule, root, targetClass, importPath) => {
1717
if (matchedDeclarations.length === 0 && nestedRules.length === 0) {
1818
if (importPath) {
1919
onError(`Could not find class '${targetClass}' in file '${importPath}'`);
20+
return [];
2021
} else {
2122
onError(`Could not find class '${targetClass}'`);
23+
return [];
2224
}
2325
}
2426

2527
nestedRules.forEach((nestedRule) => {
26-
nestedRule.selectors = nestedRule.selectors.map((selector) =>
27-
replaceClassName(selector, targetClass, atRule.parent.selector),
28+
nestedRule.selector = replaceClassName(
29+
nestedRule.selector,
30+
targetClass,
31+
atRule.parent.selector,
2832
);
29-
root.append(nestedRule);
3033
});
3134

3235
mediaQueries.forEach((mediaQuery) => {
@@ -36,21 +39,23 @@ const processAtRule = (onError, atRule, root, targetClass, importPath) => {
3639
replaceClassName(selector, targetClass, atRule.parent.selector),
3740
)),
3841
);
39-
40-
root.append(mediaQuery);
4142
});
4243

4344
atRule.replaceWith(matchedDeclarations);
45+
return [...nestedRules, ...mediaQueries];
4446
};
4547

4648
const walkAtRule = (root, result, promises) => (atRule) => {
4749
const params = postcss.list.space(atRule.params);
4850
const targetClass = params[0];
4951

50-
const onError = (message) => atRule.warn(result, message);
52+
const onError = (message) => {
53+
atRule.warn(result, message);
54+
atRule.remove();
55+
};
5156

5257
if (params.length === 1) {
53-
processAtRule(onError, atRule, root, targetClass);
58+
promises.push(Promise.resolve(processAtRule(onError, atRule, root, targetClass)));
5459
return;
5560
}
5661

@@ -61,11 +66,11 @@ const walkAtRule = (root, result, promises) => (atRule) => {
6166
readFile(resolvedPath)
6267
.then((rawData) => {
6368
const importedRoot = postcss.parse(rawData);
64-
processAtRule(onError, atRule, importedRoot, targetClass, importPath);
69+
return processAtRule(onError, atRule, importedRoot, targetClass, importPath);
6570
})
6671
.catch(() => {
6772
onError(`Could not find file '${importPath}'`);
68-
atRule.remove();
73+
return [];
6974
}),
7075
);
7176
};
@@ -77,7 +82,12 @@ const processFile = (root, result) => (resolve) => {
7782

7883
root.walkRules((rule) => rule.walkAtRules('inline', atRuleWalker));
7984

80-
return Promise.all(promises).then(resolve).catch(resolve);
85+
return Promise.all(promises)
86+
.then((newNodes) => {
87+
root.append(...newNodes);
88+
return resolve();
89+
})
90+
.catch(resolve);
8191
};
8292

8393
module.exports = postcss.plugin('postcss-inline-class', () => (root, result) =>

0 commit comments

Comments
 (0)