ts-auto-guard
Advanced tools
Comparing version 1.0.0-alpha.17 to 1.0.0-alpha.18
@@ -447,3 +447,3 @@ "use strict"; | ||
} | ||
function indexSignatureConditions(objName, keyName, index, addDependency, project, path, arrayDepth, records, outFile, options) { | ||
function indexSignatureConditions(objName, keyName, valueUsed, keyUsed, index, addDependency, project, path, arrayDepth, records, outFile, options) { | ||
var debug = options.debug; | ||
@@ -454,2 +454,8 @@ var expectedType = index.type.getText(); | ||
var keyConditions = typeConditions(keyName, index.keyType, addDependency, project, path + " " + keyName, arrayDepth, true, records, outFile, options); | ||
if (conditions) { | ||
valueUsed(); | ||
} | ||
if (keyConditions) { | ||
keyUsed(); | ||
} | ||
if (debug) { | ||
@@ -461,2 +467,5 @@ var cleanKeyReplacer = '${key.toString().replace(/"/g, \'\\\\"\')}'; | ||
"evaluate(" + keyConditions + ", `" + path + " (key: \"" + cleanKeyReplacer + "\")`, " + JSON.stringify(expectedKeyType) + ", " + keyName + ")"; | ||
if (evaluation || keyEvaluation) { | ||
keyUsed(); | ||
} | ||
if (evaluation && keyEvaluation) { | ||
@@ -474,5 +483,13 @@ return ands(evaluation, keyEvaluation); | ||
function indexSignaturesCondition(varName, indexSignatures, properties, addDependency, project, path, arrayDepth, records, outFile, options) { | ||
var valuePrefix = '_'; | ||
var valueUsed = function () { | ||
valuePrefix = ''; | ||
}; | ||
var keyPrefix = '_'; | ||
var keyUsed = function () { | ||
keyPrefix = ''; | ||
}; | ||
var conditions = ors.apply(void 0, __spread(indexSignatures | ||
.map(function (indexSignature) { | ||
return indexSignatureConditions('value', 'key', indexSignature, addDependency, project, path, arrayDepth, records, outFile, options); | ||
return indexSignatureConditions('value', 'key', valueUsed, keyUsed, indexSignature, addDependency, project, path, arrayDepth, records, outFile, options); | ||
}) | ||
@@ -488,3 +505,3 @@ .filter(function (v) { return v !== null; }))); | ||
: ''; | ||
return "Object.entries(" + varName + ")" + staticKeysFilter + "\n .every(([key,value]) => " + conditions + ")"; | ||
return "Object.entries(" + varName + ")" + staticKeysFilter + "\n .every(([" + keyPrefix + "key, " + valuePrefix + "value]) => " + conditions + ")"; | ||
} | ||
@@ -491,0 +508,0 @@ function generateTypeGuard(functionName, typeDeclaration, addDependency, project, records, outFile, options) { |
{ | ||
"name": "ts-auto-guard", | ||
"version": "1.0.0-alpha.17", | ||
"version": "1.0.0-alpha.18", | ||
"description": "Generate type guard functions from TypeScript interfaces", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/rhys-vdw/ts-auto-guard", |
@@ -1032,1 +1032,55 @@ import { each, pull } from 'lodash' | ||
) | ||
testProcessProject( | ||
'prefixes value with underscore if it goes unused', | ||
{ | ||
'test.ts': ` | ||
/** @see {isTestType} ts-auto-guard:type-guard */ | ||
export interface TestType { | ||
[index: string]: any | ||
} | ||
` | ||
}, | ||
{ | ||
'test.guard.ts': ` | ||
import { TestType } from "./test"; | ||
export function isTestType(obj: any, _argumentName?: string): obj is TestType { | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Object.entries(obj) | ||
.every(([key, _value]) => (typeof key === "string")) | ||
) | ||
} | ||
` | ||
} | ||
) | ||
testProcessProject( | ||
'prefixes key with underscore if it goes unused', | ||
{ | ||
'test.ts': ` | ||
/** @see {isTestType} ts-auto-guard:type-guard */ | ||
export interface TestType { | ||
[index: any]: string | ||
} | ||
` | ||
}, | ||
{ | ||
'test.guard.ts': ` | ||
import { TestType } from "./test"; | ||
export function isTestType(obj: any, _argumentName?: string): obj is TestType { | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Object.entries(obj) | ||
.every(([_key, value]) => (typeof value === "string")) | ||
) | ||
} | ||
` | ||
} | ||
) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
108284
1970