@samitouri / QOS-React-2 / commits / a0190f828f

Rename SafeValue to ToStringValue (#13376)

Following up on the changes I made in #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 #13362 and take care of rebase afterwards.

Philipp Spieß committed Aug 13, 2018 at 15:58 UTC a0190f828f260202392d58d43acc252b7ac97c59
2 files changed +23 -17
packages/react-dom/src/client/ReactDOMFiberInput.js
+13 -13
@@ -14,15 +14,15 @@ import warning from 'shared/warning';
14
15 import * as DOMPropertyOperations from './DOMPropertyOperations';
16 import {getFiberCurrentPropsFromNode} from './ReactDOMComponentTree';
17 -import {getSafeValue, safeValueToString} from './SafeValue';
17 +import {getToStringValue, toString} from './ToStringValue';
18 import ReactControlledValuePropTypes from '../shared/ReactControlledValuePropTypes';
19 import * as inputValueTracking from './inputValueTracking';
20
21 -import type {SafeValue} from './SafeValue';
21 +import type {ToStringValue} from './ToStringValue';
22
23 type InputWithWrapperState = HTMLInputElement & {
24 _wrapperState: {
25 - initialValue: SafeValue,
25 + initialValue: ToStringValue,
26 initialChecked: ?boolean,
27 controlled?: boolean,
28 },
@@ -117,7 +117,7 @@ export function initWrapperState(element: Element, props: Object) {
117 node._wrapperState = {
118 initialChecked:
119 props.checked != null ? props.checked : props.defaultChecked,
120 - initialValue: getSafeValue(
120 + initialValue: getToStringValue(
121 props.value != null ? props.value : defaultValue,
122 ),
123 controlled: isControlled(props),
@@ -171,7 +171,7 @@ export function updateWrapper(element: Element, props: Object) {
171
172 updateChecked(element, props);
173
174 - const value = getSafeValue(props.value);
174 + const value = getToStringValue(props.value);
175
176 if (value != null) {
177 if (props.type === 'number') {
@@ -181,17 +181,17 @@ export function updateWrapper(element: Element, props: Object) {
181 // eslint-disable-next-line
182 node.value != (value: any)
183 ) {
184 - node.value = safeValueToString(value);
184 + node.value = toString(value);
185 }
186 - } else if (node.value !== safeValueToString(value)) {
187 - node.value = safeValueToString(value);
186 + } else if (node.value !== toString(value)) {
187 + node.value = toString(value);
188 }
189 }
190
191 if (props.hasOwnProperty('value')) {
192 setDefaultValue(node, props.type, value);
193 } else if (props.hasOwnProperty('defaultValue')) {
194 - setDefaultValue(node, props.type, getSafeValue(props.defaultValue));
194 + setDefaultValue(node, props.type, getToStringValue(props.defaultValue));
195 }
196
197 if (props.checked == null && props.defaultChecked != null) {
@@ -207,7 +207,7 @@ export function postMountWrapper(
207 const node = ((element: any): InputWithWrapperState);
208
209 if (props.hasOwnProperty('value') || props.hasOwnProperty('defaultValue')) {
210 - const initialValue = safeValueToString(node._wrapperState.initialValue);
210 + const initialValue = toString(node._wrapperState.initialValue);
211 const currentValue = node.value;
212
213 // Do not assign value if it is already set. This prevents user text input
@@ -316,9 +316,9 @@ export function setDefaultValue(
316 node.ownerDocument.activeElement !== node
317 ) {
318 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);
322 }
323 }
324 }
packages/react-dom/src/client/ToStringValue.js renamed
+10 -4
@@ -7,16 +7,22 @@
7 * @flow
8 */
9
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;
17
18 // Flow does not allow string concatenation of most non-string types. To work
19 // 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 {
22 return '' + (value: any);
23 }
24
19 -export function getSafeValue(value: mixed): SafeValue {
25 +export function getToStringValue(value: mixed): ToStringValue {
26 switch (typeof value) {
27 case 'boolean':
28 case 'number':