Skip to content

Commit

Permalink
refactor: use const instead of var where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowtime2000 committed Dec 5, 2020
1 parent dc5380a commit 0c9410f
Show file tree
Hide file tree
Showing 26 changed files with 102 additions and 102 deletions.
12 changes: 6 additions & 6 deletions src/compile-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { AstObject } from './parse'
*/

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" +
Expand All @@ -42,7 +42,7 @@ export default function compileToString(str: string, config: EtaConfig): string

if (config.plugins) {
for (var i = 0; i < config.plugins.length; i++) {
var plugin = config.plugins[i]
const plugin = config.plugins[i]
if (plugin.processFnString) {
res = plugin.processFnString(res, config)
}
Expand All @@ -67,18 +67,18 @@ export default function compileToString(str: string, config: EtaConfig): string

function compileScope(buff: Array<AstObject>, config: EtaConfig) {
var i = 0
var buffLength = buff.length
const buffLength = buff.length
var 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
const type = currentBlock.t // ~, s, !, ?, r
var content = currentBlock.val || ''

if (type === 'r') {
Expand Down
2 changes: 1 addition & 1 deletion src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type TemplateFunction = (data: object, config: EtaConfig, cb?: CallbackFn
*/

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

/* ASYNC HANDLING */
Expand Down
6 changes: 3 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ export type PartialConfig = Partial<EtaConfig>
*/

function includeHelper(this: EtaConfig, 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 @@ -144,7 +144,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 src/containers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import type { TemplateFunction } from './compile'
* Stores partials and cached templates
*/

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

export { templates }
8 changes: 4 additions & 4 deletions src/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 +
Expand Down
20 changes: 10 additions & 10 deletions src/file-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,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, compiledTemplate)
}
Expand All @@ -66,10 +66,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 All @@ -96,7 +96,7 @@ function tryHandleCache(data: object, options: EtaConfigWithFilename, cb: Callba
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 @@ -106,8 +106,8 @@ function tryHandleCache(data: object, options: EtaConfigWithFilename, cb: Callba
if (typeof promiseImpl === 'function') {
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 @@ -138,7 +138,7 @@ function tryHandleCache(data: object, options: EtaConfigWithFilename, cb: Callba

function includeFile(path: string, 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 @@ -226,7 +226,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
2 changes: 1 addition & 1 deletion src/file-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ interface GenericData {
*/

export function includeFileHelper(this: EtaConfig, 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 src/file-methods.deno.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
16 changes: 8 additions & 8 deletions src/file-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fs, path, readFileSync } from './file-methods'
var _BOM = /^\uFEFF/
const _BOM = /^\uFEFF/

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

Expand Down Expand Up @@ -28,7 +28,7 @@ function getWholeFilePath(name: string, parentfile: string, isDirectory?: boolea
isDirectory ? parentfile : path.dirname(parentfile), // returns directory the parent file is in
name // file
)
var ext = path.extname(name)
const ext = path.extname(name)
if (!ext) {
includePath += '.eta'
}
Expand All @@ -55,14 +55,14 @@ function getWholeFilePath(name: string, parentfile: string, isDirectory?: boolea

function getPath(path: string, options: EtaConfig): string {
var includePath: string | false = false
var views = options.views
const views = options.views
var 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 @@ -123,20 +123,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(formattedPath, options.root || '/', true)
const pathFromRoot = getWholeFilePath(formattedPath, options.root || '/', true)

addPathToSearched(pathFromRoot)

Expand All @@ -146,7 +146,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
34 changes: 17 additions & 17 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export type AstObject = string | TemplateObject

/* END TYPES */

var templateLitReg = /`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})*}|(?!\${)[^\\`])*`/g
const templateLitReg = /`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})*}|(?!\${)[^\\`])*`/g

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

var doubleQuoteReg = /"(?:\\[\s\w"'\\`]|[^\n\r"\\])*?"/g
const doubleQuoteReg = /"(?:\\[\s\w"'\\`]|[^\n\r"\\])*?"/g

/** Escape special regular expression characters inside a string */

Expand All @@ -33,11 +33,11 @@ export default function parse(str: string, config: EtaConfig): Array<AstObject>
var buffer: Array<AstObject> = []
var trimLeftOfNextStr: string | false = false
var lastIndex = 0
var parseOptions = config.parse
const parseOptions = config.parse

if (config.plugins) {
for (var i = 0; i < config.plugins.length; i++) {
var plugin = config.plugins[i]
const plugin = config.plugins[i]
if (plugin.processTemplate) {
str = plugin.processTemplate(str, config)
}
Expand Down Expand Up @@ -81,7 +81,7 @@ export default function parse(str: string, config: EtaConfig): Array<AstObject>
}
}

var prefixes = [parseOptions.exec, parseOptions.interpolate, parseOptions.raw].reduce(function (
const prefixes = [parseOptions.exec, parseOptions.interpolate, parseOptions.raw].reduce(function (
accumulator,
prefix
) {
Expand All @@ -97,12 +97,12 @@ export default function parse(str: string, config: EtaConfig): Array<AstObject>
},
'')

var parseOpenReg = new RegExp(
const parseOpenReg = new RegExp(
'([^]*?)' + escapeRegExp(config.tags[0]) + '(-|_)?\\s*(' + prefixes + ')?\\s*',
'g'
)

var parseCloseReg = new RegExp(
const parseCloseReg = new RegExp(
'\'|"|`|\\/\\*|(\\s*(-|_)?' + escapeRegExp(config.tags[1]) + ')',
'g'
)
Expand All @@ -113,9 +113,9 @@ export default function parse(str: string, config: EtaConfig): Array<AstObject>
while ((m = parseOpenReg.exec(str))) {
lastIndex = m[0].length + m.index

var precedingString = m[1]
var wsLeft = m[2]
var prefix = m[3] || '' // by default either ~, =, or empty
const precedingString = m[1]
const wsLeft = m[2]
const prefix = m[3] || '' // by default either ~, =, or empty

pushString(precedingString, wsLeft)

Expand Down Expand Up @@ -143,9 +143,9 @@ export default function parse(str: string, config: EtaConfig): Array<AstObject>
currentObj = { t: currentType, val: content }
break
} else {
var char = closeTag[0]
const char = closeTag[0]
if (char === '/*') {
var commentCloseInd = str.indexOf('*/', parseCloseReg.lastIndex)
const commentCloseInd = str.indexOf('*/', parseCloseReg.lastIndex)

if (commentCloseInd === -1) {
ParseErr('unclosed comment', str, closeTag.index)
Expand All @@ -154,15 +154,15 @@ export default function parse(str: string, config: EtaConfig): Array<AstObject>
} else if (char === "'") {
singleQuoteReg.lastIndex = closeTag.index

var singleQuoteMatch = singleQuoteReg.exec(str)
const singleQuoteMatch = singleQuoteReg.exec(str)
if (singleQuoteMatch) {
parseCloseReg.lastIndex = singleQuoteReg.lastIndex
} else {
ParseErr('unclosed string', str, closeTag.index)
}
} else if (char === '"') {
doubleQuoteReg.lastIndex = closeTag.index
var doubleQuoteMatch = doubleQuoteReg.exec(str)
const doubleQuoteMatch = doubleQuoteReg.exec(str)

if (doubleQuoteMatch) {
parseCloseReg.lastIndex = doubleQuoteReg.lastIndex
Expand All @@ -171,7 +171,7 @@ export default function parse(str: string, config: EtaConfig): Array<AstObject>
}
} else if (char === '`') {
templateLitReg.lastIndex = closeTag.index
var templateLitMatch = templateLitReg.exec(str)
const templateLitMatch = templateLitReg.exec(str)
if (templateLitMatch) {
parseCloseReg.lastIndex = templateLitReg.lastIndex
} else {
Expand All @@ -191,7 +191,7 @@ export default function parse(str: string, config: EtaConfig): Array<AstObject>

if (config.plugins) {
for (var i = 0; i < config.plugins.length; i++) {
var plugin = config.plugins[i]
const plugin = config.plugins[i]
if (plugin.processAST) {
buffer = plugin.processAST(buffer, config)
}
Expand Down
2 changes: 1 addition & 1 deletion src/polyfills.deno.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export var promiseImpl = Promise
export const promiseImpl = Promise

export function getAsyncFunctionConstructor (): Function {
return async function () {}.constructor
Expand Down
4 changes: 2 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function render(
config?: PartialConfig,
cb?: CallbackFn
): string | Promise<string> | void {
var options = getConfig(config || {})
const options = getConfig(config || {})

if (options.async) {
var result
Expand All @@ -67,7 +67,7 @@ export default function render(
try {
// Note: if there is an error while rendering the template,
// It will bubble up and be caught here
var templateFn = handleCache(template, options)
const templateFn = handleCache(template, options)
templateFn(data, options, cb)
} catch (err) {
return cb(err)
Expand Down
Loading

0 comments on commit 0c9410f

Please sign in to comment.