refactor: strictly typed

This commit is contained in:
harttle
2019-02-23 00:49:54 +08:00
parent 51f7e66f60
commit 5b6100d12b
86 changed files with 2630 additions and 2590 deletions
+5 -3
View File
@@ -1,8 +1,10 @@
import { RenderBreakError, RenderError } from 'src/util/error'
import assert from 'src/util/assert'
import Scope from 'src/scope/scope';
import ITemplate from 'src/template/itemplate';
export default class Render {
async renderTemplates (templates, scope) {
async renderTemplates (templates: ITemplate[], scope: Scope) {
assert(scope, 'unable to evalTemplates: scope undefined')
let html = ''
@@ -10,11 +12,11 @@ export default class Render {
try {
html += await tpl.render(scope)
} catch (e) {
if (e instanceof RenderBreakError) {
if (e.name === 'RenderBreakError') {
e.resolvedHTML = html
throw e
}
throw e instanceof RenderError ? e : new RenderError(e, tpl)
throw e.name === 'RenderError' ? e : new RenderError(e, tpl)
}
}
return html
+16 -18
View File
@@ -1,23 +1,25 @@
import * as lexical from '../parser/lexical'
import assert from '../util/assert'
import Scope from 'src/scope/scope'
import { range } from 'src/util/underscore'
const operators = {
'==': (l, r) => l === r,
'!=': (l, r) => l !== r,
'>': (l, r) => l !== null && r !== null && l > r,
'<': (l, r) => l !== null && r !== null && l < r,
'>=': (l, r) => l !== null && r !== null && l >= r,
'<=': (l, r) => l !== null && r !== null && l <= r,
'contains': (l, r) => {
'==': (l: any, r: any) => l === r,
'!=': (l: any, r: any) => l !== r,
'>': (l: any, r: any) => l !== null && r !== null && l > r,
'<': (l: any, r: any) => l !== null && r !== null && l < r,
'>=': (l: any, r: any) => l !== null && r !== null && l >= r,
'<=': (l: any, r: any) => l !== null && r !== null && l <= r,
'contains': (l: any, r: any) => {
if (!l) return false
if (typeof l.indexOf !== 'function') return false
return l.indexOf(r) > -1
},
'and': (l, r) => isTruthy(l) && isTruthy(r),
'or': (l, r) => isTruthy(l) || isTruthy(r)
'and': (l: any, r: any) => isTruthy(l) && isTruthy(r),
'or': (l: any, r: any) => isTruthy(l) || isTruthy(r)
}
export function evalExp (exp, scope) {
export function evalExp (exp: string, scope: Scope): any {
assert(scope, 'unable to evalExp: scope undefined')
const operatorREs = lexical.operators
let match
@@ -35,17 +37,13 @@ export function evalExp (exp, scope) {
if ((match = exp.match(lexical.rangeLine))) {
const low = evalValue(match[1], scope)
const high = evalValue(match[2], scope)
const range = []
for (let j = low; j <= high; j++) {
range.push(j)
}
return range
return range(low, high + 1)
}
return evalValue(exp, scope)
}
export function evalValue (str, scope) {
export function evalValue (str: string, scope: Scope) {
str = str && str.trim()
if (!str) return undefined
@@ -58,10 +56,10 @@ export function evalValue (str, scope) {
throw new TypeError(`cannot eval '${str}' as value`)
}
export function isTruthy (val) {
export function isTruthy (val: any): boolean {
return !isFalsy(val)
}
export function isFalsy (val) {
export function isFalsy (val: any): boolean {
return val === false || undefined === val || val === null
}