Skip to content

Commit

Permalink
chore: rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed May 27, 2023
1 parent f722c32 commit 53beb48
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
6 changes: 2 additions & 4 deletions deno_dist/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export function compile(
str: string,
options?: Partial<Options>,
): TemplateFunction {
const Eta = this;

const config: EtaConfig = this.config;

/* ASYNC HANDLING (modified from mde/ejs) */
Expand All @@ -39,7 +37,7 @@ export function compile(
return new ctor(
config.varName,
"options",
this.compileToString.call(Eta, str, options),
this.compileToString.call(this, str, options),
) as TemplateFunction; // eslint-disable-line no-new-func
} catch (e) {
if (e instanceof SyntaxError) {
Expand All @@ -49,7 +47,7 @@ export function compile(
"\n" +
Array(e.message.length + 1).join("=") +
"\n" +
this.compileToString.call(Eta, str, options) +
this.compileToString.call(this, str, options) +
"\n", // This will put an extra newline before the callstack for extra readability
);
} else {
Expand Down
9 changes: 5 additions & 4 deletions deno_dist/file-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function readFile(this: EtaCore, path: string): string {

try {
res = fs.readFileSync(path, "utf8");
// eslint-disable-line @typescript-eslint/no-explicit-any
} catch (err: any) {
if (err?.code === "ENOENT") {
throw new EtaError(`Could not find template: ${path}`);
Expand All @@ -34,13 +35,13 @@ export function resolvePath(

template += path.extname(template) ? "" : ".eta";

let views = this.config.views;
const views = this.config.views;

if (!views) {
throw new EtaError("Views directory is not defined");
}

let baseFilePath = options && options.filepath;
const baseFilePath = options && options.filepath;

// if the file was included from another template
if (baseFilePath) {
Expand All @@ -49,7 +50,7 @@ export function resolvePath(
return this.config.filepathCache[baseFilePath];
}

let absolutePathTest = absolutePathRegExp.exec(template);
const absolutePathTest = absolutePathRegExp.exec(template);

if (absolutePathTest && absolutePathTest.length) {
const formattedPath = template.replace(/^\/*/, "");
Expand Down Expand Up @@ -78,4 +79,4 @@ function dirIsChild(parent: string, dir: string) {
return relative && !relative.startsWith("..") && !path.isAbsolute(relative);
}

let absolutePathRegExp = /^\\|^\//;
const absolutePathRegExp = /^\\|^\//;
32 changes: 14 additions & 18 deletions deno_dist/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ function handleCache(
template: string,
options: Partial<Options>,
): TemplateFunction {
let templateStore = options && options.async
const templateStore = options && options.async
? this.templatesAsync
: this.templatesSync;

if (this.resolvePath && this.readFile && !template.startsWith("@")) {
let templatePath = options.filepath as string;
const templatePath = options.filepath as string;

let cachedTemplate = templateStore.get(templatePath);
const cachedTemplate = templateStore.get(templatePath);

if (this.config.cache && cachedTemplate) {
return cachedTemplate;
} else {
let templateString = this.readFile(templatePath);
const templateString = this.readFile(templatePath);

let templateFn = this.compile(templateString, options);
const templateFn = this.compile(templateString, options);

if (this.config.cache) templateStore.define(templatePath, templateFn);

return templateFn;
}
} else {
let cachedTemplate = templateStore.get(template);
const cachedTemplate = templateStore.get(template);

if (cachedTemplate) {
return cachedTemplate;
Expand All @@ -48,22 +48,20 @@ export function render(
data: object,
meta?: { filepath: string },
): string {
let Eta = this;

let templateFn: TemplateFunction;
let options = { ...meta, async: false };
const options = { ...meta, async: false };

if (typeof template === "string") {
if (this.resolvePath && this.readFile && !template.startsWith("@")) {
options.filepath = this.resolvePath(template, options);
}

templateFn = handleCache.call(Eta, template, options);
templateFn = handleCache.call(this, template, options);
} else {
templateFn = template;
}

let res = templateFn.call(Eta, data, options);
const res = templateFn.call(this, data, options);

return res;
}
Expand All @@ -74,22 +72,20 @@ export function renderAsync(
data: object,
meta?: { filepath: string },
): Promise<string> {
let Eta = this;

let templateFn: TemplateFunction;
let options = { ...meta, async: true };
const options = { ...meta, async: true };

if (typeof template === "string") {
if (this.resolvePath && this.readFile && !template.startsWith("@")) {
options.filepath = this.resolvePath(template, options);
}

templateFn = handleCache.call(Eta, template, options);
templateFn = handleCache.call(this, template, options);
} else {
templateFn = template;
}

let res = templateFn.call(Eta, data, options);
const res = templateFn.call(this, data, options);

// Return a promise
return new Promise(function (resolve: Function, reject: Function) {
Expand All @@ -106,7 +102,7 @@ export function renderString(
template: string,
data: object,
): string {
let templateFn = this.compile(template, { async: false });
const templateFn = this.compile(template, { async: false });

return render.call(this, templateFn, data);
}
Expand All @@ -116,7 +112,7 @@ export function renderStringAsync(
template: string,
data: object,
): Promise<string> {
let templateFn = this.compile(template, { async: true });
const templateFn = this.compile(template, { async: true });

return renderAsync.call(this, templateFn, data);
}
3 changes: 1 addition & 2 deletions deno_dist/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ function replaceChar(s: string): string {
* @returns XML-escaped string
*/

export function XMLEscape(str: any): string {
// eslint-disable-line @typescript-eslint/no-explicit-any
export function XMLEscape(str: unknown): string {
// To deal with XSS. Based on Escape implementations of Mustache.JS and Marko, then customized.
const newStr = String(str);
if (/[&<>"']/.test(newStr)) {
Expand Down

0 comments on commit 53beb48

Please sign in to comment.