ts-auto-guard
Advanced tools
Comparing version 1.0.0-alpha.11 to 1.0.0-alpha.12
@@ -150,2 +150,3 @@ "use strict"; | ||
var e_2, _a, e_3, _b; | ||
var _c; | ||
var jsDocs = child.getJsDocs(); | ||
@@ -156,4 +157,4 @@ try { | ||
try { | ||
for (var _c = (e_3 = void 0, __values(doc.getInnerText().split('\n'))), _d = _c.next(); !_d.done; _d = _c.next()) { | ||
var line = _d.value; | ||
for (var _d = (e_3 = void 0, __values(doc.getInnerText().split('\n'))), _e = _d.next(); !_e.done; _e = _d.next()) { | ||
var line = _e.value; | ||
var match = line | ||
@@ -163,3 +164,3 @@ .trim() | ||
if (match !== null) { | ||
var _e = __read(match, 3), typeGuardName = _e[1], command = _e[2]; | ||
var _f = __read(match, 3), typeGuardName = _f[1], command = _f[2]; | ||
if (command !== 'type-guard') { | ||
@@ -176,3 +177,3 @@ reportError("command " + command + " is not supported!"); | ||
try { | ||
if (_d && !_d.done && (_b = _c.return)) _b.call(_c); | ||
if (_e && !_e.done && (_b = _d.return)) _b.call(_d); | ||
} | ||
@@ -192,4 +193,6 @@ finally { if (e_3) throw e_3.error; } | ||
var t = child.getType(); | ||
var symbol = t.getSymbol() || t.getAliasSymbol(); | ||
var name = symbol === null || symbol === void 0 ? void 0 : symbol.getName(); | ||
var symbols = [t.getSymbol(), t.getAliasSymbol()]; | ||
// type aliases have type __type sometimes | ||
var name = (_c = symbols | ||
.filter(function (x) { return x && x.getName() !== '__type'; })[0]) === null || _c === void 0 ? void 0 : _c.getName(); | ||
if (name) { | ||
@@ -245,5 +248,3 @@ return 'is' + name; | ||
if (conditions === null) { | ||
reportError("No conditions for " + varName + ", with array type " + arrayType.getText()); | ||
// TODO: Or `null`??? | ||
return 'true'; | ||
return "Array.isArray(" + varName + ")"; | ||
} | ||
@@ -412,4 +413,9 @@ // Bit of a hack, just check if the second argument is used before actually | ||
var propertyName = property.name; | ||
var varName = objName + "." + propertyName; | ||
var propertyPath = path + "." + propertyName; | ||
var isIdentifier = propertyName[0] !== '"'; | ||
var varName = isIdentifier | ||
? objName + "." + propertyName | ||
: objName + "[" + propertyName + "]"; | ||
var propertyPath = isIdentifier | ||
? path + "." + propertyName | ||
: path + "[" + propertyName + "]"; | ||
var expectedType = property.type.getText(); | ||
@@ -416,0 +422,0 @@ var conditions = typeConditions(varName, property.type, addDependency, project, propertyPath, arrayDepth, true, records, options); |
{ | ||
"name": "ts-auto-guard", | ||
"version": "1.0.0-alpha.11", | ||
"version": "1.0.0-alpha.12", | ||
"description": "Generate type guard functions from TypeScript interfaces", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/rhys-vdw/ts-auto-guard", |
@@ -175,2 +175,28 @@ import { each, pull } from 'lodash' | ||
testProcessProject( | ||
'generates type guards for properties with spaces', | ||
{ | ||
'test.ts': ` | ||
/** @see {isFoo} ts-auto-guard:type-guard */ | ||
export interface Foo { | ||
"foo 1": number, | ||
"bar 2": string | ||
}`, | ||
}, | ||
{ | ||
'test.guard.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 1"] === "number" && | ||
typeof obj["bar 2"] === "string" | ||
) | ||
}`, | ||
} | ||
) | ||
testProcessProject( | ||
'correctly handles default export', | ||
@@ -698,1 +724,62 @@ { | ||
) | ||
testProcessProject( | ||
'generated type guards for arrays of any', | ||
{ | ||
'test.ts': ` | ||
export interface Foo { | ||
value: any[] | ||
} | ||
`, | ||
}, | ||
{ | ||
'test.guard.ts': ` | ||
import { Foo } from "./test"; | ||
export function isFoo(obj: any, _argumentName?: string): obj is Foo { | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Array.isArray(obj.value) | ||
) | ||
}`, | ||
}, | ||
{ options: { exportAll: true } } | ||
) | ||
testProcessProject( | ||
'generated type guards for nested arrays', | ||
{ | ||
'test.ts': ` | ||
export type Foo = { | ||
value: Array<{ | ||
value: Array<number> | ||
}> | ||
} | ||
`, | ||
}, | ||
{ | ||
'test.guard.ts': ` | ||
import { Foo } from "./test"; | ||
export function isFoo(obj: any, _argumentName?: string): obj is Foo { | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Array.isArray(obj.value) && | ||
obj.value.every((e: any) => | ||
(e !== null && | ||
typeof e === "object" || | ||
typeof e === "function") && | ||
Array.isArray(e.value) && | ||
e.value.every((e: any) => | ||
typeof e === "number" | ||
) | ||
) | ||
) | ||
}`, | ||
}, | ||
{ options: { exportAll: true } } | ||
) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
84608
1542