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

Do not include deprecation code in production builds #3296

Merged
merged 1 commit into from
Apr 13, 2016
Merged
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
65 changes: 40 additions & 25 deletions modules/deprecateObjectProperties.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,56 @@
/*eslint no-empty: 0*/
import warning from './routerWarning'

let useMembrane = false
// No-op by default.
let deprecateObjectProperties = object => object

if (__DEV__) {
let useMembrane = false

try {
if (Object.defineProperty({}, 'x', { get() { return true } }).x) {
useMembrane = true
}
} catch(e) { }
}
/* eslint-disable no-empty */
} catch(e) {}
/* eslint-enable no-empty */

// wraps an object in a membrane to warn about deprecated property access
export default function deprecateObjectProperties(object, message) {
if (!useMembrane)
return object
if (useMembrane) {
deprecateObjectProperties = (object, message) => {
// Wrap the deprecated object in a membrane to warn on property access.
const membrane = {}

const membrane = {}
for (const prop in object) {
if (!Object.prototype.hasOwnProperty.call(object, prop)) {
continue
}

for (let prop in object) {
if (typeof object[prop] === 'function') {
membrane[prop] = function () {
warning(false, message)
return object[prop].apply(object, arguments)
}
} else {
Object.defineProperty(membrane, prop, {
configurable: false,
enumerable: false,
get() {
warning(false, message)
return object[prop]
if (typeof object[prop] === 'function') {
// Can't use fat arrow here because of use of arguments below.
membrane[prop] = function () {
warning(false, message)
return object[prop].apply(object, arguments)
}
continue
}
})

// These properties are non-enumerable to prevent React dev tools from
// seeing them and causing spurious warnings when accessing them. In
// principle this could be done with a proxy, but support for the
// ownKeys trap on proxies is not universal, even among browsers that
// otherwise support proxies.
Object.defineProperty(membrane, prop, {
configurable: false,
enumerable: false,
get() {
warning(false, message)
return object[prop]
}
})
}

return membrane
}
}

return membrane
}

export default deprecateObjectProperties