├── .gitignore ├── LICENSE ├── README.md ├── app.js ├── index.js ├── lib └── ast-helper.js ├── package.json └── tests ├── defs ├── bcrypt.d.ts ├── bcrypt.js.flow ├── deep-diff.d.ts └── deep-diff.js.flow └── runner.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### DEPRECATED 2 | 3 | Please see https://github.com/joarwilk/typedef-converter for a Flow -> Typescript definition converter. 4 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | "use strict"; 3 | 4 | var fs = require("fs"); 5 | var nopt = require("nopt"); 6 | var path = require("path"); 7 | var probablyTyped = require("./index"); 8 | 9 | var opts = nopt({ 10 | "typescript": path, 11 | "output": path, 12 | "help": Boolean, 13 | }, { 14 | "t": "--typescript", 15 | "o": "--output", 16 | "h": "--help", 17 | }); 18 | 19 | if (opts.typescript) { 20 | var out = probablyTyped(fs.readFileSync(opts.typescript, "utf8")); 21 | if (opts.output) { 22 | fs.writeFileSync(opts.output, out); 23 | } 24 | else { 25 | console.log(out); 26 | } 27 | } 28 | else { 29 | console.log("Converts Typescript declaration files into Flow library definitions"); 30 | console.log("Usage:"); 31 | console.log(" probably-flow-typed -t FILEPATH [-o FILEPATH]"); 32 | console.log("Options:"); 33 | console.log(" -t, --typescript FILEPATH Required. The .d.ts file to convert."); 34 | console.log(" -o, --output FILEPATH The file to output the Flow library definition to. If missing, output goes to stdout."); 35 | console.log(" -h, --help Show this help."); 36 | process.exit(0); 37 | } 38 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var util = require("util"); 2 | var ts = require("typescript"); 3 | var helper = require("./lib/ast-helper"); 4 | 5 | // https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions 6 | function escape(string){ 7 | return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string 8 | } 9 | 10 | function walkAst(node) { 11 | if (!node.clobber) { return; } 12 | // console.log(node.kindStr + " => " + node.source() + "\n============================"); 13 | 14 | switch (node.kind) { 15 | case ts.SyntaxKind.ExportKeyword: 16 | if (node.parent && node.parent.kind === ts.SyntaxKind.FunctionDeclaration) { 17 | node.clobber(node.source().replace("export", "declare")); 18 | } 19 | break; 20 | } 21 | } 22 | 23 | 24 | module.exports = function(input, opts) { 25 | // Use typescript itself to extract the AST 26 | var sourceFile = ts.createSourceFile("tmp.d.ts", input, ts.ScriptTarget.ES6, true); 27 | // convert it to a flow library definition 28 | var flowLibDef = helper(sourceFile.text, sourceFile, walkAst); 29 | // use whatever line endings the file uses. 30 | var lineEnding = "\n"; 31 | var nlIndex = sourceFile.text.indexOf("\n"); 32 | if (nlIndex > 0 && sourceFile.text[nlIndex-1] === "\r") { 33 | lineEnding = "\r\n"; 34 | } 35 | 36 | // remove all blank lines: This makes testing output a bit nicer 37 | flowLibDef = flowLibDef.split("\n").filter(function(line) { 38 | return line.trim().length > 0; 39 | }).map(function(line) { 40 | // trim all trailing whitespace (also kills stray \r's) 41 | return line.trimRight(); // Node seems to have this :D 42 | }).join(lineEnding); 43 | return "// @flow\n// Auto-generated by ProbablyFlowTyped: https://github.com/Kegsay/ProbablyFlowTyped\n" + flowLibDef; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /lib/ast-helper.js: -------------------------------------------------------------------------------- 1 | // help modify the AST by adding a clobber() function to Nodes 2 | var typescript = require("typescript"); 3 | 4 | var reverseKinds = Object.create(null); 5 | Object.keys(typescript.SyntaxKind).forEach(function(k) { 6 | reverseKinds[typescript.SyntaxKind[k]] = k; 7 | }); 8 | 9 | function insertHelpers(node, chunks) { 10 | if (node.pos == null || node.end == null) { 11 | console.log("Skipping kind: " + reverseKinds[node.kind]); 12 | return; 13 | } 14 | if (node.source) { throw new Error("source property already exists"); } 15 | 16 | // debugging 17 | if (node.kind) { 18 | node.kindStr = reverseKinds[node.kind]; 19 | } 20 | 21 | // The source of the node is between the two parsed AST indexes 22 | node.source = function() { 23 | return chunks.slice(node.pos, node.end).join(""); 24 | }; 25 | 26 | // Replace a node's text. Blanks out the elements corresponding to this chunk and replaces it 27 | // with the new text (it's fine that the new text is a string and not a char) 28 | node.clobber = function(newText) { 29 | chunks[node.pos] = newText; 30 | for (var i = node.pos + 1; i < node.end; i++) { 31 | chunks[i] = ""; 32 | } 33 | }; 34 | } 35 | 36 | module.exports = function(rawSource, programNode, fn) { 37 | var chunks = rawSource.split(""); // string -> array 38 | 39 | // Decorate each node with a source() and clobber() function 40 | function walk(node) { 41 | insertHelpers(node, chunks); 42 | typescript.forEachChild(node, walk); 43 | fn(node); 44 | } 45 | 46 | walk(programNode); 47 | 48 | return chunks.join(""); 49 | }; 50 | 51 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "probably-flow-typed", 3 | "version": "0.0.1", 4 | "description": "Converts Typescript declaration files into Flow library definitions", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node tests/runner.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/Kegsay/ProbablyFlowTyped.git" 12 | }, 13 | "keywords": [ 14 | "flow", 15 | "typescript", 16 | "flowtype", 17 | "definitions", 18 | "utility" 19 | ], 20 | "author": "Kegan Dougal", 21 | "license": "Apache-2.0", 22 | "bugs": { 23 | "url": "https://github.com/Kegsay/ProbablyFlowTyped/issues" 24 | }, 25 | "bin": { 26 | "probably-flow-typed": "./app.js" 27 | }, 28 | "homepage": "https://github.com/Kegsay/ProbablyFlowTyped#readme", 29 | "dependencies": { 30 | "glob": "^7.1.1", 31 | "nopt": "^3.0.6", 32 | "typescript": "^2.0.3" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/defs/bcrypt.d.ts: -------------------------------------------------------------------------------- 1 | // Type definitions for bcrypt 2 | // Project: https://www.npmjs.org/package/bcrypt 3 | // Definitions by: Peter Harris 4 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped 5 | 6 | declare module "bcrypt" { 7 | /** 8 | * @param rounds The cost of processing the data. Default 10. 9 | */ 10 | export function genSaltSync(rounds?: number): string; 11 | 12 | /** 13 | * @param rounds The cost of processing the data. Default 10. 14 | * @param callback A callback to be fire once the sald has been generated. Uses eio making it asynchronous. 15 | */ 16 | export function genSalt(rounds: number, callback: (err: Error, salt: string) => void): void; 17 | /** 18 | * @param callback A callback to be fire once the sald has been generated. Uses eio making it asynchronous. 19 | */ 20 | export function genSalt(callback: (err: Error, salt: string) => void): void; 21 | 22 | /** 23 | * @param data The data to be encrypted. 24 | * @param salt The salt to be used in encryption. 25 | */ 26 | export function hashSync(data: any, salt: string): string; 27 | /** 28 | * @param data The data to be encrypted. 29 | * @param rounds A salt will be generated using the rounds specified. 30 | */ 31 | export function hashSync(data: any, rounds: number): string; 32 | 33 | /** 34 | * @param data The data to be encrypted. 35 | * @param salt The salt to be used in encryption. 36 | * @param callback A callback to be fired once the data has been encrypted. Uses eio making it asynchronous. 37 | */ 38 | export function hash(data: any, salt: string, callback: (err: Error, encrypted: string) => void): void; 39 | /** 40 | * @param data The data to be encrypted. 41 | * @param rounds A salt will be generated using the rounds specified. 42 | * @param callback A callback to be fired once the data has been encrypted. Uses eio making it asynchronous. 43 | */ 44 | export function hash(data: any, rounds: number, callback: (err: Error, encrypted: string) => void): void; 45 | 46 | /** 47 | * @param data The data to be encrypted. 48 | * @param encrypted The data to be compared against. 49 | */ 50 | export function compareSync(data: any, encrypted: string): boolean; 51 | 52 | /** 53 | * @param data The data to be encrypted. 54 | * @param encrypted The data to be compared against. 55 | * @param callback A callback to be fire once the data has been compared. Uses eio making it asynchronous. 56 | */ 57 | export function compare(data: any, encrypted: string, callback: (err: Error, same: boolean) => void): void; 58 | 59 | /** 60 | * Return the number of rounds used to encrypt a given hash 61 | * 62 | * @param encrypted Hash from which the number of rounds used should be extracted. 63 | */ 64 | export function getRounds(encrypted: string): number; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /tests/defs/bcrypt.js.flow: -------------------------------------------------------------------------------- 1 | // @flow 2 | declare module "bcrypt" { 3 | /** 4 | * @param rounds The cost of processing the data. Default 10. 5 | */ 6 | declare function genSaltSync(rounds?: number): string; 7 | /** 8 | * @param rounds The cost of processing the data. Default 10. 9 | * @param callback A callback to be fire once the sald has been generated. Uses eio making it asynchronous. 10 | */ 11 | declare function genSalt(rounds: number, callback: (err: Error, salt: string) => void): void; 12 | /** 13 | * @param callback A callback to be fire once the sald has been generated. Uses eio making it asynchronous. 14 | */ 15 | declare function genSalt(callback: (err: Error, salt: string) => void): void; 16 | /** 17 | * @param data The data to be encrypted. 18 | * @param salt The salt to be used in encryption. 19 | */ 20 | declare function hashSync(data: any, salt: string): string; 21 | /** 22 | * @param data The data to be encrypted. 23 | * @param rounds A salt will be generated using the rounds specified. 24 | */ 25 | declare function hashSync(data: any, rounds: number): string; 26 | /** 27 | * @param data The data to be encrypted. 28 | * @param salt The salt to be used in encryption. 29 | * @param callback A callback to be fired once the data has been encrypted. Uses eio making it asynchronous. 30 | */ 31 | declare function hash(data: any, salt: string, callback: (err: Error, encrypted: string) => void): void; 32 | /** 33 | * @param data The data to be encrypted. 34 | * @param rounds A salt will be generated using the rounds specified. 35 | * @param callback A callback to be fired once the data has been encrypted. Uses eio making it asynchronous. 36 | */ 37 | declare function hash(data: any, rounds: number, callback: (err: Error, encrypted: string) => void): void; 38 | /** 39 | * @param data The data to be encrypted. 40 | * @param encrypted The data to be compared against. 41 | */ 42 | declare function compareSync(data: any, encrypted: string): boolean; 43 | /** 44 | * @param data The data to be encrypted. 45 | * @param encrypted The data to be compared against. 46 | * @param callback A callback to be fire once the data has been compared. Uses eio making it asynchronous. 47 | */ 48 | declare function compare(data: any, encrypted: string, callback: (err: Error, same: boolean) => void): void; 49 | /** 50 | * Return the number of rounds used to encrypt a given hash 51 | * 52 | * @param encrypted Hash from which the number of rounds used should be extracted. 53 | */ 54 | declare function getRounds(encrypted: string): number; 55 | } -------------------------------------------------------------------------------- /tests/defs/deep-diff.d.ts: -------------------------------------------------------------------------------- 1 | // Type definitions for deep-diff 2 | // Project: https://github.com/flitbit/diff/ 3 | // Definitions by: ZauberNerd 4 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped 5 | 6 | declare namespace deepDiff { 7 | interface IDiff { 8 | kind: string; 9 | path: string[]; 10 | lhs: any; 11 | rhs: any; 12 | index?: number; 13 | item?: IDiff; 14 | } 15 | 16 | interface IAccumulator { 17 | push(diff: IDiff): void; 18 | length: number; 19 | } 20 | 21 | interface IPrefilter { 22 | (path: string[], key: string): boolean; 23 | } 24 | 25 | interface IDeepDiff { 26 | diff(): IDiff; 27 | diff(lhs: Object, rhs: Object, prefilter?: IPrefilter, acc?: IAccumulator): IDiff[]; 28 | observableDiff(lhs: Object, rhs: Object, changes: Function, prefilter?: IPrefilter, path?: string[], key?: string, stack?: Object[]): void; 29 | applyDiff(target: Object, source: Object, filter: Function): void; 30 | applyChange(target: Object, source: Object, change: IDiff): void; 31 | revertChange(target: Object, source: Object, change: IDiff): void; 32 | isConflict(): boolean; 33 | noConflict(): IDeepDiff; 34 | } 35 | } 36 | 37 | declare var DeepDiff: deepDiff.IDeepDiff; 38 | 39 | declare module "deep-diff" { 40 | var diff: deepDiff.IDeepDiff; 41 | export = diff; 42 | } -------------------------------------------------------------------------------- /tests/defs/deep-diff.js.flow: -------------------------------------------------------------------------------- 1 | declare module 'deep-diff' { 2 | declare type Difference = 3 | | {kind: "N", path: Array, rhs: mixed} 4 | | {kind: "D", path: Array, lhs: mixed} 5 | | {kind: "E", path: Array, lhs: mixed, rhs: mixed} 6 | | {kind: "A", path: Array, index: number, item: Difference} 7 | declare type PrefilterFn = (path: Array, key: string) => bool|void 8 | declare module.exports: { 9 | (lhs: mixed, rhs: mixed, prefilter?: PrefilterFn, acc?: Array): ?Array, 10 | diff(lhs: mixed, rhs: mixed, prefilter?: PrefilterFn, acc?: Array): ?Array, 11 | observableDiff(lhs: mixed, rhs: mixed, observerFn: Function): void, 12 | applyChange(lhs: mixed, rhs: mixed, difference: Difference): void, 13 | revertChange(lhs: mixed, rhs: mixed, difference: Difference): void 14 | } 15 | } -------------------------------------------------------------------------------- /tests/runner.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var fs = require("fs"); 3 | var glob = require("glob"); 4 | var path = require("path"); 5 | var probsTyped = require("../index"); 6 | 7 | var defsDir = path.join(__dirname, "/defs"); 8 | 9 | var BOLD_ON = "\x1b[1m"; 10 | var BOLD_OFF = "\x1b[22m"; 11 | var RED = "\x1b[41m"; 12 | var FG_RED = "\x1b[31m"; 13 | var FG_GREEN = "\x1b[32m"; 14 | var RESET = "\x1b[0m"; 15 | 16 | var OKAY = BOLD_ON + FG_GREEN + " OK" + RESET + " "; 17 | var FAIL = BOLD_ON + FG_RED + "FAIL" + RESET + " "; 18 | 19 | glob(defsDir + "/*.d.ts", function(err, files) { 20 | if (err) { 21 | console.error(err); 22 | process.exit(1); 23 | return; 24 | } 25 | var exitCode = 0; 26 | var numFails = 0; 27 | files.forEach(function(f) { 28 | f = path.resolve(f); // convert \ to / so we can replace with out dir 29 | var outFile = f.replace(".d.ts", ".js.flow"); 30 | var expectedOutput = fs.readFileSync(outFile, "utf8"); 31 | var actualOutput = new String(probsTyped(fs.readFileSync(f, "utf8"))); 32 | // console.log("EXPECT: " + expectedOutput); 33 | // console.log("ACTUAL: " + actualOutput); 34 | var actualOutLines = actualOutput.split("\n"); 35 | var expectedOutLines = expectedOutput.split("\n"); 36 | 37 | // strictly filter out lines that start "//" on both input and output - we don't care about comments! 38 | actualOutLines = actualOutLines.filter(function(line) { 39 | return line.trim().indexOf("//") !== 0; 40 | }); 41 | expectedOutLines = expectedOutLines.filter(function(line) { 42 | return line.trim().indexOf("//") !== 0; 43 | }); 44 | 45 | if (actualOutLines.length === expectedOutLines.length) { 46 | var isOkay = true; 47 | for (var lineNum = 0; lineNum < actualOutLines.length; lineNum++) { 48 | if (actualOutLines[lineNum] === expectedOutLines[lineNum]) { 49 | continue; 50 | } 51 | isOkay = false; 52 | var lineLength = Math.max(actualOutLines[lineNum].length, expectedOutLines[lineNum].length); 53 | for (var charNum = 0; charNum < lineLength; charNum++) { 54 | if (actualOutLines[lineNum][charNum] === expectedOutLines[lineNum][charNum]) { 55 | continue; 56 | } 57 | var badChar = escape(actualOutLines[lineNum][charNum]); 58 | var actualSnippet = snippet(actualOutLines[lineNum], charNum); 59 | var expectedSnippet = snippet(expectedOutLines[lineNum], charNum); 60 | 61 | console.error( 62 | "%s%s:%s:%s %s", FAIL, outFile, lineNum + 1, charNum + 1, 63 | "Unexpected char: '" + badChar + "'. " + 64 | "Got \"" + actualSnippet + "\", expected \"" + expectedSnippet + "\"." 65 | ); 66 | numFails += 1; 67 | break; 68 | } 69 | } 70 | if (isOkay) { 71 | console.log("%s%s", OKAY, outFile); 72 | } 73 | } 74 | else { 75 | console.error( 76 | "%s%s:%s %s", FAIL, outFile, 0, 77 | "Expected " + expectedOutLines.length + " lines, got " + actualOutLines.length 78 | ); 79 | numFails += 1; 80 | } 81 | }); 82 | console.log("%s test failures out of %s tests.", numFails, files.length); 83 | process.exit(numFails > 0 ? 1 : 0); 84 | }); 85 | 86 | function snippet(line, charNum) { 87 | var leftMinSnip = Math.max(charNum - 15, 0); 88 | var leftMaxSnip = Math.max(charNum, 0); 89 | var rightMinSnip = Math.min(charNum + 1, line.length); 90 | var rightMaxSnip = Math.min(charNum + 15, line.length); 91 | return ( 92 | escape(line.substring(leftMinSnip, leftMaxSnip)) + 93 | RED + escape(line[charNum]) + RESET + 94 | escape(line.substring(rightMinSnip, rightMaxSnip)) 95 | ); 96 | } 97 | 98 | function escape(thing) { 99 | if (thing === undefined) { 100 | return ""; 101 | } 102 | var escapedThing = JSON.stringify(thing); 103 | return escapedThing.substring(1, escapedThing.length-1); 104 | } 105 | 106 | --------------------------------------------------------------------------------