This repository was archived by the owner on Mar 14, 2026. It is now read-only.
forked from mde/ejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test-d.ts
More file actions
229 lines (184 loc) · 8.23 KB
/
index.test-d.ts
File metadata and controls
229 lines (184 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { expectType, expectError, expectAssignable } from "tsd";
import * as ejs from "./index";
// =============================================================================
// Module exports
// =============================================================================
expectType<string>(ejs.VERSION);
expectType<"ejs">(ejs.name);
// =============================================================================
// resolveInclude
// =============================================================================
expectType<string>(ejs.resolveInclude("partial", "/views/index.ejs"));
expectType<string>(ejs.resolveInclude("partial", "/views", true));
// =============================================================================
// compile
// =============================================================================
// Basic compile returns TemplateFunction
const templateFn = ejs.compile("<%= foo %>");
expectType<ejs.TemplateFunction>(templateFn);
expectType<string>(templateFn({ foo: "bar" }));
// Async compile returns AsyncTemplateFunction
const asyncTemplateFn = ejs.compile("<%= foo %>", { async: true });
expectType<ejs.AsyncTemplateFunction>(asyncTemplateFn);
expectType<Promise<string>>(asyncTemplateFn({ foo: "bar" }));
// Client compile returns ClientFunction
const clientFn = ejs.compile("<%= foo %>", { client: true });
expectType<ejs.ClientFunction>(clientFn);
expectType<string>(clientFn({ foo: "bar" }));
// Async client compile returns AsyncClientFunction
const asyncClientFn = ejs.compile("<%= foo %>", { async: true, client: true });
expectType<ejs.AsyncClientFunction>(asyncClientFn);
expectType<Promise<string>>(asyncClientFn({ foo: "bar" }));
// =============================================================================
// render
// =============================================================================
// Basic render returns string
expectType<string>(ejs.render("<%= foo %>", { foo: "bar" }));
// Async render returns Promise<string>
expectType<Promise<string>>(
ejs.render("<%= foo %>", { foo: "bar" }, { async: true })
);
// Explicit async: false returns string
expectType<string>(ejs.render("<%= foo %>", { foo: "bar" }, { async: false }));
// =============================================================================
// renderFile
// =============================================================================
// With callback
ejs.renderFile("/path/to/file.ejs", (err, str) => {
expectType<Error | null>(err);
expectType<string>(str);
});
ejs.renderFile("/path/to/file.ejs", { foo: "bar" }, (err, str) => {
expectType<Error | null>(err);
expectType<string>(str);
});
ejs.renderFile("/path/to/file.ejs", { foo: "bar" }, {}, (err, str) => {
expectType<Error | null>(err);
expectType<string>(str);
});
// Without callback returns Promise
expectType<Promise<string>>(ejs.renderFile("/path/to/file.ejs"));
expectType<Promise<string>>(ejs.renderFile("/path/to/file.ejs", { foo: "bar" }));
expectType<Promise<string>>(
ejs.renderFile("/path/to/file.ejs", { foo: "bar" }, {})
);
// =============================================================================
// clearCache
// =============================================================================
expectType<void>(ejs.clearCache());
// =============================================================================
// cache
// =============================================================================
expectAssignable<ejs.Cache>(ejs.cache);
ejs.cache.set("key", templateFn);
expectType<ejs.TemplateFunction | undefined>(ejs.cache.get("key"));
ejs.cache.reset();
// =============================================================================
// fileLoader
// =============================================================================
expectAssignable<ejs.fileLoader>(ejs.fileLoader);
const customLoader: ejs.fileLoader = (path) => `loaded: ${path}`;
// Note: Assignment works at runtime (CommonJS) but not via ES module namespace
expectAssignable<ejs.fileLoader>(customLoader);
// =============================================================================
// Module-level settings
// =============================================================================
expectType<string>(ejs.localsName);
expectType<string>(ejs.openDelimiter);
expectType<string>(ejs.closeDelimiter);
expectType<string | undefined>(ejs.delimiter);
expectType<PromiseConstructorLike | undefined>(ejs.promiseImpl);
// =============================================================================
// escapeXML
// =============================================================================
expectType<string>(ejs.escapeXML("<div>"));
expectType<string>(ejs.escapeXML(undefined));
expectType<string>(ejs.escapeXML(null));
// =============================================================================
// Template class
// =============================================================================
const template = new ejs.Template("<%= foo %>");
expectType<string>(template.templateText);
expectType<string>(template.source);
expectType<
| ejs.TemplateFunction
| ejs.AsyncTemplateFunction
| ejs.ClientFunction
| ejs.AsyncClientFunction
>(template.compile());
// Template modes enum - verify enum values are assignable to modes type
const evalMode: ejs.Template.modes = ejs.Template.modes.EVAL;
const escapedMode: ejs.Template.modes = ejs.Template.modes.ESCAPED;
const rawMode: ejs.Template.modes = ejs.Template.modes.RAW;
const commentMode: ejs.Template.modes = ejs.Template.modes.COMMENT;
const literalMode: ejs.Template.modes = ejs.Template.modes.LITERAL;
// Verify modes can be used as strings
expectAssignable<string>(ejs.Template.modes.EVAL);
expectAssignable<string>(ejs.Template.modes.ESCAPED);
expectAssignable<string>(ejs.Template.modes.RAW);
expectAssignable<string>(ejs.Template.modes.COMMENT);
expectAssignable<string>(ejs.Template.modes.LITERAL);
// =============================================================================
// Options
// =============================================================================
const options: ejs.Options = {
debug: false,
compileDebug: true,
_with: true,
strict: false,
destructuredLocals: ["foo", "bar"],
rmWhitespace: false,
client: false,
escape: (markup) => String(markup),
escapeFunction: (markup) => String(markup),
filename: "/path/to/file.ejs",
root: "/views",
openDelimiter: "<",
closeDelimiter: ">",
delimiter: "%",
cache: false,
context: {},
async: false,
beautify: true,
localsName: "locals",
outputFunctionName: "echo",
views: ["/views"],
includer: (originalPath, parsedPath) => ({ filename: parsedPath }),
legacyInclude: true,
};
expectAssignable<ejs.Options>(options);
// Root can be array
const optionsWithArrayRoot: ejs.Options = {
root: ["/views", "/templates"],
};
expectAssignable<ejs.Options>(optionsWithArrayRoot);
// =============================================================================
// Callback types
// =============================================================================
const escapeCallback: ejs.EscapeCallback = (markup) => String(markup ?? "");
expectType<string>(escapeCallback("<div>"));
const includeCallback: ejs.IncludeCallback = (path, data) => "included content";
expectType<string>(includeCallback("partial", { foo: "bar" }));
const includerCallback: ejs.IncluderCallback = (originalPath, parsedPath) => ({
filename: parsedPath,
});
expectAssignable<ejs.IncluderResult>(includerCallback("partial", "/full/path"));
// IncluderResult variants
const includerResultFilename: ejs.IncluderResult = { filename: "/path" };
const includerResultTemplate: ejs.IncluderResult = { template: "<%= foo %>" };
expectAssignable<ejs.IncluderResult>(includerResultFilename);
expectAssignable<ejs.IncluderResult>(includerResultTemplate);
// =============================================================================
// __express (Express.js support)
// =============================================================================
// __express is an alias for renderFile
expectType<typeof ejs.renderFile>(ejs.__express);
// =============================================================================
// Data type
// =============================================================================
const data: ejs.Data = {
foo: "bar",
nested: { baz: 123 },
array: [1, 2, 3],
};
expectAssignable<ejs.Data>(data);