Skip to content

Commit 0aa65d7

Browse files
committed
chore: deno fmt
1 parent f468e20 commit 0aa65d7

9 files changed

+113
-52
lines changed

src/compile-string.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import type { Eta } from "./core.ts";
1010
* Compiles a template string to a function string. Most often users just use `compile()`, which calls `compileToString` and creates a new function using the result
1111
*/
1212

13-
export function compileToString(this: Eta, str: string, options?: Partial<Options>): string {
13+
export function compileToString(
14+
this: Eta,
15+
str: string,
16+
options?: Partial<Options>,
17+
): string {
1418
const config = this.config;
1519
const isAsync = options && options.async;
1620

@@ -34,13 +38,15 @@ let __eta = {res: "", e: this.config.escapeFunction, f: this.config.filterFuncti
3438
function layout(path, data) {
3539
__eta.layout = path;
3640
__eta.layoutData = data;
37-
}${config.debug ? "try {" : ""}${config.useWith ? "with(" + config.varName + "||{}){" : ""}
41+
}${config.debug ? "try {" : ""}${
42+
config.useWith ? "with(" + config.varName + "||{}){" : ""
43+
}
3844
3945
${compileBody.call(this, buffer)}
4046
if (__eta.layout) {
41-
__eta.res = ${isAsync ? "await includeAsync" : "include"} (__eta.layout, {...${
42-
config.varName
43-
}, body: __eta.res, ...__eta.layoutData});
47+
__eta.res = ${
48+
isAsync ? "await includeAsync" : "include"
49+
} (__eta.layout, {...${config.varName}, body: __eta.res, ...__eta.layoutData});
4450
}
4551
${config.useWith ? "}" : ""}${
4652
config.debug

src/compile.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { EtaParseError } from "./err.ts";
44
import type { Eta } from "./core.ts";
55
import type { EtaConfig, Options } from "./config.ts";
66

7-
export type TemplateFunction = (this: Eta, data?: object, options?: Partial<Options>) => string;
7+
export type TemplateFunction = (
8+
this: Eta,
9+
data?: object,
10+
options?: Partial<Options>,
11+
) => string;
812
/* END TYPES */
913

1014
/* istanbul ignore next */
@@ -17,19 +21,25 @@ const AsyncFunction = async function () {}.constructor; // eslint-disable-line @
1721
* @param config - A custom configuration object (optional)
1822
*/
1923

20-
export function compile(this: Eta, str: string, options?: Partial<Options>): TemplateFunction {
24+
export function compile(
25+
this: Eta,
26+
str: string,
27+
options?: Partial<Options>,
28+
): TemplateFunction {
2129
const config: EtaConfig = this.config;
2230

2331
/* ASYNC HANDLING */
2432
// code gratefully taken from https://github.com/mde/ejs and adapted
25-
const ctor = options && options.async ? (AsyncFunction as FunctionConstructor) : Function;
33+
const ctor = options && options.async
34+
? (AsyncFunction as FunctionConstructor)
35+
: Function;
2636
/* END ASYNC HANDLING */
2737

2838
try {
2939
return new ctor(
3040
config.varName,
3141
"options",
32-
this.compileToString.call(this, str, options)
42+
this.compileToString.call(this, str, options),
3343
) as TemplateFunction; // eslint-disable-line no-new-func
3444
} catch (e) {
3545
if (e instanceof SyntaxError) {
@@ -40,7 +50,7 @@ export function compile(this: Eta, str: string, options?: Partial<Options>): Tem
4050
Array(e.message.length + 1).join("=") +
4151
"\n" +
4252
this.compileToString.call(this, str, options) +
43-
"\n" // This will put an extra newline before the callstack for extra readability
53+
"\n", // This will put an extra newline before the callstack for extra readability
4454
);
4555
} else {
4656
throw e;

src/config.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ export interface EtaConfig {
5353
};
5454

5555
/** Array of plugins */
56-
plugins: Array<{ processFnString?: Function; processAST?: Function; processTemplate?: Function }>;
56+
plugins: Array<
57+
{
58+
processFnString?: Function;
59+
processAST?: Function;
60+
processTemplate?: Function;
61+
}
62+
>;
5763

5864
/** Remove all safe-to-remove whitespace */
5965
rmWhitespace: boolean;

src/core.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { Cacher } from "./storage.ts";
22
import { compile } from "./compile.ts";
3-
import { compileToString, compileBody } from "./compile-string.ts";
3+
import { compileBody, compileToString } from "./compile-string.ts";
44
import { defaultConfig } from "./config.ts";
55
import { parse } from "./parse.ts";
6-
import { render, renderAsync, renderString, renderStringAsync } from "./render.ts";
7-
import { RuntimeErr, EtaError } from "./err.ts";
6+
import {
7+
render,
8+
renderAsync,
9+
renderString,
10+
renderStringAsync,
11+
} from "./render.ts";
12+
import { EtaError, RuntimeErr } from "./err.ts";
813
import { TemplateFunction } from "./compile.ts";
914

1015
/* TYPES */
@@ -38,7 +43,10 @@ export class Eta {
3843
templatesAsync: Cacher<TemplateFunction> = new Cacher<TemplateFunction>({});
3944

4045
// resolvePath takes a relative path from the "views" directory
41-
resolvePath: null | ((this: Eta, template: string, options?: Partial<Options>) => string) = null;
46+
resolvePath:
47+
| null
48+
| ((this: Eta, template: string, options?: Partial<Options>) => string) =
49+
null;
4250
readFile: null | ((this: Eta, path: string) => string) = null;
4351

4452
// METHODS
@@ -47,23 +55,28 @@ export class Eta {
4755
this.config = { ...this.config, ...customConfig };
4856
}
4957

50-
withConfig(customConfig: Partial<EtaConfig>): this & { config: EtaConfig }{
58+
withConfig(customConfig: Partial<EtaConfig>): this & { config: EtaConfig } {
5159
return { ...this, config: { ...this.config, ...customConfig } };
5260
}
5361

5462
loadTemplate(
5563
name: string,
5664
template: string | TemplateFunction, // template string or template function
57-
options?: { async: boolean }
65+
options?: { async: boolean },
5866
): void {
5967
if (typeof template === "string") {
60-
const templates = options && options.async ? this.templatesAsync : this.templatesSync;
68+
const templates = options && options.async
69+
? this.templatesAsync
70+
: this.templatesSync;
6171

6272
templates.define(name, this.compile(template, options));
6373
} else {
6474
let templates = this.templatesSync;
6575

66-
if (template.constructor.name === "AsyncFunction" || (options && options.async)) {
76+
if (
77+
template.constructor.name === "AsyncFunction" ||
78+
(options && options.async)
79+
) {
6780
templates = this.templatesAsync;
6881
}
6982

src/err.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ export function ParseErr(message: string, str: string, indx: number): never {
4242

4343
const lineNo = whitespace.length;
4444
const colNo = whitespace[lineNo - 1].length + 1;
45-
message +=
46-
" at line " +
45+
message += " at line " +
4746
lineNo +
4847
" col " +
4948
colNo +
@@ -57,7 +56,12 @@ export function ParseErr(message: string, str: string, indx: number): never {
5756
throw new EtaParseError(message);
5857
}
5958

60-
export function RuntimeErr(originalError: Error, str: string, lineNo: number, path: string): never {
59+
export function RuntimeErr(
60+
originalError: Error,
61+
str: string,
62+
lineNo: number,
63+
path: string,
64+
): never {
6165
// code gratefully taken from https://github.com/mde/ejs and adapted
6266

6367
const lines = str.split("\n");
@@ -73,9 +77,13 @@ export function RuntimeErr(originalError: Error, str: string, lineNo: number, pa
7377
})
7478
.join("\n");
7579

76-
const header = filename ? filename + ":" + lineNo + "\n" : "line " + lineNo + "\n";
80+
const header = filename
81+
? filename + ":" + lineNo + "\n"
82+
: "line " + lineNo + "\n";
7783

78-
const err = new EtaRuntimeError(header + context + "\n\n" + originalError.message);
84+
const err = new EtaRuntimeError(
85+
header + context + "\n\n" + originalError.message,
86+
);
7987

8088
err.name = originalError.name; // the original name (e.g. ReferenceError) may be useful
8189

src/file-handling.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function readFile(this: EtaCore, path: string): string {
2929
export function resolvePath(
3030
this: EtaCore,
3131
templatePath: string,
32-
options?: Partial<Options>
32+
options?: Partial<Options>,
3333
): string {
3434
let resolvedFilePath = "";
3535

@@ -40,8 +40,9 @@ export function resolvePath(
4040
}
4141

4242
const baseFilePath = options && options.filepath;
43-
const defaultExtension =
44-
this.config.defaultExtension === undefined ? ".eta" : this.config.defaultExtension;
43+
const defaultExtension = this.config.defaultExtension === undefined
44+
? ".eta"
45+
: this.config.defaultExtension;
4546

4647
// how we index cached template paths
4748
const cacheIndex = JSON.stringify({
@@ -80,7 +81,9 @@ export function resolvePath(
8081

8182
return resolvedFilePath;
8283
} else {
83-
throw new EtaFileResolutionError(`Template '${templatePath}' is not in the views directory`);
84+
throw new EtaFileResolutionError(
85+
`Template '${templatePath}' is not in the views directory`,
86+
);
8487
}
8588
}
8689

src/parse.ts

+19-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export type AstObject = string | TemplateObject;
1717

1818
/* END TYPES */
1919

20-
const templateLitReg = /`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})*}|(?!\${)[^\\`])*`/g;
20+
const templateLitReg =
21+
/`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})*}|(?!\${)[^\\`])*`/g;
2122

2223
const singleQuoteReg = /'(?:\\[\s\w"'\\`]|[^\n\r'\\])*?'/g;
2324

@@ -74,7 +75,7 @@ export function parse(this: Eta, str: string): Array<AstObject> {
7475
strng,
7576
config,
7677
trimLeftOfNextStr, // this will only be false on the first str, the next ones will be null or undefined
77-
shouldTrimRightOfString
78+
shouldTrimRightOfString,
7879
);
7980

8081
if (strng) {
@@ -88,9 +89,13 @@ export function parse(this: Eta, str: string): Array<AstObject> {
8889
}
8990
}
9091

91-
const prefixes = [parseOptions.exec, parseOptions.interpolate, parseOptions.raw].reduce(function (
92+
const prefixes = [
93+
parseOptions.exec,
94+
parseOptions.interpolate,
95+
parseOptions.raw,
96+
].reduce(function (
9297
accumulator,
93-
prefix
98+
prefix,
9499
) {
95100
if (accumulator && prefix) {
96101
return accumulator + "|" + escapeRegExp(prefix);
@@ -101,17 +106,16 @@ export function parse(this: Eta, str: string): Array<AstObject> {
101106
// prefix and accumulator are both falsy
102107
return accumulator;
103108
}
104-
},
105-
"");
109+
}, "");
106110

107111
const parseOpenReg = new RegExp(
108112
escapeRegExp(config.tags[0]) + "(-|_)?\\s*(" + prefixes + ")?\\s*",
109-
"g"
113+
"g",
110114
);
111115

112116
const parseCloseReg = new RegExp(
113117
"'|\"|`|\\/\\*|(\\s*(-|_)?" + escapeRegExp(config.tags[1]) + ")",
114-
"g"
118+
"g",
115119
);
116120

117121
let m;
@@ -138,14 +142,13 @@ export function parse(this: Eta, str: string): Array<AstObject> {
138142

139143
trimLeftOfNextStr = closeTag[2];
140144

141-
const currentType: TagType =
142-
prefix === parseOptions.exec
143-
? "e"
144-
: prefix === parseOptions.raw
145-
? "r"
146-
: prefix === parseOptions.interpolate
147-
? "i"
148-
: "";
145+
const currentType: TagType = prefix === parseOptions.exec
146+
? "e"
147+
: prefix === parseOptions.raw
148+
? "r"
149+
: prefix === parseOptions.interpolate
150+
? "i"
151+
: "";
149152

150153
currentObj = { t: currentType, val: content };
151154
break;

src/render.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ import type { TemplateFunction } from "./compile.ts";
66
import type { Eta } from "./core.ts";
77
/* END TYPES */
88

9-
function handleCache(this: Eta, template: string, options: Partial<Options>): TemplateFunction {
10-
const templateStore = options && options.async ? this.templatesAsync : this.templatesSync;
9+
function handleCache(
10+
this: Eta,
11+
template: string,
12+
options: Partial<Options>,
13+
): TemplateFunction {
14+
const templateStore = options && options.async
15+
? this.templatesAsync
16+
: this.templatesSync;
1117

1218
if (this.resolvePath && this.readFile && !template.startsWith("@")) {
1319
const templatePath = options.filepath as string;
@@ -31,7 +37,9 @@ function handleCache(this: Eta, template: string, options: Partial<Options>): Te
3137
if (cachedTemplate) {
3238
return cachedTemplate;
3339
} else {
34-
throw new EtaNameResolutionError("Failed to get template '" + template + "'");
40+
throw new EtaNameResolutionError(
41+
"Failed to get template '" + template + "'",
42+
);
3543
}
3644
}
3745
}
@@ -40,7 +48,7 @@ export function render<T extends object>(
4048
this: Eta,
4149
template: string | TemplateFunction, // template name or template function
4250
data: T,
43-
meta?: { filepath: string }
51+
meta?: { filepath: string },
4452
): string {
4553
let templateFn: TemplateFunction;
4654
const options = { ...meta, async: false };
@@ -64,7 +72,7 @@ export function renderAsync<T extends object>(
6472
this: Eta,
6573
template: string | TemplateFunction, // template name or template function
6674
data: T,
67-
meta?: { filepath: string }
75+
meta?: { filepath: string },
6876
): Promise<string> {
6977
let templateFn: TemplateFunction;
7078
const options = { ...meta, async: true };
@@ -85,7 +93,11 @@ export function renderAsync<T extends object>(
8593
return Promise.resolve(res);
8694
}
8795

88-
export function renderString<T extends object>(this: Eta, template: string, data: T): string {
96+
export function renderString<T extends object>(
97+
this: Eta,
98+
template: string,
99+
data: T,
100+
): string {
89101
const templateFn = this.compile(template, { async: false });
90102

91103
return render.call(this, templateFn, data);
@@ -94,7 +106,7 @@ export function renderString<T extends object>(this: Eta, template: string, data
94106
export function renderStringAsync<T extends object>(
95107
this: Eta,
96108
template: string,
97-
data: T
109+
data: T,
98110
): Promise<string> {
99111
const templateFn = this.compile(template, { async: true });
100112

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function trimWS(
88
str: string,
99
config: EtaConfig,
1010
wsLeft: string | false,
11-
wsRight?: string | false
11+
wsRight?: string | false,
1212
): string {
1313
let leftTrim;
1414
let rightTrim;

0 commit comments

Comments
 (0)