Skip to content

Commit 91c7c3a

Browse files
author
Leroy Korterink
authored
Add scss syntax support in fix-nesting-at-rule (#14)
1 parent 2635fbe commit 91c7c3a

4 files changed

Lines changed: 67 additions & 10 deletions

File tree

.tape.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ module.exports = {
6363
warnings: 0,
6464
args: ['always', { only: /^html$/i }]
6565
},
66+
67+
// Proposal nesting syntax
6668
{
6769
source: '.foo { color: blue; } body .foo { color: rebeccapurple; } html .foo { color: red; }',
6870
expect: '.foo { color: blue; @nest body & { color: rebeccapurple; } @nest html & { color: red; } }',
@@ -89,6 +91,33 @@ module.exports = {
8991
args: ['always', { only: 'body' }]
9092
},
9193

94+
// SCSS nesting syntax
95+
{
96+
source: '.foo { color: blue; } body .foo { color: rebeccapurple; } html .foo { color: red; }',
97+
expect: '.foo { color: blue; body & { color: rebeccapurple; } html & { color: red; } }',
98+
args: ['always', { syntax: 'scss' }]
99+
},
100+
{
101+
source: '.foo { color: blue; } body .foo { color: rebeccapurple; } html .foo { color: red; }',
102+
expect: '.foo { color: blue; body & { color: rebeccapurple; } } html .foo { color: red; }',
103+
args: ['always', { syntax: 'scss', except: /^html$/i }]
104+
},
105+
{
106+
source: '.foo { color: blue; } body .foo { color: rebeccapurple; } html .foo { color: red; }',
107+
expect: '.foo { color: blue; body & { color: rebeccapurple; } } html .foo { color: red; }',
108+
args: ['always', { syntax: 'scss', except: 'html' }]
109+
},
110+
{
111+
source: '.foo { color: blue; } body .foo { color: rebeccapurple; } html .foo { color: red; }',
112+
expect: '.foo { color: blue; body & { color: rebeccapurple; } } html .foo { color: red; }',
113+
args: ['always', { syntax: 'scss', only: /^body$/i }]
114+
},
115+
{
116+
source: '.foo { color: blue; } body .foo { color: rebeccapurple; } html .foo { color: red; }',
117+
expect: '.foo { color: blue; body & { color: rebeccapurple; } } html .foo { color: red; }',
118+
args: ['always', { syntax: 'scss', only: 'body' }]
119+
},
120+
92121
/* Test Nesting Media Rules */
93122
{
94123
source: '.foo { color: blue; } @media (min-width: 960px) { .foo { color: rebeccapurple; } }',

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ or regular expression.
115115
}
116116
```
117117

118+
### syntax
119+
120+
The `syntax` option allows you to specify the syntax of the source files being processed. For SCSS syntax set the value to `scss`.
121+
122+
```js
123+
{
124+
"rules": {
125+
"csstools/use-nesting": ["always", { "syntax": "scss" }]
126+
}
127+
}
128+
```
129+
118130
[cli-img]: https://img.shields.io/travis/csstools/stylelint-use-nesting/main.svg
119131
[cli-url]: https://travis-ci.org/csstools/stylelint-use-nesting
120132
[git-img]: https://img.shields.io/badge/support-chat-blue.svg

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default stylelint.createPlugin(ruleName, (action, opts, context) => {
5757
} else if (areRulesPotentialNestingAtRule(rule, prev, opts)) {
5858
// fix or report the current rule if it could be nested inside the previous rule
5959
if (shouldFix) {
60-
fixNestingAtRule(rule, prev);
60+
fixNestingAtRule(rule, prev, opts);
6161

6262
isProcessing = true;
6363
} else {
@@ -66,7 +66,7 @@ export default stylelint.createPlugin(ruleName, (action, opts, context) => {
6666
} else if (areRulesPotentialNestingAtRule(prev, rule, opts)) {
6767
// fix or report the previous rule if it could be nested inside the current rule
6868
if (shouldFix) {
69-
fixNestingAtRule(prev, rule);
69+
fixNestingAtRule(prev, rule, opts);
7070

7171
isProcessing = true;
7272
} else {

src/lib/fix-nesting-at-rule.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
import postcss from 'postcss';
22

3-
export default function fixNestingAtRule(rule1, rule2) {
3+
export default function fixNestingAtRule(rule1, rule2, opts) {
4+
const syntax = Object(opts).syntax;
5+
46
rule1.remove();
57

68
rule1.selectors = rule1.selectors.map(
79
selector => `${selector.slice(0, -rule2.selector.length - 1)} &`
810
);
911

10-
const atrule = Object.assign(
11-
postcss.atRule({
12-
name: 'nest',
13-
params: String(rule1.selector)
14-
}),
12+
let ruleOrAtRule;
13+
switch (syntax) {
14+
case "scss": {
15+
ruleOrAtRule = postcss.rule({
16+
selector: String(rule1.selector),
17+
});
18+
break;
19+
}
20+
21+
default: {
22+
ruleOrAtRule = postcss.atRule({
23+
name: "nest",
24+
params: String(rule1.selector),
25+
});
26+
}
27+
}
28+
29+
const rule = Object.assign(
30+
ruleOrAtRule,
1531
{
1632
raws: Object.assign(rule1.raws, {
1733
afterName: ' '
@@ -20,7 +36,7 @@ export default function fixNestingAtRule(rule1, rule2) {
2036
}
2137
);
2238

23-
atrule.append(...rule1.nodes);
39+
rule.append(...rule1.nodes);
2440

25-
rule2.append(atrule);
41+
rule2.append(rule);
2642
}

0 commit comments

Comments
 (0)