From 8143c162e721261a3ab6626e003882ec89ceaddb Mon Sep 17 00:00:00 2001 From: CodeAnimal Date: Wed, 17 Sep 2014 01:00:47 +0100 Subject: [PATCH 1/5] Update Swig to latest version The swig definitions have been totally rewritten to the latest version as of time of commit. See: http://paularmstrong.github.io/swig/docs/api/ for how the API should work. --- swig/swig-tests.ts | 65 +++++++++++++++---- swig/swig.d.ts | 156 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 188 insertions(+), 33 deletions(-) diff --git a/swig/swig-tests.ts b/swig/swig-tests.ts index 1f68aa44c..7a6be47f8 100644 --- a/swig/swig-tests.ts +++ b/swig/swig-tests.ts @@ -2,24 +2,61 @@ import swig = require('swig'); +var swigObj: swig.Swig; var value: any; var str: string; var num: number; var bool: boolean; -var opts: swig.Options = { - allowErrors: bool, - autoescape: bool, - cache: bool, - encoding: str, - filters: value, - root: str, - tags: value, - extensions: value, - tzOffset: num +function replaceMs(input:string): string { + return input.replace(/m/g, 'f'); +} + +var opts: swig.SwigOptions = { + autoescape: bool, + cache: value, + cmtControls: ["", ""], + loader: swig.loaders.fs(), + locals: { testVal: "my test" }, + tagControls: ["", ""], + varControls: ["", ""] }; -swig.init(opts); -value = swig.compileFile(str); -value = swig.compile(str); -value = swig.compile(str, opts); +swig.setDefaults(opts); +swig.setDefaultTZOffset(num); +swigObj = new swig.Swig(opts); +str = swig.compileFile(str)(); +swig.compileFile(str, {}, function (err: any, render: (locals: any) => string) { + str = render({}); +}); +str = swig.compile(str)(); +str = swig.compile(str, opts)(); +swig.invalidateCache(); +swig.setFilter("replaceMs", replaceMs); +swig.setTag('tacos', function (): boolean { return true; }, function (): string { return 'Tacos' }, true, true); +swig.setExtension("test", function (v: string) { return 'test: ' + v }); +value = swig.precompile(str, opts); +str = swig.render(str, opts); +str = swig.renderFile(str, {}); +swig.renderFile(str, {}, function (err, output: string) { + +}); +console.log(swig.version); + + +str = swigObj.compileFile(str)(); +swigObj.compileFile(str, opts, function (err: any, render: (locals: any) => string) { + str = render({}); +}); +str = swigObj.compile(str)(); +str = swigObj.compile(str, opts)(); +swigObj.invalidateCache(); +swigObj.setFilter("replaceMs", replaceMs); +swigObj.setTag('tacos', function (): boolean { return true; }, function (): string { return 'Tacos' }, true, true); +swigObj.setExtension("test", function (v: string) { return 'test: ' + v }); +value = swigObj.precompile(str, opts); +str = swigObj.render(str, opts); +str = swigObj.renderFile(str, {}); +swigObj.renderFile(str, {}, function (err, output: string) { + +}); \ No newline at end of file diff --git a/swig/swig.d.ts b/swig/swig.d.ts index 4d8bc9ebe..140d287f7 100644 --- a/swig/swig.d.ts +++ b/swig/swig.d.ts @@ -1,27 +1,145 @@ // Type definitions for swig // Project: http://github.com/paularmstrong/swig -// Definitions by: Carlos Ballesteros Velasco +// Definitions by: Peter Harris // Definitions: https://github.com/borisyankov/DefinitelyTyped -// Imported from: https://github.com/soywiz/typescript-node-definitions/swig.d.ts - // API Documentation : http://paularmstrong.github.io/swig/docs/api/ declare module "swig" { - export function init(options: Options): void; - export function compileFile(filepath: string): any; - export function compile(source: string, options?: Options): any; - export function renderFile(pathName: string, locals?: any, cb?: (err: any, output: string) => void): string; + export class Swig { + constructor(options?: SwigOptions); - export interface Options { - allowErrors?: boolean; - autoescape?: boolean; - cache?: boolean; - encoding?: string; - filters?: any; - root?: string; - tags?: any; - extensions?: any; - tzOffset?: number; - } -} + setFilter( + name: string, + method: (input: any, ...args: any[]) => string + ): void; + setTag( + name: string, + parse: (str?: string, line?: string, parser?: Object, types?: TYPES, stack?: any, opts?: Object, swig?: Swig) => boolean, + compile: (compiler?: (content?: string, parents?: any, options?: any, blockName?: string) => string, args?: any[], content?: string, parents?: any, options?: any, blockName?: string) => string, + ends?: boolean, + blockLevel?: boolean + ): void; + setExtension(name: string, object: any): void; + parseFile(pathName: string, options?: any); + precompile(source: string, options?: SwigOptions): any; + compile(source: string, options?: SwigOptions): (locals?: any) => string; + compileFile(pathname: string, options: SwigOptions, cb: (err: any, compiledRender: (locals?: any) => string) => void): void; + compileFile(pathname: string, options?: SwigOptions): (locals?: any) => string; + render(source: string, options?: SwigOptions): string; + renderFile(pathName: string, locals: any, cb: (err: any, output: string) => void): void; + renderFile(pathName: string, locals?: any): string + run(templateFn: Function, locals?: any, filePath?: string): string; + invalidateCache(): void; + + loaders: typeof loaders; + } + + export interface TYPES { + /** Whitespace */ + WHITESPACE: number; + /** Plain string */ + STRING: number; + /** Variable filter */ + FILTER: number; + /** Empty variable filter */ + FILTEREMPTY: number; + /** Function */ + FUNCTION: number; + /** Function with no arguments */ + FUNCTIONEMPTY: number; + /** Open parenthesis */ + PARENOPEN: number; + /** Close parenthesis */ + PARENCLOSE: number; + /** Comma */ + COMMA: number; + /** Variable */ + VAR: number; + /** Number */ + NUMBER: number; + /** Math operator */ + OPERATOR: number; + /** Open square bracket */ + BRACKETOPEN: number; + /** Close square bracket */ + BRACKETCLOSE: number; + /** Key on an object using dot-notation */ + DOTKEY: number; + /** Start of an array */ + ARRAYOPEN: number; + /** End of an array + * Currently unused + ARRAYCLOSE: number, */ + /** Open curly brace */ + CURLYOPEN: number; + /** Close curly brace */ + CURLYCLOSE: number; + /** Colon (:) */ + COLON: number; + /** JavaScript-valid comparator */ + COMPARATOR: number; + /** Boolean logic */ + LOGIC: number; + /** Boolean logic "not" */ + NOT: number; + /** true or false */ + BOOL: number; + /** Variable assignment */ + ASSIGNMENT: number; + /** Start of a method */ + METHODOPEN: number; + /** End of a method + * Currently unused + METHODEND: 26, */ + /** Unknown type */ + UNKNOWN: number + } + + export interface SwigOptions { + autoescape?: boolean; + cache?: any; + cmtControls?: string[]; + loader?: TemplateLoader; + locals?: any; + tagControls?: string[]; + varControls?: string[]; + } + + export interface TemplateLoader { + resolve(to: string, from: string): string; + load(identifier, callback?: (err: any, data: any) => void): void; + load(identifier): any; + } + + export module loaders { + export function fs(basepath?: string, encoding?: string): TemplateLoader; + export function memory(mapping: any, basepath?: string): TemplateLoader; + } + + export var version: string; + export function setDefaults(options: SwigOptions): void; + export function setDefaultTZOffset(offset: number): void; + export function setFilter( + name: string, + method: (input: any, ...args: any[]) => string + ): void; + export function setTag( + name: string, + parse: (str?: string, line?: string, parser?: Object, types?: TYPES, stack?: any, opts?: Object, swig?: Swig) => boolean, + compile: (compiler?: (content?: string, parents?: any, options?: any, blockName?: string) => string, args?: any[], content?: string, parents?: any, options?: any, blockName?: string) => string, + ends?: boolean, + blockLevel?: boolean + ): void; + export function setExtension(name: string, object: any): void; + export function parseFile(pathName: string, options?: any); + export function precompile(source: string, options?: SwigOptions): any; + export function compile(source: string, options?: SwigOptions): (locals?: any) => string; + export function compileFile(pathname: string, options: SwigOptions, cb: (err: any, compiledRender: (locals?: any) => string) => void): void; + export function compileFile(pathname: string, options?: SwigOptions): (locals?: any) => string; + export function render(source: string, options?: SwigOptions): string; + export function renderFile(pathName: string, locals: any, cb: (err: any, output: string) => void): void; + export function renderFile(pathName: string, locals?: any): string + export function run(templateFn: Function, locals?: any, filePath?: string): string; + export function invalidateCache(): void; +} \ No newline at end of file From 11e519fc6e06fda5dd6ca831e8ae4f92e11ef9c3 Mon Sep 17 00:00:00 2001 From: CodeAnimal Date: Wed, 17 Sep 2014 10:18:44 +0100 Subject: [PATCH 2/5] Change TYPES to now be an enum TYPES used to be an interface in the swig module. It has now been moved to a new module, swig.lexer, and set as an enum type. --- swig/swig.d.ts | 126 +++++++++++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 61 deletions(-) diff --git a/swig/swig.d.ts b/swig/swig.d.ts index 140d287f7..88136162e 100644 --- a/swig/swig.d.ts +++ b/swig/swig.d.ts @@ -15,7 +15,7 @@ declare module "swig" { ): void; setTag( name: string, - parse: (str?: string, line?: string, parser?: Object, types?: TYPES, stack?: any, opts?: Object, swig?: Swig) => boolean, + parse: (str?: string, line?: string, parser?: Object, types?: lexer.TYPES, stack?: any, opts?: Object, swig?: Swig) => boolean, compile: (compiler?: (content?: string, parents?: any, options?: any, blockName?: string) => string, args?: any[], content?: string, parents?: any, options?: any, blockName?: string) => string, ends?: boolean, blockLevel?: boolean @@ -35,65 +35,69 @@ declare module "swig" { loaders: typeof loaders; } - export interface TYPES { - /** Whitespace */ - WHITESPACE: number; - /** Plain string */ - STRING: number; - /** Variable filter */ - FILTER: number; - /** Empty variable filter */ - FILTEREMPTY: number; - /** Function */ - FUNCTION: number; - /** Function with no arguments */ - FUNCTIONEMPTY: number; - /** Open parenthesis */ - PARENOPEN: number; - /** Close parenthesis */ - PARENCLOSE: number; - /** Comma */ - COMMA: number; - /** Variable */ - VAR: number; - /** Number */ - NUMBER: number; - /** Math operator */ - OPERATOR: number; - /** Open square bracket */ - BRACKETOPEN: number; - /** Close square bracket */ - BRACKETCLOSE: number; - /** Key on an object using dot-notation */ - DOTKEY: number; - /** Start of an array */ - ARRAYOPEN: number; - /** End of an array - * Currently unused - ARRAYCLOSE: number, */ - /** Open curly brace */ - CURLYOPEN: number; - /** Close curly brace */ - CURLYCLOSE: number; - /** Colon (:) */ - COLON: number; - /** JavaScript-valid comparator */ - COMPARATOR: number; - /** Boolean logic */ - LOGIC: number; - /** Boolean logic "not" */ - NOT: number; - /** true or false */ - BOOL: number; - /** Variable assignment */ - ASSIGNMENT: number; - /** Start of a method */ - METHODOPEN: number; - /** End of a method - * Currently unused - METHODEND: 26, */ - /** Unknown type */ - UNKNOWN: number + export module lexer { + export enum TYPES { + /** Whitespace */ + WHITESPACE = 0, + /** Plain string */ + STRING = 1, + /** Variable filter */ + FILTER = 2, + /** Empty variable filter */ + FILTEREMPTY = 3, + /** Function */ + FUNCTION = 4, + /** Function with no arguments */ + FUNCTIONEMPTY = 5, + /** Open parenthesis */ + PARENOPEN = 6, + /** Close parenthesis */ + PARENCLOSE = 7, + /** Comma */ + COMMA = 8, + /** Variable */ + VAR = 9, + /** Number */ + NUMBER = 10, + /** Math operator */ + OPERATOR = 11, + /** Open square bracket */ + BRACKETOPEN = 12, + /** Close square bracket */ + BRACKETCLOSE = 13, + /** Key on an object using dot-notation */ + DOTKEY = 14, + /** Start of an array */ + ARRAYOPEN = 15, + /** End of an array + * Currently unused + ARRAYCLOSE = 16, */ + /** Open curly brace */ + CURLYOPEN = 17, + /** Close curly brace */ + CURLYCLOSE = 18, + /** Colon (:) */ + COLON = 19, + /** JavaScript-valid comparator */ + COMPARATOR = 20, + /** Boolean logic */ + LOGIC = 21, + /** Boolean logic "not" */ + NOT = 22, + /** true or false */ + BOOL = 23, + /** Variable assignment */ + ASSIGNMENT = 24, + /** Start of a method */ + METHODOPEN = 25, + /** End of a method + * Currently unused + METHODEND = 26, */ + /** Unknown type */ + UNKNOWN = 100 + } + + export function read(str: string): string[]; } export interface SwigOptions { @@ -126,7 +130,7 @@ declare module "swig" { ): void; export function setTag( name: string, - parse: (str?: string, line?: string, parser?: Object, types?: TYPES, stack?: any, opts?: Object, swig?: Swig) => boolean, + parse: (str?: string, line?: string, parser?: Object, types?: lexer.TYPES, stack?: any, opts?: Object, swig?: Swig) => boolean, compile: (compiler?: (content?: string, parents?: any, options?: any, blockName?: string) => string, args?: any[], content?: string, parents?: any, options?: any, blockName?: string) => string, ends?: boolean, blockLevel?: boolean From b8afdd76d784eb329d8834878402f79031609500 Mon Sep 17 00:00:00 2001 From: CodeAnimal Date: Wed, 17 Sep 2014 10:57:43 +0100 Subject: [PATCH 3/5] Fix build errors Fixed the Travis CI build errors stated. --- swig/swig-tests.ts | 8 ++++---- swig/swig.d.ts | 23 ++++++++++++++++------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/swig/swig-tests.ts b/swig/swig-tests.ts index 7a6be47f8..929a67c89 100644 --- a/swig/swig-tests.ts +++ b/swig/swig-tests.ts @@ -26,7 +26,7 @@ swig.setDefaults(opts); swig.setDefaultTZOffset(num); swigObj = new swig.Swig(opts); str = swig.compileFile(str)(); -swig.compileFile(str, {}, function (err: any, render: (locals: any) => string) { +swig.compileFile(str, {}, function (err: Error, render: (locals: any) => string) { str = render({}); }); str = swig.compile(str)(); @@ -38,14 +38,14 @@ swig.setExtension("test", function (v: string) { return 'test: ' + v }); value = swig.precompile(str, opts); str = swig.render(str, opts); str = swig.renderFile(str, {}); -swig.renderFile(str, {}, function (err, output: string) { +swig.renderFile(str, {}, function (err: Error, output: string) { }); console.log(swig.version); str = swigObj.compileFile(str)(); -swigObj.compileFile(str, opts, function (err: any, render: (locals: any) => string) { +swigObj.compileFile(str, opts, function (err: Error, render: (locals: any) => string) { str = render({}); }); str = swigObj.compile(str)(); @@ -57,6 +57,6 @@ swigObj.setExtension("test", function (v: string) { return 'test: ' + v }); value = swigObj.precompile(str, opts); str = swigObj.render(str, opts); str = swigObj.renderFile(str, {}); -swigObj.renderFile(str, {}, function (err, output: string) { +swigObj.renderFile(str, {}, function (err: Error, output: string) { }); \ No newline at end of file diff --git a/swig/swig.d.ts b/swig/swig.d.ts index 88136162e..4ac885963 100644 --- a/swig/swig.d.ts +++ b/swig/swig.d.ts @@ -21,13 +21,13 @@ declare module "swig" { blockLevel?: boolean ): void; setExtension(name: string, object: any): void; - parseFile(pathName: string, options?: any); + parseFile(pathName: string, options?: any): parser.ParseReturn; precompile(source: string, options?: SwigOptions): any; compile(source: string, options?: SwigOptions): (locals?: any) => string; - compileFile(pathname: string, options: SwigOptions, cb: (err: any, compiledRender: (locals?: any) => string) => void): void; + compileFile(pathname: string, options: SwigOptions, cb: (err: Error, compiledRender: (locals?: any) => string) => void): void; compileFile(pathname: string, options?: SwigOptions): (locals?: any) => string; render(source: string, options?: SwigOptions): string; - renderFile(pathName: string, locals: any, cb: (err: any, output: string) => void): void; + renderFile(pathName: string, locals: any, cb: (err: Error, output: string) => void): void; renderFile(pathName: string, locals?: any): string run(templateFn: Function, locals?: any, filePath?: string): string; invalidateCache(): void; @@ -100,6 +100,15 @@ declare module "swig" { export function read(str: string): string[]; } + export module parser { + interface ParseReturn { + name: string; + parent: any; + tokens: any[]; + blocks: any; + } + } + export interface SwigOptions { autoescape?: boolean; cache?: any; @@ -112,8 +121,8 @@ declare module "swig" { export interface TemplateLoader { resolve(to: string, from: string): string; - load(identifier, callback?: (err: any, data: any) => void): void; - load(identifier): any; + load(identifier: string, callback?: (err: Error, data: any) => void): void; + load(identifier:string): any; } export module loaders { @@ -139,10 +148,10 @@ declare module "swig" { export function parseFile(pathName: string, options?: any); export function precompile(source: string, options?: SwigOptions): any; export function compile(source: string, options?: SwigOptions): (locals?: any) => string; - export function compileFile(pathname: string, options: SwigOptions, cb: (err: any, compiledRender: (locals?: any) => string) => void): void; + export function compileFile(pathname: string, options: SwigOptions, cb: (err: Error, compiledRender: (locals?: any) => string) => void): void; export function compileFile(pathname: string, options?: SwigOptions): (locals?: any) => string; export function render(source: string, options?: SwigOptions): string; - export function renderFile(pathName: string, locals: any, cb: (err: any, output: string) => void): void; + export function renderFile(pathName: string, locals: any, cb: (err: Error, output: string) => void): void; export function renderFile(pathName: string, locals?: any): string export function run(templateFn: Function, locals?: any, filePath?: string): string; export function invalidateCache(): void; From cf18b507b1e487a09a732e81382f7582f4673718 Mon Sep 17 00:00:00 2001 From: CodeAnimal Date: Wed, 17 Sep 2014 11:01:49 +0100 Subject: [PATCH 4/5] Add return type to static parseFile function. --- swig/swig.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swig/swig.d.ts b/swig/swig.d.ts index 4ac885963..4626fb9b9 100644 --- a/swig/swig.d.ts +++ b/swig/swig.d.ts @@ -145,7 +145,7 @@ declare module "swig" { blockLevel?: boolean ): void; export function setExtension(name: string, object: any): void; - export function parseFile(pathName: string, options?: any); + export function parseFile(pathName: string, options?: any): parser.ParseReturn; export function precompile(source: string, options?: SwigOptions): any; export function compile(source: string, options?: SwigOptions): (locals?: any) => string; export function compileFile(pathname: string, options: SwigOptions, cb: (err: Error, compiledRender: (locals?: any) => string) => void): void; From 0e05ddf68ee5df0109c616a484d437857a572ac9 Mon Sep 17 00:00:00 2001 From: CodeAnimal Date: Thu, 25 Sep 2014 01:40:38 +0100 Subject: [PATCH 5/5] Update header info Add Carlos Ballesteros Velasco back into "Definitions by" header line. --- swig/swig.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swig/swig.d.ts b/swig/swig.d.ts index 4626fb9b9..4f2b22123 100644 --- a/swig/swig.d.ts +++ b/swig/swig.d.ts @@ -1,6 +1,6 @@ // Type definitions for swig // Project: http://github.com/paularmstrong/swig -// Definitions by: Peter Harris +// Definitions by: Peter Harris , Carlos Ballesteros Velasco // Definitions: https://github.com/borisyankov/DefinitelyTyped // API Documentation : http://paularmstrong.github.io/swig/docs/api/