-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathfunction.ts
152 lines (138 loc) · 4.21 KB
/
function.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2025 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 postcss from 'postcss';
import {ArgumentList, ArgumentListProps} from '../argument-list';
import {LazySource} from '../lazy-source';
import {NodeProps} from '../node';
import {RawWithValue} from '../raw-with-value';
import * as sassInternal from '../sass-internal';
import * as utils from '../utils';
import {Expression} from '.';
/**
* The initializer properties for {@link FunctionExpression}.
*
* @category Expression
*/
export interface FunctionExpressionProps extends NodeProps {
namespace?: string;
name: string;
arguments: ArgumentList | ArgumentListProps;
raws?: FunctionExpressionRaws;
}
/**
* Raws indicating how to precisely serialize a {@link FunctionExpression}.
*
* @category Expression
*/
export interface FunctionExpressionRaws {
/**
* The function's namespace.
*
* This may be different than {@link FunctionExpression.namespace} if the
* namespace contains escape codes.
*/
namespace?: RawWithValue<string>;
/**
* The function's name.
*
* This may be different than {@link FunctionExpression.name} if the name
* contains escape codes or underscores.
*/
name?: RawWithValue<string>;
}
/**
* An expression representing a (non-interpolated) function call in Sass.
*
* @category Expression
*/
export class FunctionExpression extends Expression {
readonly sassType = 'function-call' as const;
declare raws: FunctionExpressionRaws;
/**
* This function's namespace.
*
* This is the parsed and normalized value, with escapes resolved to the
* characters they represent.
*/
get namespace(): string | undefined {
return this._namespace;
}
set namespace(namespace: string | undefined) {
// TODO - postcss/postcss#1957: Mark this as dirty
this._namespace = namespace;
}
private declare _namespace: string | undefined;
/**
* This function's name.
*
* This is the parsed and normalized value, with underscores converted to
* hyphens and escapes resolved to the characters they represent.
*/
get name(): string {
return this._name;
}
set name(name: string) {
// TODO - postcss/postcss#1957: Mark this as dirty
this._name = name;
}
private declare _name: string;
/** The arguments to pass to the function. */
get arguments(): ArgumentList {
return this._arguments!;
}
set arguments(args: ArgumentList | ArgumentListProps | undefined) {
if (this._arguments) this._arguments.parent = undefined;
this._arguments = args
? 'sassType' in args
? args
: new ArgumentList(args)
: new ArgumentList();
this._arguments.parent = this;
}
private declare _arguments: ArgumentList;
constructor(defaults: FunctionExpressionProps);
/** @hidden */
constructor(_: undefined, inner: sassInternal.FunctionExpression);
constructor(defaults?: object, inner?: sassInternal.FunctionExpression) {
super(defaults);
if (inner) {
this.source = new LazySource(inner);
this.namespace = inner.namespace ?? undefined;
this.name = inner.name;
this.arguments = new ArgumentList(undefined, inner.arguments);
}
}
clone(overrides?: Partial<FunctionExpressionProps>): this {
return utils.cloneNode(this, overrides, [
'raws',
{name: 'namespace', explicitUndefined: true},
'name',
'arguments',
]);
}
toJSON(): object;
/** @hidden */
toJSON(_: string, inputs: Map<postcss.Input, number>): object;
toJSON(_?: string, inputs?: Map<postcss.Input, number>): object {
return utils.toJSON(this, ['namespace', 'name', 'arguments'], inputs);
}
/** @hidden */
toString(): string {
return (
(this.namespace
? (this.raws.namespace?.value === this.namespace
? this.raws.namespace.raw
: sassInternal.toCssIdentifier(this.namespace)) + '.'
: '') +
(this.raws.name?.value === this.name
? this.raws.name.raw
: sassInternal.toCssIdentifier(this.name)) +
this.arguments
);
}
/** @hidden */
get nonStatementChildren(): ReadonlyArray<ArgumentList> {
return [this.arguments];
}
}