Skip to content

Commit 1dbfe3a

Browse files
committed
fix(guards): escaped property names accessed via brackets
1 parent a6ef06c commit 1dbfe3a

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

src/parser.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,20 @@ function parseInterfaceProperties(
365365
: `'${propName}'`;
366366
const isRequired = requiredProps.includes(propName);
367367

368+
const accessProp = (arg: string): string =>
369+
arg.startsWith("'") ? `arg[${arg}]` : `arg.${arg}`;
370+
368371
const guardFn = (fn: () => string, prop: Property = {}) => `
369372
${
370373
prop.hasOwnProperty('isRequired') || isRequired
371374
? ''
372-
: `typeof arg.${name} === 'undefined' ||`
375+
: `typeof ${accessProp(name)} === 'undefined' ||`
373376
}
374377
${
375378
prop.isArray || isArray
376-
? `(Array.isArray(arg.${name}) && arg.${name}.every((item: unknown) => ${
379+
? `(Array.isArray(${accessProp(name)}) && ${accessProp(
380+
name,
381+
)}.every((item: unknown) => ${
377382
prop.isPrimitiveType || isPrimitiveType
378383
? `typeof item === '${type}'`
379384
: `is${prop.typescriptType || typescriptType}(item)`
@@ -386,7 +391,7 @@ function parseInterfaceProperties(
386391
})`
387392
: `${
388393
prop.isPrimitiveType || isPrimitiveType
389-
? `typeof arg.${prop.name || name} === '${
394+
? `typeof ${accessProp(prop.name || name)} === '${
390395
(prop.type || type) === '{ [key: string]: any }'
391396
? 'object'
392397
: prop.type || type
@@ -400,15 +405,18 @@ function parseInterfaceProperties(
400405
propertyAllOf.length
401406
? `(${propertyAllOf
402407
.map(prop =>
403-
guardFn(() => `is${prop.typescriptType}(arg.${name})`, prop),
408+
guardFn(
409+
() => `is${prop.typescriptType}(${accessProp(name)})`,
410+
prop,
411+
),
404412
)
405413
.join(' && ')})`
406414
: / \| /.test(typescriptType)
407415
? `[${(typescriptType || '').replace(
408416
/ \| /g,
409417
', ',
410-
)}].includes(arg.${name})`
411-
: `is${typescriptType}(arg.${name})`,
418+
)}].includes(${accessProp(name)})`
419+
: `is${typescriptType}(${accessProp(name)})`,
412420
).replace(/\s+/g, ' ')}) &&`;
413421

414422
return {

tests/custom/api/guards/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,13 @@ export function isTestModel(arg: any): arg is models.TestModel {
264264
arg != null &&
265265
typeof arg === 'object' &&
266266
// '42test'?: string
267-
( typeof arg.'42test' === 'undefined' || typeof arg.'42test' === 'string' ) &&
267+
( typeof arg['42test'] === 'undefined' || typeof arg['42test'] === 'string' ) &&
268268
// 'anotherKey@'?: string
269-
( typeof arg.'anotherKey@' === 'undefined' || typeof arg.'anotherKey@' === 'string' ) &&
269+
( typeof arg['anotherKey@'] === 'undefined' || typeof arg['anotherKey@'] === 'string' ) &&
270270
// 'some-key'?: string
271-
( typeof arg.'some-key' === 'undefined' || typeof arg.'some-key' === 'string' ) &&
271+
( typeof arg['some-key'] === 'undefined' || typeof arg['some-key'] === 'string' ) &&
272272
// 'yet@notherKey'?: string
273-
( typeof arg.'yet@notherKey' === 'undefined' || typeof arg.'yet@notherKey' === 'string' ) &&
273+
( typeof arg['yet@notherKey'] === 'undefined' || typeof arg['yet@notherKey'] === 'string' ) &&
274274
// count?: number
275275
( typeof arg.count === 'undefined' || typeof arg.count === 'number' ) &&
276276

tests/esquare/api/guards/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ export function isRestoreForm(arg: any): arg is models.RestoreForm {
585585
arg != null &&
586586
typeof arg === 'object' &&
587587
// 'passwordСonfirm': string
588-
( typeof arg.'passwordСonfirm' === 'string' ) &&
588+
( typeof arg['passwordСonfirm'] === 'string' ) &&
589589
// guid?: string
590590
( typeof arg.guid === 'undefined' || typeof arg.guid === 'string' ) &&
591591
// password: string

tests/github/api/guards/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,15 +539,15 @@ export function isEmojis(arg: any): arg is models.Emojis {
539539
arg != null &&
540540
typeof arg === 'object' &&
541541
// '-1'?: string
542-
( typeof arg.'-1' === 'undefined' || typeof arg.'-1' === 'string' ) &&
542+
( typeof arg['-1'] === 'undefined' || typeof arg['-1'] === 'string' ) &&
543543
// '+1'?: string
544-
( typeof arg.'+1' === 'undefined' || typeof arg.'+1' === 'string' ) &&
544+
( typeof arg['+1'] === 'undefined' || typeof arg['+1'] === 'string' ) &&
545545
// '100'?: string
546-
( typeof arg.'100' === 'undefined' || typeof arg.'100' === 'string' ) &&
546+
( typeof arg['100'] === 'undefined' || typeof arg['100'] === 'string' ) &&
547547
// '1234'?: string
548-
( typeof arg.'1234' === 'undefined' || typeof arg.'1234' === 'string' ) &&
548+
( typeof arg['1234'] === 'undefined' || typeof arg['1234'] === 'string' ) &&
549549
// '8ball'?: string
550-
( typeof arg.'8ball' === 'undefined' || typeof arg.'8ball' === 'string' ) &&
550+
( typeof arg['8ball'] === 'undefined' || typeof arg['8ball'] === 'string' ) &&
551551
// a?: string
552552
( typeof arg.a === 'undefined' || typeof arg.a === 'string' ) &&
553553
// ab?: string

0 commit comments

Comments
 (0)