Skip to content

Commit

Permalink
chore: deno fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowtime2000 committed Nov 29, 2020
1 parent efd3e7f commit 145c127
Show file tree
Hide file tree
Showing 16 changed files with 551 additions and 455 deletions.
17 changes: 11 additions & 6 deletions deno_dist/browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export { default as compileToString } from './compile-string.ts'
export { default as compile } from './compile.ts'
export { default as parse } from './parse.ts'
export { default as render, renderAsync } from './render.ts'
export { templates } from './containers.ts'
export { config, config as defaultConfig, getConfig, configure } from './config.ts'
export { default as compileToString } from "./compile-string.ts";
export { default as compile } from "./compile.ts";
export { default as parse } from "./parse.ts";
export { default as render, renderAsync } from "./render.ts";
export { templates } from "./containers.ts";
export {
config,
config as defaultConfig,
configure,
getConfig,
} from "./config.ts";
84 changes: 43 additions & 41 deletions deno_dist/compile-string.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Parse from './parse.ts'
import Parse from "./parse.ts";

/* TYPES */

import type { EtaConfig } from './config.ts'
import type { AstObject } from './parse.ts'
import type { EtaConfig } from "./config.ts";
import type { AstObject } from "./parse.ts";

/* END TYPES */

Expand All @@ -18,38 +18,40 @@ import type { AstObject } from './parse.ts'
* ```
*/

export default function compileToString(str: string, config: EtaConfig): string {
var buffer: Array<AstObject> = Parse(str, config)

var res =
"var tR='',__l,__lP" +
(config.include ? ',include=E.include.bind(E)' : '') +
(config.includeFile ? ',includeFile=E.includeFile.bind(E)' : '') +
'\nfunction layout(p,d){__l=p;__lP=d}\n' +
(config.useWith ? 'with(' + config.varName + '||{}){' : '') +
export default function compileToString(
str: string,
config: EtaConfig,
): string {
var buffer: Array<AstObject> = Parse(str, config);

var res = "var tR='',__l,__lP" +
(config.include ? ",include=E.include.bind(E)" : "") +
(config.includeFile ? ",includeFile=E.includeFile.bind(E)" : "") +
"\nfunction layout(p,d){__l=p;__lP=d}\n" +
(config.useWith ? "with(" + config.varName + "||{}){" : "") +
compileScope(buffer, config) +
(config.includeFile
? 'if(__l)tR=' +
(config.async ? 'await ' : '') +
? "if(__l)tR=" +
(config.async ? "await " : "") +
`includeFile(__l,Object.assign(${config.varName},{body:tR},__lP))\n`
: config.include
? 'if(__l)tR=' +
(config.async ? 'await ' : '') +
? "if(__l)tR=" +
(config.async ? "await " : "") +
`include(__l,Object.assign(${config.varName},{body:tR},__lP))\n`
: '') +
'if(cb){cb(null,tR)} return tR' +
(config.useWith ? '}' : '')
: "") +
"if(cb){cb(null,tR)} return tR" +
(config.useWith ? "}" : "");

if (config.plugins) {
for (var i = 0; i < config.plugins.length; i++) {
var plugin = config.plugins[i]
var plugin = config.plugins[i];
if (plugin.processFnString) {
res = plugin.processFnString(res, config)
res = plugin.processFnString(res, config);
}
}
}

return res
return res;
}

/**
Expand All @@ -66,47 +68,47 @@ export default function compileToString(str: string, config: EtaConfig): string
*/

function compileScope(buff: Array<AstObject>, config: EtaConfig) {
var i = 0
var buffLength = buff.length
var returnStr = ''
var i = 0;
var buffLength = buff.length;
var returnStr = "";

for (i; i < buffLength; i++) {
var currentBlock = buff[i]
if (typeof currentBlock === 'string') {
var str = currentBlock
var currentBlock = buff[i];
if (typeof currentBlock === "string") {
var str = currentBlock;

// we know string exists
returnStr += "tR+='" + str + "'\n"
returnStr += "tR+='" + str + "'\n";
} else {
var type = currentBlock.t // ~, s, !, ?, r
var content = currentBlock.val || ''
var type = currentBlock.t; // ~, s, !, ?, r
var content = currentBlock.val || "";

if (type === 'r') {
if (type === "r") {
// raw

if (config.filter) {
content = 'E.filter(' + content + ')'
content = "E.filter(" + content + ")";
}

returnStr += 'tR+=' + content + '\n'
} else if (type === 'i') {
returnStr += "tR+=" + content + "\n";
} else if (type === "i") {
// interpolate

if (config.filter) {
content = 'E.filter(' + content + ')'
content = "E.filter(" + content + ")";
}

if (config.autoEscape) {
content = 'E.e(' + content + ')'
content = "E.e(" + content + ")";
}
returnStr += 'tR+=' + content + '\n'
returnStr += "tR+=" + content + "\n";
// reference
} else if (type === 'e') {
} else if (type === "e") {
// execute
returnStr += content + '\n' // you need a \n in case you have <% } %>
returnStr += content + "\n"; // you need a \n in case you have <% } %>
}
}
}

return returnStr
return returnStr;
}
53 changes: 30 additions & 23 deletions deno_dist/compile.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import compileToString from './compile-string.ts'
import { getConfig } from './config.ts'
import EtaErr from './err.ts'
import compileToString from "./compile-string.ts";
import { getConfig } from "./config.ts";
import EtaErr from "./err.ts";

/* TYPES */

import type { EtaConfig, PartialConfig } from './config.ts'
import type { CallbackFn } from './file-handlers.ts'
import { getAsyncFunctionConstructor } from './polyfills.ts'
export type TemplateFunction = (data: object, config: EtaConfig, cb?: CallbackFn) => string
import type { EtaConfig, PartialConfig } from "./config.ts";
import type { CallbackFn } from "./file-handlers.ts";
import { getAsyncFunctionConstructor } from "./polyfills.ts";
export type TemplateFunction = (
data: object,
config: EtaConfig,
cb?: CallbackFn,
) => string;

/* END TYPES */

Expand All @@ -27,38 +31,41 @@ export type TemplateFunction = (data: object, config: EtaConfig, cb?: CallbackFn
* ```
*/

