mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
chore: upgrade typescript
This commit is contained in:
Generated
+994
-1315
File diff suppressed because it is too large
Load Diff
+7
-7
@@ -73,15 +73,15 @@
|
||||
"@types/sinon": "^7.0.6",
|
||||
"@types/sinon-chai": "^3.2.2",
|
||||
"@types/supertest": "^2.0.7",
|
||||
"@typescript-eslint/eslint-plugin": "^2.0.0",
|
||||
"@typescript-eslint/parser": "^2.0.0",
|
||||
"@typescript-eslint/eslint-plugin": "^5.6.0",
|
||||
"@typescript-eslint/parser": "^5.6.0",
|
||||
"all-contributors-cli": "^6.20.0",
|
||||
"benchmark": "^2.1.4",
|
||||
"chai": "^4.2.0",
|
||||
"chai-as-promised": "^7.1.1",
|
||||
"coveralls": "^3.0.2",
|
||||
"cross-env": "^5.2.0",
|
||||
"eslint": "^6.8.0",
|
||||
"eslint": "^7.32.0",
|
||||
"eslint-config-standard": "^12.0.0",
|
||||
"eslint-plugin-import": "^2.15.0",
|
||||
"eslint-plugin-mocha": "^5.3.0",
|
||||
@@ -94,9 +94,9 @@
|
||||
"mocha": "^9.0.1",
|
||||
"nyc": "^15.1.0",
|
||||
"regenerator-runtime": "^0.12.1",
|
||||
"rollup": "^1.1.2",
|
||||
"rollup": "^2.61.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-version-injector": "^1.3.3",
|
||||
"semantic-release": "^17.2.3",
|
||||
@@ -104,10 +104,10 @@
|
||||
"sinon-chai": "^3.3.0",
|
||||
"supertest": "^3.4.2",
|
||||
"ts-node": "^8.0.2",
|
||||
"tslib": "^1.9.3",
|
||||
"tslib": "^2.3.1",
|
||||
"typedoc": "^0.15.0",
|
||||
"typedoc-plugin-markdown": "^2.2.17",
|
||||
"typescript": "^3.3.3"
|
||||
"typescript": "^4.5.3"
|
||||
},
|
||||
"release": {
|
||||
"branch": "master",
|
||||
|
||||
@@ -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 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('.')))
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ export default {
|
||||
tokenizer.advance()
|
||||
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)
|
||||
}
|
||||
} as TagImplOptions
|
||||
|
||||
@@ -17,7 +17,7 @@ export default {
|
||||
})
|
||||
stream.start()
|
||||
},
|
||||
render: function * (ctx: Context) {
|
||||
render: function * (ctx: Context): Generator<unknown, void, string> {
|
||||
const r = this.liquid.renderer
|
||||
const html = yield r.renderTemplates(this.templates, ctx)
|
||||
ctx.bottom()[this.variable] = html
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { assert, Tokenizer, evalToken, Emitter, TagToken, TopLevelToken, Context, Template, TagImplOptions, ParseStream } from '../../types'
|
||||
import { toEnumerable } from '../../util/collection'
|
||||
import { ForloopDrop } from '../../drop/forloop-drop'
|
||||
import { Hash } from '../../template/tag/hash'
|
||||
import { Hash, HashValue } from '../../template/tag/hash'
|
||||
|
||||
const MODIFIERS = ['offset', 'limit', 'reversed']
|
||||
|
||||
@@ -38,7 +38,7 @@ export default {
|
||||
|
||||
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
|
||||
let collection = toEnumerable(yield evalToken(this.collection, ctx))
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ export default {
|
||||
.start()
|
||||
},
|
||||
|
||||
render: function * (ctx: Context, emitter: Emitter) {
|
||||
render: function * (ctx: Context, emitter: Emitter): Generator<unknown, void, string> {
|
||||
const r = this.liquid.renderer
|
||||
|
||||
for (const { predicate, templates } of this.branches) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Emitter } from './emitter'
|
||||
|
||||
export class StreamedEmitter implements Emitter {
|
||||
public buffer = '';
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
public stream: NodeJS.ReadWriteStream = new (require('stream').PassThrough)()
|
||||
public write (html: any) {
|
||||
this.stream.write(stringify(html))
|
||||
|
||||
+1
-1
@@ -33,7 +33,7 @@ export class Loader {
|
||||
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 dirs = this.options[type]
|
||||
for (const filepath of this.candidates(file, dirs, currentFile, type !== LookupType.Root)) {
|
||||
|
||||
@@ -14,7 +14,7 @@ import { FS } from '../fs/fs'
|
||||
import { toThenable, Thenable } from '../util/async'
|
||||
|
||||
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 fs: FS
|
||||
@@ -51,13 +51,13 @@ export default class Parser {
|
||||
}
|
||||
return new HTML(token)
|
||||
} catch (e) {
|
||||
throw new ParseError(e, token)
|
||||
throw new ParseError(e as Error, token)
|
||||
}
|
||||
}
|
||||
public parseStream (tokens: TopLevelToken[]) {
|
||||
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)
|
||||
? currentFile + ',' + file
|
||||
: type + ':' + file
|
||||
@@ -72,8 +72,9 @@ export default class Parser {
|
||||
// remove cached task if failed
|
||||
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)
|
||||
return this.liquid.parse(sync ? this.fs.readFileSync(filepath) : yield this.fs.readFile(filepath), filepath)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ export class Expression {
|
||||
public constructor (tokens: IterableIterator<Token>) {
|
||||
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')
|
||||
const operands: any[] = []
|
||||
for (const token of this.postfix) {
|
||||
@@ -51,8 +51,8 @@ function evalPropertyAccessToken (token: PropertyAccessToken, ctx: Context, leni
|
||||
try {
|
||||
return ctx.get([token.propertyName, ...props])
|
||||
} catch (e) {
|
||||
if (lenient && e.name === 'InternalUndefinedVariableError') return null
|
||||
throw (new UndefinedVariableError(e, token))
|
||||
if (lenient && (e as Error).name === 'InternalUndefinedVariableError') return null
|
||||
throw (new UndefinedVariableError(e as Error, token))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ export class Render {
|
||||
html && emitter.write(html)
|
||||
if (emitter['break'] || emitter['continue']) break
|
||||
} 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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
|
||||
super(token)
|
||||
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)
|
||||
emitter.write(val)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,10 @@ import { evalToken } from '../../render/expression'
|
||||
import { Context } from '../../context/context'
|
||||
import { Tokenizer } from '../../parser/tokenizer'
|
||||
|
||||
export interface HashValue {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Key-Value Pairs Representing Tag Arguments
|
||||
* Example:
|
||||
@@ -11,14 +15,14 @@ import { Tokenizer } from '../../parser/tokenizer'
|
||||
* hash['reversed'] === undefined
|
||||
*/
|
||||
export class Hash {
|
||||
hash: { [key: string]: any } = {}
|
||||
hash: HashValue = {}
|
||||
constructor (markup: string) {
|
||||
const tokenizer = new Tokenizer(markup, {})
|
||||
for (const hash of tokenizer.readHashes()) {
|
||||
this.hash[hash.name.content] = hash.value
|
||||
}
|
||||
}
|
||||
* render (ctx: Context) {
|
||||
* render (ctx: Context): Generator<unknown, HashValue, unknown> {
|
||||
const hash = {}
|
||||
for (const key of Object.keys(this.hash)) {
|
||||
hash[key] = this.hash[key] === undefined ? true : yield evalToken(this.hash[key], ctx)
|
||||
|
||||
@@ -2,10 +2,10 @@ import { Context } from '../../context/context'
|
||||
import { TagToken } from '../../tokens/tag-token'
|
||||
import { TopLevelToken } from '../../tokens/toplevel-token'
|
||||
import { TagImpl } from './tag-impl'
|
||||
import { Hash } from '../../template/tag/hash'
|
||||
import { HashValue } from '../../template/tag/hash'
|
||||
import { Emitter } from '../../emitters/emitter'
|
||||
|
||||
export interface TagImplOptions {
|
||||
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,6 +3,7 @@ import { Liquid } from '../../liquid'
|
||||
import { TemplateImpl } from '../../template/template-impl'
|
||||
import { Emitter, Hash, Context, TagToken, Template, TopLevelToken } from '../../types'
|
||||
import { TagImpl } from './tag-impl'
|
||||
import { HashValue } from './hash'
|
||||
|
||||
export class Tag extends TemplateImpl<TagToken> implements Template {
|
||||
public name: string
|
||||
@@ -20,8 +21,8 @@ export class Tag extends TemplateImpl<TagToken> implements Template {
|
||||
this.impl.parse(token, tokens)
|
||||
}
|
||||
}
|
||||
public * render (ctx: Context, emitter: Emitter) {
|
||||
const hash = yield new Hash(this.token.args).render(ctx)
|
||||
public * render (ctx: Context, emitter: Emitter): Generator<unknown, unknown, HashValue | unknown> {
|
||||
const hash = (yield new Hash(this.token.args).render(ctx)) as HashValue
|
||||
const impl = this.impl
|
||||
if (isFunction(impl.render)) return yield impl.render(ctx, emitter, hash)
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ export class Value {
|
||||
this.initial = tokenizer.readExpression()
|
||||
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')
|
||||
let val = yield this.initial.evaluate(ctx, lenient)
|
||||
|
||||
|
||||
+7
-9
@@ -34,10 +34,8 @@ function isAsyncIterator (val: any): val is IterableIterator<any> {
|
||||
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)
|
||||
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 (isAsyncIterator(val)) return reduce()
|
||||
return createResolvedThenable(val)
|
||||
@@ -45,18 +43,18 @@ export function toThenable<T> (val: IterableIterator<any> | Thenable<T> | any):
|
||||
function reduce<T> (prev?: T): Thenable<T> {
|
||||
let state
|
||||
try {
|
||||
state = (val as IterableIterator<any>).next(prev)
|
||||
state = val.next(prev)
|
||||
} catch (err) {
|
||||
return createRejectedThenable(err)
|
||||
return createRejectedThenable(err as Error)
|
||||
}
|
||||
|
||||
if (state.done) return createResolvedThenable(state.value)
|
||||
return toThenable(state.value!).then(reduce, err => {
|
||||
let state
|
||||
try {
|
||||
state = (val as IterableIterator<any>).throw!(err)
|
||||
state = val.throw!(err)
|
||||
} catch (e) {
|
||||
return createRejectedThenable(e)
|
||||
return createRejectedThenable(e as Error)
|
||||
}
|
||||
if (state.done) return createResolvedThenable(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))
|
||||
}
|
||||
|
||||
// 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
|
||||
toThenable(val)
|
||||
.then((x: any) => {
|
||||
|
||||
@@ -7,6 +7,7 @@ export function isString (value: any): value is string {
|
||||
return typeof value === 'string'
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export function isFunction (value: any): value is Function {
|
||||
return typeof value === 'function'
|
||||
}
|
||||
@@ -65,16 +66,16 @@ export function isArray (value: any): value is any[] {
|
||||
* @return {Object} Returns object.
|
||||
*/
|
||||
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)
|
||||
) {
|
||||
object = object || {}
|
||||
for (const k in object) {
|
||||
if (object.hasOwnProperty(k)) {
|
||||
if (iteratee(object[k], k, object) === false) break
|
||||
obj = obj || {}
|
||||
for (const k in obj) {
|
||||
if (Object.hasOwnProperty.call(obj, k)) {
|
||||
if (iteratee(obj[k], k, obj) === false) break
|
||||
}
|
||||
}
|
||||
return object
|
||||
return obj
|
||||
}
|
||||
|
||||
export function last <T>(arr: T[]): T;
|
||||
|
||||
Reference in New Issue
Block a user