Added flow to templating index

This commit is contained in:
Gregory Schier 2017-07-19 11:56:51 -07:00
parent 01e10138ee
commit 0902aa6ef9
2 changed files with 44 additions and 3 deletions

View File

@ -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<Object>
};
// 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<string> {
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;

25
flow-typed/nunjucks.js vendored Normal file
View File

@ -0,0 +1,25 @@
declare class Nunjucks<T> {
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: <T>(config: Config) => Nunjucks<T>
}
}