UNPKG

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