From c65742602b7914cd06923c8bdb53df51d6124ec7 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Wed, 13 Sep 2017 13:42:20 -0700 Subject: [PATCH] Add some docs for why template literals use a chain of .concat() calls. --- .../src/index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/babel-plugin-transform-es2015-template-literals/src/index.js b/packages/babel-plugin-transform-es2015-template-literals/src/index.js index 0c7d9aacdc..1cdcc39187 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/src/index.js +++ b/packages/babel-plugin-transform-es2015-template-literals/src/index.js @@ -1,4 +1,19 @@ export default function({ types: t }) { + /** + * This function groups the objects into multiple calls to `.concat()` in + * order to preserve execution order of the primitive conversion, e.g. + * + * "".concat(obj.foo, "foo", obj2.foo, "foo2") + * + * would evaluate both member expressions _first_ then, `concat` will + * convert each one to a primitive, whereas + * + * "".concat(obj.foo, "foo").concat(obj2.foo, "foo2") + * + * would evaluate the member, then convert it to a primitive, then evaluate + * the second member and convert that one, which reflects the spec behavior + * of template literals. + */ function buildConcatCallExressions(items) { let avail = true; return items.reduce(function(left, right) {