aurelia / aurelia/validation

Unable to parse accessor function when using arrow functions with a block body

Open
#584 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
129
Forks
125
PR merge metrics
No merged PRs in 30d

Description

**I'm submitting a bug report**

* **Library Version:**
2.0.0

* **Operating System:**
Windows 10

* **Node Version:**
20.11.1

* **NPM Version:**
10.2.4

* **JSPM OR Webpack AND Version**
webpack 5.90.3

* **Browser:**
all

* **Language:**
TypeScript 5.4.2

**Current behavior:**
I'm using Webpack 5 with `new AureliaPlugin({dist: "es2015})` and typescript `target: "es6"`.
This config causes a runtime error when using arrow functions with a block body as accessor functions:

```
Inner Error:
Message: Unable to parse accessor function:
(model) => {
return model.name;
}
Inner Error Stack:
Error: Unable to parse accessor function:
(model) => {
return model.name;
}
at getAccessorExpression (webpack-internal:///aurelia-validation:569:15)
at PropertyAccessorParser.parse (webpack-internal:///aurelia-validation:552:30)
at FluentEnsure.ensure (webpack-internal:///aurelia-validation:1651:44)
at ValidationRules.ensure (webpack-internal:///aurelia-validation:1715:58)
```

Workaround: instead of using
```
// failing accessor function
.ensure((model) => {
return model.name;
})
```
it is possible to use the old fashioned style or the one-liner
```
// working accessor functions
.ensure(function(model) {
return model.name;
})

.ensure((model) => model.name)
```

**Expected/desired behavior:**

* **What is the expected behavior?**
Update arrow function regex to allow arrow functions with a function body. This could be handy to support automatically formatted code.
https://github.com/aurelia/validation/blob/f1628a3ccf3d526b98dadeda7ac9672f70c20e37/src/property-accessor-parser.ts#L33

Contributor guide

Open the contributing guide

Research direction

Start with src/property-accessor-parser.ts at the linked line, where the accessor function is parsed. Reproduce the reported failure with a block-bodied arrow function and compare it with the working function and expression-bodied arrow examples. Done means block-bodied arrow accessors parse successfully without breaking the existing forms.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, webpack
Domain
frontend, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.