Skip to content

Commit bc39d0c

Browse files
nex3Goodwine
andcommitted
Add support for the @include rule
Co-authored-by: Carlos Israel Ortiz García <goodwine@google.com>
1 parent bf50fad commit bc39d0c

24 files changed

+3062
-33
lines changed

lib/src/js/parser.dart

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import '../visitor/interface/expression.dart';
2020
import '../visitor/interface/statement.dart';
2121
import 'reflection.dart';
2222
import 'set.dart';
23+
import 'utils.dart';
2324
import 'visitor/expression.dart';
2425
import 'visitor/statement.dart';
2526

@@ -32,14 +33,16 @@ class ParserExports {
3233
required Function toCssIdentifier,
3334
required Function createExpressionVisitor,
3435
required Function createStatementVisitor,
35-
required Function setToJS});
36+
required Function setToJS,
37+
required Function mapToRecord});
3638

3739
external set parse(Function function);
3840
external set parseIdentifier(Function function);
3941
external set toCssIdentifier(Function function);
4042
external set createStatementVisitor(Function function);
4143
external set createExpressionVisitor(Function function);
4244
external set setToJS(Function function);
45+
external set mapToRecord(Function function);
4346
}
4447

4548
/// An empty interpolation, used to initialize empty AST entries to modify their
@@ -61,7 +64,8 @@ ParserExports loadParserExports() {
6164
(JSExpressionVisitorObject inner) => JSExpressionVisitor(inner)),
6265
createStatementVisitor: allowInterop(
6366
(JSStatementVisitorObject inner) => JSStatementVisitor(inner)),
64-
setToJS: allowInterop((Set<Object?> set) => JSSet([...set])));
67+
setToJS: allowInterop((Set<Object?> set) => JSSet([...set])),
68+
mapToRecord: allowInterop(mapToObject));
6569
}
6670

6771
/// Modifies the prototypes of the Sass AST classes to provide access to JS.
@@ -88,6 +92,10 @@ void _updateAstPrototypes() {
8892
'accept',
8993
(Expression self, ExpressionVisitor<Object?> visitor) =>
9094
self.accept(visitor));
95+
var arguments = ArgumentList([], {}, bogusSpan);
96+
var include = IncludeRule('a', arguments, bogusSpan);
97+
getJSClass(include)
98+
.defineGetter('arguments', (IncludeRule self) => self.arguments);
9199

92100
_addSupportsConditionToInterpolation();
93101

lib/src/js/utils.dart

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:js/js_util.dart';
1111

1212
import '../syntax.dart';
1313
import '../utils.dart';
14+
import '../util/map.dart';
1415
import '../value.dart';
1516
import 'array.dart';
1617
import 'function.dart';
@@ -223,6 +224,18 @@ Map<String, Object?> objectToMap(Object object) {
223224
return map;
224225
}
225226

227+
@JS("Object")
228+
external JSClass get _jsObjectClass;
229+
230+
/// Converts a JavaScript record into a map from property names to their values.
231+
Object mapToObject(Map<String, Object?> map) {
232+
var result = callConstructor<Object>(_jsObjectClass, const []);
233+
for (var (key, value) in map.pairs) {
234+
setProperty(result, key, value);
235+
}
236+
return result;
237+
}
238+
226239
/// Converts a JavaScript separator string into a [ListSeparator].
227240
ListSeparator jsToDartSeparator(String? separator) => switch (separator) {
228241
' ' => ListSeparator.space,

pkg/sass-parser/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 0.4.8
22

3+
Add support for parsing the `@include` rule.
4+
35
Add support for parsing the `@mixin` rule.
46

57
Add support for parsing the `@return` rule.

pkg/sass-parser/lib/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ import {Root} from './src/statement/root';
88
import * as sassInternal from './src/sass-internal';
99
import {Stringifier} from './src/stringifier';
1010

11+
export {
12+
Argument,
13+
ArgumentExpressionProps,
14+
ArgumentObjectProps,
15+
ArgumentProps,
16+
ArgumentRaws,
17+
} from './src/argument';
18+
export {
19+
ArgumentList,
20+
ArgumentListObjectProps,
21+
ArgumentListProps,
22+
ArgumentListRaws,
23+
NewArguments,
24+
} from './src/argument-list';
1125
export {
1226
Configuration,
1327
ConfigurationProps,
@@ -50,6 +64,11 @@ export {
5064
NumberExpressionProps,
5165
NumberExpressionRaws,
5266
} from './src/expression/number';
67+
export {
68+
IncludeRule,
69+
IncludeRuleProps,
70+
IncludeRuleRaws,
71+
} from './src/statement/include-rule';
5372
export {
5473
Interpolation,
5574
InterpolationProps,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`an argument list toJSON 1`] = `
4+
{
5+
"inputs": [
6+
{
7+
"css": "@include x(foo, bar...)",
8+
"hasBOM": false,
9+
"id": "<input css _____>",
10+
},
11+
],
12+
"nodes": [
13+
<foo>,
14+
<bar...>,
15+
],
16+
"raws": {},
17+
"sassType": "argument-list",
18+
"source": <1:11-1:24 in 0>,
19+
}
20+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`a argument toJSON with a name 1`] = `
4+
{
5+
"inputs": [
6+
{
7+
"css": "@include x($baz: qux)",
8+
"hasBOM": false,
9+
"id": "<input css _____>",
10+
},
11+
],
12+
"name": "baz",
13+
"raws": {},
14+
"rest": false,
15+
"sassType": "argument",
16+
"value": <qux>,
17+
}
18+
`;
19+
20+
exports[`a argument toJSON with no name 1`] = `
21+
{
22+
"inputs": [
23+
{
24+
"css": "@include x(qux)",
25+
"hasBOM": false,
26+
"id": "<input css _____>",
27+
},
28+
],
29+
"raws": {},
30+
"rest": false,
31+
"sassType": "argument",
32+
"value": <qux>,
33+
}
34+
`;
35+
36+
exports[`a argument toJSON with rest 1`] = `
37+
{
38+
"inputs": [
39+
{
40+
"css": "@include x(qux...)",
41+
"hasBOM": false,
42+
"id": "<input css _____>",
43+
},
44+
],
45+
"raws": {},
46+
"rest": true,
47+
"sassType": "argument",
48+
"value": <qux>,
49+
}
50+
`;

0 commit comments

Comments
 (0)