Skip to content

Commit 793104a

Browse files
Rename SafeValue to ToStringValue
Following up on the changes I made in facebook#13367, @gaearon suggest that "safe" could be read as necessary for security. To avoid misleading a reader, I'm changing the name. A few names where discussed in the previous PR. I think `ToStringValue` makes sense since the value itself is not a string yet but an opaque type that can be cast to a string. For the actual string concatenation, I've used `toString` now to avoid confusion: `toStringValueToString` is super weird and it's namespaced anyhow. Definitely open for suggestions here. :) I'll wait until we wrap up facebook#13362 and take care of rebase afterwards.
1 parent 33602d4 commit 793104a

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

packages/react-dom/src/client/ReactDOMFiberInput.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import warning from 'shared/warning';
1414

1515
import * as DOMPropertyOperations from './DOMPropertyOperations';
1616
import {getFiberCurrentPropsFromNode} from './ReactDOMComponentTree';
17-
import {getSafeValue, safeValueToString} from './SafeValue';
17+
import {getToStringValue, toString} from './ToStringValue';
1818
import ReactControlledValuePropTypes from '../shared/ReactControlledValuePropTypes';
1919
import * as inputValueTracking from './inputValueTracking';
2020

21-
import type {SafeValue} from './SafeValue';
21+
import type {ToStringValue} from './ToStringValue';
2222

2323
type InputWithWrapperState = HTMLInputElement & {
2424
_wrapperState: {
25-
initialValue: SafeValue,
25+
initialValue: ToStringValue,
2626
initialChecked: ?boolean,
2727
controlled?: boolean,
2828
},
@@ -117,7 +117,7 @@ export function initWrapperState(element: Element, props: Object) {
117117
node._wrapperState = {
118118
initialChecked:
119119
props.checked != null ? props.checked : props.defaultChecked,
120-
initialValue: getSafeValue(
120+
initialValue: getToStringValue(
121121
props.value != null ? props.value : defaultValue,
122122
),
123123
controlled: isControlled(props),
@@ -171,7 +171,7 @@ export function updateWrapper(element: Element, props: Object) {
171171

172172
updateChecked(element, props);
173173

174-
const value = getSafeValue(props.value);
174+
const value = getToStringValue(props.value);
175175

176176
if (value != null) {
177177
if (props.type === 'number') {
@@ -181,17 +181,17 @@ export function updateWrapper(element: Element, props: Object) {
181181
// eslint-disable-next-line
182182
node.value != (value: any)
183183
) {
184-
node.value = safeValueToString(value);
184+
node.value = toString(value);
185185
}
186-
} else if (node.value !== safeValueToString(value)) {
187-
node.value = safeValueToString(value);
186+
} else if (node.value !== toString(value)) {
187+
node.value = toString(value);
188188
}
189189
}
190190

191191
if (props.hasOwnProperty('value')) {
192192
setDefaultValue(node, props.type, value);
193193
} else if (props.hasOwnProperty('defaultValue')) {
194-
setDefaultValue(node, props.type, getSafeValue(props.defaultValue));
194+
setDefaultValue(node, props.type, getToStringValue(props.defaultValue));
195195
}
196196

197197
if (props.checked == null && props.defaultChecked != null) {
@@ -207,7 +207,7 @@ export function postMountWrapper(
207207
const node = ((element: any): InputWithWrapperState);
208208

209209
if (props.hasOwnProperty('value') || props.hasOwnProperty('defaultValue')) {
210-
const initialValue = safeValueToString(node._wrapperState.initialValue);
210+
const initialValue = toString(node._wrapperState.initialValue);
211211
const currentValue = node.value;
212212

213213
// Do not assign value if it is already set. This prevents user text input
@@ -316,9 +316,9 @@ export function setDefaultValue(
316316
node.ownerDocument.activeElement !== node
317317
) {
318318
if (value == null) {
319-
node.defaultValue = safeValueToString(node._wrapperState.initialValue);
320-
} else if (node.defaultValue !== safeValueToString(value)) {
321-
node.defaultValue = safeValueToString(value);
319+
node.defaultValue = toString(node._wrapperState.initialValue);
320+
} else if (node.defaultValue !== toString(value)) {
321+
node.defaultValue = toString(value);
322322
}
323323
}
324324
}

packages/react-dom/src/client/SafeValue.js packages/react-dom/src/client/ToStringValue.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@
77
* @flow
88
*/
99

10-
export opaque type SafeValue = boolean | number | Object | string | null | void;
10+
export opaque type ToStringValue =
11+
| boolean
12+
| number
13+
| Object
14+
| string
15+
| null
16+
| void;
1117

1218
// Flow does not allow string concatenation of most non-string types. To work
1319
// around this limitation, we use an opaque type that can only be obtained by
14-
// passing the value through getSafeValue first.
15-
export function safeValueToString(value: SafeValue): string {
20+
// passing the value through getToStringValue first.
21+
export function toString(value: ToStringValue): string {
1622
return '' + (value: any);
1723
}
1824

19-
export function getSafeValue(value: mixed): SafeValue {
25+
export function getToStringValue(value: mixed): ToStringValue {
2026
switch (typeof value) {
2127
case 'boolean':
2228
case 'number':

0 commit comments

Comments
 (0)