Skip to content

Commit 46c1d44

Browse files
committedDec 5, 2015
deprecate context.history and context.location
refs remix-run#2646
1 parent bfd571e commit 46c1d44

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed
 

‎.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"extends": "rackt",
3+
"globals": {
4+
"__DEV__": true
5+
},
36
"rules": {
47
"react/jsx-uses-react": 1,
58
"react/jsx-no-undef": 2,

‎modules/RoutingContext.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import deprecateObjectProperties from './deprecateObjectProperties'
12
import invariant from 'invariant'
23
import React, { Component } from 'react'
34
import { isReactChildren } from './RouteUtils'
@@ -23,7 +24,10 @@ class RoutingContext extends Component {
2324
return history.isActive(...args)
2425
}
2526
}
26-
return { history, location, router }
27+
return deprecateObjectProperties({ history, location, router }, {
28+
history: '`context.history` is deprecated, please use context.router',
29+
location: '`context.location` is deprecated, please use a route component\'s `props.location` instead. If this is a deeply nested component, please refer to the strategies described in https://github.com/rackt/react-router/blob/v1.1.0/CHANGES.md#v110'
30+
})
2731
}
2832

2933
createElement(component, props) {

‎modules/deprecateObjectProperties.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*eslint no-empty: 0*/
2+
import warning from 'warning'
3+
4+
let useMembrane = false
5+
6+
if (__DEV__) {
7+
try {
8+
Object.defineProperty({}, 'x', { get() { return true } }).x
9+
useMembrane = true
10+
} catch(e) { }
11+
}
12+
13+
// wraps an object in a membrane to warn about deprecated property access
14+
export default function deprecateObjectProperties(object, deprecatedKeys) {
15+
if (!useMembrane)
16+
return object
17+
18+
const membrane = {}
19+
20+
for (let prop in object) {
21+
Object.defineProperty(membrane, prop, {
22+
configurable: false,
23+
enumerable: true,
24+
get() {
25+
if (deprecatedKeys[prop])
26+
warning(!deprecatedKeys[prop], deprecatedKeys[prop])
27+
return object[prop]
28+
}
29+
})
30+
}
31+
32+
return membrane
33+
}
34+

0 commit comments

Comments
 (0)
Please sign in to comment.