Skip to content

Commit

Permalink
Add support for function call expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Feb 20, 2025
1 parent 7c4ff8f commit 39129e3
Show file tree
Hide file tree
Showing 10 changed files with 559 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/src/js/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ void _updateAstPrototypes() {
getJSClass(
ContentRule(arguments, bogusSpan),
).defineGetter('arguments', (ContentRule self) => self.arguments);
getJSClass(
FunctionExpression('a', arguments, bogusSpan),
).defineGetter('arguments', (FunctionExpression self) => self.arguments);
getJSClass(
IfExpression(arguments, bogusSpan),
).defineGetter('arguments', (IfExpression self) => self.arguments);

_addSupportsConditionToInterpolation();

Expand Down
2 changes: 2 additions & 0 deletions pkg/sass-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Add support for parsing map expressions.

* Add support for parsing function calls.

## 0.4.14

* Add support for parsing color expressions.
Expand Down
5 changes: 5 additions & 0 deletions pkg/sass-parser/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export {
ColorExpressionProps,
ColorExpressionRaws,
} from './src/expression/color';
export {
FunctionExpression,
FunctionExpressionProps,
FunctionExpressionRaws,
} from './src/expression/function';
export {
ListExpression,
ListExpressionProps,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`a function expression toJSON if() 1`] = `
{
"arguments": <(cond, true, false)>,
"inputs": [
{
"css": "@#{if(cond, true, false)}",
"hasBOM": false,
"id": "<input css _____>",
},
],
"name": "if",
"raws": {},
"sassType": "function-call",
}
`;

exports[`a function expression toJSON with a namespace 1`] = `
{
"arguments": <(bar)>,
"inputs": [
{
"css": "@#{baz.foo(bar)}",
"hasBOM": false,
"id": "<input css _____>",
},
],
"name": "foo",
"namespace": "baz",
"raws": {},
"sassType": "function-call",
"source": <1:4-1:16 in 0>,
}
`;

exports[`a function expression toJSON without a namespace 1`] = `
{
"arguments": <(bar)>,
"inputs": [
{
"css": "@#{foo(bar)}",
"hasBOM": false,
"id": "<input css _____>",
},
],
"name": "foo",
"raws": {},
"sassType": "function-call",
"source": <1:4-1:12 in 0>,
}
`;
8 changes: 8 additions & 0 deletions pkg/sass-parser/lib/src/expression/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import * as sassInternal from '../sass-internal';

import {ArgumentList} from '../argument-list';
import {Expression} from '.';
import {BinaryOperationExpression} from './binary-operation';
import {BooleanExpression} from './boolean';
import {ColorExpression} from './color';
import {FunctionExpression} from './function';
import {ListExpression} from './list';
import {MapExpression} from './map';
import {NumberExpression} from './number';
Expand All @@ -20,6 +22,12 @@ const visitor = sassInternal.createExpressionVisitor<Expression>({
visitStringExpression: inner => new StringExpression(undefined, inner),
visitBooleanExpression: inner => new BooleanExpression(undefined, inner),
visitColorExpression: inner => new ColorExpression(undefined, inner),
visitFunctionExpression: inner => new FunctionExpression(undefined, inner),
visitIfExpression: inner =>
new FunctionExpression({
name: 'if',
arguments: new ArgumentList(undefined, inner.arguments),
}),
visitListExpression: inner => new ListExpression(undefined, inner),
visitMapExpression: inner => new MapExpression(undefined, inner),
visitNumberExpression: inner => new NumberExpression(undefined, inner),
Expand Down
2 changes: 2 additions & 0 deletions pkg/sass-parser/lib/src/expression/from-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Expression, ExpressionProps} from '.';
import {BinaryOperationExpression} from './binary-operation';
import {BooleanExpression} from './boolean';
import {ColorExpression} from './color';
import {FunctionExpression} from './function';
import {ListExpression} from './list';
import {MapExpression} from './map';
import {NumberExpression} from './number';
Expand All @@ -18,6 +19,7 @@ export function fromProps(props: ExpressionProps): Expression {
if ('left' in props) return new BinaryOperationExpression(props);
if ('separator' in props) return new ListExpression(props);
if ('nodes' in props) return new MapExpression(props);
if ('name' in props) return new FunctionExpression(props);
if ('value' in props) {
if (typeof props.value === 'boolean') return new BooleanExpression(props);
if (typeof props.value === 'number') return new NumberExpression(props);
Expand Down
Loading

0 comments on commit 39129e3

Please sign in to comment.