Implement "Records and Tuples" transform (#12145)

Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Rick Button 2021-02-21 11:42:17 -05:00 committed by GitHub
parent c10825ab05
commit 407e8b5c7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 331 additions and 1 deletions

View File

@ -0,0 +1,3 @@
src
test
*.log

View File

@ -0,0 +1,32 @@
{
"name": "@babel/plugin-proposal-record-and-tuple",
"version": "7.12.0",
"description": "A transform for Record and Tuple syntax.",
"repository": {
"type": "git",
"url": "https://github.com/babel/babel.git",
"directory": "packages/babel-plugin-proposal-record-and-tuple"
},
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "./lib/index.js",
"exports": "./lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {
"@babel/helper-module-imports": "workspace:^7.12.13",
"@babel/helper-plugin-utils": "workspace:^7.12.13",
"@babel/helper-validator-option": "workspace:^7.12.17",
"@babel/plugin-syntax-record-and-tuple": "workspace:^7.12.1"
},
"peerDependencies": {
"@babel/core": "^7.12.0"
},
"devDependencies": {
"@babel/core": "workspace:*",
"@babel/helper-plugin-test-runner": "workspace:*"
}
}

View File

@ -0,0 +1,88 @@
/*
** Copyright 2020 Bloomberg Finance L.P.
**
** Licensed under the MIT License (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** https://opensource.org/licenses/MIT
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
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";
const v = new OptionValidator(PACKAGE_JSON.name);
export default declare((api, options) => {
api.assertVersion(7);
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();
function getOr(map, key, getDefault) {
let value = map.get(key);
if (!value) map.set(key, (value = getDefault()));
return value;
}
function getBuiltIn(name, path, programPath) {
if (!shouldImportPolyfill) return t.identifier(name);
if (!programPath) {
throw new Error("Internal error: unable to find the Program node.");
}
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;
});
return t.identifier(localBindingName);
}
return {
name: "proposal-record-and-tuple",
inherits: syntaxRecordAndTuple,
visitor: {
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, state) {
const tuple = getBuiltIn("Tuple", path, state.programPath);
const wrapped = t.callExpression(tuple, path.node.elements);
path.replaceWith(wrapped);
},
},
};
});

View File

@ -0,0 +1,8 @@
const r2 = #{
a: #{
b: 456,
},
e: 789,
};
const t2 = #[1, #[2, 3, #[4], 5], 6];

View File

@ -0,0 +1,6 @@
{
"plugins": [["../../../../", {
"syntaxType": "hash",
"importPolyfill": true
}]]
}

View File

@ -0,0 +1,12 @@
var _Tuple = require("@bloomberg/record-tuple-polyfill").Tuple;
var _Record = require("@bloomberg/record-tuple-polyfill").Record;
const r2 = _Record({
a: _Record({
b: 456
}),
e: 789
});
const t2 = _Tuple(1, _Tuple(2, 3, _Tuple(4), 5), 6);

View File

@ -0,0 +1,8 @@
const r2 = #{
a: #{
b: 456,
},
e: 789,
};
const t2 = #[1, #[2, 3, #[4], 5], 6];

View File

@ -0,0 +1,6 @@
{
"plugins": [["../../../../", {
"syntaxType": "hash",
"polyfillModuleName": "my-polyfill"
}]]
}

View File

@ -0,0 +1,11 @@
import { Tuple as _Tuple } from "my-polyfill";
import { Record as _Record } from "my-polyfill";
const r2 = _Record({
a: _Record({
b: 456
}),
e: 789
});
const t2 = _Tuple(1, _Tuple(2, 3, _Tuple(4), 5), 6);

View File

@ -0,0 +1,8 @@
const r2 = #{
a: #{
b: 456,
},
e: 789,
};
const t2 = #[1, #[2, 3, #[4], 5], 6];

View File

@ -0,0 +1,7 @@
{
"plugins": [["../../../../", {
"syntaxType": "hash",
"importPolyfill": true,
"polyfillModuleName": "my-polyfill"
}]]
}

View File

@ -0,0 +1,11 @@
import { Tuple as _Tuple } from "my-polyfill";
import { Record as _Record } from "my-polyfill";
const r2 = _Record({
a: _Record({
b: 456
}),
e: 789
});
const t2 = _Tuple(1, _Tuple(2, 3, _Tuple(4), 5), 6);

View File

@ -0,0 +1,8 @@
const r2 = #{
a: #{
b: 456,
},
e: 789,
};
const t2 = #[1, #[2, 3, #[4], 5], 6];

View File

@ -0,0 +1,6 @@
{
"plugins": [["../../../../", {
"syntaxType": "hash",
"importPolyfill": true
}]]
}

View File

@ -0,0 +1,11 @@
import { Tuple as _Tuple } from "@bloomberg/record-tuple-polyfill";
import { Record as _Record } from "@bloomberg/record-tuple-polyfill";
const r2 = _Record({
a: _Record({
b: 456
}),
e: 789
});
const t2 = _Tuple(1, _Tuple(2, 3, _Tuple(4), 5), 6);

View File

@ -0,0 +1,23 @@
"use strict";
const r1 = {|
a: 1,
b: 2,
c: 3,
|};
const r2 = {|
a: {|
b: {|
c: 123,
|},
d: 456,
|},
e: 789,
|};
const t1 = [||];
const t2 = [|1,2,3|];
const t3 = [|1, [|2, 3, [|4|], 5|], 6|];

View File

@ -0,0 +1,3 @@
{
"plugins": [["../../../../", { "syntaxType": "bar" }]]
}

View File

@ -0,0 +1,19 @@
"use strict";
const r1 = Record({
a: 1,
b: 2,
c: 3
});
const r2 = Record({
a: Record({
b: Record({
c: 123
}),
d: 456
}),
e: 789
});
const t1 = Tuple();
const t2 = Tuple(1, 2, 3);
const t3 = Tuple(1, Tuple(2, 3, Tuple(4), 5), 6);

View File

@ -0,0 +1,21 @@
"use strict";
const r1 = #{
a: 1,
b: 2,
c: 3,
};
const r2 = #{
a: #{
b: #{
c: 123,
},
d: 456,
},
e: 789,
};
const t1 = #[1,2,3];
const t2 = #[1, #[2, 3, #[4], 5], 6];

View File

@ -0,0 +1,3 @@
{
"plugins": [["../../../../", { "syntaxType": "hash" }]]
}

View File

@ -0,0 +1,18 @@
"use strict";
const r1 = Record({
a: 1,
b: 2,
c: 3
});
const r2 = Record({
a: Record({
b: Record({
c: 123
}),
d: 456
}),
e: 789
});
const t1 = Tuple(1, 2, 3);
const t2 = Tuple(1, Tuple(2, 3, Tuple(4), 5), 6);

View File

@ -0,0 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);

View File

@ -1385,6 +1385,21 @@ __metadata:
languageName: unknown languageName: unknown
linkType: soft 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:*"
"@babel/helper-module-imports": "workspace:^7.12.13"
"@babel/helper-plugin-test-runner": "workspace:*"
"@babel/helper-plugin-utils": "workspace:^7.12.13"
"@babel/helper-validator-option": "workspace:^7.12.17"
"@babel/plugin-syntax-record-and-tuple": "workspace:^7.12.1"
peerDependencies:
"@babel/core": ^7.12.0
languageName: unknown
linkType: soft
"@babel/plugin-proposal-throw-expressions@workspace:*, @babel/plugin-proposal-throw-expressions@workspace:packages/babel-plugin-proposal-throw-expressions": "@babel/plugin-proposal-throw-expressions@workspace:*, @babel/plugin-proposal-throw-expressions@workspace:packages/babel-plugin-proposal-throw-expressions":
version: 0.0.0-use.local version: 0.0.0-use.local
resolution: "@babel/plugin-proposal-throw-expressions@workspace:packages/babel-plugin-proposal-throw-expressions" resolution: "@babel/plugin-proposal-throw-expressions@workspace:packages/babel-plugin-proposal-throw-expressions"
@ -1729,7 +1744,7 @@ __metadata:
languageName: unknown languageName: unknown
linkType: soft linkType: soft
"@babel/plugin-syntax-record-and-tuple@workspace:*, @babel/plugin-syntax-record-and-tuple@workspace:packages/babel-plugin-syntax-record-and-tuple": "@babel/plugin-syntax-record-and-tuple@workspace:*, @babel/plugin-syntax-record-and-tuple@workspace:^7.12.1, @babel/plugin-syntax-record-and-tuple@workspace:packages/babel-plugin-syntax-record-and-tuple":
version: 0.0.0-use.local version: 0.0.0-use.local
resolution: "@babel/plugin-syntax-record-and-tuple@workspace:packages/babel-plugin-syntax-record-and-tuple" resolution: "@babel/plugin-syntax-record-and-tuple@workspace:packages/babel-plugin-syntax-record-and-tuple"
dependencies: dependencies: