UNPKG

16.4 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 /* tslint:disable:ban-types */
261 /**
262 * Values that are received from Sass as an argument to a javascript function.
263 */
264 export type Value = Null | Number | String | Color | Boolean | List | Map;
265
266 /**
267 * Values that are legal to return to Sass from a javascript function.
268 */
269 export type ReturnValue = Value | Error;
270
271 // *** Sass Null ***
272
273 export interface Null {
274 /**
275 * This property doesn't exist, but its presence forces the typescript
276 * compiler to properly type check this type. Without it, it seems to
277 * allow things that aren't types.Null to match it in case statements and
278 * assignments.
279 */
280 readonly ___NULL___: unique symbol;
281 }
282
283 interface NullConstructor {
284 (): Null;
285 NULL: Null;
286 }
287 export const Null: NullConstructor;
288
289 // *** Sass Number ***
290
291 export interface Number {
292 getValue(): number;
293 setValue(n: number): void;
294 getUnit(): string;
295 setUnit(u: string): void;
296 }
297 interface NumberConstructor {
298 /**
299 * Constructs a new Sass number. Does not require use of the `new` keyword.
300 */
301 new(value: number, unit?: string): Number;
302 /**
303 * Constructs a new Sass number. Can also be used with the `new` keyword.
304 */
305 (value: number, unit?: string): Number;
306 }
307
308 export const Number: NumberConstructor;
309
310 // *** Sass String ***
311
312 export interface String {
313 getValue(): string;
314 setValue(s: string): void;
315 }
316
317 interface StringConstructor {
318 /**
319 * Constructs a new Sass string. Does not require use of the `new` keyword.
320 */
321 new(value: string): String;
322 /**
323 * Constructs a new Sass string. Can also be used with the `new` keyword.
324 */
325 (value: string): String;
326 }
327
328 export const String: StringConstructor;
329
330 // *** Sass Color ***
331
332 export interface Color {
333 /**
334 * Get the red component of the color.
335 * @returns integer between 0 and 255 inclusive;
336 */
337 getR(): number;
338 /**
339 * Set the red component of the color.
340 * @returns integer between 0 and 255 inclusive;
341 */
342 setR(r: number): void;
343 /**
344 * Get the green component of the color.
345 * @returns integer between 0 and 255 inclusive;
346 */
347 getG(): number;
348 /**
349 * Set the green component of the color.
350 * @param g integer between 0 and 255 inclusive;
351 */
352 setG(g: number): void;
353 /**
354 * Get the blue component of the color.
355 * @returns integer between 0 and 255 inclusive;
356 */
357 getB(): number;
358 /**
359 * Set the blue component of the color.
360 * @param b integer between 0 and 255 inclusive;
361 */
362 setB(b: number): void;
363 /**
364 * Get the alpha transparency component of the color.
365 * @returns number between 0 and 1 inclusive;
366 */
367 getA(): number;
368 /**
369 * Set the alpha component of the color.
370 * @param a number between 0 and 1 inclusive;
371 */
372 setA(a: number): void;
373 }
374
375 interface ColorConstructor {
376 /**
377 * Constructs a new Sass color given the RGBA component values. Do not invoke with the `new` keyword.
378 *
379 * @param r integer 0-255 inclusive
380 * @param g integer 0-255 inclusive
381 * @param b integer 0-255 inclusive
382 * @param [a] float 0 - 1 inclusive
383 * @returns a SassColor instance.
384 */
385 new(r: number, g: number, b: number, a?: number): Color;
386
387 /**
388 * Constructs a new Sass color given a 4 byte number. Do not invoke with the `new` keyword.
389 *
390 * If a single number is passed it is assumed to be a number that contains
391 * all the components which are extracted using bitmasks and bitshifting.
392 *
393 * @param hexN A number that is usually written in hexadecimal form. E.g. 0xff0088cc.
394 * @returns a Sass Color instance.
395 * @example
396 * // Comparison with byte array manipulation
397 * let a = new ArrayBuffer(4);
398 * let hexN = 0xCCFF0088; // 0xAARRGGBB
399 * let a32 = new Uint32Array(a); // Uint32Array [ 0 ]
400 * a32[0] = hexN;
401 * let a8 = new Uint8Array(a); // Uint8Array [ 136, 0, 255, 204 ]
402 * let componentBytes = [a8[2], a8[1], a8[0], a8[3] / 255] // [ 136, 0, 255, 0.8 ]
403 * let c = sass.types.Color(hexN);
404 * let components = [c.getR(), c.getG(), c.getR(), c.getA()] // [ 136, 0, 255, 0.8 ]
405 * assert.deepEqual(componentBytes, components); // does not raise.
406 */
407 new(hexN: number): Color;
408
409 /**
410 * Constructs a new Sass color given the RGBA component values. Do not invoke with the `new` keyword.
411 *
412 * @param r integer 0-255 inclusive
413 * @param g integer 0-255 inclusive
414 * @param b integer 0-255 inclusive
415 * @param [a] float 0 - 1 inclusive
416 * @returns a SassColor instance.
417 */
418 (r: number, g: number, b: number, a?: number): Color;
419
420 /**
421 * Constructs a new Sass color given a 4 byte number. Do not invoke with the `new` keyword.
422 *
423 * If a single number is passed it is assumed to be a number that contains
424 * all the components which are extracted using bitmasks and bitshifting.
425 *
426 * @param hexN A number that is usually written in hexadecimal form. E.g. 0xff0088cc.
427 * @returns a Sass Color instance.
428 * @example
429 * // Comparison with byte array manipulation
430 * let a = new ArrayBuffer(4);
431 * let hexN = 0xCCFF0088; // 0xAARRGGBB
432 * let a32 = new Uint32Array(a); // Uint32Array [ 0 ]
433 * a32[0] = hexN;
434 * let a8 = new Uint8Array(a); // Uint8Array [ 136, 0, 255, 204 ]
435 * let componentBytes = [a8[2], a8[1], a8[0], a8[3] / 255] // [ 136, 0, 255, 0.8 ]
436 * let c = sass.types.Color(hexN);
437 * let components = [c.getR(), c.getG(), c.getR(), c.getA()] // [ 136, 0, 255, 0.8 ]
438 * assert.deepEqual(componentBytes, components); // does not raise.
439 */
440 (hexN: number): Color;
441 }
442
443 export const Color: ColorConstructor;
444
445 // *** Sass Boolean ***
446
447 export interface Boolean {
448 getValue(): boolean;
449 }
450
451 interface BooleanConstructor {
452 (bool: boolean): Boolean;
453 TRUE: Boolean;
454 FALSE: Boolean;
455 }
456
457 export const Boolean: BooleanConstructor;
458
459 // *** Sass List ***
460
461 export interface Enumerable {
462 getValue(index: number): Value;
463 setValue(index: number, value: Value): void;
464 getLength(): number;
465 }
466
467 export interface List extends Enumerable {
468 getSeparator(): boolean;
469 setSeparator(isComma: boolean): void;
470 }
471 interface ListConstructor {
472 new(length: number, commaSeparator?: boolean): List;
473 (length: number, commaSeparator?: boolean): List;
474 }
475 export const List: ListConstructor;
476
477 // *** Sass Map ***
478
479 export interface Map extends Enumerable {
480 getKey(index: number): Value;
481 setKey(index: number, key: Value): void;
482 }
483 interface MapConstructor {
484 new(length: number): Map;
485 (length: number): Map;
486 }
487 export const Map: MapConstructor;
488
489 // *** Sass Error ***
490
491 export interface Error {
492 /**
493 * This property doesn't exist, but its presence forces the typescript
494 * compiler to properly type check this type. Without it, it seems to
495 * allow things that aren't types.Error to match it in case statements and
496 * assignments.
497 */
498 readonly ___SASS_ERROR___: unique symbol;
499 // why isn't there a getMessage() method?
500 }
501
502 interface ErrorConstructor {
503 /**
504 * An error return value for async functions.
505 * For synchronous functions, this can be returned or a standard error object can be thrown.
506 */
507 new(message: string): Error;
508 /**
509 * An error return value for async functions.
510 * For synchronous functions, this can be returned or a standard error object can be thrown.
511 */
512 (message: string): Error;
513 }
514 export const Error: ErrorConstructor;
515
516 /* eslint-enable @typescript-eslint/ban-types */
517 /* tslint:enable:ban-types */
518}
519
520// *** Top level Constants ***
521
522export const NULL: types.Null;
523export const TRUE: types.Boolean;
524export const FALSE: types.Boolean;
525export const info: string;
526export function render(options: Options, callback: SassRenderCallback): void;
527export function renderSync(options: SyncOptions): Result;