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 debug names + terser unsafe: false #2799

Merged
merged 3 commits into from
Feb 14, 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
9 changes: 9 additions & 0 deletions .changeset/new-actors-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"mobx": patch
---

- fix: user provided debug names are not preserved on production
- fix: property atom's debug name is dynamic on production
- fix: `observable(primitive, options)` ignores `options`
- fix: `getDebugName(action)` throws `[MobX] Cannot obtain atom from undefined`
- [fix: terser using `unsafe: true`](https://github.com/mobxjs/mobx/issues/2751#issuecomment-778171773)
93 changes: 93 additions & 0 deletions packages/mobx/__tests__/v5/base/extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,3 +874,96 @@ test("comparer.shallow should work", () => {
expect(sh(obs(new Map([[{}, 1]])), obs(new Map([[{}, 1]])))).toBe(false)
expect(sh(obs(new Map([["a", {}]])), obs(new Map([["a", {}]])))).toBe(false)
})

test("getDebugName(action)", () => {
expect(mobx.getDebugName(mobx.action(() => {}))).toBe("<unnamed action>")
expect(mobx.getDebugName(mobx.action(function fn() {}))).toBe("fn")
expect(mobx.getDebugName(mobx.action("custom", function fn() {}))).toBe("custom")
})

test("Default debug names - development", () => {
expect(mobx.getDebugName(mobx.observable({ x() {} }).x)).toBe("x")
expect(/Atom@\d+/.test(mobx.getDebugName(mobx.createAtom()))).toBe(true)
expect(/ComputedValue@\d+/.test(mobx.getDebugName(mobx.computed(() => {})))).toBe(true)
expect(mobx.getDebugName(mobx.action(function fn() {}))).toBe("fn")
expect(/ObservableObject@\d+/.test(mobx.getDebugName(mobx.observable({})))).toBe(true)
expect(/ObservableObject@\d+.x/.test(mobx.getDebugName(mobx.observable({ x: "x" }), "x"))).toBe(
true
)
expect(
/ObservableObject@\d+.x/.test(mobx.getDebugName(mobx.observable({ get x() {} }), "x"))
).toBe(true)
expect(/ObservableArray@\d+/.test(mobx.getDebugName(mobx.observable([])))).toBe(true)
expect(
/ObservableArray@\d+/.test(mobx.getDebugName(mobx.observable([], { proxy: false })))
).toBe(true)
expect(/ObservableMap@\d+/.test(mobx.getDebugName(mobx.observable(new Map())))).toBe(true)
expect(/ObservableSet@\d+/.test(mobx.getDebugName(mobx.observable(new Set())))).toBe(true)
expect(/ObservableValue@\d+/.test(mobx.getDebugName(mobx.observable("x")))).toBe(true)
expect(
/Reaction@\d+/.test(
mobx.getDebugName(
mobx.reaction(
() => {},
() => {}
)
)
)
).toBe(true)
expect(/Autorun@\d+/.test(mobx.getDebugName(mobx.autorun(() => {})))).toBe(true)
})

test("Default debug names - production", () => {
const mobx = require(`../../../dist/mobx.cjs.production.min.js`)

expect(mobx.getDebugName(mobx.observable({ x() {} }).x)).toBe("x") // perhaps should be "<unnamed action>"??
expect(mobx.getDebugName(mobx.createAtom())).toBe("Atom")
expect(mobx.getDebugName(mobx.computed(() => {}))).toBe("ComputedValue")
expect(mobx.getDebugName(mobx.action(function fn() {}))).toBe("fn")
expect(mobx.getDebugName(mobx.observable({}))).toBe("ObservableObject")
expect(mobx.getDebugName(mobx.observable({ x: "x" }), "x")).toBe("ObservableObject.key")
expect(mobx.getDebugName(mobx.observable({ get x() {} }), "x")).toBe("ObservableObject.key")
expect(mobx.getDebugName(mobx.observable([]))).toBe("ObservableArray")
expect(mobx.getDebugName(mobx.observable([], { proxy: false }))).toBe("ObservableArray")
expect(mobx.getDebugName(mobx.observable(new Map()))).toBe("ObservableMap")
expect(mobx.getDebugName(mobx.observable(new Set()))).toBe("ObservableSet")
expect(mobx.getDebugName(mobx.observable("x"))).toBe("ObservableValue")
expect(
mobx.getDebugName(
mobx.reaction(
() => {},
() => {}
)
)
).toBe("Reaction")
expect(mobx.getDebugName(mobx.autorun(() => {}))).toBe("Autorun")
})

test("User provided debug names are always respected", () => {
const mobxDevelopment = mobx
const mobxProduction = require(`../../../dist/mobx.cjs.production.min.js`)

const name = "CustomName"

;[mobxDevelopment, mobxProduction].forEach(mobx => {
expect(mobx.getDebugName(mobx.action(name, function fn() {}))).toBe(name)
expect(mobx.getDebugName(mobx.createAtom(name))).toBe(name)
expect(mobx.getDebugName(mobx.computed(() => {}, { name }))).toBe(name)
expect(mobx.getDebugName(mobx.observable({}, {}, { name }))).toBe(name)
expect(mobx.getDebugName(mobx.observable([], { name }))).toBe(name)
expect(mobx.getDebugName(mobx.observable([], { name, proxy: false }))).toBe(name)
expect(mobx.getDebugName(mobx.observable(new Map(), { name }))).toBe(name)
expect(mobx.getDebugName(mobx.observable(new Set(), { name }))).toBe(name)
expect(mobx.getDebugName(mobx.observable("x", { name }))).toBe(name)
expect(
mobx.getDebugName(
mobx.reaction(
() => {},
() => {},
{ name }
)
)
).toBe(name)
expect(mobx.getDebugName(mobx.autorun(() => {}, { name }))).toBe(name)
})
})
Loading