Skip to content

Commit b0ee838

Browse files
authored
Merge pull request mobxjs#1511 from quanganhtran/master
Throw error in development when @observable is used on getters
2 parents 6b50a8c + 8cca76d commit b0ee838

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎src/api/computed.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const computedDecorator = createPropDecorator(
2424
) => {
2525
const { get, set } = descriptor // initialValue is the descriptor for get / set props
2626
// Optimization: faster on decorator target or instance? Assuming target
27-
// Optimiziation: find out if declaring on instance isn't just faster. (also makes the property descriptor simpler). But, more memory usage..
27+
// Optimization: find out if declaring on instance isn't just faster. (also makes the property descriptor simpler). But, more memory usage..
2828
const options = decoratorArgs[0] || {}
2929
defineComputedProperty(instance, propertyName, { ...options, get, set })
3030
}

‎src/api/observabledecorator.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defineObservableProperty } from "../types/observableobject"
2-
import { fail } from "../utils/utils"
2+
import { fail, invariant } from "../utils/utils"
33
import { IEnhancer } from "../types/modifiers"
44
import { createPropDecorator, BabelDescriptor } from "../utils/decorators2"
55

@@ -18,6 +18,12 @@ export function createDecoratorForEnhancer(enhancer: IEnhancer<any>): IObservabl
1818
_decoratorTarget,
1919
decoratorArgs: any[]
2020
) => {
21+
if (process.env.NODE_ENV !== "production") {
22+
invariant(
23+
!descriptor || !descriptor.get,
24+
`@observable cannot be used on getter (property "${propertyName}"), use @computed instead.`
25+
)
26+
}
2127
const initialValue = descriptor
2228
? descriptor.initializer ? descriptor.initializer.call(target) : descriptor.value
2329
: undefined

‎src/utils/decorators2.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function createPropDecorator(
7474
descriptor: BabelDescriptor | undefined,
7575
applyImmediately?: any
7676
// This is a special parameter to signal the direct application of a decorator, allow extendObservable to skip the entire type decoration part,
77-
// as the instance to apply the deorator to equals the target
77+
// as the instance to apply the decorator to equals the target
7878
) {
7979
if (applyImmediately === true) {
8080
propertyCreator(target, prop, descriptor, target, decoratorArguments)

‎test/base/decorate.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,20 @@ test("decorate should work with ES6 constructor", () => {
351351
title: observable
352352
})
353353
})
354+
355+
test("decorate should not allow @observable on getter", function() {
356+
const obj = {
357+
x: 0,
358+
get y() {
359+
return 0
360+
}
361+
}
362+
363+
decorate(obj, {
364+
x: observable,
365+
y: observable
366+
})
367+
368+
expect(() => obj.x).toThrow(/"y"/)
369+
expect(() => obj.y).toThrow()
370+
})

0 commit comments

Comments
 (0)