Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for function call expressions #2519

Merged
merged 2 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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