| 1 | /* |
| 2 | ** Copyright 2020 Bloomberg Finance L.P. |
| 3 | ** |
| 4 | ** Licensed under the MIT License (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
| 7 | ** |
| 8 | ** https://opensource.org/licenses/MIT |
| 9 | ** |
| 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
| 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import { declare } from "@babel/helper-plugin-utils"; |
| 18 | import syntaxRecordAndTuple from "@babel/plugin-syntax-record-and-tuple"; |
| 19 | import type { Options as SyntaxOptions } from "@babel/plugin-syntax-record-and-tuple"; |
| 20 | import { types as t } from "@babel/core"; |
| 21 | import { addNamed, isModule } from "@babel/helper-module-imports"; |
| 22 | import { OptionValidator } from "@babel/helper-validator-option"; |
| 23 | import type { NodePath } from "@babel/traverse"; |
| 24 | |
| 25 | declare const PACKAGE_JSON: { name: string; version: string }; |
| 26 | const v = new OptionValidator(PACKAGE_JSON.name); |
| 27 | |
| 28 | export interface Options extends SyntaxOptions { |
| 29 | polyfillModuleName?: string; |
| 30 | importPolyfill?: boolean; |
| 31 | } |
| 32 | |
| 33 | type State = { |
| 34 | programPath: NodePath<t.Program>; |
| 35 | }; |
| 36 | |
| 37 | // program -> cacheKey -> localBindingName |
| 38 | type Cache = Map<string, string>; |
| 39 | type ImportCache = WeakMap<t.Program, Cache>; |
| 40 | |
| 41 | export default declare<State>((api, options: Options) => { |
| 42 | api.assertVersion(7); |
| 43 | |
| 44 | const polyfillModuleName = v.validateStringOption( |
| 45 | "polyfillModuleName", |
| 46 | options.polyfillModuleName, |
| 47 | "@bloomberg/record-tuple-polyfill", |
| 48 | ); |
| 49 | const shouldImportPolyfill = v.validateBooleanOption( |
| 50 | "importPolyfill", |
| 51 | options.importPolyfill, |
| 52 | !!options.polyfillModuleName, |
| 53 | ); |
| 54 | |
| 55 | const importCaches: ImportCache = new WeakMap(); |
| 56 | |
| 57 | function getOr<K, V>(map: Map<K, V>, key: K, getDefault: () => V): V; |
| 58 | function getOr<K extends object, V>( |
| 59 | map: WeakMap<K, V>, |
| 60 | key: K, |
| 61 | getDefault: () => V, |
| 62 | ): V; |
| 63 | function getOr<K extends object, V>( |
| 64 | map: WeakMap<K, V>, |
| 65 | key: K, |
| 66 | getDefault: () => V, |
| 67 | ) { |
| 68 | let value = map.get(key); |
| 69 | if (!value) map.set(key, (value = getDefault())); |
| 70 | return value; |
| 71 | } |
| 72 | |
| 73 | function getBuiltIn( |
| 74 | name: "Record" | "Tuple", |
| 75 | programPath: NodePath<t.Program>, |
| 76 | ) { |
| 77 | if (!shouldImportPolyfill) return t.identifier(name); |
| 78 | if (!programPath) { |
| 79 | throw new Error("Internal error: unable to find the Program node."); |
| 80 | } |
| 81 | |
| 82 | const cacheKey = `${name}:${isModule(programPath)}`; |
| 83 | |
| 84 | const cache = getOr( |
| 85 | importCaches, |
| 86 | programPath.node, |
| 87 | () => new Map<string, string>(), |
| 88 | ); |
| 89 | const localBindingName = getOr(cache, cacheKey, () => { |
| 90 | return addNamed(programPath, name, polyfillModuleName, { |
| 91 | importedInterop: "uncompiled", |
| 92 | }).name; |
| 93 | }); |
| 94 | |
| 95 | return t.identifier(localBindingName); |
| 96 | } |
| 97 | |
| 98 | return { |
| 99 | name: "proposal-record-and-tuple", |
| 100 | inherits: syntaxRecordAndTuple, |
| 101 | visitor: { |
| 102 | Program(path, state) { |
| 103 | state.programPath = path; |
| 104 | }, |
| 105 | RecordExpression(path, state) { |
| 106 | const record = getBuiltIn("Record", state.programPath); |
| 107 | |
| 108 | const object = t.objectExpression(path.node.properties); |
| 109 | const wrapped = t.callExpression(record, [object]); |
| 110 | path.replaceWith(wrapped); |
| 111 | }, |
| 112 | TupleExpression(path, state) { |
| 113 | const tuple = getBuiltIn("Tuple", state.programPath); |
| 114 | |
| 115 | const wrapped = t.callExpression(tuple, path.node.elements); |
| 116 | path.replaceWith(wrapped); |
| 117 | }, |
| 118 | }, |
| 119 | }; |
| 120 | }); |