Place the following expression on the shape layer > contents > “rectangle” > rectangle path > size
margin = 50;
text_layer = thisComp.layer(“test”) //pick whip to desired text layer
text_width = text_layer.sourceRectAtTime().width;
text_height = text_layer.sourceRectAtTime().height;
shape_width = text_width + margin*2;
shape_height = text_height + margin*2;
[shape_width, shape_height]