-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use TS Morph to generate type predicates
- Loading branch information
Showing
4 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { InterfaceDeclaration, MethodDeclaration, Project, SyntaxKind } from "ts-morph" | ||
|
||
// open a new project with just BrowserTypes as the only source file | ||
const project = new Project(); | ||
const sourceFile = project.addSourceFileAtPath("./src/api/BrowserTypes.ts") | ||
|
||
//get a list of all interfaces in the file | ||
const interfaces = sourceFile.getChildrenOfKind(SyntaxKind.InterfaceDeclaration); | ||
|
||
// get a list of all conversion functions in the Convert class | ||
const convert = sourceFile.getClass("Convert"); | ||
const convertFunctions = (convert?.getChildrenOfKind(SyntaxKind.MethodDeclaration) ?? []).filter(func => func.getReturnType().getText() === "string"); | ||
|
||
// generate a list of Interfaces that have an associated conversion function | ||
const matchedInterfaces = convertFunctions.map(func => { | ||
const valueParameter = func.getParameter("value"); | ||
|
||
const matchingInterface = interfaces.find(interfaceNode => { | ||
return valueParameter?.getType().getText(valueParameter) === interfaceNode.getName(); | ||
}); | ||
|
||
|
||
if(matchingInterface != null) { | ||
return {func, matchingInterface}; | ||
} | ||
|
||
return undefined; | ||
}).filter(((value => value != null) as <T>(value: T | null | undefined) => value is T)); | ||
|
||
// write a type predicate for each matched interface | ||
matchedInterfaces.forEach(matched => writePredicate(matched.matchingInterface, matched.func)); | ||
|
||
|
||
function writePredicate(matchingInterface: InterfaceDeclaration, func: MethodDeclaration): void{ | ||
const predicateName = `is${matchingInterface.getName()}`; | ||
|
||
sourceFile.addStatements(`export function ${predicateName}(value: any): value is ${matchingInterface.getName()} { | ||
try{ | ||
Convert.${func.getName()}(value); | ||
return true; | ||
} catch(e: any){ | ||
return false; | ||
} | ||
}`); | ||
|
||
} | ||
|
||
sourceFile.formatText(); | ||
|
||
project.saveSync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
// see https://www.typescriptlang.org/tsconfig to better understand tsconfigs | ||
"files": ["generate-type-predicates.ts"], | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"lib": ["dom", "esnext"], | ||
"importHelpers": true, | ||
// output .d.ts declaration files for consumers | ||
"declaration": true, | ||
"outDir": "dist", | ||
// output .js.map sourcemap files for consumers | ||
"sourceMap": true, | ||
// match output dir to input dir. e.g. dist/index instead of dist/src/index | ||
"rootDir": "./", | ||
// stricter type-checking for stronger correctness. Recommended by TS | ||
"strict": true, | ||
// linter checks for common issues | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
// noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
// use Node's module resolution algorithm, instead of the legacy TS one | ||
"moduleResolution": "node", | ||
// transpile JSX to React.createElement | ||
"jsx": "react", | ||
// interop between ESM and CJS modules. Recommended by TS | ||
"esModuleInterop": true, | ||
// significant perf increase by skipping checking .d.ts files, particularly those in node_modules. Recommended by TS | ||
"skipLibCheck": true, | ||
// error out if import and file system have a casing mismatch. Recommended by TS | ||
"forceConsistentCasingInFileNames": true, | ||
// `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc` | ||
"noEmit": true, | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters