ts-auto-guard
Advanced tools
Comparing version 1.0.0-alpha.18 to 1.0.0-alpha.19
@@ -95,2 +95,8 @@ #!/usr/bin/env node | ||
{ | ||
description: 'Allows customisation of the filename for the generated guards file', | ||
name: 'guard-file-name', | ||
type: String, | ||
typeLabel: '{underline extension}', | ||
}, | ||
{ | ||
description: 'Overrides the default behavior for --import-guards by skipping export from source.', | ||
@@ -119,3 +125,6 @@ name: 'prevent-export-imported', | ||
]; | ||
var options = lodash_1.defaults(command_line_args_1.default(optionList), { paths: [], help: false }); | ||
var options = lodash_1.defaults(command_line_args_1.default(optionList), { | ||
paths: [], | ||
help: false, | ||
}); | ||
function run() { | ||
@@ -157,2 +166,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
shortCircuitCondition: options.shortcircuit, | ||
guardFileName: options['guard-file-name'], | ||
}, | ||
@@ -159,0 +169,0 @@ project: project, |
@@ -8,2 +8,3 @@ import { Project } from 'ts-morph'; | ||
debug?: boolean; | ||
guardFileName?: string; | ||
} | ||
@@ -10,0 +11,0 @@ export interface IGenerateOptions { |
@@ -104,4 +104,5 @@ "use strict"; | ||
} | ||
function outFilePath(sourcePath) { | ||
return sourcePath.replace(/\.(ts|tsx|d\.ts)$/, '.guard.ts'); | ||
function outFilePath(sourcePath, options) { | ||
var _a; | ||
return sourcePath.replace(/\.(ts|tsx|d\.ts)$/, "." + ((_a = options.guardFileName) !== null && _a !== void 0 ? _a : 'guard') + ".ts"); | ||
} | ||
@@ -436,2 +437,5 @@ // https://github.com/dsherret/ts-simple-ast/issues/108#issuecomment-342665874 | ||
if (debug) { | ||
if (expectedType.startsWith('import')) { | ||
expectedType = expectedType.replace(process.cwd(), '.'); | ||
} | ||
return (conditions && | ||
@@ -623,3 +627,3 @@ "evaluate(" + conditions + ", `" + propertyPath + "`, " + JSON.stringify(expectedType) + ", " + varName + ")"); | ||
} | ||
var outFile = project.createSourceFile(outFilePath(sourceFile.getFilePath()), '', { overwrite: true }); | ||
var outFile = project.createSourceFile(outFilePath(sourceFile.getFilePath(), options), '', { overwrite: true }); | ||
try { | ||
@@ -626,0 +630,0 @@ for (var allTypesDeclarations_1 = __values(allTypesDeclarations), allTypesDeclarations_1_1 = allTypesDeclarations_1.next(); !allTypesDeclarations_1_1.done; allTypesDeclarations_1_1 = allTypesDeclarations_1.next()) { |
{ | ||
"name": "ts-auto-guard", | ||
"version": "1.0.0-alpha.18", | ||
"version": "1.0.0-alpha.19", | ||
"description": "Generate type guard functions from TypeScript interfaces", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/rhys-vdw/ts-auto-guard", |
@@ -134,2 +134,12 @@ # ts-auto-guard | ||
## Change Guard File Name | ||
ts-auto-guard will create a `.guard.ts` file by default, but this can be overriden. | ||
``` | ||
ts-auto-guard --guard-file-name="debug" | ||
``` | ||
Will result in a guard file called `.debug.ts`. | ||
## Add Import to Source File | ||
@@ -154,2 +164,2 @@ ts-auto-guard supports an `ìmport-guards` flag. This flag will add an import statement at the top and a named export at the bottom of the source files for the generated type guards. The `ìmport-guards` flag also optionally accepts a custom name for the import alias, if none is passed then `TypeGuards` is used as a default. | ||
export { Guards } | ||
``` | ||
``` |
@@ -149,2 +149,129 @@ import { each, pull } from 'lodash' | ||
testProcessProject( | ||
'allows the name of the guard file file to be specified', | ||
{ | ||
'test.ts': ` | ||
/** @see {isFoo} ts-auto-guard:type-guard */ | ||
export interface Foo { | ||
foo: number, | ||
bar: string | ||
}`, | ||
}, | ||
{ | ||
'test.debug.ts': ` | ||
import { Foo } from "./test"; | ||
export function isFoo(obj: any, _argumentName?: string): obj is Foo { | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.foo === "number" && | ||
typeof obj.bar === "string" | ||
) | ||
}`, | ||
}, | ||
{ | ||
options: { | ||
guardFileName: 'debug' | ||
} | ||
} | ||
) | ||
testProcessProject( | ||
'show debug info', | ||
{ | ||
'test.ts': ` | ||
/** @see {isFoo} ts-auto-guard:type-guard */ | ||
export interface Foo { | ||
foo: number, | ||
bar: Bar | ||
} | ||
/** @see {isBar} ts-auto-guard:type-guard */ | ||
export interface Bar { | ||
bar: number, | ||
} | ||
`, | ||
}, | ||
{ | ||
'test.guard.ts': ` | ||
import { Foo, Bar } from "./test"; | ||
function evaluate( | ||
isCorrect: boolean, | ||
varName: string, | ||
expected: string, | ||
actual: any | ||
): boolean { | ||
if (!isCorrect) { | ||
console.error( | ||
\`\${varName} type mismatch, expected: \${expected}, found:\`, | ||
actual | ||
) | ||
} | ||
return isCorrect | ||
} | ||
export function isFoo(obj: any, argumentName: string = "foo"): obj is Foo { | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
evaluate(typeof obj.foo === "number", \`\${argumentName}.foo\`, "number", obj.foo) && | ||
evaluate(isBar(obj.bar) as boolean, \`\${argumentName}.bar\`, "import(\\"/test\\").Bar", obj.bar) | ||
) | ||
} | ||
export function isBar(obj: any, argumentName: string = "bar"): obj is Bar { | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
evaluate(typeof obj.bar === "number", \`\${argumentName}.bar\`, "number", obj.bar) | ||
) | ||
} | ||
` | ||
}, | ||
{ | ||
options: { | ||
debug: true, | ||
} | ||
} | ||
) | ||
testProcessProject( | ||
'uses correct import file name if guard file is renamed', | ||
{ | ||
'test.ts': ` | ||
/** @see {isFoo} ts-auto-guard:type-guard */ | ||
export interface Foo { | ||
foo: number, | ||
bar: string | ||
}`, | ||
}, | ||
{ | ||
'test.debug.ts': ` | ||
import { Foo } from "./test"; | ||
export function isFoo(obj: any, _argumentName?: string): obj is Foo { | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.foo === "number" && | ||
typeof obj.bar === "string" | ||
) | ||
}`, | ||
}, | ||
{ | ||
options: { | ||
guardFileName: 'debug', | ||
importGuards: 'CustomGuardAlias', | ||
}, | ||
skip: true | ||
} | ||
) | ||
testProcessProject( | ||
'generates type guards for simple interface', | ||
@@ -151,0 +278,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
112254
2102
164