Skip to content

Commit 342847f

Browse files
authored
fix(validation): property accessor ignore instrumenter (#1839)
* fix(validation): property accessor ignore instrumenter This supports instrumentation code from @jsdevtools/coverage-istanbul-loader. * chore: fix deepscan issue * fix(validation): allowed rules.off on object w/o rules
1 parent ff761fb commit 342847f

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

packages/__tests__/src/validation/rule-provider.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,14 @@ describe('validation/rule-provider.spec.ts', function () {
445445
sut.off();
446446
});
447447

448+
it('calling .off on an object without rules does not cause error', function () {
449+
const { sut } = setup();
450+
const obj: Person = new Person((void 0)!, (void 0)!, (void 0)!);
451+
assert.equal(validationRulesRegistrar.get(obj), void 0);
452+
sut.off(obj);
453+
assert.equal(validationRulesRegistrar.get(obj), void 0);
454+
});
455+
448456
it('can define multiple ruleset for the same object using tagging', function () {
449457
const { sut } = setup();
450458
const obj: Person = new Person((void 0)!, (void 0)!, (void 0)!);
@@ -843,6 +851,7 @@ describe('validation/rule-provider.spec.ts', function () {
843851
}
844852

845853
const cov_1wjh4ld5ut: any = {};
854+
const cov_1wjh4ld5ut1: () => any = () => ({});
846855
const a: string = 'foo';
847856
const positiveDataRows = [
848857
{ property: 'prop', expected: 'prop' },
@@ -947,6 +956,16 @@ describe('validation/rule-provider.spec.ts', function () {
947956
{ property: function (o: any) { /* istanbul ignore next */ cov_1wjh4ld5ut.f[9]++;cov_1wjh4ld5ut.s[50]++; return o.prop; }, expected: 'prop' },
948957
{ property: function (o: any) { "use strict"; /* istanbul ignore next */ cov_1wjh4ld5ut.s[50]++; return o.prop; }, expected: 'prop' },
949958
{ property: function (o: any) { "use strict"; /* istanbul ignore next */ cov_1wjh4ld5ut.f[9]++;cov_1wjh4ld5ut.s[50]++; return o.prop; }, expected: 'prop' },
959+
960+
// for the instrumenter: @jsdevtools/coverage-istanbul-loader
961+
{ property: function (o: any) { cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
962+
{ property: function (o: any) { cov_1wjh4ld5ut1().f[9]++;cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
963+
{ property: function (o: any) { "use strict"; cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
964+
{ property: function (o: any) { "use strict"; cov_1wjh4ld5ut1().f[9]++;cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
965+
{ property: function (o: any) { /* istanbul ignore next */ cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
966+
{ property: function (o: any) { /* istanbul ignore next */ cov_1wjh4ld5ut1().f[9]++;cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
967+
{ property: function (o: any) { "use strict"; /* istanbul ignore next */ cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
968+
{ property: function (o: any) { "use strict"; /* istanbul ignore next */ cov_1wjh4ld5ut1().f[9]++;cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
950969
];
951970
for(const { property, expected } of positiveDataRows) {
952971
it(`parses ${property.toString()} to ${expected}`, function () {

packages/validation/src/rule-provider.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ export const validationRulesRegistrar = Object.freeze({
8080
return Metadata.get(key, target) ?? Metadata.getOwn(key, target.constructor);
8181
},
8282
unset(target: IValidateable, tag?: string): void {
83-
const keys = Metadata.getOwn(Protocol.annotation.name, target) as string[];
83+
const keys = Metadata.getOwn(Protocol.annotation.name, target);
84+
if (!Array.isArray(keys)) return;
8485
for (const key of keys.slice(0)) {
8586
if (key.startsWith(validationRulesRegistrar.name) && (tag === void 0 || key.endsWith(tag))) {
8687
Metadata.delete(Protocol.annotation.keyFor(key), target);
@@ -111,7 +112,7 @@ class ValidationMessageEvaluationContext {
111112
}
112113
}
113114

114-
export interface PropertyRule extends IAstEvaluator {}
115+
export interface PropertyRule extends IAstEvaluator { }
115116
export class PropertyRule<TObject extends IValidateable = IValidateable, TValue = unknown> implements IPropertyRule {
116117
public static readonly $TYPE: string = 'PropertyRule';
117118
private latestRule?: IValidationRule;
@@ -533,7 +534,7 @@ export class ValidationRules<TObject extends IValidateable = IValidateable> impl
533534
}
534535

535536
// eslint-disable-next-line no-useless-escape
536-
const classicAccessorPattern = /^function\s*\([$_\w\d]+\)\s*\{(?:\s*["']{1}use strict["']{1};)?(?:[$_\s\w\d\/\*.['"\]+;]+)?\s*return\s+[$_\w\d]+((\.[$_\w\d]+|\[['"$_\w\d]+\])+)\s*;?\s*\}$/;
537+
const classicAccessorPattern = /^function\s*\([$_\w\d]+\)\s*\{(?:\s*["']{1}use strict["']{1};)?(?:[$_\s\w\d\/\*.['"\]+;\(\)]+)?\s*return\s+[$_\w\d]+((\.[$_\w\d]+|\[['"$_\w\d]+\])+)\s*;?\s*\}$/;
537538
const arrowAccessorPattern = /^\(?[$_\w\d]+\)?\s*=>\s*[$_\w\d]+((\.[$_\w\d]+|\[['"$_\w\d]+\])+)$/;
538539
export const rootObjectSymbol = '$root';
539540
export type PropertyAccessor<TObject extends IValidateable = IValidateable, TValue = unknown> = (object: TObject) => TValue;

0 commit comments

Comments
 (0)