Skip to content

Commit e429dc4

Browse files
committed
Use engine-specified version of ToString(argument)
1 parent 96e03b2 commit e429dc4

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,12 @@ function evaluateInstruction(
538538
return null;
539539
}
540540

541-
const expressionString = tryToStringValue(subExprValue.value);
542-
if (expressionString === undefined) {
541+
const expressionValue = subExprValue.value;
542+
if (
543+
typeof expressionValue === 'object' ||
544+
typeof expressionValue === 'symbol' ||
545+
typeof expressionValue === 'function'
546+
) {
543547
// value is not supported (function, object) or invalid (symbol)
544548
return null;
545549
}
@@ -551,8 +555,12 @@ function evaluateInstruction(
551555
return null;
552556
}
553557

554-
// Ref: https://tc39.es/ecma262/2024/#sec-template-literals-runtime-semantics-evaluation
555-
resultString = resultString.concat(expressionString, suffix);
558+
// Spec states that concat calls ToString(argument) internally on its parameters
559+
// -> we don't have to implement ToString(argument) ourselves and just use the engine implementation
560+
// Refs: https://tc39.es/ecma262/2024/#sec-tostring
561+
// https://tc39.es/ecma262/2024/#sec-string.prototype.concat
562+
// https://tc39.es/ecma262/2024/#sec-template-literals-runtime-semantics-evaluation
563+
resultString = resultString.concat(expressionValue as string, suffix);
556564
}
557565

558566
const result: InstructionValue = {
@@ -598,28 +606,5 @@ function read(constants: Constants, place: Place): Constant | null {
598606
return constants.get(place.identifier.id) ?? null;
599607
}
600608

601-
/**
602-
* Implements 7.1.17 ToString(argument) from ES2024 for primitive types only. If `Symbol` is passed, no exception is thrown and `undefined` is returned.
603-
* @link https://tc39.es/ecma262/2024/#sec-tostring
604-
*/
605-
function tryToStringValue(argument: unknown): string | undefined {
606-
switch (typeof argument) {
607-
case 'string':
608-
return argument;
609-
case 'undefined':
610-
return 'undefined';
611-
case 'boolean':
612-
return argument ? 'true' : 'false';
613-
case 'number':
614-
return argument.toString(10);
615-
case 'bigint':
616-
return argument.toString(10);
617-
case 'function':
618-
case 'object':
619-
case 'symbol':
620-
return undefined;
621-
}
622-
}
623-
624609
type Constant = Primitive | LoadGlobal;
625610
type Constants = Map<IdentifierId, Constant>;

0 commit comments

Comments
 (0)