Skip to content

Commit 5e96b7a

Browse files
JoviDeCroockclaude
andcommitted
Golf: loose equality where the operands make it identical
Only converted comparisons whose operands are already known to be the same type (typeof results, for-in keys, string literals, numbers, object identity). Value comparisons that can see coercible operands (value === true/false/null/'', defaultProps undefined checks, prop diffing) stay strict. Core was left alone: the same change measured +1 B br there. compat 3490 -> 3482 B br hooks 1324 -> 1322 B br jsx-runtime 740 -> 735 B br Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 6b777ba commit 5e96b7a

8 files changed

Lines changed: 46 additions & 50 deletions

File tree

compat/src/Children.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const Children = {
1414
},
1515
only(children) {
1616
const normalized = toChildArray(children);
17-
if (normalized.length !== 1) throw 'Children.only';
17+
if (normalized.length != 1) throw 'Children.only';
1818
return normalized[0];
1919
},
2020
toArray: toChildArray

compat/src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function createFactory(type) {
5959
* @returns {boolean}
6060
*/
6161
function isValidElement(element) {
62-
return !!element && element.$$typeof === REACT_ELEMENT_TYPE;
62+
return !!element && element.$$typeof == REACT_ELEMENT_TYPE;
6363
}
6464

6565
/**
@@ -68,7 +68,7 @@ function isValidElement(element) {
6868
* @returns {boolean}
6969
*/
7070
function isFragment(element) {
71-
return isValidElement(element) && element.type === Fragment;
71+
return isValidElement(element) && element.type == Fragment;
7272
}
7373

7474
/**
@@ -118,7 +118,7 @@ function findDOMNode(component) {
118118
return (
119119
(component &&
120120
((component._vnode && component._vnode._dom) ||
121-
(component.nodeType === 1 && component))) ||
121+
(component.nodeType == 1 && component))) ||
122122
null
123123
);
124124
}

compat/src/render.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const MODE_HYDRATE = 1 << 5;
2828
const CAMEL_PROPS =
2929
/^(?:accent|alignment|arabic|baseline|cap|clip(?!PathU)|color|dominant|fill|flood|font|glyph(?!R)|horiz|image(!S)|letter|lighting|marker(?!H|W|U)|overline|paint|pointer|shape|stop|strikethrough|stroke|text(?!L)|transform|underline|unicode|units|v|vector|vert|word|writing|x(?!C))[A-Z]/;
3030
const CAMEL_REPLACE = /[A-Z0-9]/g;
31-
const IS_DOM = typeof document !== 'undefined';
31+
const IS_DOM = typeof document != 'undefined';
3232

3333
/**
3434
* This is taken from https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L84
@@ -177,63 +177,59 @@ function handleDomVNode(vnode) {
177177
let value = props[i];
178178

179179
if (
180-
(i === 'value' && 'defaultValue' in props && value == null) ||
180+
(i == 'value' && 'defaultValue' in props && value == null) ||
181181
// Emulate React's behavior of not rendering the contents of noscript tags on the client.
182-
(IS_DOM && i === 'children' && type === 'noscript') ||
183-
i === 'class' ||
184-
i === 'className'
182+
(IS_DOM && i == 'children' && type == 'noscript') ||
183+
i == 'class' ||
184+
i == 'className'
185185
) {
186186
// Skip applying value if it is null/undefined and we already set
187187
// a default value
188188
continue;
189189
}
190190

191-
if (i === 'style' && typeof value === 'object') {
191+
if (i == 'style' && typeof value == 'object') {
192192
let cloned;
193193
for (let key in value) {
194-
if (typeof value[key] === 'number' && !IS_NON_DIMENSIONAL.test(key)) {
194+
if (typeof value[key] == 'number' && !IS_NON_DIMENSIONAL.test(key)) {
195195
if (!cloned) {
196196
cloned = value = assign({}, value);
197197
}
198198
value[key] += 'px';
199199
}
200200
}
201-
} else if (
202-
i === 'defaultValue' &&
203-
'value' in props &&
204-
props.value == null
205-
) {
201+
} else if (i == 'defaultValue' && 'value' in props && props.value == null) {
206202
// `defaultValue` is treated as a fallback `value` when a value prop is present but null/undefined.
207203
// `defaultValue` for Elements with no value prop is the same as the DOM defaultValue property.
208204
i = 'value';
209-
} else if (i === 'download' && value === true) {
205+
} else if (i == 'download' && value === true) {
210206
// Calling `setAttribute` with a truthy value will lead to it being
211207
// passed as a stringified value, e.g. `download="true"`. React
212208
// converts it to an empty string instead, otherwise the attribute
213209
// value will be used as the file name and the file will be called
214210
// "true" upon downloading it.
215211
value = '';
216-
} else if (i === 'translate' && value === 'no') {
212+
} else if (i == 'translate' && value === 'no') {
217213
value = false;
218-
} else if (i[0] === 'o' && i[1] === 'n') {
214+
} else if (i[0] == 'o' && i[1] == 'n') {
219215
let lowerCased = i.toLowerCase();
220-
if (lowerCased === 'ondoubleclick') {
216+
if (lowerCased == 'ondoubleclick') {
221217
i = 'ondblclick';
222218
} else if (
223-
lowerCased === 'onchange' &&
224-
(type === 'input' || type === 'textarea') &&
219+
lowerCased == 'onchange' &&
220+
(type == 'input' || type == 'textarea') &&
225221
!onChangeInputType(props.type)
226222
) {
227223
lowerCased = i = 'oninput';
228-
} else if (lowerCased === 'onfocus') {
224+
} else if (lowerCased == 'onfocus') {
229225
i = 'onfocusin';
230-
} else if (lowerCased === 'onblur') {
226+
} else if (lowerCased == 'onblur') {
231227
i = 'onfocusout';
232228
}
233229

234230
// Add support for onInput and onChange, see #3561
235231
// if we have an oninput prop already change it to oninputCapture
236-
if (lowerCased === 'oninput') {
232+
if (lowerCased == 'oninput') {
237233
i = lowerCased;
238234
if (normalizedProps[i]) {
239235
i = 'oninputCapture';
@@ -289,9 +285,9 @@ function handleDomVNode(vnode) {
289285
let oldVNodeHook = options.vnode;
290286
options.vnode = vnode => {
291287
// only normalize props on Element nodes
292-
if (typeof vnode.type === 'string') {
288+
if (typeof vnode.type == 'string') {
293289
handleDomVNode(vnode);
294-
} else if (typeof vnode.type === 'function') {
290+
} else if (typeof vnode.type == 'function') {
295291
const shouldApplyRef =
296292
'prototype' in vnode.type && vnode.type.prototype.render;
297293
if ('ref' in vnode.props && shouldApplyRef) {
@@ -337,7 +333,7 @@ options.diffed = function (vnode) {
337333

338334
if (
339335
dom != null &&
340-
vnode.type === 'textarea' &&
336+
vnode.type == 'textarea' &&
341337
'value' in props &&
342338
props.value !== dom.value
343339
) {

compat/src/suspense.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function detachedClone(vnode, detachedParent, parentDom) {
5252

5353
vnode = assign({ constructor: UNDEFINED }, vnode);
5454
if (vnode._component != null) {
55-
if (vnode._component._parentDom === parentDom) {
55+
if (vnode._component._parentDom == parentDom) {
5656
vnode._component._parentDom = detachedParent;
5757
}
5858

@@ -85,7 +85,7 @@ function removeOriginal(vnode, detachedParent, originalParent) {
8585
);
8686

8787
if (vnode._component) {
88-
if (vnode._component._parentDom === detachedParent) {
88+
if (vnode._component._parentDom == detachedParent) {
8989
if (vnode._dom) {
9090
originalParent.appendChild(vnode._dom);
9191
}

compat/src/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export const assign = Object.assign;
77
* @returns {boolean}
88
*/
99
export function shallowDiffers(a, b) {
10-
for (let i in a) if (i !== '__source' && a[i] !== b[i]) return true;
11-
for (let i in b) if (i !== '__source' && !(i in a)) return true;
10+
for (let i in a) if (i != '__source' && a[i] !== b[i]) return true;
11+
for (let i in b) if (i != '__source' && !(i in a)) return true;
1212
return false;
1313
}
1414

hooks/src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ options._render = vnode => {
5656

5757
const hooks = currentComponent.__hooks;
5858
if (hooks) {
59-
if (previousComponent === currentComponent) {
59+
if (previousComponent == currentComponent) {
6060
currentComponent._renderCallbacks = [];
6161
} else {
6262
hooks._pendingEffects.some(invokeCleanup);
@@ -223,7 +223,7 @@ export function useReducer(reducer, initialState, init) {
223223
// have values that aren't equal to one another this pushes
224224
// us to update further down the tree
225225
let updatedHook = false;
226-
let shouldUpdate = this.props !== p;
226+
let shouldUpdate = this.props != p;
227227
hooks._list.some(hookItem => {
228228
if (hookItem._nextValue) {
229229
updatedHook = true;
@@ -468,7 +468,7 @@ function afterNextFrame(callback) {
468468
* @returns {void}
469469
*/
470470
function afterPaint(newQueueLength) {
471-
if (newQueueLength === 1 || prevRaf !== options.requestAnimationFrame) {
471+
if (newQueueLength == 1 || prevRaf != options.requestAnimationFrame) {
472472
prevRaf = options.requestAnimationFrame;
473473
(prevRaf || afterNextFrame)(flushAfterPaintEffects);
474474
}
@@ -512,7 +512,7 @@ function invokeEffect(hook) {
512512
function argsChanged(oldArgs, newArgs) {
513513
return (
514514
!oldArgs ||
515-
oldArgs.length !== newArgs.length ||
515+
oldArgs.length != newArgs.length ||
516516
newArgs.some((arg, index) => !ObjectIs(arg, oldArgs[index]))
517517
);
518518
}

jsx-runtime/src/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ const CSS_REGEX = /[A-Z]/g;
9292
* @returns {*}
9393
*/
9494
function normalizeAttrValue(value) {
95-
return value !== null &&
96-
typeof value === 'object' &&
97-
typeof value.valueOf === 'function'
95+
return value != null &&
96+
typeof value == 'object' &&
97+
typeof value.valueOf == 'function'
9898
? value.valueOf()
9999
: value;
100100
}
@@ -110,13 +110,13 @@ function normalizeAttrValue(value) {
110110
function jsxAttr(name, value) {
111111
if (options.attr) {
112112
const result = options.attr(name, value);
113-
if (typeof result === 'string') return result;
113+
if (typeof result == 'string') return result;
114114
}
115115

116116
value = normalizeAttrValue(value);
117117

118-
if (name === 'ref' || name === 'key') return '';
119-
if (name === 'style' && typeof value === 'object') {
118+
if (name == 'ref' || name == 'key') return '';
119+
if (name == 'style' && typeof value == 'object') {
120120
let str = '';
121121
for (let prop in value) {
122122
let val = value[prop];
@@ -136,8 +136,8 @@ function jsxAttr(name, value) {
136136
if (
137137
value == null ||
138138
value === false ||
139-
typeof value === 'function' ||
140-
typeof value === 'object'
139+
typeof value == 'function' ||
140+
typeof value == 'object'
141141
) {
142142
return '';
143143
} else if (value === true) return name;
@@ -155,13 +155,13 @@ function jsxAttr(name, value) {
155155
function jsxEscape(value) {
156156
if (
157157
value == null ||
158-
typeof value === 'boolean' ||
159-
typeof value === 'function'
158+
typeof value == 'boolean' ||
159+
typeof value == 'function'
160160
) {
161161
return null;
162162
}
163163

164-
if (typeof value === 'object') {
164+
if (typeof value == 'object') {
165165
// Check for VNode
166166
if (value.constructor === undefined) return value;
167167

jsx-runtime/src/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const ENCODED_ENTITIES = /["&<]/;
33
/** @param {string} str */
44
export function encodeEntities(str) {
55
// Skip all work for strings with no entities needing encoding:
6-
if (str.length === 0 || ENCODED_ENTITIES.test(str) === false) return str;
6+
if (!str.length || !ENCODED_ENTITIES.test(str)) return str;
77

88
let last = 0,
99
i = 0,
@@ -26,11 +26,11 @@ export function encodeEntities(str) {
2626
continue;
2727
}
2828
// Append skipped/buffered characters and the encoded entity:
29-
if (i !== last) out += str.slice(last, i);
29+
if (i != last) out += str.slice(last, i);
3030
out += ch;
3131
// Start the next seek/buffer after the entity's offset:
3232
last = i + 1;
3333
}
34-
if (i !== last) out += str.slice(last, i);
34+
if (i != last) out += str.slice(last, i);
3535
return out;
3636
}

0 commit comments

Comments
 (0)