ts-auto-guard
Advanced tools
Comparing version 1.0.0-alpha.12 to 1.0.0-alpha.13
@@ -406,10 +406,13 @@ "use strict"; | ||
var debug = options.debug; | ||
var hasSpaces = (property.name || '').includes(' ') && | ||
["'", "\""].every(function (quote) { return !(propertyName || '').includes(quote); }); | ||
var propertyName = property.name; | ||
var isIdentifier = propertyName[0] !== '"'; | ||
var isIdentifier = propertyName[0] !== '"' && propertyName[0] !== "'" && !hasSpaces; | ||
var strippedName = propertyName.replace(/"/g, ''); | ||
var varName = isIdentifier | ||
? objName + "." + propertyName | ||
: objName + "[" + propertyName + "]"; | ||
: objName + "[\"" + strippedName + "\"]"; | ||
var propertyPath = isIdentifier | ||
? path + "." + propertyName | ||
: path + "[" + propertyName + "]"; | ||
: path + "[\"" + strippedName + "\"]"; | ||
var expectedType = property.type.getText(); | ||
@@ -416,0 +419,0 @@ var conditions = typeConditions(varName, property.type, addDependency, project, propertyPath, arrayDepth, true, records, options); |
{ | ||
"name": "ts-auto-guard", | ||
"version": "1.0.0-alpha.12", | ||
"version": "1.0.0-alpha.13", | ||
"description": "Generate type guard functions from TypeScript interfaces", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/rhys-vdw/ts-auto-guard", |
@@ -201,2 +201,28 @@ import { each, pull } from 'lodash' | ||
testProcessProject( | ||
'generates type guards for properties with spaces in types instead of interfaces', | ||
{ | ||
'test.ts': ` | ||
/** @see {isFoo} ts-auto-guard:type-guard */ | ||
export type 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', | ||
@@ -203,0 +229,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
85846
1569