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 /* eslint-disable @typescript-eslint/ban-types */
143 /* tslint:disable:ban-types */
144 /**
145 * Values that are received from Sass as an argument to a javascript function.
146 */
147 export type Value = Null | Number | String | Color | Boolean | List | Map;
148
149 /**
150 * Values that are legal to return to Sass from a javascript function.
151 */
152 export type ReturnValue = Value | Error;
153
154 // *** Sass Null ***
155
156 export interface Null {
157 /**
158 * This property doesn't exist, but its presence forces the typescript
159 * compiler to properly type check this type. Without it, it seems to
160 * allow things that aren't types.Null to match it in case statements and
161 * assignments.
162 */
163 readonly ___NULL___: unique symbol;
164 }
165
166 interface NullConstructor {
167 (): Null;
168 NULL: Null;
169 }
170 export const Null: NullConstructor;
171
172 // *** Sass Number ***
173
174 export interface Number {
175 getValue(): number;
176 setValue(n: number): void;
177 getUnit(): string;
178 setUnit(u: string): void;
179 }
180 interface NumberConstructor {
181 /**
182 * Constructs a new Sass number. Does not require use of the `new` keyword.
183 */
184 new(value: number, unit?: string): Number;
185 /**
186 * Constructs a new Sass number. Can also be used with the `new` keyword.
187 */
188 (value: number, unit?: string): Number;
189 }
190
191 export const Number: NumberConstructor;
192
193 // *** Sass String ***
194
195 export interface String {
196 getValue(): string;
197 setValue(s: string): void;
198 }
199
200 interface StringConstructor {
201 /**
202 * Constructs a new Sass string. Does not require use of the `new` keyword.
203 */
204 new (value: string): String;
205 /**
206 * Constructs a new Sass string. Can also be used with the `new` keyword.
207 */
208 (value: string): String;
209 }
210
211 export const String: StringConstructor;
212
213 // *** Sass Color ***
214
215 export interface Color {
216 /**
217 * Get the red component of the color.
218 * @returns integer between 0 and 255 inclusive;
219 */
220 getR(): number;
221 /**
222 * Set the red component of the color.
223 * @returns integer between 0 and 255 inclusive;
224 */
225 setR(r: number): void;
226 /**
227 * Get the green component of the color.
228 * @returns integer between 0 and 255 inclusive;
229 */
230 getG(): number;
231 /**
232 * Set the green component of the color.
233 * @param g integer between 0 and 255 inclusive;
234 */
235 setG(g: number): void;
236 /**
237 * Get the blue component of the color.
238 * @returns integer between 0 and 255 inclusive;
239 */
240 getB(): number;
241 /**
242 * Set the blue component of the color.
243 * @param b integer between 0 and 255 inclusive;
244 */
245 setB(b: number): void;
246 /**
247 * Get the alpha transparency component of the color.
248 * @returns number between 0 and 1 inclusive;
249 */
250 getA(): number;
251 /**
252 * Set the alpha component of the color.
253 * @param a number between 0 and 1 inclusive;
254 */
255 setA(a: number): void;
256 }
257
258 interface ColorConstructor {
259 /**
260 * Constructs a new Sass color given the RGBA component values. Do not invoke with the `new` keyword.
261 *
262 * @param r integer 0-255 inclusive
263 * @param g integer 0-255 inclusive
264 * @param b integer 0-255 inclusive
265 * @param [a] float 0 - 1 inclusive
266 * @returns a SassColor instance.
267 */
268 new (r: number, g: number, b: number, a?: number): Color;
269
270 /**
271 * Constructs a new Sass color given a 4 byte number. Do not invoke with the `new` keyword.
272 *
273 * If a single number is passed it is assumed to be a number that contains
274 * all the components which are extracted using bitmasks and bitshifting.
275 *
276 * @param hexN A number that is usually written in hexadecimal form. E.g. 0xff0088cc.
277 * @returns a Sass Color instance.
278 * @example
279 * // Comparison with byte array manipulation
280 * let a = new ArrayBuffer(4);
281 * let hexN = 0xCCFF0088; // 0xAARRGGBB
282 * let a32 = new Uint32Array(a); // Uint32Array [ 0 ]
283 * a32[0] = hexN;
284 * let a8 = new Uint8Array(a); // Uint8Array [ 136, 0, 255, 204 ]
285 * let componentBytes = [a8[2], a8[1], a8[0], a8[3] / 255] // [ 136, 0, 255, 0.8 ]
286 * let c = sass.types.Color(hexN);
287 * let components = [c.getR(), c.getG(), c.getR(), c.getA()] // [ 136, 0, 255, 0.8 ]
288 * assert.deepEqual(componentBytes, components); // does not raise.
289 */
290 new (hexN: number): Color;
291
292 /**
293 * Constructs a new Sass color given the RGBA component values. Do not invoke with the `new` keyword.
294 *
295 * @param r integer 0-255 inclusive
296 * @param g integer 0-255 inclusive
297 * @param b integer 0-255 inclusive
298 * @param [a] float 0 - 1 inclusive
299 * @returns a SassColor instance.
300 */
301 (r: number, g: number, b: number, a?: number): Color;
302
303 /**
304 * Constructs a new Sass color given a 4 byte number. Do not invoke with the `new` keyword.
305 *
306 * If a single number is passed it is assumed to be a number that contains
307 * all the components which are extracted using bitmasks and bitshifting.
308 *
309 * @param hexN A number that is usually written in hexadecimal form. E.g. 0xff0088cc.
310 * @returns a Sass Color instance.
311 * @example
312 * // Comparison with byte array manipulation
313 * let a = new ArrayBuffer(4);
314 * let hexN = 0xCCFF0088; // 0xAARRGGBB
315 * let a32 = new Uint32Array(a); // Uint32Array [ 0 ]
316 * a32[0] = hexN;
317 * let a8 = new Uint8Array(a); // Uint8Array [ 136, 0, 255, 204 ]
318 * let componentBytes = [a8[2], a8[1], a8[0], a8[3] / 255] // [ 136, 0, 255, 0.8 ]
319 * let c = sass.types.Color(hexN);
320 * let components = [c.getR(), c.getG(), c.getR(), c.getA()] // [ 136, 0, 255, 0.8 ]
321 * assert.deepEqual(componentBytes, components); // does not raise.
322 */
323 (hexN: number): Color;
324 }
325
326 export const Color: ColorConstructor;
327
328 // *** Sass Boolean ***
329
330 export interface Boolean {
331 getValue(): boolean;
332 }
333
334 interface BooleanConstructor {
335 (bool: boolean): Boolean;
336 TRUE: Boolean;
337 FALSE: Boolean;
338 }
339
340 export const Boolean: BooleanConstructor;
341
342 // *** Sass List ***
343
344 export interface Enumerable {
345 getValue(index: number): Value;
346 setValue(index: number, value: Value): void;
347 getLength(): number;
348 }
349
350 export interface List extends Enumerable {
351 getSeparator(): boolean;
352 setSeparator(isComma: boolean): void;
353 }
354 interface ListConstructor {
355 new (length: number, commaSeparator?: boolean): List;
356 (length: number, commaSeparator?: boolean): List;
357 }
358 export const List: ListConstructor;
359
360 // *** Sass Map ***
361
362 export interface Map extends Enumerable {
363 getKey(index: number): Value;
364 setKey(index: number, key: Value): void;
365 }
366 interface MapConstructor {
367 new (length: number): Map;
368 (length: number): Map;
369 }
370 export const Map: MapConstructor;
371
372 // *** Sass Error ***
373
374 export interface Error {
375 /**
376 * This property doesn't exist, but its presence forces the typescript
377 * compiler to properly type check this type. Without it, it seems to
378 * allow things that aren't types.Error to match it in case statements and
379 * assignments.
380 */
381 readonly ___SASS_ERROR___: unique symbol;
382 // why isn't there a getMessage() method?
383 }
384
385 interface ErrorConstructor {
386 /**
387 * An error return value for async functions.
388 * For synchronous functions, this can be returned or a standard error object can be thrown.
389 */
390 new (message: string): Error;
391 /**
392 * An error return value for async functions.
393 * For synchronous functions, this can be returned or a standard error object can be thrown.
394 */
395 (message: string): Error;
396 }
397 export const Error: ErrorConstructor;
398
399 /* eslint-enable @typescript-eslint/ban-types */
400 /* tslint:enable:ban-types */
401}
402
403// *** Top level Constants ***
404
405export const NULL: types.Null;
406export const TRUE: types.Boolean;
407export const FALSE: types.Boolean;
408export const info: string;
409export function render(options: Options, callback: SassRenderCallback): void;
410export function renderSync(options: SyncOptions): Result;