Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug #59

Merged
merged 5 commits into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deno_dist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ Made with ❤ by [@nebrelbug](https://github.com/eta-dev) and all these wonderfu
<td align="center"><a href="https://twitter.com/ioan_chiriac"><img src="https://avatars2.githubusercontent.com/u/173203?v=4" width="100px;" alt=""/><br /><sub><b>Ioan CHIRIAC</b></sub></a><br /><a href="https://github.com/eta-dev/eta/commits?author=ichiriac" title="Code">💻</a> <a href="#ideas-ichiriac" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="https://www.linkedin.com/in/craig-morten/"><img src="https://avatars1.githubusercontent.com/u/46491566?v=4" width="100px;" alt=""/><br /><sub><b>Craig Morten</b></sub></a><br /><a href="https://github.com/eta-dev/eta/commits?author=asos-craigmorten" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/trojanh"><img src="https://avatars0.githubusercontent.com/u/22974490?v=4" width="100px;" alt=""/><br /><sub><b>Rajan Tiwari</b></sub></a><br /><a href="#example-trojanh" title="Examples">💡</a></td>
<td align="center"><a href="https://shadowtime2000.github.io"><img src="https://avatars1.githubusercontent.com/u/66655515?v=4" width="100px;" alt=""/><br /><sub><b>shadowtime2000</b></sub></a><br /><a href="https://github.com/eta-dev/eta/commits?author=shadowtime2000" title="Code">💻</a> <a href="#ideas-shadowtime2000" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="https://shadowtime2000.github.io"><img src="https://avatars1.githubusercontent.com/u/66655515?v=4" width="100px;" alt=""/><br /><sub><b>shadowtime2000</b></sub></a><br /><a href="https://github.com/eta-dev/eta/commits?author=shadowtime2000" title="Code">💻</a> <a href="#ideas-shadowtime2000" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/eta-dev/eta/commits?author=shadowtime2000" title="Tests">⚠️</a></td>
<td align="center"><a href="https://hamidihamza.com"><img src="https://avatars0.githubusercontent.com/u/22576950?v=4" width="100px;" alt=""/><br /><sub><b>Hamza Hamidi</b></sub></a><br /><a href="https://github.com/eta-dev/eta/commits?author=hamzahamidi" title="Documentation">📖</a></td>
</tr>
<tr>
Expand Down
22 changes: 11 additions & 11 deletions deno_dist/compile-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export default function compileToString(
str: string,
config: EtaConfig,
): string {
var buffer: Array<AstObject> = Parse(str, config);
const buffer: Array<AstObject> = Parse(str, config);

var res = "var tR='',__l,__lP" +
let 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" +
Expand All @@ -43,8 +43,8 @@ export default function compileToString(
(config.useWith ? "}" : "");

if (config.plugins) {
for (var i = 0; i < config.plugins.length; i++) {
var plugin = config.plugins[i];
for (let i = 0; i < config.plugins.length; i++) {
const plugin = config.plugins[i];
if (plugin.processFnString) {
res = plugin.processFnString(res, config);
}
Expand All @@ -68,20 +68,20 @@ export default function compileToString(
*/

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

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

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

if (type === "r") {
// raw
Expand Down
12 changes: 5 additions & 7 deletions deno_dist/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,15 @@ export default function compile(
str: string,
config?: PartialConfig,
): TemplateFunction {
var options: EtaConfig = getConfig(config || {});
var ctor; // constructor
const options: EtaConfig = getConfig(config || {});

/* ASYNC HANDLING */
// The below code is modified from mde/ejs. All credit should go to them.
if (options.async) {
ctor = getAsyncFunctionConstructor() as FunctionConstructor;
} else {
ctor = Function;
}
const ctor = options.async
? (getAsyncFunctionConstructor() as FunctionConstructor)
: Function;
/* END ASYNC HANDLING */

try {
return new ctor(
options.varName,
Expand Down
6 changes: 3 additions & 3 deletions deno_dist/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ function includeHelper(
templateNameOrPath: string,
data: object,
): string {
var template = this.templates.get(templateNameOrPath);
const template = this.templates.get(templateNameOrPath);
if (!template) {
throw EtaErr('Could not fetch template "' + templateNameOrPath + '"');
}
return template(data, this);
}

/** Eta's base (global) configuration */
var config: EtaConfig = {
const config: EtaConfig = {
async: false,
autoEscape: true,
autoTrim: [false, "nl"],
Expand Down Expand Up @@ -154,7 +154,7 @@ var config: EtaConfig = {
function getConfig(override: PartialConfig, baseConfig?: EtaConfig): EtaConfig {
// TODO: run more tests on this

var res: PartialConfig = {}; // Linked
const res: PartialConfig = {}; // Linked
copyProps(res, config); // Creates deep clone of eta.config, 1 layer deep

if (baseConfig) {
Expand Down
2 changes: 1 addition & 1 deletion deno_dist/containers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import type { TemplateFunction } from "./compile.ts";
* Stores partials and cached templates
*/

var templates = new Cacher<TemplateFunction>({});
const templates = new Cacher<TemplateFunction>({});

export { templates };
8 changes: 4 additions & 4 deletions deno_dist/err.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function setPrototypeOf(obj: any, proto: any) {
*/

export default function EtaErr(message: string): Error {
var err = new Error(message);
const err = new Error(message);
setPrototypeOf(err, EtaErr.prototype);
return err;
}
Expand All @@ -37,10 +37,10 @@ EtaErr.prototype = Object.create(Error.prototype, {
*/

export function ParseErr(message: string, str: string, indx: number): void {
var whitespace = str.slice(0, indx).split(/\n/);
const whitespace = str.slice(0, indx).split(/\n/);

var lineNo = whitespace.length;
var colNo = whitespace[lineNo - 1].length + 1;
const lineNo = whitespace.length;
const colNo = whitespace[lineNo - 1].length + 1;
message += " at line " +
lineNo +
" col " +
Expand Down
34 changes: 21 additions & 13 deletions deno_dist/file-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ export function loadFile(
options: PartialConfigWithFilename,
noCache?: boolean,
): TemplateFunction {
var config = getConfig(options);
var template = readFile(filePath);
const config = getConfig(options);
const template = readFile(filePath);
try {
var compiledTemplate = compile(template, config);
const compiledTemplate = compile(template, config);
if (!noCache) {
config.templates.define(
(config as EtaConfigWithFilename).filename,
Expand All @@ -73,10 +73,10 @@ export function loadFile(
*/

function handleCache(options: EtaConfigWithFilename): TemplateFunction {
var filename = options.filename;
const filename = options.filename;

if (options.cache) {
var func = options.templates.get(filename);
const func = options.templates.get(filename);
if (func) {
return func;
}
Expand Down Expand Up @@ -107,7 +107,7 @@ function tryHandleCache(
try {
// Note: if there is an error while rendering the template,
// It will bubble up and be caught here
var templateFn = handleCache(options);
const templateFn = handleCache(options);
templateFn(data, options, cb);
} catch (err) {
return cb(err);
Expand All @@ -118,8 +118,8 @@ function tryHandleCache(
return new promiseImpl<string>(
function (resolve: Function, reject: Function) {
try {
var templateFn = handleCache(options);
var result = templateFn(data, options);
const templateFn = handleCache(options);
const result = templateFn(data, options);
resolve(result);
} catch (err) {
reject(err);
Expand Down Expand Up @@ -156,7 +156,10 @@ function includeFile(
options: EtaConfig,
): [TemplateFunction, EtaConfig] {
// the below creates a new options object, using the parent filepath of the old options object and the path
var newFileOptions = getConfig({ filename: getPath(path, options) }, options);
const newFileOptions = getConfig(
{ filename: getPath(path, options) },
options,
);
// TODO: make sure properties are currectly copied over
return [handleCache(newFileOptions as EtaConfigWithFilename), newFileOptions];
}
Expand Down Expand Up @@ -215,8 +218,8 @@ function renderFile(
And we want to also make (filename, data, options, cb) available
*/

var renderConfig: EtaConfigWithFilename;
var callback: CallbackFn | undefined;
let renderConfig: EtaConfigWithFilename;
let callback: CallbackFn | undefined;
data = data || {}; // If data is undefined, we don't want accessing data.settings to error

// First, assign our callback function to `callback`
Expand Down Expand Up @@ -250,7 +253,7 @@ function renderFile(
}
// Undocumented after Express 2, but still usable, esp. for
// items that are unsafe to be passed along with data, like `root`
var viewOpts = data.settings["view options"];
const viewOpts = data.settings["view options"];

if (viewOpts) {
copyProps(renderConfig, viewOpts);
Expand Down Expand Up @@ -284,7 +287,12 @@ function renderFileAsync(
config?: PartialConfig,
cb?: CallbackFn,
): Promise<string> | void {
return renderFile(filename, data, { ...config, async: true }, cb);
return renderFile(
filename,
typeof config === "function" ? { ...data, async: true } : data,
typeof config === "object" ? { ...config, async: true } : config,
cb,
);
}

export { includeFile, renderFile, renderFileAsync };
2 changes: 1 addition & 1 deletion deno_dist/file-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export function includeFileHelper(
path: string,
data: GenericData,
): string {
var templateAndConfig = includeFile(path, this);
const templateAndConfig = includeFile(path, this);
return templateAndConfig[0](data, templateAndConfig[1]);
}
2 changes: 1 addition & 1 deletion deno_dist/file-methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * as fs from "https://deno.land/std@0.66.0/fs/mod.ts";
export * as path from "https://deno.land/std@0.66.0/path/mod.ts";

export var readFileSync = Deno.readTextFileSync;
export const readFileSync = Deno.readTextFileSync;
28 changes: 12 additions & 16 deletions deno_dist/file-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fs, path, readFileSync } from "./file-methods.ts";
var _BOM = /^\uFEFF/;
const _BOM = /^\uFEFF/;

// express is set like: app.engine('html', require('eta').renderFile)

Expand Down Expand Up @@ -28,14 +28,10 @@ function getWholeFilePath(
parentfile: string,
isDirectory?: boolean,
): string {
var includePath = path.resolve(
const includePath = path.resolve(
isDirectory ? parentfile : path.dirname(parentfile), // returns directory the parent file is in
name, // file
);
var ext = path.extname(name);
if (!ext) {
includePath += ".eta";
}
) + (path.extname(name) ? "" : ".eta");
return includePath;
}

Expand All @@ -58,15 +54,15 @@ function getWholeFilePath(
*/

function getPath(path: string, options: EtaConfig): string {
var includePath: string | false = false;
var views = options.views;
var searchedPaths: Array<string> = [];
let includePath: string | false = false;
const views = options.views;
let searchedPaths: Array<string> = [];

// If these four values are the same,
// getPath() will return the same result every time.
// We can cache the result to avoid expensive
// file operations.
var pathOptions = JSON.stringify({
const pathOptions = JSON.stringify({
filename: options.filename, // filename of the template which called includeFile()
path: path,
root: options.root,
Expand Down Expand Up @@ -99,7 +95,7 @@ function getPath(path: string, options: EtaConfig): string {
views: Array<string> | string | undefined,
path: string,
): string | false {
var filePath;
let filePath;

// If views is an array, then loop through each directory
// And attempt to find the template
Expand Down Expand Up @@ -132,20 +128,20 @@ function getPath(path: string, options: EtaConfig): string {
}

// Path starts with '/', 'C:\', etc.
var match = /^[A-Za-z]+:\\|^\//.exec(path);
const match = /^[A-Za-z]+:\\|^\//.exec(path);

// Absolute path, like /partials/partial.eta
if (match && match.length) {
// We have to trim the beginning '/' off the path, or else
// path.resolve(dir, path) will always resolve to just path
var formattedPath = path.replace(/^\/*/, "");
const formattedPath = path.replace(/^\/*/, "");

// First, try to resolve the path within options.views
includePath = searchViews(views, formattedPath);
if (!includePath) {
// If that fails, searchViews will return false. Try to find the path
// inside options.root (by default '/', the base of the filesystem)
var pathFromRoot = getWholeFilePath(
const pathFromRoot = getWholeFilePath(
formattedPath,
options.root || "/",
true,
Expand All @@ -159,7 +155,7 @@ function getPath(path: string, options: EtaConfig): string {
// Relative paths
// Look relative to a passed filename first
if (options.filename) {
var filePath = getWholeFilePath(path, options.filename);
const filePath = getWholeFilePath(path, options.filename);

addPathToSearched(filePath);

Expand Down
Loading