chore: upgrade typescript

This commit is contained in:
Harttle
2021-12-11 20:23:09 +08:00
committed by Harttle
parent 6801552fe6
commit 95fc705b5d
19 changed files with 1045 additions and 1360 deletions
+994 -1315
View File
File diff suppressed because it is too large Load Diff
+7 -7
View File
@@ -73,15 +73,15 @@
"@types/sinon": "^7.0.6", "@types/sinon": "^7.0.6",
"@types/sinon-chai": "^3.2.2", "@types/sinon-chai": "^3.2.2",
"@types/supertest": "^2.0.7", "@types/supertest": "^2.0.7",
"@typescript-eslint/eslint-plugin": "^2.0.0", "@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^2.0.0", "@typescript-eslint/parser": "^5.6.0",
"all-contributors-cli": "^6.20.0", "all-contributors-cli": "^6.20.0",
"benchmark": "^2.1.4", "benchmark": "^2.1.4",
"chai": "^4.2.0", "chai": "^4.2.0",
"chai-as-promised": "^7.1.1", "chai-as-promised": "^7.1.1",
"coveralls": "^3.0.2", "coveralls": "^3.0.2",
"cross-env": "^5.2.0", "cross-env": "^5.2.0",
"eslint": "^6.8.0", "eslint": "^7.32.0",
"eslint-config-standard": "^12.0.0", "eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.15.0", "eslint-plugin-import": "^2.15.0",
"eslint-plugin-mocha": "^5.3.0", "eslint-plugin-mocha": "^5.3.0",
@@ -94,9 +94,9 @@
"mocha": "^9.0.1", "mocha": "^9.0.1",
"nyc": "^15.1.0", "nyc": "^15.1.0",
"regenerator-runtime": "^0.12.1", "regenerator-runtime": "^0.12.1",
"rollup": "^1.1.2", "rollup": "^2.61.0",
"rollup-plugin-replace": "^2.1.0", "rollup-plugin-replace": "^2.1.0",
"rollup-plugin-typescript2": "^0.21.1", "rollup-plugin-typescript2": "^0.31.1",
"rollup-plugin-uglify": "^5.0.2", "rollup-plugin-uglify": "^5.0.2",
"rollup-plugin-version-injector": "^1.3.3", "rollup-plugin-version-injector": "^1.3.3",
"semantic-release": "^17.2.3", "semantic-release": "^17.2.3",
@@ -104,10 +104,10 @@
"sinon-chai": "^3.3.0", "sinon-chai": "^3.3.0",
"supertest": "^3.4.2", "supertest": "^3.4.2",
"ts-node": "^8.0.2", "ts-node": "^8.0.2",
"tslib": "^1.9.3", "tslib": "^2.3.1",
"typedoc": "^0.15.0", "typedoc": "^0.15.0",
"typedoc-plugin-markdown": "^2.2.17", "typedoc-plugin-markdown": "^2.2.17",
"typescript": "^3.3.3" "typescript": "^4.5.3"
}, },
"release": { "release": {
"branch": "master", "branch": "master",
+1 -1
View File
@@ -20,7 +20,7 @@ export function sort<T> (this: FilterImpl, arr: T[], property?: string) {
export const size = (v: string | any[]) => (v && v.length) || 0 export const size = (v: string | any[]) => (v && v.length) || 0
export function map<T1, T2> (this: FilterImpl, arr: Scope[], property: string) { export function map (this: FilterImpl, arr: Scope[], property: string) {
return toArray(arr).map(obj => this.context.getFromScope(obj, property.split('.'))) return toArray(arr).map(obj => this.context.getFromScope(obj, property.split('.')))
} }
+1 -1
View File
@@ -9,7 +9,7 @@ export default {
tokenizer.advance() tokenizer.advance()
this.value = tokenizer.remaining() this.value = tokenizer.remaining()
}, },
render: function * (ctx: Context) { render: function * (ctx: Context): Generator<unknown, void, unknown> {
ctx.bottom()[this.key] = yield this.liquid._evalValue(this.value, ctx) ctx.bottom()[this.key] = yield this.liquid._evalValue(this.value, ctx)
} }
} as TagImplOptions } as TagImplOptions
+1 -1
View File
@@ -17,7 +17,7 @@ export default {
}) })
stream.start() stream.start()
}, },
render: function * (ctx: Context) { render: function * (ctx: Context): Generator<unknown, void, string> {
const r = this.liquid.renderer const r = this.liquid.renderer
const html = yield r.renderTemplates(this.templates, ctx) const html = yield r.renderTemplates(this.templates, ctx)
ctx.bottom()[this.variable] = html ctx.bottom()[this.variable] = html
+2 -2
View File
@@ -1,7 +1,7 @@
import { assert, Tokenizer, evalToken, Emitter, TagToken, TopLevelToken, Context, Template, TagImplOptions, ParseStream } from '../../types' import { assert, Tokenizer, evalToken, Emitter, TagToken, TopLevelToken, Context, Template, TagImplOptions, ParseStream } from '../../types'
import { toEnumerable } from '../../util/collection' import { toEnumerable } from '../../util/collection'
import { ForloopDrop } from '../../drop/forloop-drop' import { ForloopDrop } from '../../drop/forloop-drop'
import { Hash } from '../../template/tag/hash' import { Hash, HashValue } from '../../template/tag/hash'
const MODIFIERS = ['offset', 'limit', 'reversed'] const MODIFIERS = ['offset', 'limit', 'reversed']
@@ -38,7 +38,7 @@ export default {
stream.start() stream.start()
}, },
render: function * (ctx: Context, emitter: Emitter) { render: function * (ctx: Context, emitter: Emitter): Generator<unknown, void | string, HashValue | Template[]> {
const r = this.liquid.renderer const r = this.liquid.renderer
let collection = toEnumerable(yield evalToken(this.collection, ctx)) let collection = toEnumerable(yield evalToken(this.collection, ctx))
+1 -1
View File
@@ -22,7 +22,7 @@ export default {
.start() .start()
}, },
render: function * (ctx: Context, emitter: Emitter) { render: function * (ctx: Context, emitter: Emitter): Generator<unknown, void, string> {
const r = this.liquid.renderer const r = this.liquid.renderer
for (const { predicate, templates } of this.branches) { for (const { predicate, templates } of this.branches) {
+1
View File
@@ -3,6 +3,7 @@ import { Emitter } from './emitter'
export class StreamedEmitter implements Emitter { export class StreamedEmitter implements Emitter {
public buffer = ''; public buffer = '';
// eslint-disable-next-line @typescript-eslint/no-var-requires
public stream: NodeJS.ReadWriteStream = new (require('stream').PassThrough)() public stream: NodeJS.ReadWriteStream = new (require('stream').PassThrough)()
public write (html: any) { public write (html: any) {
this.stream.write(stringify(html)) this.stream.write(stringify(html))
+1 -1
View File
@@ -33,7 +33,7 @@ export class Loader {
this.contains = this.options.fs.contains || (() => true) this.contains = this.options.fs.contains || (() => true)
} }
public * lookup (file: string, type: LookupType, sync?: boolean, currentFile?: string) { public * lookup (file: string, type: LookupType, sync?: boolean, currentFile?: string): Generator<unknown, string, string> {
const { fs } = this.options const { fs } = this.options
const dirs = this.options[type] const dirs = this.options[type]
for (const filepath of this.candidates(file, dirs, currentFile, type !== LookupType.Root)) { for (const filepath of this.candidates(file, dirs, currentFile, type !== LookupType.Root)) {
+5 -4
View File
@@ -14,7 +14,7 @@ import { FS } from '../fs/fs'
import { toThenable, Thenable } from '../util/async' import { toThenable, Thenable } from '../util/async'
export default class Parser { export default class Parser {
public parseFile: (file: string, sync?: boolean, type?: LookupType, currentFile?: string) => IterableIterator<any> public parseFile: (file: string, sync?: boolean, type?: LookupType, currentFile?: string) => Generator<unknown, Template[], Template[] | string>
private liquid: Liquid private liquid: Liquid
private fs: FS private fs: FS
@@ -51,13 +51,13 @@ export default class Parser {
} }
return new HTML(token) return new HTML(token)
} catch (e) { } catch (e) {
throw new ParseError(e, token) throw new ParseError(e as Error, token)
} }
} }
public parseStream (tokens: TopLevelToken[]) { public parseStream (tokens: TopLevelToken[]) {
return new ParseStream(tokens, (token, tokens) => this.parseToken(token, tokens)) return new ParseStream(tokens, (token, tokens) => this.parseToken(token, tokens))
} }
private * _parseFileCached (file: string, sync?: boolean, type: LookupType = LookupType.Root, currentFile?: string) { private * _parseFileCached (file: string, sync?: boolean, type: LookupType = LookupType.Root, currentFile?: string): Generator<unknown, Template[], Template[]> {
const key = this.loader.shouldLoadRelative(file) const key = this.loader.shouldLoadRelative(file)
? currentFile + ',' + file ? currentFile + ',' + file
: type + ':' + file : type + ':' + file
@@ -72,8 +72,9 @@ export default class Parser {
// remove cached task if failed // remove cached task if failed
this.cache!.remove(key) this.cache!.remove(key)
} }
return []
} }
private * _parseFile (file: string, sync?: boolean, type: LookupType = LookupType.Root, currentFile?: string): IterableIterator<any> { private * _parseFile (file: string, sync?: boolean, type: LookupType = LookupType.Root, currentFile?: string): Generator<unknown, Template[], string> {
const filepath = yield this.loader.lookup(file, type, sync, currentFile) const filepath = yield this.loader.lookup(file, type, sync, currentFile)
return this.liquid.parse(sync ? this.fs.readFileSync(filepath) : yield this.fs.readFile(filepath), filepath) return this.liquid.parse(sync ? this.fs.readFileSync(filepath) : yield this.fs.readFile(filepath), filepath)
} }
+3 -3
View File
@@ -20,7 +20,7 @@ export class Expression {
public constructor (tokens: IterableIterator<Token>) { public constructor (tokens: IterableIterator<Token>) {
this.postfix = [...toPostfix(tokens)] this.postfix = [...toPostfix(tokens)]
} }
public * evaluate (ctx: Context, lenient: boolean): any { public * evaluate (ctx: Context, lenient: boolean): Generator<unknown, unknown, unknown> {
assert(ctx, 'unable to evaluate: context not defined') assert(ctx, 'unable to evaluate: context not defined')
const operands: any[] = [] const operands: any[] = []
for (const token of this.postfix) { for (const token of this.postfix) {
@@ -51,8 +51,8 @@ function evalPropertyAccessToken (token: PropertyAccessToken, ctx: Context, leni
try { try {
return ctx.get([token.propertyName, ...props]) return ctx.get([token.propertyName, ...props])
} catch (e) { } catch (e) {
if (lenient && e.name === 'InternalUndefinedVariableError') return null if (lenient && (e as Error).name === 'InternalUndefinedVariableError') return null
throw (new UndefinedVariableError(e, token)) throw (new UndefinedVariableError(e as Error, token))
} }
} }
+1 -1
View File
@@ -26,7 +26,7 @@ export class Render {
html && emitter.write(html) html && emitter.write(html)
if (emitter['break'] || emitter['continue']) break if (emitter['break'] || emitter['continue']) break
} catch (e) { } catch (e) {
const err = RenderError.is(e) ? e : new RenderError(e, tpl) const err = RenderError.is(e) ? e : new RenderError(e as Error, tpl)
throw err throw err
} }
} }
+1 -1
View File
@@ -12,7 +12,7 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
super(token) super(token)
this.value = new Value(token.content, liquid) this.value = new Value(token.content, liquid)
} }
public * render (ctx: Context, emitter: Emitter) { public * render (ctx: Context, emitter: Emitter): Generator<unknown, void, unknown> {
const val = yield this.value.value(ctx, false) const val = yield this.value.value(ctx, false)
emitter.write(val) emitter.write(val)
} }
+6 -2
View File
@@ -2,6 +2,10 @@ import { evalToken } from '../../render/expression'
import { Context } from '../../context/context' import { Context } from '../../context/context'
import { Tokenizer } from '../../parser/tokenizer' import { Tokenizer } from '../../parser/tokenizer'
export interface HashValue {
[key: string]: any;
}
/** /**
* Key-Value Pairs Representing Tag Arguments * Key-Value Pairs Representing Tag Arguments
* Example: * Example:
@@ -11,14 +15,14 @@ import { Tokenizer } from '../../parser/tokenizer'
* hash['reversed'] === undefined * hash['reversed'] === undefined
*/ */
export class Hash { export class Hash {
hash: { [key: string]: any } = {} hash: HashValue = {}
constructor (markup: string) { constructor (markup: string) {
const tokenizer = new Tokenizer(markup, {}) const tokenizer = new Tokenizer(markup, {})
for (const hash of tokenizer.readHashes()) { for (const hash of tokenizer.readHashes()) {
this.hash[hash.name.content] = hash.value this.hash[hash.name.content] = hash.value
} }
} }
* render (ctx: Context) { * render (ctx: Context): Generator<unknown, HashValue, unknown> {
const hash = {} const hash = {}
for (const key of Object.keys(this.hash)) { for (const key of Object.keys(this.hash)) {
hash[key] = this.hash[key] === undefined ? true : yield evalToken(this.hash[key], ctx) hash[key] = this.hash[key] === undefined ? true : yield evalToken(this.hash[key], ctx)
+2 -2
View File
@@ -2,10 +2,10 @@ import { Context } from '../../context/context'
import { TagToken } from '../../tokens/tag-token' import { TagToken } from '../../tokens/tag-token'
import { TopLevelToken } from '../../tokens/toplevel-token' import { TopLevelToken } from '../../tokens/toplevel-token'
import { TagImpl } from './tag-impl' import { TagImpl } from './tag-impl'
import { Hash } from '../../template/tag/hash' import { HashValue } from '../../template/tag/hash'
import { Emitter } from '../../emitters/emitter' import { Emitter } from '../../emitters/emitter'
export interface TagImplOptions { export interface TagImplOptions {
parse?: (this: TagImpl, token: TagToken, remainingTokens: TopLevelToken[]) => void; parse?: (this: TagImpl, token: TagToken, remainingTokens: TopLevelToken[]) => void;
render: (this: TagImpl, ctx: Context, emitter: Emitter, hash: Hash) => any; render: (this: TagImpl, ctx: Context, emitter: Emitter, hash: HashValue) => void | string | Promise<void | string> | Generator<unknown, void | string, unknown>;
} }
+3 -2
View File
@@ -3,6 +3,7 @@ import { Liquid } from '../../liquid'
import { TemplateImpl } from '../../template/template-impl' import { TemplateImpl } from '../../template/template-impl'
import { Emitter, Hash, Context, TagToken, Template, TopLevelToken } from '../../types' import { Emitter, Hash, Context, TagToken, Template, TopLevelToken } from '../../types'
import { TagImpl } from './tag-impl' import { TagImpl } from './tag-impl'
import { HashValue } from './hash'
export class Tag extends TemplateImpl<TagToken> implements Template { export class Tag extends TemplateImpl<TagToken> implements Template {
public name: string public name: string
@@ -20,8 +21,8 @@ export class Tag extends TemplateImpl<TagToken> implements Template {
this.impl.parse(token, tokens) this.impl.parse(token, tokens)
} }
} }
public * render (ctx: Context, emitter: Emitter) { public * render (ctx: Context, emitter: Emitter): Generator<unknown, unknown, HashValue | unknown> {
const hash = yield new Hash(this.token.args).render(ctx) const hash = (yield new Hash(this.token.args).render(ctx)) as HashValue
const impl = this.impl const impl = this.impl
if (isFunction(impl.render)) return yield impl.render(ctx, emitter, hash) if (isFunction(impl.render)) return yield impl.render(ctx, emitter, hash)
} }
+1 -1
View File
@@ -16,7 +16,7 @@ export class Value {
this.initial = tokenizer.readExpression() this.initial = tokenizer.readExpression()
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, liquid.filters.get(name), args, liquid)) this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, liquid.filters.get(name), args, liquid))
} }
public * value (ctx: Context, lenient: boolean) { public * value (ctx: Context, lenient: boolean): Generator<unknown, unknown, unknown> {
lenient = lenient || (ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default') lenient = lenient || (ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default')
let val = yield this.initial.evaluate(ctx, lenient) let val = yield this.initial.evaluate(ctx, lenient)
+7 -9
View File
@@ -34,10 +34,8 @@ function isAsyncIterator (val: any): val is IterableIterator<any> {
return val && isFunction(val.next) && isFunction(val.throw) && isFunction(val.return) return val && isFunction(val.next) && isFunction(val.throw) && isFunction(val.return)
} }
type Task<T> = Thenable<T>
// convert an async iterator to a thenable (Promise compatible) // convert an async iterator to a thenable (Promise compatible)
export function toThenable<T> (val: IterableIterator<any> | Thenable<T> | any): Thenable<T> { export function toThenable<T> (val: IteratorResult<unknown, T> | Thenable<T> | any): Thenable<T> {
if (isThenable(val)) return val if (isThenable(val)) return val
if (isAsyncIterator(val)) return reduce() if (isAsyncIterator(val)) return reduce()
return createResolvedThenable(val) return createResolvedThenable(val)
@@ -45,18 +43,18 @@ export function toThenable<T> (val: IterableIterator<any> | Thenable<T> | any):
function reduce<T> (prev?: T): Thenable<T> { function reduce<T> (prev?: T): Thenable<T> {
let state let state
try { try {
state = (val as IterableIterator<any>).next(prev) state = val.next(prev)
} catch (err) { } catch (err) {
return createRejectedThenable(err) return createRejectedThenable(err as Error)
} }
if (state.done) return createResolvedThenable(state.value) if (state.done) return createResolvedThenable(state.value)
return toThenable(state.value!).then(reduce, err => { return toThenable(state.value!).then(reduce, err => {
let state let state
try { try {
state = (val as IterableIterator<any>).throw!(err) state = val.throw!(err)
} catch (e) { } catch (e) {
return createRejectedThenable(e) return createRejectedThenable(e as Error)
} }
if (state.done) return createResolvedThenable(state.value) if (state.done) return createResolvedThenable(state.value)
return reduce(state.value) return reduce(state.value)
@@ -64,12 +62,12 @@ export function toThenable<T> (val: IterableIterator<any> | Thenable<T> | any):
} }
} }
export function toPromise<T> (val: IterableIterator<any> | Thenable<T> | T): Promise<T> { export function toPromise<T> (val: Generator<unknown, T, unknown> | Thenable<T> | T): Promise<T> {
return Promise.resolve(toThenable(val)) return Promise.resolve(toThenable(val))
} }
// get the value of async iterator in synchronous manner // get the value of async iterator in synchronous manner
export function toValue<T> (val: IterableIterator<any> | Thenable<T> | T): T { export function toValue<T> (val: Generator<unknown, T, unknown> | Thenable<T> | T): T {
let ret: T let ret: T
toThenable(val) toThenable(val)
.then((x: any) => { .then((x: any) => {
+7 -6
View File
@@ -7,6 +7,7 @@ export function isString (value: any): value is string {
return typeof value === 'string' return typeof value === 'string'
} }
// eslint-disable-next-line @typescript-eslint/ban-types
export function isFunction (value: any): value is Function { export function isFunction (value: any): value is Function {
return typeof value === 'function' return typeof value === 'function'
} }
@@ -65,16 +66,16 @@ export function isArray (value: any): value is any[] {
* @return {Object} Returns object. * @return {Object} Returns object.
*/ */
export function forOwn <T> ( export function forOwn <T> (
object: {[key: string]: T} | undefined, obj: {[key: string]: T} | undefined,
iteratee: ((val: T, key: string, obj: {[key: string]: T}) => boolean | void) iteratee: ((val: T, key: string, obj: {[key: string]: T}) => boolean | void)
) { ) {
object = object || {} obj = obj || {}
for (const k in object) { for (const k in obj) {
if (object.hasOwnProperty(k)) { if (Object.hasOwnProperty.call(obj, k)) {
if (iteratee(object[k], k, object) === false) break if (iteratee(obj[k], k, obj) === false) break
} }
} }
return object return obj
} }
export function last <T>(arr: T[]): T; export function last <T>(arr: T[]): T;