in packages/babel-plugin-fbt-runtime/index.js [135:217]
StringLiteral(path) {
// $FlowFixMe[object-this-reference] Babel transforms run with the plugin context by default
const {fbtSentinel, reactNativeMode} = getPluginOptions(this);
if (fbtSentinel == null || fbtSentinel.trim() == '') {
// eslint-disable-next-line fb-www/no-new-error
throw new Error(`fbtSentinel must be a non-empty string. `
+ `Current value is ${String(fbtSentinel)} (${typeof fbtSentinel})`);
}
const sentinelLength = fbtSentinel.length;
let phrase = path.node.value;
if (
!phrase.startsWith(fbtSentinel) ||
!phrase.endsWith(fbtSentinel) ||
phrase.length <= sentinelLength * 2
) {
return;
}
phrase = (JSON.parse(
phrase.slice(sentinelLength, phrase.length - sentinelLength),
) /*: SentinelPayload */);
const payload = phrase.jsfbt.t;
const runtimeInput = mapLeaves(
payload,
convertJSFBTLeafToRuntimeInputText,
);
// $FlowFixMe[prop-missing] replaceWithSourceString's type is not defined yet
path.replaceWithSourceString(JSON.stringify(runtimeInput));
const parentNode = path.parentPath && path.parentPath.node;
invariant(isCallExpression(parentNode),
'Expected parent node to be a BabelNodeCallExpression');
// Append runtime options - key for runtime dictionary lookup
if (parentNode.arguments.length === 1) {
// Second param 'args' could be omitted sometimes. Use null here
parentNode.arguments.push(t.nullLiteral());
}
invariant(
parentNode.arguments.length === 2,
'Expecting options to be the third param',
);
let shiftedJsfbt;
let enumCount = 0;
if (reactNativeMode) {
({enumCount, shiftedJsfbt} = shiftEnumsToTop(phrase.jsfbt));
}
if (enumCount > 0) {
invariant(
shiftedJsfbt != null,
'Expecting shiftedJsfbt to be defined',
);
parentNode.arguments.push(
// The expected method name is `objectExpression` but
// it already works as-is apparently...
// $FlowFixMe[prop-missing] Use objectExpression() instead
t.ObjectExpression([
t.objectProperty(
t.identifier('ehk'), // enumHashKey
_buildEnumToHashKeyObjectExpression(
shiftedJsfbt,
enumCount,
),
),
]),
);
} else {
parentNode.arguments.push(
// The expected method name is `objectExpression` but
// it already works as-is apparently...
// $FlowFixMe[prop-missing] Use objectExpression() instead
t.ObjectExpression([
t.objectProperty(
t.identifier('hk'),
t.stringLiteral(fbtHashKey(payload)),
),
]),
);
}
},