Skip to content

Commit

Permalink
fix(validation): property accessor ignore instrumenter (#1839)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Sayan751 committed Nov 14, 2023
1 parent ff761fb commit 342847f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 19 additions & 0 deletions packages/__tests__/src/validation/rule-provider.spec.ts
Expand Up @@ -445,6 +445,14 @@ describe('validation/rule-provider.spec.ts', function () {
sut.off();
});

it('calling .off on an object without rules does not cause error', function () {
const { sut } = setup();
const obj: Person = new Person((void 0)!, (void 0)!, (void 0)!);
assert.equal(validationRulesRegistrar.get(obj), void 0);
sut.off(obj);
assert.equal(validationRulesRegistrar.get(obj), void 0);
});

it('can define multiple ruleset for the same object using tagging', function () {
const { sut } = setup();
const obj: Person = new Person((void 0)!, (void 0)!, (void 0)!);
Expand Down Expand Up @@ -843,6 +851,7 @@ describe('validation/rule-provider.spec.ts', function () {
}

const cov_1wjh4ld5ut: any = {};
const cov_1wjh4ld5ut1: () => any = () => ({});
const a: string = 'foo';
const positiveDataRows = [
{ property: 'prop', expected: 'prop' },
Expand Down Expand Up @@ -947,6 +956,16 @@ describe('validation/rule-provider.spec.ts', function () {
{ property: function (o: any) { /* istanbul ignore next */ cov_1wjh4ld5ut.f[9]++;cov_1wjh4ld5ut.s[50]++; return o.prop; }, expected: 'prop' },
{ property: function (o: any) { "use strict"; /* istanbul ignore next */ cov_1wjh4ld5ut.s[50]++; return o.prop; }, expected: 'prop' },
{ property: function (o: any) { "use strict"; /* istanbul ignore next */ cov_1wjh4ld5ut.f[9]++;cov_1wjh4ld5ut.s[50]++; return o.prop; }, expected: 'prop' },

// for the instrumenter: @jsdevtools/coverage-istanbul-loader
{ property: function (o: any) { cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
{ property: function (o: any) { cov_1wjh4ld5ut1().f[9]++;cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
{ property: function (o: any) { "use strict"; cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
{ property: function (o: any) { "use strict"; cov_1wjh4ld5ut1().f[9]++;cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
{ property: function (o: any) { /* istanbul ignore next */ cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
{ property: function (o: any) { /* istanbul ignore next */ cov_1wjh4ld5ut1().f[9]++;cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
{ property: function (o: any) { "use strict"; /* istanbul ignore next */ cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
{ property: function (o: any) { "use strict"; /* istanbul ignore next */ cov_1wjh4ld5ut1().f[9]++;cov_1wjh4ld5ut1().s[50]++; return o.prop; }, expected: 'prop' },
];
for(const { property, expected } of positiveDataRows) {
it(`parses ${property.toString()} to ${expected}`, function () {
Expand Down
7 changes: 4 additions & 3 deletions packages/validation/src/rule-provider.ts
Expand Up @@ -80,7 +80,8 @@ export const validationRulesRegistrar = Object.freeze({
return Metadata.get(key, target) ?? Metadata.getOwn(key, target.constructor);
},
unset(target: IValidateable, tag?: string): void {
const keys = Metadata.getOwn(Protocol.annotation.name, target) as string[];
const keys = Metadata.getOwn(Protocol.annotation.name, target);
if (!Array.isArray(keys)) return;
for (const key of keys.slice(0)) {
if (key.startsWith(validationRulesRegistrar.name) && (tag === void 0 || key.endsWith(tag))) {
Metadata.delete(Protocol.annotation.keyFor(key), target);
Expand Down Expand Up @@ -111,7 +112,7 @@ class ValidationMessageEvaluationContext {
}
}

export interface PropertyRule extends IAstEvaluator {}
export interface PropertyRule extends IAstEvaluator { }
export class PropertyRule<TObject extends IValidateable = IValidateable, TValue = unknown> implements IPropertyRule {
public static readonly $TYPE: string = 'PropertyRule';
private latestRule?: IValidationRule;
Expand Down Expand Up @@ -533,7 +534,7 @@ export class ValidationRules<TObject extends IValidateable = IValidateable> impl
}

// eslint-disable-next-line no-useless-escape
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*\}$/;
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*\}$/;
const arrowAccessorPattern = /^\(?[$_\w\d]+\)?\s*=>\s*[$_\w\d]+((\.[$_\w\d]+|\[['"$_\w\d]+\])+)$/;
export const rootObjectSymbol = '$root';
export type PropertyAccessor<TObject extends IValidateable = IValidateable, TValue = unknown> = (object: TObject) => TValue;
Expand Down

0 comments on commit 342847f

Please sign in to comment.