-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathindex.ts
More file actions
200 lines (178 loc) · 5.9 KB
/
index.ts
File metadata and controls
200 lines (178 loc) · 5.9 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2021 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 {List, ValueObject} from 'immutable';
import {ListSeparator} from './list';
import {SassBoolean} from './boolean';
import {SassColor} from './color';
import {SassMap} from './map';
import {SassNumber} from './number';
import {SassString} from './string';
import {valueError} from '../utils';
import {SassCalculation} from './calculations';
import {SassMixin} from './mixin';
/**
* A SassScript value.
*
* All SassScript values are immutable.
*
* Concrete values (such as `SassColor`) are implemented as subclasses and get
* instantiated as normal JS classes.
*
* Untyped values can be cast to particular types using `assert*()` functions,
* which throw user-friendly error messages if they fail.
*
* All values, except `false` and `null`, count as `true`.
*
* All values can be used as lists. Maps count as lists of pairs, while all
* other values count as single-value lists. Empty maps are equal to empty
* lists.
*/
export abstract class Value implements ValueObject {
/** Whether `this` counts as `true`. */
get isTruthy(): boolean {
return true;
}
/** Returns JS null if `this` is `sassNull`. Otherwise, returns `this`. */
get realNull(): Value | null {
return this;
}
/** `this` as a list. */
get asList(): List<Value> {
return List([this]);
}
/** The separator for `this` as a list. */
get separator(): ListSeparator {
return null;
}
/** Whether `this`, as a list, has brackets. */
get hasBrackets(): boolean {
return false;
}
// Subclasses can override this to change the behavior of
// `sassIndexToListIndex`.
protected get lengthAsList(): number {
return 1;
}
/**
* Converts `sassIndex` to a JS index into the array returned by `asList`.
*
* Sass indices start counting at 1, and may be negative in order to index
* from the end of the list.
*
* `sassIndex` must be...
* - a number, and
* - an integer, and
* - a valid index into `asList`.
*
* Otherwise, this throws an error.
*
* If `this` came from a function argument, `name` is the argument name
* (without the `$`) and is used for error reporting.
*/
sassIndexToListIndex(sassIndex: Value, name?: string): number {
const index = sassIndex.assertNumber().assertInt();
if (index === 0) {
throw Error('List index may not be 0.');
}
if (Math.abs(index) > this.lengthAsList) {
throw valueError(
`Invalid index ${sassIndex} for a list with ${this.lengthAsList} elements`,
name,
);
}
return index < 0 ? this.lengthAsList + index : index - 1;
}
/** Returns `this.asList.get(index)`. */
get(index: number): Value | undefined {
return index < 1 && index >= -1 ? this : undefined;
}
/**
* Casts `this` to `SassBoolean`; throws if `this` isn't a boolean.
*
* If `this` came from a function argument, `name` is the argument name
* (without the `$`) and is used for error reporting.
*/
assertBoolean(name?: string): SassBoolean {
throw valueError(`${this} is not a boolean`, name);
}
/**
* Casts `this` to `SassCalculation`; throws if `this` isn't a calculation.
*
* If `this` came from a function argument, `name` is the argument name
* (without the `$`) and is used for error reporting.
*/
assertCalculation(name?: string): SassCalculation {
throw valueError(`${this} is not a calculation`, name);
}
/**
* Casts `this` to `SassColor`; throws if `this` isn't a color.
*
* If `this` came from a function argument, `name` is the argument name
* (without the `$`) and is used for error reporting.
*/
assertColor(name?: string): SassColor {
throw valueError(`${this} is not a color`, name);
}
/**
* Casts `this` to `SassFunction`; throws if `this` isn't a function
* reference.
*
* If `this` came from a function argument, `name` is the argument name
* (without the `$`) and is used for error reporting.
*/
assertFunction(name?: string): Value {
throw valueError(`${this} is not a function reference`, name);
// TODO(awjin): Narrow the return type to SassFunction.
}
/**
* Casts `this` to `SassMixin`; throws if `this` isn't a mixin
* reference.
*
* If `this` came from a function argument, `name` is the argument name
* (without the `$`) and is used for error reporting.
*/
assertMixin(name?: string): SassMixin {
throw valueError(`${this} is not a mixin reference`, name);
}
/**
* Casts `this` to `SassMap`; throws if `this` isn't a map.
*
* If `this` came from a function argument, `name` is the argument name
* (without the `$`) and is used for error reporting.
*/
assertMap(name?: string): SassMap {
throw valueError(`${this} is not a map`, name);
}
/**
* Returns `this` as a `SassMap` if it counts as one (including empty lists),
* or `null` if it does not.
*/
tryMap(): SassMap | null {
return null;
}
/**
* Casts `this` to `SassString`; throws if `this` isn't a string.
*
* If `this` came from a function argument, `name` is the argument name
* (without the `$`) and is used for error reporting.
*/
assertNumber(name?: string): SassNumber {
throw valueError(`${this} is not a number`, name);
}
/**
* Casts `this` to `SassString`; throws if `this` isn't a string.
*
* If `this` came from a function argument, `name` is the argument name
* (without the `$`) and is used for error reporting.
*/
assertString(name?: string): SassString {
throw valueError(`${this} is not a string`, name);
}
/** Whether `this == other` in SassScript. */
abstract equals(other: Value): boolean;
/** This is the same for values that are `==` in SassScript. */
abstract hashCode(): number;
/** A meaningful descriptor for this value. */
abstract toString(): string;
}