export default function compile(str: string, config?: PartialConfig): TemplateFunction {
var options: EtaConfig = getConfig(config || {})
var ctor // constructor
export default function compile(
str: string,
config?: PartialConfig,
): TemplateFunction {
var options: EtaConfig = getConfig(config || {});
var ctor; // constructor

/* ASYNC HANDLING */
// The below code is modified from mde/ejs. All credit should go to them.
if (options.async) {
ctor = getAsyncFunctionConstructor() as FunctionConstructor
ctor = getAsyncFunctionConstructor() as FunctionConstructor;
} else {
ctor = Function
ctor = Function;
}
/* END ASYNC HANDLING */
try {
return new ctor(
options.varName,
'E', // EtaConfig
'cb', // optional callback
compileToString(str, options)
) as TemplateFunction // eslint-disable-line no-new-func
"E", // EtaConfig
"cb", // optional callback
compileToString(str, options),
) as TemplateFunction; // eslint-disable-line no-new-func
} catch (e) {
if (e instanceof SyntaxError) {
throw EtaErr(
'Bad template syntax\n\n' +
"Bad template syntax\n\n" +
e.message +
'\n' +
Array(e.message.length + 1).join('=') +
'\n' +
"\n" +
Array(e.message.length + 1).join("=") +
"\n" +
compileToString(str, options) +
'\n' // This will put an extra newline before the callstack for extra readability
)
"\n", // This will put an extra newline before the callstack for extra readability
);
} else {
throw e
throw e;
}
}
}
Loading

0 comments on commit 145c127

Please sign in to comment.