UNPKG

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