Skip to content

Commit

Permalink
refactor: added return types to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowtime2000 committed Oct 9, 2020
1 parent 01fcf6e commit ba6c9ec
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function getConfig(override: PartialConfig, baseConfig?: EtaConfig): EtaConfig {

/** Update Eta's base config */

function configure(options: PartialConfig) {
function configure(options: PartialConfig): Partial<EtaConfig> {
return copyProps(config, options)
}

Expand Down
2 changes: 1 addition & 1 deletion src/err.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ EtaErr.prototype = Object.create(Error.prototype, {
* Throws an EtaErr with a nicely formatted error and message showing where in the template the error occurred.
*/

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

var lineNo = whitespace.length
Expand Down
6 changes: 3 additions & 3 deletions src/file-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ function includeFile(path: string, options: EtaConfig): [TemplateFunction, EtaCo
* ```
*/

function renderFile(filename: string, data: DataObj, config?: PartialConfig, cb?: CallbackFn): any
function renderFile(filename: string, data: DataObj, config?: PartialConfig, cb?: CallbackFn): ( Promise<string> | void )

function renderFile(filename: string, data: DataObj, cb?: CallbackFn): any
function renderFile(filename: string, data: DataObj, cb?: CallbackFn): ( Promise<string> | void )

function renderFile(filename: string, data: DataObj, config?: PartialConfig, cb?: CallbackFn) {
function renderFile(filename: string, data: DataObj, config?: PartialConfig, cb?: CallbackFn): ( Promise<string> | void ) {
/*
Here we have some function overloading.
Essentially, the first 2 arguments to renderFile should always be the filename and data
Expand Down
4 changes: 2 additions & 2 deletions src/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function getWholeFilePath(name: string, parentfile: string, isDirectory?: boolea
* @return absolute path to template
*/

function getPath(path: string, options: EtaConfig) {
function getPath(path: string, options: EtaConfig): string {
var includePath: string | false = false
var views = options.views
var searchedPaths: Array<string> = []
Expand Down Expand Up @@ -176,7 +176,7 @@ function getPath(path: string, options: EtaConfig) {
* Reads a file synchronously
*/

function readFile(filePath: string) {
function readFile(filePath: string): string {
try {
return readFileSync(filePath).toString().replace(_BOM, '') // TODO: is replacing BOM's necessary?
} catch {
Expand Down
8 changes: 4 additions & 4 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { copyProps } from './utils'
*/
class Cacher<T> {
constructor(private cache: Record<string, T>) {}
define(key: string, val: T) {
define(key: string, val: T): void {
this.cache[key] = val
}
get(key: string): T {
Expand All @@ -17,13 +17,13 @@ class Cacher<T> {
// TODO: create plugin to allow referencing helpers, filters with dot notation
return this.cache[key]
}
remove(key: string) {
remove(key: string): void {
delete this.cache[key]
}
reset() {
reset(): void {
this.cache = {}
}
load(cacheObj: Record<string, T>) {
load(cacheObj: Record<string, T>): void {
copyProps(this.cache, cacheObj)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ interface EscapeMap {

/* END TYPES */

export function hasOwnProp(obj: object, prop: string) {
export function hasOwnProp(obj: object, prop: string): boolean {
return Object.prototype.hasOwnProperty.call(obj, prop)
}

export function copyProps<T>(toObj: T, fromObj: T) {
export function copyProps<T>(toObj: T, fromObj: T): T {
for (var key in fromObj) {
if (hasOwnProp((fromObj as unknown) as object, key)) {
toObj[key] = fromObj[key]
Expand Down Expand Up @@ -113,7 +113,7 @@ function replaceChar(s: string): string {
* @returns XML-escaped string
*/

function XMLEscape(str: any) {
function XMLEscape(str: any): string {
// eslint-disable-line @typescript-eslint/no-explicit-any
// To deal with XSS. Based on Escape implementations of Mustache.JS and Marko, then customized.
var newStr = String(str)
Expand Down

0 comments on commit ba6c9ec

Please sign in to comment.