Skip to content

Commit

Permalink
Add support for the @include rule
Browse files Browse the repository at this point in the history
Co-authored-by: Carlos Israel Ortiz García <goodwine@google.com>
  • Loading branch information
nex3 and Goodwine committed Dec 12, 2024
1 parent bf50fad commit bc39d0c
Show file tree
Hide file tree
Showing 24 changed files with 3,062 additions and 33 deletions.
12 changes: 10 additions & 2 deletions lib/src/js/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import '../visitor/interface/expression.dart';
import '../visitor/interface/statement.dart';
import 'reflection.dart';
import 'set.dart';
import 'utils.dart';
import 'visitor/expression.dart';
import 'visitor/statement.dart';

Expand All @@ -32,14 +33,16 @@ class ParserExports {
required Function toCssIdentifier,
required Function createExpressionVisitor,
required Function createStatementVisitor,
required Function setToJS});
required Function setToJS,
required Function mapToRecord});

external set parse(Function function);
external set parseIdentifier(Function function);
external set toCssIdentifier(Function function);
external set createStatementVisitor(Function function);
external set createExpressionVisitor(Function function);
external set setToJS(Function function);
external set mapToRecord(Function function);
}

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

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

_addSupportsConditionToInterpolation();

Expand Down
13 changes: 13 additions & 0 deletions lib/src/js/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:js/js_util.dart';

import '../syntax.dart';
import '../utils.dart';
import '../util/map.dart';
import '../value.dart';
import 'array.dart';
import 'function.dart';
Expand Down Expand Up @@ -223,6 +224,18 @@ Map<String, Object?> objectToMap(Object object) {
return map;
}

@JS("Object")
external JSClass get _jsObjectClass;

/// Converts a JavaScript record into a map from property names to their values.
Object mapToObject(Map<String, Object?> map) {
var result = callConstructor<Object>(_jsObjectClass, const []);
for (var (key, value) in map.pairs) {
setProperty(result, key, value);
}
return result;
}

/// Converts a JavaScript separator string into a [ListSeparator].
ListSeparator jsToDartSeparator(String? separator) => switch (separator) {
' ' => ListSeparator.space,
Expand Down
2 changes: 2 additions & 0 deletions pkg/sass-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.4.8

Add support for parsing the `@include` rule.

Add support for parsing the `@mixin` rule.

Add support for parsing the `@return` rule.
Expand Down
19 changes: 19 additions & 0 deletions pkg/sass-parser/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import {Root} from './src/statement/root';
import * as sassInternal from './src/sass-internal';
import {Stringifier} from './src/stringifier';

export {
Argument,
ArgumentExpressionProps,
ArgumentObjectProps,
ArgumentProps,
ArgumentRaws,
} from './src/argument';
export {
ArgumentList,
ArgumentListObjectProps,
ArgumentListProps,
ArgumentListRaws,
NewArguments,
} from './src/argument-list';
export {
Configuration,
ConfigurationProps,
Expand Down Expand Up @@ -50,6 +64,11 @@ export {
NumberExpressionProps,
NumberExpressionRaws,
} from './src/expression/number';
export {
IncludeRule,
IncludeRuleProps,
IncludeRuleRaws,
} from './src/statement/include-rule';
export {
Interpolation,
InterpolationProps,
Expand Down
20 changes: 20 additions & 0 deletions pkg/sass-parser/lib/src/__snapshots__/argument-list.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`an argument list toJSON 1`] = `
{
"inputs": [
{
"css": "@include x(foo, bar...)",
"hasBOM": false,
"id": "<input css _____>",
},
],
"nodes": [
<foo>,
<bar...>,
],
"raws": {},
"sassType": "argument-list",
"source": <1:11-1:24 in 0>,
}
`;
50 changes: 50 additions & 0 deletions pkg/sass-parser/lib/src/__snapshots__/argument.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`a argument toJSON with a name 1`] = `
{
"inputs": [
{
"css": "@include x($baz: qux)",
"hasBOM": false,
"id": "<input css _____>",
},
],
"name": "baz",
"raws": {},
"rest": false,
"sassType": "argument",
"value": <qux>,
}
`;
exports[`a argument toJSON with no name 1`] = `
{
"inputs": [
{
"css": "@include x(qux)",
"hasBOM": false,
"id": "<input css _____>",
},
],
"raws": {},
"rest": false,
"sassType": "argument",
"value": <qux>,
}
`;
exports[`a argument toJSON with rest 1`] = `
{
"inputs": [
{
"css": "@include x(qux...)",
"hasBOM": false,
"id": "<input css _____>",
},
],
"raws": {},
"rest": true,
"sassType": "argument",
"value": <qux>,
}
`;
Loading

0 comments on commit bc39d0c

Please sign in to comment.