Skip to content

Commit

Permalink
use @babel/helper-validator-option for record/tuple transform
Browse files Browse the repository at this point in the history
  • Loading branch information
rickbutton committed Dec 16, 2020
1 parent 944afda commit d243f7b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@babel/helper-module-imports": "workspace:^7.10.4",
"@babel/helper-plugin-utils": "workspace:^7.10.4",
"@babel/helper-validator-option": "workspace:^7.10.4",
"@babel/plugin-syntax-record-and-tuple": "workspace:^7.10.4"
},
"peerDependencies": {
Expand Down
98 changes: 54 additions & 44 deletions packages/babel-plugin-proposal-record-and-tuple/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,71 @@ import { declare } from "@babel/helper-plugin-utils";
import syntaxRecordAndTuple from "@babel/plugin-syntax-record-and-tuple";
import { types as t } from "@babel/core";
import { addNamed, isModule } from "@babel/helper-module-imports";
import { OptionValidator } from "@babel/helper-validator-option";
import { name as packageName } from "../package.json";

const v = new OptionValidator(packageName);

export default declare((api, options) => {
api.assertVersion(7);
api.assertVersion(7);

const {
importPolyfill: shouldImportPolyfill = !!options.polyfillModuleName,
polyfillModuleName = "@bloomberg/record-tuple-polyfill",
} = options;
const polyfillModuleName = v.validateStringOption(
"polyfillModuleName",
options.polyfillModuleName,
"@bloomberg/record-tuple-polyfill",
);
const shouldImportPolyfill = v.validateBooleanOption(
"importPolyfill",
options.importPolyfill,
!!options.polyfillModuleName,
);

// program -> cacheKey -> localBindingName
const importCaches = new WeakMap();
// program -> cacheKey -> localBindingName
const importCaches = new WeakMap();

function getOr(map, key, getDefault) {
let value = map.get(key);
if (!value) map.set(key, (value = getDefault()));
return value;
}
function getOr(map, key, getDefault) {
let value = map.get(key);
if (!value) map.set(key, (value = getDefault()));
return value;
}

function getBuiltIn(name, path) {
if (!shouldImportPolyfill) return t.identifier(name);
function getBuiltIn(name, path) {
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.");
}
const programPath = path.find(p => p.isProgram());
if (!programPath) {
throw new Error("Internal error: unable to find the Program node.");
}

const cacheKey = `${name}:${isModule(programPath)}`;
const cacheKey = `${name}:${isModule(programPath)}`;

const cache = getOr(importCaches, programPath.node, () => new Map());
const localBindingName = getOr(cache, cacheKey, () => {
return addNamed(programPath, name, polyfillModuleName, {
importedInterop: "uncompiled",
}).name;
});
const cache = getOr(importCaches, programPath.node, () => new Map());
const localBindingName = getOr(cache, cacheKey, () => {
return addNamed(programPath, name, polyfillModuleName, {
importedInterop: "uncompiled",
}).name;
});

return t.identifier(localBindingName);
}
return t.identifier(localBindingName);
}

return {
name: "@bloomberg/babel-plugin-proposal-record-and-tuple",
inherits: syntaxRecordAndTuple,
visitor: {
RecordExpression(path) {
const record = getBuiltIn("Record", path);
return {
name: "@bloomberg/babel-plugin-proposal-record-and-tuple",
inherits: syntaxRecordAndTuple,
visitor: {
RecordExpression(path) {
const record = getBuiltIn("Record", path);

const object = t.objectExpression(path.node.properties);
const wrapped = t.callExpression(record, [object]);
path.replaceWith(wrapped);
},
TupleExpression(path) {
const tuple = getBuiltIn("Tuple", path);
const object = t.objectExpression(path.node.properties);
const wrapped = t.callExpression(record, [object]);
path.replaceWith(wrapped);
},
TupleExpression(path) {
const tuple = getBuiltIn("Tuple", path);

const wrapped = t.callExpression(tuple, path.node.elements);
path.replaceWith(wrapped);
},
},
};
const wrapped = t.callExpression(tuple, path.node.elements);
path.replaceWith(wrapped);
},
},
};
});
36 changes: 33 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 4
cacheKey: 7
Expand Down Expand Up @@ -783,6 +780,12 @@ __metadata:
languageName: node
linkType: hard

"@babel/helper-validator-option@workspace:^7.10.4, @babel/helper-validator-option@workspace:^7.11.4, @babel/helper-validator-option@workspace:packages/babel-helper-validator-option":
version: 0.0.0-use.local
resolution: "@babel/helper-validator-option@workspace:packages/babel-helper-validator-option"
languageName: unknown
linkType: soft

"@babel/helper-validator-option@workspace:^7.12.1, @babel/helper-validator-option@workspace:^7.12.11, @babel/helper-validator-option@workspace:packages/babel-helper-validator-option":
version: 0.0.0-use.local
resolution: "@babel/helper-validator-option@workspace:packages/babel-helper-validator-option"
Expand Down Expand Up @@ -1365,6 +1368,20 @@ __metadata:
languageName: unknown
linkType: soft

"@babel/plugin-proposal-record-and-tuple@workspace:packages/babel-plugin-proposal-record-and-tuple":
version: 0.0.0-use.local
resolution: "@babel/plugin-proposal-record-and-tuple@workspace:packages/babel-plugin-proposal-record-and-tuple"
dependencies:
"@babel/core": "workspace:^7.10.4"
"@babel/helper-module-imports": "workspace:^7.10.4"
"@babel/helper-plugin-utils": "workspace:^7.10.4"
"@babel/helper-validator-option": "workspace:^7.10.4"
"@babel/plugin-syntax-record-and-tuple": "workspace:^7.10.4"
peerDependencies:
"@babel/core": ^7.0.0
languageName: unknown
linkType: soft

"@babel/plugin-proposal-throw-expressions@workspace:*, @babel/plugin-proposal-throw-expressions@workspace:packages/babel-plugin-proposal-throw-expressions":
version: 0.0.0-use.local
resolution: "@babel/plugin-proposal-throw-expressions@workspace:packages/babel-plugin-proposal-throw-expressions"
Expand All @@ -1378,6 +1395,19 @@ __metadata:
languageName: unknown
linkType: soft

"@babel/plugin-proposal-throw-expressions@workspace:^7.10.4, @babel/plugin-proposal-throw-expressions@workspace:packages/babel-plugin-proposal-throw-expressions":
version: 0.0.0-use.local
resolution: "@babel/plugin-proposal-throw-expressions@workspace:packages/babel-plugin-proposal-throw-expressions"
dependencies:
"@babel/core": "workspace:*"
"@babel/helper-plugin-test-runner": "workspace:*"
"@babel/helper-plugin-utils": "workspace:^7.10.4"
"@babel/plugin-syntax-throw-expressions": "workspace:^7.12.1"
peerDependencies:
"@babel/core": ^7.0.0-0
languageName: unknown
linkType: soft

"@babel/plugin-proposal-unicode-property-regex@npm:^7.10.4, @babel/plugin-proposal-unicode-property-regex@npm:^7.4.4":
version: 7.10.4
resolution: "@babel/plugin-proposal-unicode-property-regex@npm:7.10.4"
Expand Down

0 comments on commit d243f7b

Please sign in to comment.