diff --git a/types/nunjucks/index.d.ts b/types/nunjucks/index.d.ts index 002f8b93f8..d307bd27d5 100644 --- a/types/nunjucks/index.d.ts +++ b/types/nunjucks/index.d.ts @@ -1,148 +1,139 @@ -// Type definitions for nunjucks +// Type definitions for nunjucks 3.0 // Project: http://mozilla.github.io/nunjucks/ // Definitions by: Ruben Slabbert // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 +export function render(name: string, context?: object): string; +export function render(name: string, context?: object, callback?: (err: any, res: string) => any): void; -declare namespace Nunjucks { - function render(name: string, context?: Object): string; - function render(name: string, context?: Object, callback?: (err: any, res: string) => any): void; +export function renderString(src: string, context: object): string; +export function renderString(src: string, context: object, callback?: (err: any, res: string) => any): void; - function renderString(src: string, context: Object): string; - function renderString(src: string, context: Object, callback?: (err: any, res: string) => any): void; +export function compile(src: string, env?: Environment, callback?: (err: any, res: Template) => any): Template; - function compile(src: string, env?: Environment): Template; - function compile(src: string, env?: Environment, callback?: (err: any, res: Template) => any): Template; +export function precompile(path: string, opts?: PrecompileOptions): string; +export function precompileString(src: string, opts?: PrecompileOptions): string; - function precompile(path: string, opts?: PrecompileOptions): string; - function precompileString(src: string, opts?: PrecompileOptions): string; - - interface PrecompileOptions { - name?: string; - asFunction?: boolean; - force?: boolean; - env?: Environment; - include?: string[]; - exclude?: string[]; - wrapper?(templates: { name: string, template: string }, opts: PrecompileOptions): string; - } - - class Template { - constructor(src: string, env?: Environment, eagerCompile?: boolean); - render(context?: Object): string; - render(context?: Object, callback?: (err: any, res: string) => any): void; - } - - function configure(options: ConfigureOptions): Environment; - function configure(path: string, options?: ConfigureOptions): Environment; - function configure(path: string[], options?: ConfigureOptions): Environment; - - interface ConfigureOptions { - autoescape?: boolean; - throwOnUndefined?: boolean; - trimBlocks?: boolean; - lstripBlocks?: boolean; - watch?: boolean; - noCache?: boolean; - web?: { - useCache?: boolean, - async?: boolean - }; - express?: Object; - tags?: { - blockStart?: string, - blockEnd?: string, - variableStart?: string, - variableEnd?: string, - commentStart?: string, - commentEnd?: string - }; - } - - class Environment { - options: { - autoescape: boolean; - }; - - constructor(); - constructor(loader: ILoader, opts?: ConfigureOptions); - constructor(loaders: ILoader[], opts?: ConfigureOptions); - - render(name: string, context?: Object): string; - render(name: string, context?: Object, callback?: (err: any, res: string) => any): void; - - renderString(name: string, context: Object): string; - renderString(name: string, context: Object, callback?: (err: any, res: string) => any): void; - - addFilter(name: string, func: (...args: any[]) => any, async?: boolean): void; - getFilter(name: string): void; - - addExtension(name: string, ext: Extension): void; - removeExtension(name: string): void; - getExtension(name: string): Extension; - hasExtension(name: string): void; - - addGlobal(name: string, value: any): void; - - getTemplate(name: string, eagerCompile?: boolean): Template; - getTemplate(name: string, eagerCompile?: boolean, callback?: (err: any, templ: Template) => Template): void; - - express(app: Object): void; - } - - interface Extension { - tags: string[]; - // Parser API is undocumented it is suggested to check the source: https://github.com/mozilla/nunjucks/blob/master/src/parser.js - parse(parser: any, nodes: any, lexer: any): any; - } - - function installJinjaCompat(): void; - - interface ILoader { - async?: boolean; - getSource(name: string): LoaderSource; - extend?(extender: ILoader): ILoader; - } - - // Needs both Loader and ILoader since nunjucks uses a custom object system - // Object system is also responsible for the extend methods - class Loader { - on(name: string, func: (...args: any[]) => any): void; - emit(name: string, ...args: any[]): void; - resolve(from: string, to: string): string; - isRelative(filename: string): boolean; - extend(toExtend: ILoader): ILoader; - } - - interface LoaderSource { - src: string; - path: string; - noCache: boolean; - } - - interface FileSystemLoaderOptions { - /** if true, the system will automatically update templates when they are changed on the filesystem */ - watch?: boolean; - - /** if true, the system will avoid using a cache and templates will be recompiled every single time */ - noCache?: boolean; - } - - class FileSystemLoader extends Loader implements ILoader { - init(searchPaths: string[], opts: any): void; - getSource(name: string): LoaderSource; - constructor(searchPaths?: string | string[], opts?: FileSystemLoaderOptions); - } - - export class WebLoader implements ILoader { - constructor(baseUrl: string, opts?: any); - getSource(name: string): LoaderSource; - } - - class PrecompiledLoader extends Loader implements ILoader { - init(searchPaths: string[], opts: any): void; - getSource(name: string): LoaderSource; - } +export interface PrecompileOptions { + name?: string; + asFunction?: boolean; + force?: boolean; + env?: Environment; + include?: string[]; + exclude?: string[]; + wrapper?(templates: { name: string, template: string }, opts: PrecompileOptions): string; } -export = Nunjucks; +export class Template { + constructor(src: string, env?: Environment, eagerCompile?: boolean); + render(context?: object): string; + render(context?: object, callback?: (err: any, res: string) => any): void; +} + +export function configure(options: ConfigureOptions): Environment; +export function configure(path: string | string[], options?: ConfigureOptions): Environment; + +export interface ConfigureOptions { + autoescape?: boolean; + throwOnUndefined?: boolean; + trimBlocks?: boolean; + lstripBlocks?: boolean; + watch?: boolean; + noCache?: boolean; + web?: { + useCache?: boolean, + async?: boolean + }; + express?: object; + tags?: { + blockStart?: string, + blockEnd?: string, + variableStart?: string, + variableEnd?: string, + commentStart?: string, + commentEnd?: string + }; +} + +export class Environment { + options: { + autoescape: boolean; + }; + + constructor(loader?: ILoader | ILoader[] | null, opts?: ConfigureOptions); + render(name: string, context?: object): string; + render(name: string, context?: object, callback?: (err: any, res: string) => any): void; + + renderString(name: string, context: object): string; + renderString(name: string, context: object, callback?: (err: any, res: string) => any): void; + + addFilter(name: string, func: (...args: any[]) => any, async?: boolean): void; + getFilter(name: string): void; + + addExtension(name: string, ext: Extension): void; + removeExtension(name: string): void; + getExtension(name: string): Extension; + hasExtension(name: string): void; + + addGlobal(name: string, value: any): void; + + getTemplate(name: string, eagerCompile?: boolean): Template; + getTemplate(name: string, eagerCompile?: boolean, callback?: (err: any, templ: Template) => Template): void; + + express(app: object): void; +} + +export interface Extension { + tags: string[]; + // Parser API is undocumented it is suggested to check the source: https://github.com/mozilla/nunjucks/blob/master/src/parser.js + parse(parser: any, nodes: any, lexer: any): any; +} + +export function installJinjaCompat(): void; + +export interface ILoader { + async?: boolean; + getSource(name: string): LoaderSource; + extend?(extender: ILoader): ILoader; +} + +// Needs both Loader and ILoader since nunjucks uses a custom object system +// Object system is also responsible for the extend methods +export class Loader { + on(name: string, func: (...args: any[]) => any): void; + emit(name: string, ...args: any[]): void; + resolve(from: string, to: string): string; + isRelative(filename: string): boolean; + extend(toExtend: ILoader): ILoader; +} + +export interface LoaderSource { + src: string; + path: string; + noCache: boolean; +} + +export interface FileSystemLoaderOptions { + /** if true, the system will automatically update templates when they are changed on the filesystem */ + watch?: boolean; + + /** if true, the system will avoid using a cache and templates will be recompiled every single time */ + noCache?: boolean; +} + +export class FileSystemLoader extends Loader implements ILoader { + init(searchPaths: string[], opts: any): void; + getSource(name: string): LoaderSource; + constructor(searchPaths?: string | string[], opts?: FileSystemLoaderOptions); +} + +export class WebLoader implements ILoader { + constructor(baseUrl: string, opts?: any); + getSource(name: string): LoaderSource; +} + +export class PrecompiledLoader extends Loader implements ILoader { + init(searchPaths: string[], opts: any): void; + getSource(name: string): LoaderSource; +} diff --git a/types/nunjucks/nunjucks-tests.ts b/types/nunjucks/nunjucks-tests.ts index 7407cc65ba..9f3afcddfe 100644 --- a/types/nunjucks/nunjucks-tests.ts +++ b/types/nunjucks/nunjucks-tests.ts @@ -1,27 +1,35 @@ - import nunjucks = require("nunjucks"); - nunjucks.configure({ autoescape: false }); let rendered = nunjucks.render("./noexists.html"); -let ctx = { items: ["Hello", "this", "is", "for", "testing"] }; -let src = "{% for item in items %}{{item}}{% endfor %}"; +nunjucks.render('foo.html', { username: 'James' }); +nunjucks.render('async.html', (err: any, res: string) => {}); + +const ctx = { items: ["Hello", "this", "is", "for", "testing"] }; +const src = "{% for item in items %}{{item}}{% endfor %}"; rendered = nunjucks.renderString(src, ctx); +nunjucks.renderString('Hello {{ username }}', { username: 'James' }); -let compiled = nunjucks.compile(src); +const compiled = nunjucks.compile(src); rendered = compiled.render(ctx); +nunjucks.compile('Hello {{ username }}').render({ username: 'James' }); rendered = nunjucks.precompileString(src, { name: "TestyWesty" }); -let template = new nunjucks.Template(src); +const template = new nunjucks.Template(src); rendered = template.render(ctx); let env = nunjucks.configure({ autoescape: false }); +nunjucks.configure('/views'); +nunjucks.configure('views', { + autoescape: true, + watch: true +}); rendered = env.renderString(src, ctx); env.addExtension("SpawnGlitter", { diff --git a/types/nunjucks/tsconfig.json b/types/nunjucks/tsconfig.json index 39cf9bfbca..5824ce2f1f 100644 --- a/types/nunjucks/tsconfig.json +++ b/types/nunjucks/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ @@ -20,4 +20,4 @@ "index.d.ts", "nunjucks-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/nunjucks/tslint.json b/types/nunjucks/tslint.json index a41bf5d19a..2c7c1bed53 100644 --- a/types/nunjucks/tslint.json +++ b/types/nunjucks/tslint.json @@ -1,79 +1,6 @@ { "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 + "interface-name": false } }