diff --git a/app/templating/index.js b/app/templating/index.js index 68c6fc000..3b7e35297 100644 --- a/app/templating/index.js +++ b/app/templating/index.js @@ -1,7 +1,23 @@ +// @flow import nunjucks from 'nunjucks'; import * as extensions from './extensions'; import BaseExtension from './base-extension'; +export class RenderError extends Error { + message: string; + path: string | null; + location: {line: number, column: number}; + type: string; + reason: string; +} + +export type NunjucksTag = { + name: string, + displayName: string, + description: string, + args: Array +}; + // Some constants export const RENDER_ALL = 'all'; export const RENDER_VARS = 'variables'; @@ -20,7 +36,7 @@ let nunjucksAll = null; * @param {Object} [config.path] - Path to include in the error message * @param {Object} [config.renderMode] - Only render variables (not tags) */ -export function render (text, config = {}) { +export function render (text: string, config: Object = {}): Promise { const context = config.context || {}; const path = config.path || null; const renderMode = config.renderMode || RENDER_ALL; @@ -43,7 +59,7 @@ export function render (text, config = {}) { ? 'undefined' : 'error'; - const newError = new Error(sanitizedMsg); + const newError = new RenderError(sanitizedMsg); newError.path = path || null; newError.message = sanitizedMsg; newError.location = {line, column}; @@ -60,7 +76,7 @@ export function render (text, config = {}) { /** * Reload Nunjucks environments. Useful for if plugins change. */ -export function reload () { +export function reload (): void { nunjucksAll = null; nunjucksVariablesOnly = null; nunjucksTagsOnly = null; diff --git a/flow-typed/nunjucks.js b/flow-typed/nunjucks.js new file mode 100644 index 000000000..fe08fd8b3 --- /dev/null +++ b/flow-typed/nunjucks.js @@ -0,0 +1,25 @@ +declare class Nunjucks { + extensions: {[string]: T}, + addExtension: (name: string, ext: T) => void, + addFilter: (name: string, ext: Function) => void, + renderString: (text: string, context: Object, callback: Function) => void +} + +declare type Config = { + autoescape: boolean, + throwOnUndefined: boolean, + tags: { + blockStart: string, + blockEnd: string, + variableStart: string, + variableEnd: string, + commentStart: string, + commentEnd: string + } +}; + +declare module 'nunjucks' { + declare module.exports: { + configure: (config: Config) => Nunjucks + } +}