Skip to content

Commit

Permalink
Fix isPlainObject (#3197) (#3198)
Browse files Browse the repository at this point in the history
  • Loading branch information
urugator authored Dec 15, 2021
1 parent 021f34e commit 9b90e25
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-kangaroos-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

fix `isPlainObject` impl (fixes #3197)
17 changes: 16 additions & 1 deletion packages/mobx/__tests__/v5/base/make-observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,4 +1653,19 @@ test("makeObservable throws when mixing @decorators with annotations", () => {
}

expect(() => new Test()).toThrow(/makeObservable second arg must be nullish when using decorators/);
})
})

test("makeAutoObservable + Object.create #3197", () => {
const proto = {
action() { },
*flow() { },
get computed() { return null },
};
const o = Object.create(proto);
o.observable = 5;
makeAutoObservable(o);
expect(isAction(proto.action)).toBe(true);
expect(isFlow(proto.flow)).toBe(true);
expect(isComputedProp(o, "computed")).toBe(true);
expect(isObservableProp(o, "observable")).toBe(true);
});
11 changes: 6 additions & 5 deletions packages/mobx/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function warnAboutProxyRequirement(msg: string) {
if (__DEV__ && globalState.verifyProxies) {
die(
"MobX is currently configured to be able to run in ES5 mode, but in ES5 MobX won't be able to " +
msg
msg
)
}
}
Expand All @@ -55,7 +55,7 @@ export function once(func: Lambda): Lambda {
}
}

export const noop = () => {}
export const noop = () => { }

export function isFunction(fn: any): fn is Function {
return typeof fn === "function"
Expand Down Expand Up @@ -84,7 +84,8 @@ export function isPlainObject(value) {
if (!isObject(value)) return false
const proto = Object.getPrototypeOf(value)
if (proto == null) return true
return proto.constructor?.toString() === plainObjectString
const protoConstructor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
return typeof protoConstructor === "function" && protoConstructor.toString() === plainObjectString
}

// https://stackoverflow.com/a/37865170
Expand Down Expand Up @@ -153,8 +154,8 @@ export const ownKeys: (target: any) => Array<string | symbol> =
typeof Reflect !== "undefined" && Reflect.ownKeys
? Reflect.ownKeys
: hasGetOwnPropertySymbols
? obj => Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj) as any)
: /* istanbul ignore next */ Object.getOwnPropertyNames
? obj => Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj) as any)
: /* istanbul ignore next */ Object.getOwnPropertyNames

export function stringifyKey(key: any): string {
if (typeof key === "string") return key
Expand Down

0 comments on commit 9b90e25

Please sign in to comment.