UNPKG

16.2 kBTypeScriptView Raw
1/// <reference types="node" />
2
3export type ImporterReturnType =
4 | { file: string }
5 | { file?: string | undefined; contents: string }
6 | Error
7 | null
8 | types.Null
9 | types.Error;
10
11/**
12 * The context value is a value that is shared for the duration of a single render.
13 * The context object is the implicit `this` for importers and sass functions
14 * that are implemented in javascript.
15 *
16 * A render can be detected as asynchronous if the `callback` property is set on the context object.
17 */
18export interface Context {
19 options: Options;
20 callback: SassRenderCallback | undefined;
21 [data: string]: any;
22}
23
24export interface AsyncContext extends Context {
25 callback: SassRenderCallback;
26}
27
28export interface SyncContext extends Context {
29 callback: undefined;
30}
31
32export type AsyncImporter = (
33 this: AsyncContext,
34 url: string,
35 prev: string,
36 done: (data: ImporterReturnType) => void,
37) => void;
38export type SyncImporter = (this: SyncContext, url: string, prev: string) => ImporterReturnType;
39export type Importer = AsyncImporter | SyncImporter;
40
41// These function types enumerate up to 6 js arguments. More than that will be incorrectly marked by the compiler as an error.
42
43// ** Sync Sass functions receiving fixed # of arguments ***
44export type SyncSassFn = (this: SyncContext, ...$args: types.Value[]) => types.ReturnValue;
45
46// ** Sync Sass functions receiving variable # of arguments ***
47export type SyncSassVarArgFn1 = (this: SyncContext, $arg1: types.Value[]) => types.ReturnValue;
48export type SyncSassVarArgFn2 = (this: SyncContext, $arg1: types.Value, $arg2: types.Value[]) => types.ReturnValue;
49export type SyncSassVarArgFn3 = (
50 this: SyncContext,
51 $arg1: types.Value,
52 $arg2: types.Value,
53 $arg3: types.Value[],
54) => types.ReturnValue;
55export type SyncSassVarArgFn4 = (
56 this: SyncContext,
57 $arg1: types.Value,
58 $arg2: types.Value,
59 $arg3: types.Value,
60 $arg4: types.Value[],
61) => types.ReturnValue;
62export type SyncSassVarArgFn5 = (
63 this: SyncContext,
64 $arg1: types.Value,
65 $arg2: types.Value,
66 $arg3: types.Value,
67 $arg4: types.Value,
68 $arg5: types.Value[],
69) => types.ReturnValue;
70export type SyncSassVarArgFn6 = (
71 this: SyncContext,
72 $arg1: types.Value,
73 $arg2: types.Value,
74 $arg3: types.Value,
75 $arg4: types.Value,
76 $arg5: types.Value,
77 $arg6: types.Value[],
78) => types.ReturnValue;
79
80export type SassFunctionCallback = ($result: types.ReturnValue) => void;
81
82// ** Async Sass functions receiving fixed # of arguments ***
83export type AsyncSassFn0 = (this: AsyncContext, cb: SassFunctionCallback) => void;
84export type AsyncSassFn1 = (this: AsyncContext, $arg1: types.Value, cb: SassFunctionCallback) => void;
85export type AsyncSassFn2 = (
86 this: AsyncContext,
87 $arg1: types.Value,
88 $arg2: types.Value,
89 cb: SassFunctionCallback,
90) => void;
91export type AsyncSassFn3 = (
92 this: AsyncContext,
93 $arg1: types.Value,
94 $arg2: types.Value,
95 $arg3: types.Value,
96 cb: SassFunctionCallback,
97) => void;
98export type AsyncSassFn4 = (
99 this: AsyncContext,
100 $arg1: types.Value,
101 $arg2: types.Value,
102 $arg3: types.Value,
103 $arg4: types.Value,
104 cb: SassFunctionCallback,
105) => void;
106export type AsyncSassFn5 = (
107 this: AsyncContext,
108 $arg1: types.Value,
109 $arg2: types.Value,
110 $arg3: types.Value,
111 $arg4: types.Value,
112 $arg5: types.Value,
113 cb: SassFunctionCallback,
114) => void;
115export type AsyncSassFn6 = (
116 this: AsyncContext,
117 $arg1: types.Value,
118 $arg2: types.Value,
119 $arg3: types.Value,
120 $arg4: types.Value,
121 $arg5: types.Value,
122 $arg6: types.Value,
123 cb: SassFunctionCallback,
124) => void;
125
126// *** Async Sass Functions receiving variable # of arguments ***
127export type AsyncSassVarArgFn1 = (this: AsyncContext, $arg1: types.Value[], cb: SassFunctionCallback) => void;
128export type AsyncSassVarArgFn2 = (
129 this: AsyncContext,
130 $arg1: types.Value,
131 $arg2: types.Value[],
132 cb: SassFunctionCallback,
133) => void;
134export type AsyncSassVarArgFn3 = (
135 this: AsyncContext,
136 $arg1: types.Value,
137 $arg2: types.Value,
138 $arg3: types.Value[],
139 cb: SassFunctionCallback,
140) => void;
141export type AsyncSassVarArgFn4 = (
142 this: AsyncContext,
143 $arg1: types.Value,
144 $arg2: types.Value,
145 $arg3: types.Value,
146 $arg4: types.Value[],
147 cb: SassFunctionCallback,
148) => void;
149export type AsyncSassVarArgFn5 = (
150 this: AsyncContext,
151 $arg1: types.Value,
152 $arg2: types.Value,
153 $arg3: types.Value,
154 $arg4: types.Value,
155 $arg5: types.Value[],
156 cb: SassFunctionCallback,
157) => void;
158export type AsyncSassVarArgFn6 = (
159 this: AsyncContext,
160 $arg1: types.Value,
161 $arg2: types.Value,
162 $arg3: types.Value,
163 $arg4: types.Value,
164 $arg5: types.Value,
165 $arg6: types.Value[],
166 cb: SassFunctionCallback,
167) => void;
168
169export type SyncSassFunction =
170 | SyncSassFn
171 | SyncSassVarArgFn1
172 | SyncSassVarArgFn2
173 | SyncSassVarArgFn3
174 | SyncSassVarArgFn4
175 | SyncSassVarArgFn5
176 | SyncSassVarArgFn6;
177
178export type AsyncSassFunction =
179 | AsyncSassFn0
180 | AsyncSassFn1
181 | AsyncSassFn2
182 | AsyncSassFn3
183 | AsyncSassFn4
184 | AsyncSassFn5
185 | AsyncSassFn6
186 | AsyncSassVarArgFn1
187 | AsyncSassVarArgFn2
188 | AsyncSassVarArgFn3
189 | AsyncSassVarArgFn4
190 | AsyncSassVarArgFn5
191 | AsyncSassVarArgFn6;
192
193export type SassFunction = SyncSassFunction | AsyncSassFunction;
194
195export type FunctionDeclarations<FunctionType extends SassFunction = SassFunction> = Record<string, FunctionType>;
196
197export interface Options {
198 file?: string | undefined;
199 data?: string | undefined;
200 importer?: Importer | Importer[] | undefined;
201 functions?: FunctionDeclarations | undefined;
202 includePaths?: string[] | undefined;
203 indentedSyntax?: boolean | undefined;
204 indentType?: string | undefined;
205 indentWidth?: number | undefined;
206 linefeed?: string | undefined;
207 omitSourceMapUrl?: boolean | undefined;
208 outFile?: string | undefined;
209 outputStyle?: "compact" | "compressed" | "expanded" | "nested" | undefined;
210 precision?: number | undefined;
211 sourceComments?: boolean | undefined;
212 sourceMap?: boolean | string | undefined;
213 sourceMapContents?: boolean | undefined;
214 sourceMapEmbed?: boolean | undefined;
215 sourceMapRoot?: string | undefined;
216 [key: string]: any;
217}
218
219export interface SyncOptions extends Options {
220 functions?: FunctionDeclarations<SyncSassFunction> | undefined;
221 importer?: SyncImporter | SyncImporter[] | undefined;
222}
223
224/**
225 * The error object returned to javascript by sass's render methods.
226 *
227 * This is not the same thing as types.Error.
228 */
229export interface SassError extends Error {
230 message: string;
231 line: number;
232 column: number;
233 status: number;
234 file: string;
235}
236
237/**
238 * The result of successfully compiling a Sass file.
239 */
240export interface Result {
241 css: Buffer;
242 map: Buffer;
243 stats: {
244 entry: string;
245 start: number;
246 end: number;
247 duration: number;
248 includedFiles: string[];
249 };
250}
251export type SassRenderCallback = (err: SassError, result: Result) => any;
252
253// Note, most node-sass constructors can be invoked as a function or with a new
254// operator. The exception: the types Null and Boolean for which new is
255// forbidden.
256//
257// Because of this, the new-able object notation is used here, a class does not
258// work for these types.
259export namespace types {
260 /**
261 * Values that are received from Sass as an argument to a javascript function.
262 */
263 export type Value = Null | Number | String | Color | Boolean | List | Map;
264
265 /**
266 * Values that are legal to return to Sass from a javascript function.
267 */
268 export type ReturnValue = Value | Error;
269
270 // *** Sass Null ***
271
272 export interface Null {
273 /**
274 * This property doesn't exist, but its presence forces the typescript
275 * compiler to properly type check this type. Without it, it seems to
276 * allow things that aren't types.Null to match it in case statements and
277 * assignments.
278 */
279 readonly ___NULL___: unique symbol;
280 }
281
282 interface NullConstructor {
283 (): Null;
284 NULL: Null;
285 }
286 export const Null: NullConstructor;
287
288 // *** Sass Number ***
289
290 export interface Number {
291 getValue(): number;
292 setValue(n: number): void;
293 getUnit(): string;
294 setUnit(u: string): void;
295 }
296 interface NumberConstructor {
297 /**
298 * Constructs a new Sass number. Does not require use of the `new` keyword.
299 */
300 new(value: number, unit?: string): Number;
301 /**
302 * Constructs a new Sass number. Can also be used with the `new` keyword.
303 */
304 (value: number, unit?: string): Number;
305 }
306
307 export const Number: NumberConstructor;
308
309 // *** Sass String ***
310
311 export interface String {
312 getValue(): string;
313 setValue(s: string): void;
314 }
315
316 interface StringConstructor {
317 /**
318 * Constructs a new Sass string. Does not require use of the `new` keyword.
319 */
320 new(value: string): String;
321 /**
322 * Constructs a new Sass string. Can also be used with the `new` keyword.
323 */
324 (value: string): String;
325 }
326
327 export const String: StringConstructor;
328
329 // *** Sass Color ***
330
331 export interface Color {
332 /**
333 * Get the red component of the color.
334 * @returns integer between 0 and 255 inclusive;
335 */
336 getR(): number;
337 /**
338 * Set the red component of the color.
339 * @returns integer between 0 and 255 inclusive;
340 */
341 setR(r: number): void;
342 /**
343 * Get the green component of the color.
344 * @returns integer between 0 and 255 inclusive;
345 */
346 getG(): number;
347 /**
348 * Set the green component of the color.
349 * @param g integer between 0 and 255 inclusive;
350 */
351 setG(g: number): void;
352 /**
353 * Get the blue component of the color.
354 * @returns integer between 0 and 255 inclusive;
355 */
356 getB(): number;
357 /**
358 * Set the blue component of the color.
359 * @param b integer between 0 and 255 inclusive;
360 */
361 setB(b: number): void;
362 /**
363 * Get the alpha transparency component of the color.
364 * @returns number between 0 and 1 inclusive;
365 */
366 getA(): number;
367 /**
368 * Set the alpha component of the color.
369 * @param a number between 0 and 1 inclusive;
370 */
371 setA(a: number): void;
372 }
373
374 interface ColorConstructor {
375 /**
376 * Constructs a new Sass color given the RGBA component values. Do not invoke with the `new` keyword.
377 *
378 * @param r integer 0-255 inclusive
379 * @param g integer 0-255 inclusive
380 * @param b integer 0-255 inclusive
381 * @param [a] float 0 - 1 inclusive
382 * @returns a SassColor instance.
383 */
384 new(r: number, g: number, b: number, a?: number): Color;
385
386 /**
387 * Constructs a new Sass color given a 4 byte number. Do not invoke with the `new` keyword.
388 *
389 * If a single number is passed it is assumed to be a number that contains
390 * all the components which are extracted using bitmasks and bitshifting.
391 *
392 * @param hexN A number that is usually written in hexadecimal form. E.g. 0xff0088cc.
393 * @returns a Sass Color instance.
394 * @example
395 * // Comparison with byte array manipulation
396 * let a = new ArrayBuffer(4);
397 * let hexN = 0xCCFF0088; // 0xAARRGGBB
398 * let a32 = new Uint32Array(a); // Uint32Array [ 0 ]
399 * a32[0] = hexN;
400 * let a8 = new Uint8Array(a); // Uint8Array [ 136, 0, 255, 204 ]
401 * let componentBytes = [a8[2], a8[1], a8[0], a8[3] / 255] // [ 136, 0, 255, 0.8 ]
402 * let c = sass.types.Color(hexN);
403 * let components = [c.getR(), c.getG(), c.getR(), c.getA()] // [ 136, 0, 255, 0.8 ]
404 * assert.deepEqual(componentBytes, components); // does not raise.
405 */
406 new(hexN: number): Color;
407
408 /**
409 * Constructs a new Sass color given the RGBA component values. Do not invoke with the `new` keyword.
410 *
411 * @param r integer 0-255 inclusive
412 * @param g integer 0-255 inclusive
413 * @param b integer 0-255 inclusive
414 * @param [a] float 0 - 1 inclusive
415 * @returns a SassColor instance.
416 */
417 (r: number, g: number, b: number, a?: number): Color;
418
419 /**
420 * Constructs a new Sass color given a 4 byte number. Do not invoke with the `new` keyword.
421 *
422 * If a single number is passed it is assumed to be a number that contains
423 * all the components which are extracted using bitmasks and bitshifting.
424 *
425 * @param hexN A number that is usually written in hexadecimal form. E.g. 0xff0088cc.
426 * @returns a Sass Color instance.
427 * @example
428 * // Comparison with byte array manipulation
429 * let a = new ArrayBuffer(4);
430 * let hexN = 0xCCFF0088; // 0xAARRGGBB
431 * let a32 = new Uint32Array(a); // Uint32Array [ 0 ]
432 * a32[0] = hexN;
433 * let a8 = new Uint8Array(a); // Uint8Array [ 136, 0, 255, 204 ]
434 * let componentBytes = [a8[2], a8[1], a8[0], a8[3] / 255] // [ 136, 0, 255, 0.8 ]
435 * let c = sass.types.Color(hexN);
436 * let components = [c.getR(), c.getG(), c.getR(), c.getA()] // [ 136, 0, 255, 0.8 ]
437 * assert.deepEqual(componentBytes, components); // does not raise.
438 */
439 (hexN: number): Color;
440 }
441
442 export const Color: ColorConstructor;
443
444 // *** Sass Boolean ***
445
446 export interface Boolean {
447 getValue(): boolean;
448 }
449
450 interface BooleanConstructor {
451 (bool: boolean): Boolean;
452 TRUE: Boolean;
453 FALSE: Boolean;
454 }
455
456 export const Boolean: BooleanConstructor;
457
458 // *** Sass List ***
459
460 export interface Enumerable {
461 getValue(index: number): Value;
462 setValue(index: number, value: Value): void;
463 getLength(): number;
464 }
465
466 export interface List extends Enumerable {
467 getSeparator(): boolean;
468 setSeparator(isComma: boolean): void;
469 }
470 interface ListConstructor {
471 new(length: number, commaSeparator?: boolean): List;
472 (length: number, commaSeparator?: boolean): List;
473 }
474 export const List: ListConstructor;
475
476 // *** Sass Map ***
477
478 export interface Map extends Enumerable {
479 getKey(index: number): Value;
480 setKey(index: number, key: Value): void;
481 }
482 interface MapConstructor {
483 new(length: number): Map;
484 (length: number): Map;
485 }
486 export const Map: MapConstructor;
487
488 // *** Sass Error ***
489
490 export interface Error {
491 /**
492 * This property doesn't exist, but its presence forces the typescript
493 * compiler to properly type check this type. Without it, it seems to
494 * allow things that aren't types.Error to match it in case statements and
495 * assignments.
496 */
497 readonly ___SASS_ERROR___: unique symbol;
498 // why isn't there a getMessage() method?
499 }
500
501 interface ErrorConstructor {
502 /**
503 * An error return value for async functions.
504 * For synchronous functions, this can be returned or a standard error object can be thrown.
505 */
506 new(message: string): Error;
507 /**
508 * An error return value for async functions.
509 * For synchronous functions, this can be returned or a standard error object can be thrown.
510 */
511 (message: string): Error;
512 }
513 export const Error: ErrorConstructor;
514}
515
516// *** Top level Constants ***
517
518export const NULL: types.Null;
519export const TRUE: types.Boolean;
520export const FALSE: types.Boolean;
521export const info: string;
522export function render(options: Options, callback: SassRenderCallback): void;
523export function renderSync(options: SyncOptions): Result;