Skip to content

Commit b19b57f

Browse files
authored
Merge pull request #2478 from sass/if
Add support for `@if` and `@else`
2 parents 8818d93 + 23ff19d commit b19b57f

28 files changed

+988
-18
lines changed

pkg/sass-parser/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* Add support for parsing declarations.
66

7+
* Add support for parsing the `@if` and `@else` rules.
8+
79
## 0.4.8
810

911
* Add support for parsing the `@mixin` rule.

pkg/sass-parser/lib/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export {
105105
DeclarationRaws,
106106
} from './src/statement/declaration';
107107
export {EachRule, EachRuleProps, EachRuleRaws} from './src/statement/each-rule';
108+
export {ElseRule, ElseRuleProps, ElseRuleRaws} from './src/statement/else-rule';
108109
export {
109110
ErrorRule,
110111
ErrorRuleProps,
@@ -128,6 +129,7 @@ export {
128129
GenericAtRuleProps,
129130
GenericAtRuleRaws,
130131
} from './src/statement/generic-at-rule';
132+
export {IfRule, IfRuleProps, IfRuleRaws} from './src/statement/if-rule';
131133
export {
132134
MixinRule,
133135
MixinRuleProps,

pkg/sass-parser/lib/src/configured-variable.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class ConfiguredVariable extends Node {
102102
*/
103103
declare name: string;
104104

105-
/** The expresison whose value the variable is assigned. */
105+
/** The expression whose value the variable is assigned. */
106106
get expression(): Expression {
107107
return this._expression!;
108108
}

pkg/sass-parser/lib/src/sass-internal.ts

+18
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ declare namespace SassInternal {
166166
readonly parameters: ParameterList;
167167
}
168168

169+
class IfRule extends Statement {
170+
readonly clauses: IfClause[];
171+
readonly lastClause: ElseClause | null;
172+
}
173+
174+
class IfClause {
175+
readonly expression: Expression;
176+
readonly children: Statement[];
177+
}
178+
179+
class ElseClause {
180+
readonly children: Statement[];
181+
}
182+
169183
class IncludeRule extends Statement {
170184
readonly namespace: string | null;
171185
readonly name: string;
@@ -329,6 +343,9 @@ export type ExtendRule = SassInternal.ExtendRule;
329343
export type ForRule = SassInternal.ForRule;
330344
export type ForwardRule = SassInternal.ForwardRule;
331345
export type FunctionRule = SassInternal.FunctionRule;
346+
export type IfRule = SassInternal.IfRule;
347+
export type IfClause = SassInternal.IfClause;
348+
export type ElseClause = SassInternal.ElseClause;
332349
export type IncludeRule = SassInternal.IncludeRule;
333350
export type LoudComment = SassInternal.LoudComment;
334351
export type MediaRule = SassInternal.MediaRule;
@@ -363,6 +380,7 @@ export interface StatementVisitorObject<T> {
363380
visitForRule(node: ForRule): T;
364381
visitForwardRule(node: ForwardRule): T;
365382
visitFunctionRule(node: FunctionRule): T;
383+
visitIfRule(node: IfRule): T;
366384
visitIncludeRule(node: IncludeRule): T;
367385
visitLoudComment(node: LoudComment): T;
368386
visitMediaRule(node: MediaRule): T;

pkg/sass-parser/lib/src/statement/__snapshots__/declaration.test.ts.snap

+6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ exports[`a property declaration toJSON with expression and no nodes 1`] = `
1010
"id": "<input css _____>",
1111
},
1212
],
13+
"prop": "foo",
1314
"propInterpolation": <foo>,
1415
"raws": {},
1516
"sassType": "decl",
1617
"source": <1:4-1:12 in 0>,
1718
"type": "decl",
19+
"value": "bar",
1820
}
1921
`;
2022
@@ -31,11 +33,13 @@ exports[`a property declaration toJSON with expression and nodes 1`] = `
3133
"nodes": [
3234
<baz: bang>,
3335
],
36+
"prop": "foo",
3437
"propInterpolation": <foo>,
3538
"raws": {},
3639
"sassType": "decl",
3740
"source": <1:4-1:24 in 0>,
3841
"type": "decl",
42+
"value": "bar",
3943
}
4044
`;
4145
@@ -51,10 +55,12 @@ exports[`a property declaration toJSON with no expression and nodes 1`] = `
5155
"nodes": [
5256
<baz: bang>,
5357
],
58+
"prop": "foo",
5459
"propInterpolation": <foo>,
5560
"raws": {},
5661
"sassType": "decl",
5762
"source": <1:4-1:20 in 0>,
5863
"type": "decl",
64+
"value": "",
5965
}
6066
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`an @else rule toJSON with an expression 1`] = `
4+
{
5+
"elseCondition": <bar>,
6+
"inputs": [
7+
{
8+
"css": "@if foo {} @else if bar {}",
9+
"hasBOM": false,
10+
"id": "<input css _____>",
11+
},
12+
],
13+
"name": "else",
14+
"nodes": [],
15+
"params": "if bar",
16+
"raws": {},
17+
"sassType": "else-rule",
18+
"source": <1:1-1:27 in 0>,
19+
"type": "atrule",
20+
}
21+
`;
22+
23+
exports[`an @else rule toJSON with no expression 1`] = `
24+
{
25+
"inputs": [
26+
{
27+
"css": "@if foo {} @else {}",
28+
"hasBOM": false,
29+
"id": "<input css _____>",
30+
},
31+
],
32+
"name": "else",
33+
"nodes": [],
34+
"params": "",
35+
"raws": {},
36+
"sassType": "else-rule",
37+
"source": <1:1-1:20 in 0>,
38+
"type": "atrule",
39+
}
40+
`;

pkg/sass-parser/lib/src/statement/__snapshots__/function-rule.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ exports[`a @function rule toJSON 1`] = `
1313
"name": "function",
1414
"nodes": [],
1515
"parameters": <($bar)>,
16+
"params": "foo($bar)",
1617
"raws": {},
1718
"sassType": "function-rule",
1819
"source": <1:1-1:23 in 0>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`an @if rule toJSON 1`] = `
4+
{
5+
"ifCondition": <foo>,
6+
"inputs": [
7+
{
8+
"css": "@if foo {}",
9+
"hasBOM": false,
10+
"id": "<input css _____>",
11+
},
12+
],
13+
"name": "if",
14+
"nodes": [],
15+
"params": "foo",
16+
"raws": {},
17+
"sassType": "if-rule",
18+
"source": <1:1-1:11 in 0>,
19+
"type": "atrule",
20+
}
21+
`;

pkg/sass-parser/lib/src/statement/__snapshots__/mixin-rule.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ exports[`a @mixin rule toJSON 1`] = `
1313
"name": "mixin",
1414
"nodes": [],
1515
"parameters": <($bar)>,
16+
"params": "foo($bar)",
1617
"raws": {},
1718
"sassType": "mixin-rule",
1819
"source": <1:1-1:20 in 0>,

pkg/sass-parser/lib/src/statement/__snapshots__/variable-declaration.test.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ exports[`a variable declaration toJSON 1`] = `
1313
},
1414
],
1515
"namespace": "baz",
16+
"prop": "baz.$foo",
1617
"raws": {},
1718
"sassType": "variable-declaration",
1819
"source": <1:1-1:16 in 0>,
1920
"type": "decl",
21+
"value": ""bar"",
2022
"variableName": "foo",
2123
}
2224
`;

pkg/sass-parser/lib/src/statement/debug-rule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class DebugRule
6666
this.debugExpression = {text: value?.toString() ?? ''};
6767
}
6868

69-
/** The expresison whose value is emitted when the debug rule is executed. */
69+
/** The expression whose value is emitted when the debug rule is executed. */
7070
get debugExpression(): Expression {
7171
return this._debugExpression!;
7272
}

pkg/sass-parser/lib/src/statement/declaration.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export class Declaration
222222
toJSON(_?: string, inputs?: Map<postcss.Input, number>): object {
223223
return utils.toJSON(
224224
this,
225-
['propInterpolation', 'expression', 'nodes'],
225+
['prop', 'value', 'propInterpolation', 'expression', 'nodes'],
226226
inputs,
227227
);
228228
}

pkg/sass-parser/lib/src/statement/each-rule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class EachRule
9595
throw new Error("EachRule.params can't be overwritten.");
9696
}
9797

98-
/** The expresison whose value is iterated over. */
98+
/** The expression whose value is iterated over. */
9999
get eachExpression(): Expression {
100100
return this._eachExpression!;
101101
}

0 commit comments

Comments
 (0)