tslint-consistent-codestyle
tslint-consistent-codestyle copied to clipboard
no-accessor-recursion - improve detection
In my code, i sometimes use the pattern
class CustomError extends Error {
constructor(someBigObject: any) {
super()
this.object = someBigObject
}
get message(): string {
const value = `Failed ${util.inspect(this.object)}`
this.message = value
return value
}
set message(value: string) {
Object.defineProperty(this, 'message', {value, configurable: true, enumerable: true, writable: true})
}
}
Ideally, this should pass, and only fail if there's a get/access in the set and a set/assignment in the get (or a get/access in the get or a set/assignment in the set, obviously)
That seems reasonable to me. I'm definitely onboard with the change to only show an error if the get accessor contains a read or the set accessor contains a write access of the property.
Though I'm not sure about displaying an error if the get accessor invokes the set accessor which in turn invokes the get accessor. Then we would probably also need to detect the get accessor invoking a method which invokes the get accessor (with an arbitrary amount of nesting). And then I probably need to solve the halting problem 😆
Implementation status: I already added the necessary code to distinguish read and write access to tsutils. Once I update that library I can also improve the detection of accessors with computed names.