Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: property accessor ignore instrumenter #578

Merged
merged 2 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/property-accessor-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class PropertyAccessorParser {

export function getAccessorExpression(fn: string): string {
/* tslint:disable:max-line-length */
const classic = /^function\s*\([$_\w\d]+\)\s*\{(?:\s*"use strict";)?(?:[$_\s\w\d\/\*.['"\]+;]+)?\s*return\s+[$_\w\d]+\.([$_\w\d]+)\s*;?\s*\}$/;
const classic = /^function\s*\([$_\w\d]+\)\s*\{(?:\s*"use strict";)?(?:[$_\s\w\d\/\*.['"\]+;\(\)]+)?\s*return\s+[$_\w\d]+\.([$_\w\d]+)\s*;?\s*\}$/;
/* tslint:enable:max-line-length */
const arrow = /^\(?[$_\w\d]+\)?\s*=>\s*[$_\w\d]+\.([$_\w\d]+)$/;
const match = classic.exec(fn) || arrow.exec(fn);
Expand Down
132 changes: 76 additions & 56 deletions test/property-accessor-parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,76 @@
import { Container } from 'aurelia-dependency-injection';
import { BindingLanguage } from 'aurelia-templating';
import { TemplatingBindingLanguage } from 'aurelia-templating-binding';
import {
PropertyAccessorParser,
getAccessorExpression as parse
} from '../src/aurelia-validation';

describe('PropertyAccessorParser', () => {
let parser: PropertyAccessorParser;

beforeAll(() => {
const container = new Container();
container.registerInstance(BindingLanguage, container.get(TemplatingBindingLanguage));
parser = container.get(PropertyAccessorParser);
});

it('parses properties', () => {
expect(parser.parse('firstName')).toEqual('firstName');
expect(parser.parse('3_letter_id')).toEqual('3_letter_id');
expect(parser.parse('. @$# ???')).toEqual('. @$# ???');
expect(parser.parse(42)).toEqual(42);
expect(parser.parse((x: any) => x.firstName)).toEqual('firstName');
});

it('parses function bodies', () => {
expect(parse('function(a){return a.b}')).toEqual('b');
expect(parse('function(a){return a.b;}')).toEqual('b');
expect(parse('function (a){return a.b;}')).toEqual('b');
expect(parse('function(a) {return a.b;}')).toEqual('b');
expect(parse('function (a) { return a.b; }')).toEqual('b');
expect(parse('function (a1) { return a1.bcde; }')).toEqual('bcde');
expect(parse('function ($a1) { return $a1.bcde; }')).toEqual('bcde');
expect(parse('function (_) { return _.bc_de; }')).toEqual('bc_de');
expect(parse('function (a) {"use strict"; return a.b; }')).toEqual('b');
expect(parse('function (a) { "use strict"; return a.b; }')).toEqual('b');
expect(parse('a=>a.b')).toEqual('b');
expect(parse('a =>a.b')).toEqual('b');
expect(parse('a=> a.b')).toEqual('b');
expect(parse('a => a.b')).toEqual('b');
expect(parse('a => a.bcde')).toEqual('bcde');
expect(parse('_ => _.b')).toEqual('b');
expect(parse('$ => $.b')).toEqual('b');
expect(parse('(x) => x.name')).toEqual('name');

// tslint:disable-next-line:max-line-length
expect(parse('function(a){__gen$field.f[\'10\']++;__aGen$field.g[\'10\']++;return a.b;}'))
.toEqual('b');
// tslint:disable-next-line:max-line-length
expect(parse('function(a){"use strict";_gen$field.f[\'10\']++;_aGen$field.g[\'10\']++;return a.b;}'))
.toEqual('b');
// tslint:disable-next-line:max-line-length
expect(parse('function(a){/* istanbul ignore next */ cov_1wjh4ld5ut.f[9]++;cov_1wjh4ld5ut.s[50]++;return a.b;}'))
.toEqual('b');
});
});
import { Container } from 'aurelia-dependency-injection';
import { BindingLanguage } from 'aurelia-templating';
import { TemplatingBindingLanguage } from 'aurelia-templating-binding';
import {
PropertyAccessorParser,
getAccessorExpression as parse
} from '../src/aurelia-validation';

describe('PropertyAccessorParser', () => {
let parser: PropertyAccessorParser;

beforeAll(() => {
const container = new Container();
container.registerInstance(BindingLanguage, container.get(TemplatingBindingLanguage));
parser = container.get(PropertyAccessorParser);
});

it('parses properties', () => {
expect(parser.parse('firstName')).toEqual('firstName');
expect(parser.parse('3_letter_id')).toEqual('3_letter_id');
expect(parser.parse('. @$# ???')).toEqual('. @$# ???');
expect(parser.parse(42)).toEqual(42);
expect(parser.parse((x: any) => x.firstName)).toEqual('firstName');
});

it('parses function bodies', () => {
expect(parse('function(a){return a.b}')).toEqual('b');
expect(parse('function(a){return a.b;}')).toEqual('b');
expect(parse('function (a){return a.b;}')).toEqual('b');
expect(parse('function(a) {return a.b;}')).toEqual('b');
expect(parse('function (a) { return a.b; }')).toEqual('b');
expect(parse('function (a1) { return a1.bcde; }')).toEqual('bcde');
expect(parse('function ($a1) { return $a1.bcde; }')).toEqual('bcde');
expect(parse('function (_) { return _.bc_de; }')).toEqual('bc_de');
expect(parse('function (a) {"use strict"; return a.b; }')).toEqual('b');
expect(parse('function (a) { "use strict"; return a.b; }')).toEqual('b');
expect(parse('a=>a.b')).toEqual('b');
expect(parse('a =>a.b')).toEqual('b');
expect(parse('a=> a.b')).toEqual('b');
expect(parse('a => a.b')).toEqual('b');
expect(parse('a => a.bcde')).toEqual('bcde');
expect(parse('_ => _.b')).toEqual('b');
expect(parse('$ => $.b')).toEqual('b');
expect(parse('(x) => x.name')).toEqual('name');

// tslint:disable-next-line:max-line-length
expect(parse('function(a){__gen$field.f[\'10\']++;__aGen$field.g[\'10\']++;return a.b;}'))
.toEqual('b');
// tslint:disable-next-line:max-line-length
expect(parse('function(a){"use strict";_gen$field.f[\'10\']++;_aGen$field.g[\'10\']++;return a.b;}'))
.toEqual('b');
// tslint:disable-next-line:max-line-length
expect(parse('function(a){/* istanbul ignore next */ cov_1wjh4ld5ut.f[9]++;cov_1wjh4ld5ut.s[50]++;return a.b;}'))
.toEqual('b');

// tslint:disable-next-line:max-line-length
expect(parse('function(a){__gen$field().f[\'10\']++;__aGen$field().g[\'10\']++;return a.b;}'))
.toEqual('b');
// tslint:disable-next-line:max-line-length
expect(parse('function(a){"use strict";_gen$field().f[\'10\']++;_aGen$field().g[\'10\']++;return a.b;}'))
.toEqual('b');
// tslint:disable-next-line:max-line-length
expect(parse('function(a){/* istanbul ignore next */ cov_1wjh4ld5ut().f[9]++;cov_1wjh4ld5ut().s[50]++;return a.b;}'))
.toEqual('b');

// tslint:disable-next-line:max-line-length
expect(parse('function(a){__gen$field().f[\'10\']++;__aGen$field.g[\'10\']++;return a.b;}'))
.toEqual('b');
// tslint:disable-next-line:max-line-length
expect(parse('function(a){"use strict";_gen$field().f[\'10\']++;_aGen$field.g[\'10\']++;return a.b;}'))
.toEqual('b');
// tslint:disable-next-line:max-line-length
expect(parse('function(a){/* istanbul ignore next */ cov_1wjh4ld5ut().f[9]++;cov_1wjh4ld5ut.s[50]++;return a.b;}'))
.toEqual('b');
});
});