Debian Codegraph @

packages/babel-plugin-proposal-record-and-tuple/src/index.ts

raw symbols SCIP
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 { declarescip-typescript npm @babel/helper-plugin-utils 7.20.2 src/`index.ts`/declare(). } from "@babel/helper-plugin-utils"scip-typescript npm @babel/helper-plugin-utils 7.20.2 src/`index.ts`/;
18 import syntaxRecordAndTuple from "@babel/plugin-syntax-record-and-tuple"scip-typescript npm @babel/plugin-syntax-record-and-tuple 7.18.6 src/`index.ts`/;
19 import type { Optionsscip-typescript npm @babel/plugin-syntax-record-and-tuple 7.18.6 src/`index.ts`/Options# as SyntaxOptionsscip-typescript npm @babel/plugin-syntax-record-and-tuple 7.18.6 src/`index.ts`/Options# } from "@babel/plugin-syntax-record-and-tuple"scip-typescript npm @babel/plugin-syntax-record-and-tuple 7.18.6 src/`index.ts`/;
20 import { types as tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/ } from "@babel/core"scip-typescript npm @babel/core 7.20.12 src/`index.ts`/;
21 import { addNamedscip-typescript npm @babel/helper-module-imports 7.18.6 src/`index.ts`/addNamed()., isModulescip-typescript npm @babel/helper-module-imports 7.18.6 src/`is-module.ts`/isModule(). } from "@babel/helper-module-imports"scip-typescript npm @babel/helper-module-imports 7.18.6 src/`index.ts`/;
22 import { OptionValidatorscip-typescript npm @babel/helper-validator-option 7.18.6 src/`validator.ts`/OptionValidator# } from "@babel/helper-validator-option"scip-typescript npm @babel/helper-validator-option 7.18.6 src/`index.ts`/;
23 import type { NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# } from "@babel/traverse"scip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/;
24
25 declare const PACKAGE_JSONscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/PACKAGE_JSON.: { namescip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/PACKAGE_JSON.typeLiteral3:name.: string; versionscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/PACKAGE_JSON.typeLiteral3:version.: string };
26 const vscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/v. = new OptionValidatorscip-typescript npm @babel/helper-validator-option 7.18.6 src/`validator.ts`/OptionValidator#`<constructor>`().(PACKAGE_JSONscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/PACKAGE_JSON..namescip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/PACKAGE_JSON.typeLiteral3:name.);
27
28 export interface Optionsscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/Options# extends SyntaxOptionsscip-typescript npm @babel/plugin-syntax-record-and-tuple 7.18.6 src/`index.ts`/Options# {
29 polyfillModuleNamescip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/Options#polyfillModuleName.?: string;
30 importPolyfillscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/Options#importPolyfill.?: boolean;
31 }
32
33 type Statescip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/State# = {
34 programPathscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/State#typeLiteral4:programPath.: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Programscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Program#>;
35 };
36
37 // program -> cacheKey -> localBindingName
38 type Cachescip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/Cache# = Mapscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#<string, string>;
39 type ImportCachescip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/ImportCache# = WeakMapscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakMap#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Programscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Program#, Cachescip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/Cache#>;
40
41 export default declarescip-typescript npm @babel/helper-plugin-utils 7.20.2 src/`index.ts`/declare().<Statescip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/State#>((api, options: Optionsscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/Options#) => {
42 api.assertVersionscip-typescript npm @babel/core 7.20.12 src/config/helpers/`config-api.ts`/ConfigAPI#typeLiteral7:assertVersion.(7);
43
44 const polyfillModuleName = vscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/v..validateStringOptionscip-typescript npm @babel/helper-validator-option 7.18.6 src/`validator.ts`/OptionValidator#validateStringOption().(
45 "polyfillModuleName",
46 options.polyfillModuleNamescip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/Options#polyfillModuleName.,
47 "@bloomberg/record-tuple-polyfill",
48 );
49 const shouldImportPolyfill = vscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/v..validateBooleanOptionscip-typescript npm @babel/helper-validator-option 7.18.6 src/`validator.ts`/OptionValidator#validateBooleanOption().(
50 "importPolyfill",
51 options.importPolyfillscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/Options#importPolyfill.,
52 !!options.polyfillModuleNamescip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/Options#polyfillModuleName.,
53 );
54
55 const importCaches: ImportCachescip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/ImportCache# = new WeakMapscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakMap#();
56
57 function getOr<K, V>(map: Mapscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#<K, V>, key: K, getDefault: () => V): V;
58 function getOr<K extends object, V>(
59 map: WeakMapscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakMap#<K, V>,
60 key: K,
61 getDefault: () => V,
62 ): V;
63 function getOr<K extends object, V>(
64 map: WeakMapscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakMap#<K, V>,
65 key: K,
66 getDefault: () => V,
67 ) {
68 let value = map.getscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakMap#get().(key);
69 if (!value) map.setscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakMap#set().(key, (value = getDefault()));
70 return value;
71 }
72
73 function getBuiltIn(
74 name: "Record" | "Tuple",
75 programPath: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Programscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Program#>,
76 ) {
77 if (!shouldImportPolyfill) return tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.identifierscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/identifier().(name);
78 if (!programPath) {
79 throw new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#("Internal error: unable to find the Program node.");
80 }
81
82 const cacheKey = `${name}:${isModulescip-typescript npm @babel/helper-module-imports 7.18.6 src/`is-module.ts`/isModule().(programPath)}`;
83
84 const cache = getOr(
85 importCaches,
86 programPath.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.,
87 () => new Mapscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Map#<string, string>(),
88 );
89 const localBindingName = getOr(cache, cacheKey, () => {
90 return addNamedscip-typescript npm @babel/helper-module-imports 7.18.6 src/`index.ts`/addNamed().(programPath, name, polyfillModuleName, {
91 importedInteropscip-typescript npm @babel/helper-module-imports 7.18.6 src/`import-injector.ts`/ImportOptions#typeLiteral1:importedInterop.: "uncompiled",
92 }).namescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#name.;
93 });
94
95 return tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.identifierscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/identifier().(localBindingName);
96 }
97
98 return {
99 namescip-typescript npm @babel/core 7.20.12 src/config/validation/`plugins.ts`/PluginObject#typeLiteral0:name.: "proposal-record-and-tuple",
100 inheritsscip-typescript npm @babel/core 7.20.12 src/config/validation/`plugins.ts`/PluginObject#typeLiteral0:inherits.: syntaxRecordAndTuple,
101 visitorscip-typescript npm @babel/core 7.20.12 src/config/validation/`plugins.ts`/PluginObject#typeLiteral0:visitor.: {
102 Program(path, state) {
103 state.programPathscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/State#typeLiteral4:programPath. = path;
104 },
105 RecordExpression(path, state) {
106 const record = getBuiltIn("Record", state.programPathscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/State#typeLiteral4:programPath.);
107
108 const object = tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.objectExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/objectExpression().(path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..propertiesscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/RecordExpression#properties.);
109 const wrapped = tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.callExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/callExpression().(record, [object]);
110 path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(wrapped);
111 },
112 TupleExpression(path, state) {
113 const tuple = getBuiltIn("Tuple", state.programPathscip-typescript npm @babel/plugin-proposal-record-and-tuple 7.18.6 src/`index.ts`/State#typeLiteral4:programPath.);
114
115 const wrapped = tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.callExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/callExpression().(tuple, path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..elementsscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/TupleExpression#elements.);
116 path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(wrapped);
117 },
118 },
119 };
120 });