From c8f810d5ae80e8f4379815dc83ebb7e0728ad08e Mon Sep 17 00:00:00 2001 From: Tomasz Rybarczyk Date: Wed, 4 Sep 2019 17:25:37 +0200 Subject: [PATCH] Merge `OnVisit` into `OnType` --- src/ReadDTS.js | 8 +++++--- src/ReadDTS.purs | 29 ++++++++++++----------------- src/ReadDTS.ts | 20 ++++++++++---------- test/Main.purs | 26 +++++++++++--------------- 4 files changed, 38 insertions(+), 45 deletions(-) diff --git a/src/ReadDTS.js b/src/ReadDTS.js index 159b3cb..c7f4df2 100644 --- a/src/ReadDTS.js +++ b/src/ReadDTS.js @@ -12,7 +12,7 @@ exports.compilerOptions = { target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS }; -function _readDTS(fileName, options, onVisit, onType) { +function _readDTS(fileName, options, onType) { // Build a program using the set of root file names in fileNames var program = ts.createProgram([fileName], options); var checker = program.getTypeChecker(); @@ -44,12 +44,12 @@ function _readDTS(fileName, options, onVisit, onType) { }); var x = { name: node.name.text, members: members }; // console.log(x); - output.push(onVisit.interface(x)); + output.push(onType.interface(x)); } else { var x = { name: node.name.text, type: getTSType(nodeType) }; // console.log(x); - output.push(onVisit.typeAlias(x)); + output.push(onType.typeAlias(x)); } } else if (node.kind === ts.SyntaxKind.ModuleDeclaration) { @@ -109,6 +109,8 @@ function _readDTS(fileName, options, onVisit, onType) { // return { type: "typeparam", name: checker.typeToString(memType) }; // } else { + console.log("UNKONWN"); + console.log(memType); return onType.unknown(checker.typeToString(memType)); // return { unknown: checker.typeToString(memType), flags: memType.flags }; } diff --git a/src/ReadDTS.purs b/src/ReadDTS.purs index c6cd7b5..5a07ffb 100644 --- a/src/ReadDTS.purs +++ b/src/ReadDTS.purs @@ -1,23 +1,20 @@ module ReadDTS where import Effect (Effect) -import Effect.Uncurried (EffectFn4, runEffectFn4) +import Effect.Uncurried (EffectFn3, runEffectFn3) import Foreign (Foreign) import Node.Path (FilePath) type CompilerOptions = Foreign -type OnVisit a t = - { interface ∷ +type OnType t = + { interface ∷ { name ∷ String , members ∷ Array { name ∷ String, type ∷ t, optional ∷ Boolean } } - → a - , typeAlias ∷ { name ∷ String, type ∷ t } → a - } - -type OnType t = - { unionOrIntersection ∷ Array t → t + → t + , typeAlias ∷ { name ∷ String, type ∷ t } → t + , unionOrIntersection ∷ Array t → t , primitive ∷ String → t , stringLiteral ∷ String → t , numberLiteral ∷ Number → t @@ -25,21 +22,19 @@ type OnType t = } readDTS - ∷ ∀ a t + ∷ ∀ t . FilePath → CompilerOptions - → OnVisit a t → OnType t - → Effect (Array a) -readDTS = runEffectFn4 _readDTS + → Effect (Array t) +readDTS = runEffectFn3 _readDTS foreign import _readDTS - ∷ ∀ a t - . EffectFn4 + ∷ ∀ t + . EffectFn3 FilePath CompilerOptions - (OnVisit a t) (OnType t) - (Array a) + (Array t) foreign import compilerOptions ∷ CompilerOptions diff --git a/src/ReadDTS.ts b/src/ReadDTS.ts index 4aa7878..b748632 100644 --- a/src/ReadDTS.ts +++ b/src/ReadDTS.ts @@ -5,31 +5,29 @@ export const compilerOptions = { module: ts.ModuleKind.CommonJS }; -export function _readDTS( +export function _readDTS( fileName: string, options: ts.CompilerOptions, - onVisit: { + onType: { interface: (x: { name: string, members: { name: string, type: t, optional: boolean }[] - }) => a, - typeAlias: (x: { name: string, type: t }) => a - }, - onType: { + }) => t, + typeAlias: (x: { name: string, type: t }) => t, unionOrIntersection: (types: t[]) => t, stringLiteral: (s: string) => t, numberLiteral: (n: number) => t, primitive: (name: string) => t, unknown: (name: string) => t } -): a[] { +): t[] { // Build a program using the set of root file names in fileNames let program = ts.createProgram([fileName], options); let checker = program.getTypeChecker(); - let output: a[] = []; + let output: t[] = []; // Visit every sourceFile in the program for (const sourceFile of program.getSourceFiles()) { @@ -65,11 +63,11 @@ export function _readDTS( let x = { name: node.name.text, members }; // console.log(x); - output.push(onVisit.interface(x)); + output.push(onType.interface(x)); } else { let x = { name: node.name.text, type: getTSType(nodeType) }; // console.log(x); - output.push(onVisit.typeAlias(x)); + output.push(onType.typeAlias(x)); } } else if (node.kind === ts.SyntaxKind.ModuleDeclaration) { @@ -133,6 +131,8 @@ export function _readDTS( // return { type: "typeparam", name: checker.typeToString(memType) }; // } else { + // console.log("UNKONWN"); + // console.log(memType); return onType.unknown(checker.typeToString(memType)); // return { unknown: checker.typeToString(memType), flags: memType.flags }; } diff --git a/test/Main.purs b/test/Main.purs index ab8d130..53e8244 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -7,26 +7,22 @@ import Data.String (joinWith) import Effect (Effect) import Effect.Class.Console (log) import Effect.Console (logShow) -import ReadDTS (OnType, OnVisit, compilerOptions, readDTS) +import ReadDTS (OnType, compilerOptions, readDTS) -stringOnVisit ∷ OnVisit String String -stringOnVisit = +stringOnType ∷ OnType String +stringOnType = { interface: \{ name, members } → let onMember r = joinWith " " [r.name, if r.optional then "?:" else ":", r.type] in - "Interface " <> name <> ": " <> joinWith "; " (map onMember members) - , typeAlias: \r → joinWith " " ["type", r.name, ":", r.type] - } - -stringOnType ∷ OnType String -stringOnType = - { unionOrIntersection: joinWith " | " - , primitive: show - , stringLiteral: show - , numberLiteral: show - , unknown: show + "interface " <> name <> ": " <> joinWith "; " (map onMember members) + , typeAlias: \r → "typeAlias: " <> joinWith " " ["type", r.name, ":", r.type] + , unionOrIntersection: append "union: " <<< joinWith " | " + , primitive: append "primitive: " <<< show + , stringLiteral: append "stringLiteral: " <<< show + , numberLiteral: append "numberLiteral: " <<< show + , unknown: append "unkown: " <<< show } fileName ∷ String @@ -34,5 +30,5 @@ fileName = "test/simple.d.ts" main ∷ Effect Unit main = do - res ← readDTS fileName compilerOptions stringOnVisit stringOnType + res ← readDTS fileName compilerOptions stringOnType for_ res log