UNPKG

22.6 kBTypeScriptView Raw
1import {LegacyPluginThis} from './plugin_this';
2
3/**
4 * A synchronous callback that implements a custom Sass function. This can be
5 * passed to [[LegacySharedOptions.functions]] for either [[render]] or
6 * [[renderSync]].
7 *
8 * If this throws an error, Sass will treat that as the function failing with
9 * that error message.
10 *
11 * ```js
12 * const result = sass.renderSync({
13 * file: 'style.scss',
14 * functions: {
15 * "sum($arg1, $arg2)": (arg1, arg2) => {
16 * if (!(arg1 instanceof sass.types.Number)) {
17 * throw new Error("$arg1: Expected a number");
18 * } else if (!(arg2 instanceof sass.types.Number)) {
19 * throw new Error("$arg2: Expected a number");
20 * }
21 * return new sass.types.Number(arg1.getValue() + arg2.getValue());
22 * }
23 * }
24 * });
25 * ```
26 *
27 * @param args - One argument for each argument that's declared in the signature
28 * that's passed to [[LegacySharedOptions.functions]]. If the signature [takes
29 * arbitrary arguments](https://sass-lang.com/documentation/at-rules/function#taking-arbitrary-arguments),
30 * they're passed as a single argument list in the last argument.
31 *
32 * @category Legacy
33 * @deprecated This only works with the legacy [[render]] and [[renderSync]]
34 * APIs. Use [[CustomFunction]] with [[compile]], [[compileString]],
35 * [[compileAsync]], and [[compileStringAsync]] instead.
36 */
37export type LegacySyncFunction = (
38 this: LegacyPluginThis,
39 ...args: LegacyValue[]
40) => LegacyValue;
41
42/**
43 * An asynchronous callback that implements a custom Sass function. This can be
44 * passed to [[LegacySharedOptions.functions]], but only for [[render]].
45 *
46 * An asynchronous function must return `undefined`. Its final argument will
47 * always be a callback, which it should call with the result of the function
48 * once it's done running.
49 *
50 * If this throws an error, Sass will treat that as the function failing with
51 * that error message.
52 *
53 * ```js
54 * sass.render({
55 * file: 'style.scss',
56 * functions: {
57 * "sum($arg1, $arg2)": (arg1, arg2, done) => {
58 * if (!(arg1 instanceof sass.types.Number)) {
59 * throw new Error("$arg1: Expected a number");
60 * } else if (!(arg2 instanceof sass.types.Number)) {
61 * throw new Error("$arg2: Expected a number");
62 * }
63 * done(new sass.types.Number(arg1.getValue() + arg2.getValue()));
64 * }
65 * }
66 * }, (result, error) => {
67 * // ...
68 * });
69 * ```
70 *
71 * This is passed one argument for each argument that's declared in the
72 * signature that's passed to [[LegacySharedOptions.functions]]. If the
73 * signature [takes arbitrary
74 * arguments](https://sass-lang.com/documentation/at-rules/function#taking-arbitrary-arguments),
75 * they're passed as a single argument list in the last argument before the
76 * callback.
77 *
78 * @category Legacy
79 * @deprecated This only works with the legacy [[render]] and [[renderSync]]
80 * APIs. Use [[CustomFunction]] with [[compile]], [[compileString]],
81 * [[compileAsync]], and [[compileStringAsync]] instead.
82 */
83export type LegacyAsyncFunction =
84 | ((this: LegacyPluginThis, done: (result: LegacyValue) => void) => void)
85 | ((
86 this: LegacyPluginThis,
87 arg1: LegacyValue,
88 done: LegacyAsyncFunctionDone
89 ) => void)
90 | ((
91 this: LegacyPluginThis,
92 arg1: LegacyValue,
93 arg2: LegacyValue,
94 done: LegacyAsyncFunctionDone
95 ) => void)
96 | ((
97 this: LegacyPluginThis,
98 arg1: LegacyValue,
99 arg2: LegacyValue,
100 arg3: LegacyValue,
101 done: LegacyAsyncFunctionDone
102 ) => void)
103 | ((
104 this: LegacyPluginThis,
105 arg1: LegacyValue,
106 arg2: LegacyValue,
107 arg3: LegacyValue,
108 arg4: LegacyValue,
109 done: LegacyAsyncFunctionDone
110 ) => void)
111 | ((
112 this: LegacyPluginThis,
113 arg1: LegacyValue,
114 arg2: LegacyValue,
115 arg3: LegacyValue,
116 arg4: LegacyValue,
117 arg5: LegacyValue,
118 done: LegacyAsyncFunctionDone
119 ) => void)
120 | ((
121 this: LegacyPluginThis,
122 arg1: LegacyValue,
123 arg2: LegacyValue,
124 arg3: LegacyValue,
125 arg4: LegacyValue,
126 arg5: LegacyValue,
127 arg6: LegacyValue,
128 done: LegacyAsyncFunctionDone
129 ) => void)
130 | ((
131 this: LegacyPluginThis,
132 ...args: [...LegacyValue[], LegacyAsyncFunctionDone]
133 ) => void);
134
135/**
136 * The function called by a [[LegacyAsyncFunction]] to indicate that it's
137 * finished.
138 *
139 * @param result - If this is a [[LegacyValue]], that indicates that the
140 * function call completed successfully. If it's a [[types.Error]], that
141 * indicates that the function call failed.
142 *
143 * @category Legacy
144 * @deprecated This only works with the legacy [[render]] and [[renderSync]]
145 * APIs. Use [[CustomFunction]] with [[compile]], [[compileString]],
146 * [[compileAsync]], and [[compileStringAsync]] instead.
147 */
148export type LegacyAsyncFunctionDone = (
149 result: LegacyValue | types.Error
150) => void;
151
152/**
153 * A callback that implements a custom Sass function. For [[renderSync]], this
154 * must be a [[LegacySyncFunction]] which returns its result directly; for
155 * [[render]], it may be either a [[LegacySyncFunction]] or a
156 * [[LegacyAsyncFunction]] which calls a callback with its result.
157 *
158 * See [[LegacySharedOptions.functions]] for more details.
159 *
160 * @category Legacy
161 * @deprecated This only works with the legacy [[render]] and [[renderSync]]
162 * APIs. Use [[CustomFunction]] with [[compile]], [[compileString]],
163 * [[compileAsync]], and [[compileStringAsync]] instead.
164 */
165export type LegacyFunction<sync extends 'sync' | 'async'> = sync extends 'async'
166 ? LegacySyncFunction | LegacyAsyncFunction
167 : LegacySyncFunction;
168
169/**
170 * A type representing all the possible values that may be passed to or returned
171 * from a [[LegacyFunction]].
172 *
173 * @category Legacy
174 * @deprecated This only works with the legacy [[render]] and [[renderSync]]
175 * APIs. Use [[Value]] with [[compile]], [[compileString]], [[compileAsync]],
176 * and [[compileStringAsync]] instead.
177 */
178export type LegacyValue =
179 | types.Null
180 | types.Number
181 | types.String
182 | types.Boolean
183 | types.Color
184 | types.List
185 | types.Map;
186
187/**
188 * A shorthand for `sass.types.Boolean.TRUE`.
189 *
190 * @category Legacy
191 * @deprecated This only works with the legacy [[render]] and [[renderSync]]
192 * APIs. Use [[sassTrue]] with [[compile]], [[compileString]], [[compileAsync]],
193 * and [[compileStringAsync]] instead.
194 */
195export const TRUE: types.Boolean<true>;
196
197/**
198 * A shorthand for `sass.types.Boolean.FALSE`.
199 *
200 * @category Legacy
201 * @deprecated This only works with the legacy [[render]] and [[renderSync]]
202 * APIs. Use [[sassFalse]] with [[compile]], [[compileString]], [[compileAsync]],
203 * and [[compileStringAsync]] instead.
204 */
205export const FALSE: types.Boolean<false>;
206
207/**
208 * A shorthand for `sass.types.Null.NULL`.
209 *
210 * @category Legacy
211 * @deprecated This only works with the legacy [[render]] and [[renderSync]]
212 * APIs. Use [[sassNull]] with [[compile]], [[compileString]], [[compileAsync]],
213 * and [[compileStringAsync]] instead.
214 */
215export const NULL: types.Null;
216
217/**
218 * The namespace for value types used in the legacy function API.
219 *
220 * @category Legacy
221 * @deprecated This only works with the legacy [[render]] and [[renderSync]]
222 * APIs. Use [[Value]] with [[compile]], [[compileString]], [[compileAsync]],
223 * and [[compileStringAsync]] instead.
224 */
225export namespace types {
226 /**
227 * The class for Sass's singleton [`null`
228 * value](https://sass-lang.com/documentation/values/null). The value itself
229 * can be accessed through the [[NULL]] field.
230 */
231 export class Null {
232 /** Sass's singleton `null` value. */
233 static readonly NULL: Null;
234 }
235
236 /**
237 * Sass's [number type](https://sass-lang.com/documentation/values/numbers).
238 */
239 export class Number {
240 /**
241 * @param value - The numeric value of the number.
242 *
243 * @param unit - If passed, the number's unit.
244 *
245 * Complex units can be represented as
246 * `<unit>*<unit>*.../<unit>*<unit>*...`, with numerator units on the
247 * left-hand side of the `/` and denominator units on the right. A number
248 * with only numerator units may omit the `/` and the units after it, and a
249 * number with only denominator units may be represented
250 * with no units before the `/`.
251 *
252 * @example
253 *
254 * ```scss
255 * new sass.types.Number(0.5); // == 0.5
256 * new sass.types.Number(10, "px"); // == 10px
257 * new sass.types.Number(10, "px*px"); // == 10px * 1px
258 * new sass.types.Number(10, "px/s"); // == math.div(10px, 1s)
259 * new sass.types.Number(10, "px*px/s*s"); // == 10px * math.div(math.div(1px, 1s), 1s)
260 * ```
261 */
262 constructor(value: number, unit?: string);
263
264 /**
265 * Returns the value of the number, ignoring units.
266 *
267 * **Heads up!** This means that `96px` and `1in` will return different
268 * values, even though they represent the same length.
269 *
270 * @example
271 *
272 * ```js
273 * const number = new sass.types.Number(10, "px");
274 * number.getValue(); // 10
275 * ```
276 */
277 getValue(): number;
278
279 /**
280 * Destructively modifies this number by setting its numeric value to
281 * `value`, independent of its units.
282 *
283 * @deprecated Use [[constructor]] instead.
284 */
285 setValue(value: number): void;
286
287 /**
288 * Returns a string representation of this number's units. Complex units are
289 * returned in the same format that [[constructor]] accepts them.
290 *
291 * @example
292 *
293 * ```js
294 * // number is `10px`.
295 * number.getUnit(); // "px"
296 *
297 * // number is `math.div(10px, 1s)`.
298 * number.getUnit(); // "px/s"
299 * ```
300 */
301 getUnit(): string;
302
303 /**
304 * Destructively modifies this number by setting its units to `unit`,
305 * independent of its numeric value. Complex units are specified in the same
306 * format as [[constructor]].
307 *
308 * @deprecated Use [[constructor]] instead.
309 */
310 setUnit(unit: string): void;
311 }
312
313 /**
314 * Sass's [string type](https://sass-lang.com/documentation/values/strings).
315 *
316 * **Heads up!** This API currently provides no way of distinguishing between
317 * a [quoted](https://sass-lang.com/documentation/values/strings#quoted) and
318 * [unquoted](https://sass-lang.com/documentation/values/strings#unquoted)
319 * string.
320 */
321 export class String {
322 /**
323 * Creates an unquoted string with the given contents.
324 *
325 * **Heads up!** This API currently provides no way of creating a
326 * [quoted](https://sass-lang.com/documentation/values/strings#quoted)
327 * string.
328 */
329 constructor(value: string);
330
331 /**
332 * Returns the contents of the string. If the string contains escapes,
333 * those escapes are included literally if it’s
334 * [unquoted](https://sass-lang.com/documentation/values/strings#unquoted),
335 * while the values of the escapes are included if it’s
336 * [quoted](https://sass-lang.com/documentation/values/strings#quoted).
337 *
338 * @example
339 *
340 * ```
341 * // string is `Arial`.
342 * string.getValue(); // "Arial"
343 *
344 * // string is `"Helvetica Neue"`.
345 * string.getValue(); // "Helvetica Neue"
346 *
347 * // string is `\1F46D`.
348 * string.getValue(); // "\\1F46D"
349 *
350 * // string is `"\1F46D"`.
351 * string.getValue(); // "👭"
352 * ```
353 */
354 getValue(): string;
355
356 /**
357 * Destructively modifies this string by setting its numeric value to
358 * `value`.
359 *
360 * **Heads up!** Even if the string was originally quoted, this will cause
361 * it to become unquoted.
362 *
363 * @deprecated Use [[constructor]] instead.
364 */
365 setValue(value: string): void;
366 }
367
368 /**
369 * Sass's [boolean type](https://sass-lang.com/documentation/values/booleans).
370 *
371 * Custom functions should respect Sass’s notion of
372 * [truthiness](https://sass-lang.com/documentation/at-rules/control/if#truthiness-and-falsiness)
373 * by treating `false` and `null` as falsey and everything else as truthy.
374 *
375 * **Heads up!** Boolean values can't be constructed, they can only be
376 * accessed through the [[TRUE]] and [[FALSE]] constants.
377 */
378 export class Boolean<T extends boolean = boolean> {
379 /**
380 * Returns `true` if this is Sass's `true` value and `false` if this is
381 * Sass's `false` value.
382 *
383 * @example
384 *
385 * ```js
386 * // boolean is `true`.
387 * boolean.getValue(); // true
388 * boolean === sass.types.Boolean.TRUE; // true
389 *
390 * // boolean is `false`.
391 * boolean.getValue(); // false
392 * boolean === sass.types.Boolean.FALSE; // true
393 * ```
394 */
395 getValue(): T;
396
397 /** Sass's `true` value. */
398 static readonly TRUE: Boolean<true>;
399
400 /** Sass's `false` value. */
401 static readonly FALSE: Boolean<false>;
402 }
403
404 /**
405 * Sass's [color type](https://sass-lang.com/documentation/values/colors).
406 */
407 export class Color {
408 /**
409 * Creates a new Sass color with the given red, green, blue, and alpha
410 * channels. The red, green, and blue channels must be integers between 0
411 * and 255 (inclusive), and alpha must be between 0 and 1 (inclusive).
412 *
413 * @example
414 *
415 * ```js
416 * new sass.types.Color(107, 113, 127); // #6b717f
417 * new sass.types.Color(0, 0, 0, 0); // rgba(0, 0, 0, 0)
418 * ```
419 */
420 constructor(r: number, g: number, b: number, a?: number);
421
422 /**
423 * Creates a new Sass color with alpha, red, green, and blue channels taken
424 * from respective two-byte chunks of a hexidecimal number.
425 *
426 * @example
427 *
428 * ```js
429 * new sass.types.Color(0xff6b717f); // #6b717f
430 * new sass.types.Color(0x00000000); // rgba(0, 0, 0, 0)
431 * ```
432 */
433 constructor(argb: number);
434
435 /**
436 * Returns the red channel of the color as an integer from 0 to 255.
437 *
438 * @example
439 *
440 * ```js
441 * // color is `#6b717f`.
442 * color.getR(); // 107
443 *
444 * // color is `#b37399`.
445 * color.getR(); // 179
446 * ```
447 */
448 getR(): number;
449
450 /**
451 * Sets the red channel of the color. The value must be an integer between 0
452 * and 255 (inclusive).
453 *
454 * @deprecated Use [[constructor]] instead.
455 */
456 setR(value: number): void;
457
458 /**
459 * Returns the green channel of the color as an integer from 0 to 255.
460 *
461 * @example
462 *
463 * ```js
464 * // color is `#6b717f`.
465 * color.getG(); // 113
466 *
467 * // color is `#b37399`.
468 * color.getG(); // 115
469 * ```
470 */
471 getG(): number;
472
473 /**
474 * Sets the green channel of the color. The value must be an integer between
475 * 0 and 255 (inclusive).
476 *
477 * @deprecated Use [[constructor]] instead.
478 */
479 setG(value: number): void;
480
481 /**
482 * Returns the blue channel of the color as an integer from 0 to 255.
483 *
484 * @example
485 *
486 * ```js
487 * // color is `#6b717f`.
488 * color.getB(); // 127
489 *
490 * // color is `#b37399`.
491 * color.getB(); // 153
492 * ```
493 */
494 getB(): number;
495
496 /**
497 * Sets the blue channel of the color. The value must be an integer between
498 * 0 and 255 (inclusive).
499 *
500 * @deprecated Use [[constructor]] instead.
501 */
502 setB(value: number): void;
503
504 /**
505 * Returns the alpha channel of the color as a number from 0 to 1.
506 *
507 * @example
508 *
509 * ```js
510 * // color is `#6b717f`.
511 * color.getA(); // 1
512 *
513 * // color is `transparent`.
514 * color.getA(); // 0
515 * ```
516 */
517 getA(): number;
518
519 /**
520 * Sets the alpha channel of the color. The value must be between 0 and 1
521 * (inclusive).
522 *
523 * @deprecated Use [[constructor]] instead.
524 */
525 setA(value: number): void;
526 }
527
528 /**
529 * Sass's [list type](https://sass-lang.com/documentation/values/lists).
530 *
531 * **Heads up!** This list type’s methods use 0-based indexing, even though
532 * within Sass lists use 1-based indexing. These methods also don’t support
533 * using negative numbers to index backwards from the end of the list.
534 */
535 export class List {
536 /**
537 * Creates a new Sass list.
538 *
539 * **Heads up!** The initial values of the list elements are undefined.
540 * These elements must be set using [[setValue]] before accessing them or
541 * passing the list back to Sass.
542 *
543 * @example
544 *
545 * ```js
546 * const list = new sass.types.List(3);
547 * list.setValue(0, new sass.types.Number(10, "px"));
548 * list.setValue(1, new sass.types.Number(15, "px"));
549 * list.setValue(2, new sass.types.Number(32, "px"));
550 * list; // 10px, 15px, 32px
551 * ```
552 *
553 * @param length - The number of (initially undefined) elements in the list.
554 * @param commaSeparator - If `true`, the list is comma-separated; otherwise,
555 * it's space-separated. Defaults to `true`.
556 */
557 constructor(length: number, commaSeparator?: boolean);
558
559 /**
560 * Returns the element at `index`, or `undefined` if that value hasn't yet
561 * been set.
562 *
563 * @example
564 *
565 * ```js
566 * // list is `10px, 15px, 32px`
567 * list.getValue(0); // 10px
568 * list.getValue(2); // 32px
569 * ```
570 *
571 * @param index - A (0-based) index into this list.
572 * @throws `Error` if `index` is less than 0 or greater than or equal to the
573 * number of elements in this list.
574 */
575 getValue(index: number): LegacyValue | undefined;
576
577 /**
578 * Sets the element at `index` to `value`.
579 *
580 * @example
581 *
582 * ```js
583 * // list is `10px, 15px, 32px`
584 * list.setValue(1, new sass.types.Number(18, "px"));
585 * list; // 10px, 18px, 32px
586 * ```
587 *
588 * @param index - A (0-based) index into this list.
589 * @throws `Error` if `index` is less than 0 or greater than or equal to the
590 * number of elements in this list.
591 */
592 setValue(index: number, value: LegacyValue): void;
593
594 /**
595 * Returns `true` if this list is comma-separated and `false` otherwise.
596 *
597 * @example
598 *
599 * ```js
600 * // list is `10px, 15px, 32px`
601 * list.getSeparator(); // true
602 *
603 * // list is `1px solid`
604 * list.getSeparator(); // false
605 * ```
606 */
607 getSeparator(): boolean;
608
609 /**
610 * Sets whether the list is comma-separated.
611 *
612 * @param isComma - `true` to make the list comma-separated, `false` otherwise.
613 */
614 setSeparator(isComma: boolean): void;
615
616 /**
617 * Returns the number of elements in the list.
618 *
619 * @example
620 *
621 * ```js
622 * // list is `10px, 15px, 32px`
623 * list.getLength(); // 3
624 *
625 * // list is `1px solid`
626 * list.getLength(); // 2
627 * ```
628 */
629 getLength(): number;
630 }
631
632 /**
633 * Sass's [map type](https://sass-lang.com/documentation/values/maps).
634 *
635 * **Heads up!** This map type is represented as a list of key-value pairs
636 * rather than a mapping from keys to values. The only way to find the value
637 * associated with a given key is to iterate through the map checking for that
638 * key. Maps created through this API are still forbidden from having duplicate
639 * keys.
640 */
641 export class Map {
642 /**
643 * Creates a new Sass map.
644 *
645 * **Heads up!** The initial keys and values of the map are undefined. They
646 * must be set using [[setKey]] and [[setValue]] before accessing them or
647 * passing the map back to Sass.
648 *
649 * @example
650 *
651 * ```js
652 * const map = new sass.types.Map(2);
653 * map.setKey(0, new sass.types.String("width"));
654 * map.setValue(0, new sass.types.Number(300, "px"));
655 * map.setKey(1, new sass.types.String("height"));
656 * map.setValue(1, new sass.types.Number(100, "px"));
657 * map; // (width: 300px, height: 100px)
658 * ```
659 *
660 * @param length - The number of (initially undefined) key/value pairs in the map.
661 */
662 constructor(length: number);
663
664 /**
665 * Returns the value in the key/value pair at `index`.
666 *
667 * @example
668 *
669 * ```js
670 * // map is `(width: 300px, height: 100px)`
671 * map.getValue(0); // 300px
672 * map.getValue(1); // 100px
673 * ```
674 *
675 * @param index - A (0-based) index of a key/value pair in this map.
676 * @throws `Error` if `index` is less than 0 or greater than or equal to the
677 * number of pairs in this map.
678 */
679 getValue(index: number): LegacyValue;
680
681 /**
682 * Sets the value in the key/value pair at `index` to `value`.
683 *
684 * @example
685 *
686 * ```js
687 * // map is `("light": 200, "medium": 400, "bold": 600)`
688 * map.setValue(1, new sass.types.Number(300));
689 * map; // ("light": 200, "medium": 300, "bold": 600)
690 * ```
691 *
692 * @param index - A (0-based) index of a key/value pair in this map.
693 * @throws `Error` if `index` is less than 0 or greater than or equal to the
694 * number of pairs in this map.
695 */
696 setValue(index: number, value: LegacyValue): void;
697
698 /**
699 * Returns the key in the key/value pair at `index`.
700 *
701 * @example
702 *
703 * ```js
704 * // map is `(width: 300px, height: 100px)`
705 * map.getKey(0); // width
706 * map.getKey(1); // height
707 * ```
708 *
709 * @param index - A (0-based) index of a key/value pair in this map.
710 * @throws `Error` if `index` is less than 0 or greater than or equal to the
711 * number of pairs in this map.
712 */
713 getKey(index: number): LegacyValue;
714
715 /**
716 * Sets the value in the key/value pair at `index` to `value`.
717 *
718 * @example
719 *
720 * ```js
721 * // map is `("light": 200, "medium": 400, "bold": 600)`
722 * map.setValue(1, new sass.types.String("lighter"));
723 * map; // ("lighter": 200, "medium": 300, "bold": 600)
724 * ```
725 *
726 * @param index - A (0-based) index of a key/value pair in this map.
727 * @throws `Error` if `index` is less than 0 or greater than or equal to the
728 * number of pairs in this map.
729 */
730 setKey(index: number, key: LegacyValue): void;
731
732 /**
733 * Returns the number of key/value pairs in this map.
734 *
735 * @example
736 *
737 * ```js
738 * // map is `("light": 200, "medium": 400, "bold": 600)`
739 * map.getLength(); // 3
740 *
741 * // map is `(width: 300px, height: 100px)`
742 * map.getLength(); // 2
743 * ```
744 */
745 getLength(): number;
746 }
747
748 /**
749 * An error that can be returned from a Sass function to signal that it
750 * encountered an error. This is the only way to signal an error
751 * asynchronously from a [[LegacyAsyncFunction]].
752 */
753 export class Error {
754 constructor(message: string);
755 }
756}
757
\No newline at end of file