Skip to content

Commit

Permalink
remove backtracking from record/tuple transform
Browse files Browse the repository at this point in the history
  • Loading branch information
rickbutton committed Dec 16, 2020
1 parent d243f7b commit 3e4d2be
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions packages/babel-plugin-proposal-record-and-tuple/src/index.js
Expand Up @@ -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.");
}
Expand All @@ -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);
Expand Down

0 comments on commit 3e4d2be

Please sign in to comment.