diff --git a/types/ejs/ejs-tests.ts b/types/ejs/ejs-tests.ts index b5b46f03b5..a7b7b66944 100644 --- a/types/ejs/ejs-tests.ts +++ b/types/ejs/ejs-tests.ts @@ -1,3 +1,61 @@ +/// + import ejs = require("ejs"); -var people = ['geddy', 'neil', 'alex']; -var html = ejs.render('<%= people.join(", "); %>', { people: people }); +import { readFileSync as read } from 'fs'; +import LRU = require("lru-cache"); +import { TemplateFunction, Options } from "ejs"; + +const fileName = 'test.ejs'; +const people = ['geddy', 'neil', 'alex']; +const data = { people }; +const template = '<%= people.join(", "); %>'; +const options = { filename: fileName }; +let result: string; +let ejsFunction: TemplateFunction; + +const SimpleCallback = (err: any, html?: string) => { + if (err) { + return null; + } + return html; +}; + +result = ejs.render(template); +result = ejs.render(template, data); +result = ejs.render(template, data, options); + +result = ejs.renderFile(fileName, SimpleCallback); +result = ejs.renderFile(fileName, data, SimpleCallback); +result = ejs.renderFile(fileName, data, options, SimpleCallback); + +ejsFunction = ejs.compile(''); +ejsFunction = ejs.compile(read(fileName, "utf8")); +ejsFunction = ejs.compile(template); +ejsFunction = ejs.compile(template, options); +ejsFunction = ejs.compile(template, { cache: true, filename: fileName }); +ejsFunction = ejs.compile(template, { cache: true, filename: fileName, root: "./" }); +ejsFunction = ejs.compile(template, { context: { foo: 'FOO' } }); +ejsFunction = ejs.compile(template, { compileDebug: false }); +ejsFunction = ejs.compile(template, { client: true }); +ejsFunction = ejs.compile('<$= people.join(", "); $>', { delimiter: '$' }); +ejsFunction = ejs.compile('<%= locals.people.join(", "); %>', { _with: false }); +ejsFunction = ejs.compile('<%= locals.people.join(", "); %>', { strict: true }); +ejsFunction = ejs.compile('<%= it.people.join(", "); %>', { _with: false, localsName: "it" }); +ejsFunction = ejs.compile(template, { rmWhitespace: true }); +const customEscape = (str: string) => !str ? '' : str.toUpperCase(); +ejsFunction = ejs.compile(template, { escape: customEscape }); + +result = ejsFunction(); +result = ejsFunction({}); +result = ejsFunction(data); + +/** @see https://github.com/mde/ejs/tree/v2.5.7#custom-fileloader */ +ejs.fileLoader = (path: string) => ""; + +/** @see https://github.com/mde/ejs/tree/v2.5.7#caching */ +ejs.clearCache(); +ejs.cache = LRU(100); + +/** @see https://github.com/mde/ejs/tree/v2.5.7#custom-delimiters */ +ejs.delimiter = "%"; +delete ejs.delimiter; diff --git a/types/ejs/index.d.ts b/types/ejs/index.d.ts index a5bfb23ee9..e785f2c6ae 100644 --- a/types/ejs/index.d.ts +++ b/types/ejs/index.d.ts @@ -1,90 +1,124 @@ -// Type definitions for ejs.js v2.3.3 +// Type definitions for ejs.js 2.5 // Project: http://ejs.co/ -// Definitions by: Ben Liddicott +// Definitions by: Ben Liddicott // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 -declare namespace Ejs { - type Data = { [name: string]: any }; - type Dependencies = string[]; - var cache: Cache; - var localsName: string; - function resolveInclude(name: string, filename: string): string; - function compile(template: string, opts?: Options): (TemplateFunction); - function render(template: string, data?: Data, opts?: Options): string; +export interface Data { + [name: string]: any; +} +/** + * EJS template function cache. This can be a LRU object from lru-cache NPM + * module. By default, it is {@link module:utils.cache}, a simple in-process + * cache that grows continuously. + */ +export let cache: Cache; +/** + * Name of the object containing the locals. + * + * This variable is overridden by {@link Options}`.localsName` if it is not + * `undefined`. + */ +export let localsName: string; +/** + * Get the path to the included file from the parent file path and the + * specified path. + */ +export function resolveInclude(name: string, filename: string, isDir: boolean): string; +/** + * Compile the given `str` of ejs into a template function. + */ +export function compile(template: string, opts?: Options): (TemplateFunction); +/** + * 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; - type RenderFileCallback = (err: Error, str?: string) => T; - function renderFile(path: string, cb: RenderFileCallback): T; - function renderFile(path: string, data: Data, cb: RenderFileCallback): T; - function renderFile(path: string, data: Data, opts: Options, cb: RenderFileCallback): T; +export type RenderFileCallback = (err: Error, str?: string) => T; - function clearCache(): any; +/** + * Render an EJS file at the given `path` and callback `cb(err, str)`. + * + * 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 renderFile(path: string, cb: RenderFileCallback): T; +export function renderFile(path: string, data: Data, cb: RenderFileCallback): T; +export function renderFile(path: string, data: Data, opts: Options, cb: RenderFileCallback): T; - interface TemplateFunction { - (data: Data): any; - } - interface Options { - cache?: any; - filename?: string; - context?: any; - compileDebug?: boolean; - client?: boolean; - delimiter?: string; - debug?: any; - _with?: boolean; - } - class Template { - constructor(text: string, opts: Options); - opts: Options; - templateText: string; - mode: string; - truncate: boolean; - currentLine: number; - source: string; - dependencies: Dependencies; - createRegex(): RegExp; - compile(): TemplateFunction; - generateSource(): any; - parseTemplateText(): string[]; - scanLine(line: string): any; +/** + * Clear intermediate JavaScript cache. Calls {@link Cache#reset}. + */ +export function clearCache(): void; - } - namespace Template { - interface MODES { - EVAL: string; - ESCAPED: string; - RAW: string; - COMMENT: string; - LITERAL: string; - } - } - function escapeRegexChars(s: string): string; - function escapeXML(markup: string): string; - function shallowCopy(to: T1, fro: any): T1; - interface Cache { - _data: { [name: string]: any }; - set(key: string, val: any): any; - get(key: string): any; - } - function resolve(from1: string, to: string): string; - function resolve(from1: string, from2: string, to: string): string; - function resolve(from1: string, from2: string, from3: string, to: string): string; - function resolve(from1: string, from2: string, from3: string, from4: string, to: string): string; - function resolve(from1: string, from2: string, from3: string, from4: string, from5: string, to: string): string; - function resolve(from1: string, from2: string, from3: string, from4: string, from5: string, from6: string, to: string): string; - function resolve(from1: string, from2: string, from3: string, from4: string, from5: string, from6: string, from7: string, to: string): string; - function resolve(from1: string, from2: string, from3: string, from4: string, from5: string, from6: string, from7: string, from8: string, to: string): string; - function resolve(from1: string, from2: string, from3: string, from4: string, from5: string, from6: string, from7: string, from8: string, from9: string, to: string): string; - function resolve(...args: string[]): string; - function normalize(path: string): string; - function isAbsolute(path: string): boolean; - function join(...args: string[]): string; - function relative(from: string, to: string): string; - var sep: string; - var delimiter: string; - function dirname(path: string): string; - function basename(path: string): string; - function extname(path: string): string; - function filter(xs: any, f: any): any; // TODO WHUT? +export type TemplateFunction = (data?: Data) => string; +export interface Options { + /** Compiled functions are cached, requires `filename` */ + cache?: boolean; + /** + * The name of the file being rendered. Not required if you are using `renderFile()`. + * Used by `cache` to key caches, and for includes. + */ + filename?: string; + /** Set project root for includes with an absolute path (/file.ejs). */ + root?: string; + /** Function execution context */ + context?: any; + /** When `false` no debug instrumentation is compiled */ + compileDebug?: boolean; + /** When `true`, compiles a function that can be rendered in the browser without needing to load the EJS Runtime (ejs.min.js). */ + client?: boolean; + /** Character to use with angle brackets for open/close */ + delimiter?: string; + /** Output generated function body */ + debug?: boolean; + /** When set to `true`, generated function is in strict mode */ + strict?: boolean; + /** + * Whether or not to use `with() {}` constructs. + * If `false` then the locals will be stored in the `locals` object. Set to `false` in strict mode. + */ + _with?: boolean; + /** Name to use for the object storing local variables when not using `with` Defaults to `locals` */ + localsName?: string; + /** + * Remove all safe-to-remove whitespace, including leading and trailing whitespace. + * It also enables a safer version of `-%>` line slurping for all scriptlet tags (it does not strip new lines of tags in the middle of a line). + */ + rmWhitespace?: boolean; + /** + * The escaping function used with `<%=` construct. + * It is used in rendering and is `.toString()`ed in the generation of client functions. + * (By default escapes XML). + */ + escape?(str: string): string; } -export = Ejs; +export function escapeRegexChars(s: string): string; +/** + * Escape characters reserved in XML. + * + * This is simply an export of {@link module:utils.escapeXML}. + * + * If `markup` is `undefined` or `null`, the empty string is returned. + */ +export function escapeXML(markup: string): string; +export interface Cache { + set(key: string, val: TemplateFunction): void; + get(key: string): TemplateFunction; +} +export let delimiter: string; + +/** + * Custom file loader. Useful for template preprocessing or restricting access + * to a certain part of the filesystem. + */ +export function fileLoader(filePath: string): string; + +/** + * Name for detection of EJS. + */ +export const name = "ejs"; diff --git a/types/ejs/tslint.json b/types/ejs/tslint.json index a41bf5d19a..3db14f85ea 100644 --- a/types/ejs/tslint.json +++ b/types/ejs/tslint.json @@ -1,79 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } -} +{ "extends": "dtslint/dt.json" }