From 3e4d2be9f9acf1435388ac37c87f56ef7a79120d Mon Sep 17 00:00:00 2001 From: Richard Button Date: Wed, 16 Dec 2020 16:47:39 +0000 Subject: [PATCH] remove backtracking from record/tuple transform --- .../src/index.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/babel-plugin-proposal-record-and-tuple/src/index.js b/packages/babel-plugin-proposal-record-and-tuple/src/index.js index 6483e2dad538..82203bbc3fc2 100644 --- a/packages/babel-plugin-proposal-record-and-tuple/src/index.js +++ b/packages/babel-plugin-proposal-record-and-tuple/src/index.js @@ -46,10 +46,8 @@ export default declare((api, options) => { return value; } - function getBuiltIn(name, path) { + function getBuiltIn(name, path, programPath) { if (!shouldImportPolyfill) return t.identifier(name); - - const programPath = path.find(p => p.isProgram()); if (!programPath) { throw new Error("Internal error: unable to find the Program node."); } @@ -70,15 +68,18 @@ export default declare((api, options) => { name: "@bloomberg/babel-plugin-proposal-record-and-tuple", inherits: syntaxRecordAndTuple, visitor: { - RecordExpression(path) { - const record = getBuiltIn("Record", path); + Program(path, state) { + state.programPath = path; + }, + RecordExpression(path, state) { + const record = getBuiltIn("Record", path, state.programPath); const object = t.objectExpression(path.node.properties); const wrapped = t.callExpression(record, [object]); path.replaceWith(wrapped); }, - TupleExpression(path) { - const tuple = getBuiltIn("Tuple", path); + TupleExpression(path, state) { + const tuple = getBuiltIn("Tuple", path, state.programPath); const wrapped = t.callExpression(tuple, path.node.elements); path.replaceWith(wrapped);