-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathfrom-props.ts
47 lines (44 loc) · 2.08 KB
/
from-props.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2024 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import * as sass from 'sass';
import {AnyExpression, ExpressionProps} from '.';
import {BinaryOperationExpression} from './binary-operation';
import {BooleanExpression} from './boolean';
import {ColorExpression} from './color';
import {FunctionExpression, FunctionExpressionProps} from './function';
import {InterpolatedFunctionExpression} from './interpolated-function';
import {ListExpression} from './list';
import {MapExpression} from './map';
import {NullExpression} from './null';
import {NumberExpression} from './number';
import {ParenthesizedExpression} from './parenthesized';
import {StringExpression} from './string';
import {UnaryOperationExpression} from './unary-operation';
import {VariableExpression} from './variable';
/** Constructs an expression from {@link ExpressionProps}. */
export function fromProps(props: ExpressionProps): AnyExpression {
if ('text' in props) return new StringExpression(props);
if ('left' in props) return new BinaryOperationExpression(props);
if ('operand' in props) return new UnaryOperationExpression(props);
if ('separator' in props) return new ListExpression(props);
if ('nodes' in props) return new MapExpression(props);
if ('inParens' in props) return new ParenthesizedExpression(props);
if ('variableName' in props) return new VariableExpression(props);
if ('name' in props) {
if (typeof props.name === 'string') {
return new FunctionExpression(props as FunctionExpressionProps);
} else {
return new InterpolatedFunctionExpression(props);
}
}
if ('value' in props) {
if (props.value === null) return new NullExpression();
if (typeof props.value === 'boolean') return new BooleanExpression(props);
if (typeof props.value === 'number') return new NumberExpression(props);
if (props.value instanceof sass.SassColor) {
return new ColorExpression(props);
}
}
throw new Error(`Unknown node type, keys: ${Object.keys(props)}`);
}