Skip to content

Commit

Permalink
Fix #1653 (#2757)
Browse files Browse the repository at this point in the history
* Fix error stringification on minified build

* Fix `isObservableProp` not supporting `Symbols`

* Fix `makeAutoObservable` not ignoring `inferredAnnotationsSymbol`

* changeset
  • Loading branch information
urugator committed Jan 28, 2021
1 parent 765c405 commit 39eca50
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/odd-maps-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"mobx": patch
---

Fix error stringification on minified build
Fix `isObservableProp` not supporting `Symbols`
Fix `makeAutoObservable` not ignoring `inferredAnnotationsSymbol`
16 changes: 15 additions & 1 deletion packages/mobx/__tests__/v5/base/make-observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
_getAdministration,
configure,
flow,
override
override,
$mobx
} from "../../../src/mobx"

test("makeObservable picks up decorators", () => {
Expand Down Expand Up @@ -1409,3 +1410,16 @@ test("@override must override", () => {
/^\[MobX\] 'Child\.prototype\.action' is decorated with 'override', but no such decorated member was found on prototype\./
)
})

test("makeAutoObservable + production build #2751", () => {
const mobx = require(`../../../dist/mobx.cjs.production.min.js`)
class Foo {
x = "x"
constructor() {
mobx.makeAutoObservable(this)
}
}
const foo = new Foo()
expect(mobx.isObservableObject(foo)).toBe(true)
expect(mobx.isObservableProp(foo, "x")).toBe(true)
})
4 changes: 2 additions & 2 deletions packages/mobx/src/api/isobservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isStringish
} from "../internal"

function _isObservable(value, property?: string): boolean {
function _isObservable(value, property?: PropertyKey): boolean {
if (!value) return false
if (property !== undefined) {
if (__DEV__ && (isObservableMap(value) || isObservableArray(value)))
Expand Down Expand Up @@ -40,7 +40,7 @@ export function isObservable(value: any): boolean {
return _isObservable(value)
}

export function isObservableProp(value: any, propName: string): boolean {
export function isObservableProp(value: any, propName: PropertyKey): boolean {
if (__DEV__ && !isStringish(propName)) return die(`expected a property name as second argument`)
return _isObservable(value, propName)
}
2 changes: 1 addition & 1 deletion packages/mobx/src/api/makeObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function makeAutoObservable<T extends object, AdditionalKeys extends Prop
adm.make_(key, target[inferredAnnotationsSymbol][key])
}
} else {
const ignoreKeys = { [$mobx]: 1, constructor: 1 }
const ignoreKeys = { [$mobx]: 1, [inferredAnnotationsSymbol]: 1, constructor: 1 }
const make = key => {
if (ignoreKeys[key]) return
ignoreKeys[key] = 1
Expand Down
6 changes: 3 additions & 3 deletions packages/mobx/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const niceErrors = {
0: `Invalid value for configuration 'enforceActions', expected 'never', 'always' or 'observed'`,
1(annotationType, fieldName) {
return `Cannot apply '${annotationType}' to '${fieldName}': Field not found.`
1(annotationType, key: PropertyKey) {
return `Cannot apply '${annotationType}' to '${key.toString()}': Field not found.`
},
5: "'keys()' can only be used on observable objects, arrays, sets and maps",
6: "'values()' can only be used on observable objects, arrays, sets and maps",
Expand Down Expand Up @@ -73,7 +73,7 @@ export function die(error: string | keyof typeof errors, ...args: any[]): never
throw new Error(
typeof error === "number"
? `[MobX] minified error nr: ${error}${
args.length ? " " + args.join(",") : ""
args.length ? " " + args.map(String).join(",") : ""
}. Find the full error at: https://github.com/mobxjs/mobx/blob/main/packages/mobx/src/errors.ts`
: `[MobX] ${error}`
)
Expand Down

0 comments on commit 39eca50

Please sign in to comment.