From cf180375d0cff657f9c27d18a077e784fe6ad61f Mon Sep 17 00:00:00 2001 From: Viko Date: Sat, 26 May 2018 15:32:56 -0600 Subject: [PATCH 1/3] ejs: Add support for the async option --- types/ejs/ejs-tests.ts | 12 +++++++++++- types/ejs/index.d.ts | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/types/ejs/ejs-tests.ts b/types/ejs/ejs-tests.ts index a7b7b66944..8a0b2404d7 100644 --- a/types/ejs/ejs-tests.ts +++ b/types/ejs/ejs-tests.ts @@ -3,7 +3,7 @@ import ejs = require("ejs"); import { readFileSync as read } from 'fs'; import LRU = require("lru-cache"); -import { TemplateFunction, Options } from "ejs"; +import { TemplateFunction, AsyncTemplateFunction, Options } from "ejs"; const fileName = 'test.ejs'; const people = ['geddy', 'neil', 'alex']; @@ -12,6 +12,8 @@ const template = '<%= people.join(", "); %>'; const options = { filename: fileName }; let result: string; let ejsFunction: TemplateFunction; +let asyncResult: Promise; +let ejsAsyncFunction: AsyncTemplateFunction; const SimpleCallback = (err: any, html?: string) => { if (err) { @@ -44,11 +46,19 @@ ejsFunction = ejs.compile('<%= it.people.join(", "); %>', { _with: false, locals ejsFunction = ejs.compile(template, { rmWhitespace: true }); const customEscape = (str: string) => !str ? '' : str.toUpperCase(); ejsFunction = ejs.compile(template, { escape: customEscape }); +ejsFunction = ejs.compile(template, { async: false }); + +ejsAsyncFunction = ejs.compile(template, { async: true }); +ejsAsyncFunction = ejs.compile(template, { client: true, async: true }); result = ejsFunction(); result = ejsFunction({}); result = ejsFunction(data); +asyncResult = ejsAsyncFunction(); +asyncResult = ejsAsyncFunction({}); +asyncResult = ejsAsyncFunction(data); + /** @see https://github.com/mde/ejs/tree/v2.5.7#custom-fileloader */ ejs.fileLoader = (path: string) => ""; diff --git a/types/ejs/index.d.ts b/types/ejs/index.d.ts index e56ad1a8ec..655d6563e6 100644 --- a/types/ejs/index.d.ts +++ b/types/ejs/index.d.ts @@ -2,7 +2,7 @@ // Project: http://ejs.co/ // Definitions by: Ben Liddicott // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.4 +// TypeScript Version: 2.8 export interface Data { [name: string]: any; @@ -28,14 +28,22 @@ export function resolveInclude(name: string, filename: string, isDir: boolean): /** * Compile the given `str` of ejs into a template function. */ -export function compile(template: string, opts?: Options): (TemplateFunction); +export function compile(template: string, opts?: Options & { async: false }): TemplateFunction; +export function compile(template: string, opts: Options & { async: true }): AsyncTemplateFunction; +export function compile(template: string, opts: Options & { async: boolean }): TemplateFunction | AsyncTemplateFunction; +export function compile(template: string, opts: Exclude): TemplateFunction; +export function compile(template: string, opts?: Options): TemplateFunction | AsyncTemplateFunction; /** * Render the given `template` of ejs. * * If you would like to include options but not data, you need to explicitly * call this function with `data` being an empty object or `null`. */ -export function render(template: string, data?: Data, opts?: Options): string; +export function render(template: string, data?: Data, opts?: Options & { async: false }): string; +export function render(template: string, data: Data, opts: Options & { async: true }): Promise; +export function render(template: string, data: Data, opts: Options & { async: boolean }): string | Promise; +export function render(template: string, data: Data, opts: Exclude): string; +export function render(template: string, data?: Data, opts?: Options): string | Promise; export type RenderFileCallback = (err: Error, str?: string) => T; @@ -55,6 +63,7 @@ export function renderFile(path: string, data: Data, opts: Options, cb: Rende export function clearCache(): void; export type TemplateFunction = (data?: Data) => string; +export type AsyncTemplateFunction = (data?: Data) => Promise; export interface Options { /** Compiled functions are cached, requires `filename` */ cache?: boolean; @@ -95,6 +104,8 @@ export interface Options { * (By default escapes XML). */ escape?(str: string): string; + /** Whether or not to use a Promise */ + async?: boolean; } export function escapeRegexChars(s: string): string; From ca6c7c59e3c721cfa1eaa767657bf9f351467c8f Mon Sep 17 00:00:00 2001 From: Viko Date: Sat, 26 May 2018 15:53:02 -0600 Subject: [PATCH 2/3] ejs: Fix mem-fs-editor and version in header --- types/ejs/index.d.ts | 2 +- types/mem-fs-editor/index.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/types/ejs/index.d.ts b/types/ejs/index.d.ts index 655d6563e6..9371bb0da7 100644 --- a/types/ejs/index.d.ts +++ b/types/ejs/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for ejs.js 2.5 +// Type definitions for ejs.js 2.6 // Project: http://ejs.co/ // Definitions by: Ben Liddicott // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped diff --git a/types/mem-fs-editor/index.d.ts b/types/mem-fs-editor/index.d.ts index 01234bbc0c..636afa2698 100644 --- a/types/mem-fs-editor/index.d.ts +++ b/types/mem-fs-editor/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/SBoudrias/mem-fs-editor#readme // Definitions by: My Food Bag // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.4 +// TypeScript Version: 2.8 /// From 55aa0245a6c466f6f54e0e3d33079d4b8f5d35de Mon Sep 17 00:00:00 2001 From: Viko Date: Fri, 1 Jun 2018 17:33:11 -0600 Subject: [PATCH 3/3] ejs, mem-fs-editor: Correct types per @uniqueiniquity's comments --- types/ejs/index.d.ts | 10 ++++------ types/mem-fs-editor/index.d.ts | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/types/ejs/index.d.ts b/types/ejs/index.d.ts index 9371bb0da7..73eb716e23 100644 --- a/types/ejs/index.d.ts +++ b/types/ejs/index.d.ts @@ -2,7 +2,7 @@ // Project: http://ejs.co/ // Definitions by: Ben Liddicott // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 2.4 export interface Data { [name: string]: any; @@ -30,8 +30,7 @@ export function resolveInclude(name: string, filename: string, isDir: boolean): */ export function compile(template: string, opts?: Options & { async: false }): TemplateFunction; export function compile(template: string, opts: Options & { async: true }): AsyncTemplateFunction; -export function compile(template: string, opts: Options & { async: boolean }): TemplateFunction | AsyncTemplateFunction; -export function compile(template: string, opts: Exclude): TemplateFunction; +export function compile(template: string, opts: Options & { async?: never }): TemplateFunction; export function compile(template: string, opts?: Options): TemplateFunction | AsyncTemplateFunction; /** * Render the given `template` of ejs. @@ -40,9 +39,8 @@ export function compile(template: string, opts?: Options): TemplateFunction | As * call this function with `data` being an empty object or `null`. */ export function render(template: string, data?: Data, opts?: Options & { async: false }): string; -export function render(template: string, data: Data, opts: Options & { async: true }): Promise; -export function render(template: string, data: Data, opts: Options & { async: boolean }): string | Promise; -export function render(template: string, data: Data, opts: Exclude): string; +export function render(template: string, data: Data | undefined, opts: Options & { async: true }): Promise; +export function render(template: string, data: Data | undefined, opts: Options & { async?: never }): string; export function render(template: string, data?: Data, opts?: Options): string | Promise; export type RenderFileCallback = (err: Error, str?: string) => T; diff --git a/types/mem-fs-editor/index.d.ts b/types/mem-fs-editor/index.d.ts index 636afa2698..01234bbc0c 100644 --- a/types/mem-fs-editor/index.d.ts +++ b/types/mem-fs-editor/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/SBoudrias/mem-fs-editor#readme // Definitions by: My Food Bag // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 2.4 ///