Skip to content

Commit 5651392

Browse files
authored
Don't use data object for Eta configuration (#214)
1 parent 811b6be commit 5651392

File tree

3 files changed

+18
-92
lines changed

3 files changed

+18
-92
lines changed

deno_dist/file-handlers.ts

+7-35
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import EtaErr from "./err.ts";
44
import compile from "./compile.ts";
55
import { getConfig } from "./config.ts";
66
import { getPath, readFile } from "./file-utils.ts";
7-
import { copyProps } from "./utils.ts";
87
import { promiseImpl } from "./polyfills.ts";
98

109
/* TYPES */
@@ -19,10 +18,6 @@ import type { TemplateFunction } from "./compile.ts";
1918
export type CallbackFn = (err: Error | null, str?: string) => void;
2019

2120
interface DataObj {
22-
/** Express.js settings may be stored here */
23-
settings?: {
24-
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
25-
};
2621
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
2722
}
2823

@@ -173,8 +168,7 @@ function includeFile(
173168
*
174169
* This can take two different function signatures:
175170
*
176-
* - `renderFile(filename, dataAndConfig, [cb])`
177-
* - Eta will merge `dataAndConfig` into `eta.config`
171+
* - `renderFile(filename, data, [cb])`
178172
* - `renderFile(filename, data, [config], [cb])`
179173
*
180174
* Note that renderFile does not immediately return the rendered result. If you pass in a callback function, it will be called with `(err, res)`. Otherwise, `renderFile` will return a `Promise` that resolves to the render result.
@@ -189,7 +183,6 @@ function includeFile(
189183
*
190184
* let rendered = await eta.renderFile("./template.eta", data, {cache: true})
191185
*
192-
* let rendered = await eta.renderFile("./template", {...data, cache: true})
193186
* ```
194187
*/
195188

@@ -224,14 +217,13 @@ function renderFile(
224217
/*
225218
Here we have some function overloading.
226219
Essentially, the first 2 arguments to renderFile should always be the filename and data
227-
However, with Express, configuration options will be passed along with the data.
228-
Thus, Express will call renderFile with (filename, dataAndOptions, cb)
229-
And we want to also make (filename, data, options, cb) available
220+
Express will call renderFile with (filename, data, cb)
221+
We also want to make (filename, data, options, cb) available
230222
*/
231223

232224
let renderConfig: EtaConfigWithFilename;
233225
let callback: CallbackFn | undefined;
234-
data = data || {}; // If data is undefined, we don't want accessing data.settings to error
226+
data = data || {};
235227

236228
// First, assign our callback function to `callback`
237229
// We can leave it undefined if neither parameter is a function;
@@ -250,26 +242,8 @@ function renderFile(
250242
(config as PartialConfig) || {},
251243
) as EtaConfigWithFilename;
252244
} else {
253-
// Otherwise, get the config from the data object
254-
// And then grab some config options from data.settings
255-
// Which is where Express sometimes stores them
256-
renderConfig = getConfig(data as PartialConfig) as EtaConfigWithFilename;
257-
if (data.settings) {
258-
// Pull a few things from known locations
259-
if (data.settings.views) {
260-
renderConfig.views = data.settings.views;
261-
}
262-
if (data.settings["view cache"]) {
263-
renderConfig.cache = true;
264-
}
265-
// Undocumented after Express 2, but still usable, esp. for
266-
// items that are unsafe to be passed along with data, like `root`
267-
const viewOpts = data.settings["view options"];
268-
269-
if (viewOpts) {
270-
copyProps(renderConfig, viewOpts);
271-
}
272-
}
245+
// Otherwise, get the default config
246+
renderConfig = getConfig({}) as EtaConfigWithFilename;
273247
}
274248

275249
// Set the filename option on the template
@@ -286,8 +260,7 @@ function renderFile(
286260
*
287261
* This can take two different function signatures:
288262
*
289-
* - `renderFile(filename, dataAndConfig, [cb])`
290-
* - Eta will merge `dataAndConfig` into `eta.config`
263+
* - `renderFile(filename, data, [cb])`
291264
* - `renderFile(filename, data, [config], [cb])`
292265
*
293266
* Note that renderFile does not immediately return the rendered result. If you pass in a callback function, it will be called with `(err, res)`. Otherwise, `renderFile` will return a `Promise` that resolves to the render result.
@@ -302,7 +275,6 @@ function renderFile(
302275
*
303276
* let rendered = await eta.renderFile("./template.eta", data, {cache: true})
304277
*
305-
* let rendered = await eta.renderFile("./template", {...data, cache: true})
306278
* ```
307279
*/
308280

src/file-handlers.ts

+7-35
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import EtaErr from "./err.js";
44
import compile from "./compile.js";
55
import { getConfig } from "./config.js";
66
import { getPath, readFile } from "./file-utils.js";
7-
import { copyProps } from "./utils.js";
87
import { promiseImpl } from "./polyfills.js";
98

109
/* TYPES */
@@ -15,10 +14,6 @@ import type { TemplateFunction } from "./compile.js";
1514
export type CallbackFn = (err: Error | null, str?: string) => void;
1615

1716
interface DataObj {
18-
/** Express.js settings may be stored here */
19-
settings?: {
20-
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
21-
};
2217
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
2318
}
2419

@@ -150,8 +145,7 @@ function includeFile(path: string, options: EtaConfig): [TemplateFunction, EtaCo
150145
*
151146
* This can take two different function signatures:
152147
*
153-
* - `renderFile(filename, dataAndConfig, [cb])`
154-
* - Eta will merge `dataAndConfig` into `eta.config`
148+
* - `renderFile(filename, data, [cb])`
155149
* - `renderFile(filename, data, [config], [cb])`
156150
*
157151
* Note that renderFile does not immediately return the rendered result. If you pass in a callback function, it will be called with `(err, res)`. Otherwise, `renderFile` will return a `Promise` that resolves to the render result.
@@ -166,7 +160,6 @@ function includeFile(path: string, options: EtaConfig): [TemplateFunction, EtaCo
166160
*
167161
* let rendered = await eta.renderFile("./template.eta", data, {cache: true})
168162
*
169-
* let rendered = await eta.renderFile("./template", {...data, cache: true})
170163
* ```
171164
*/
172165

@@ -192,14 +185,13 @@ function renderFile(
192185
/*
193186
Here we have some function overloading.
194187
Essentially, the first 2 arguments to renderFile should always be the filename and data
195-
However, with Express, configuration options will be passed along with the data.
196-
Thus, Express will call renderFile with (filename, dataAndOptions, cb)
197-
And we want to also make (filename, data, options, cb) available
188+
Express will call renderFile with (filename, data, cb)
189+
We also want to make (filename, data, options, cb) available
198190
*/
199191

200192
let renderConfig: EtaConfigWithFilename;
201193
let callback: CallbackFn | undefined;
202-
data = data || {}; // If data is undefined, we don't want accessing data.settings to error
194+
data = data || {};
203195

204196
// First, assign our callback function to `callback`
205197
// We can leave it undefined if neither parameter is a function;
@@ -216,26 +208,8 @@ function renderFile(
216208
if (typeof config === "object") {
217209
renderConfig = getConfig((config as PartialConfig) || {}) as EtaConfigWithFilename;
218210
} else {
219-
// Otherwise, get the config from the data object
220-
// And then grab some config options from data.settings
221-
// Which is where Express sometimes stores them
222-
renderConfig = getConfig(data as PartialConfig) as EtaConfigWithFilename;
223-
if (data.settings) {
224-
// Pull a few things from known locations
225-
if (data.settings.views) {
226-
renderConfig.views = data.settings.views;
227-
}
228-
if (data.settings["view cache"]) {
229-
renderConfig.cache = true;
230-
}
231-
// Undocumented after Express 2, but still usable, esp. for
232-
// items that are unsafe to be passed along with data, like `root`
233-
const viewOpts = data.settings["view options"];
234-
235-
if (viewOpts) {
236-
copyProps(renderConfig, viewOpts);
237-
}
238-
}
211+
// Otherwise, get the default config
212+
renderConfig = getConfig({}) as EtaConfigWithFilename;
239213
}
240214

241215
// Set the filename option on the template
@@ -252,8 +226,7 @@ function renderFile(
252226
*
253227
* This can take two different function signatures:
254228
*
255-
* - `renderFile(filename, dataAndConfig, [cb])`
256-
* - Eta will merge `dataAndConfig` into `eta.config`
229+
* - `renderFile(filename, data, [cb])`
257230
* - `renderFile(filename, data, [config], [cb])`
258231
*
259232
* Note that renderFile does not immediately return the rendered result. If you pass in a callback function, it will be called with `(err, res)`. Otherwise, `renderFile` will return a `Promise` that resolves to the render result.
@@ -268,7 +241,6 @@ function renderFile(
268241
*
269242
* let rendered = await eta.renderFile("./template.eta", data, {cache: true})
270243
*
271-
* let rendered = await eta.renderFile("./template", {...data, cache: true})
272244
* ```
273245
*/
274246

test/file-handlers.spec.ts

+4-22
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,10 @@ describe("Simple renderFile tests", () => {
6767
templates.define(fakeFilePath, compile("This template does not exist"));
6868

6969
// renderFile should just look straight in the cache for the template
70-
renderFile(fakeFilePath, { cache: true }, function (_err: Error | null, res?: string) {
70+
renderFile(fakeFilePath, {}, { cache: true }, function (_err: Error | null, res?: string) {
7171
expect(res).toEqual("This template does not exist");
7272
});
7373
});
74-
75-
it("parses a simple template w/ settings from Express", async () => {
76-
renderFile(
77-
filePath,
78-
{
79-
name: "<p>Ben</p>",
80-
cache: true,
81-
settings: {
82-
views: [path.join(__dirname, "templates"), path.join(__dirname, "othertemplates")],
83-
"view cache": true,
84-
"view options": { autoEscape: false },
85-
},
86-
},
87-
function (_err: Error | null, res?: string) {
88-
expect(res).toEqual("Hi <p>Ben</p>");
89-
}
90-
);
91-
});
9274
});
9375

9476
describe("File location tests", () => {
@@ -105,9 +87,9 @@ describe("File location tests", () => {
10587

10688
describe("renderFile error tests", () => {
10789
it("render file with callback works on error", (done) => {
108-
function cb(err: Error, _res?: string) {
90+
function cb(err: Error | null, _res?: string) {
10991
expect(err).toBeTruthy();
110-
expect(err.message).toMatch(
92+
expect(err?.message).toMatch(
11193
buildRegEx(`
11294
var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
11395
function layout(p,d){__l=p;__lP=d}
@@ -120,7 +102,7 @@ if(cb){cb(null,tR)} return tR
120102
done();
121103
}
122104

123-
renderFile(errFilePath, { name: "Ada Lovelace", async: true }, cb);
105+
renderFile(errFilePath, { name: "Ada Lovelace" }, { async: true }, cb);
124106
});
125107

126108
test("throws with bad inner JS syntax using Promises", async () => {

0 commit comments

Comments
 (0)