Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isPlainObject (#3197) #3198

Merged
merged 3 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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