Debian Codegraph @

packages/babel-types/src/converters/toKeyAlias.ts

raw
1 import { isIdentifier, isStringLiteral } from "../validators/generated";
2 import cloneNode from "../clone/cloneNode";
3 import removePropertiesDeep from "../modifications/removePropertiesDeep";
4 import type * as t from "..";
5
6 export default function toKeyAlias(
7 node: t.Method | t.Property,
8 key: t.Node = node.key,
9 ): string {
10 let alias;
11
12 // @ts-expect-error todo(flow->ts): maybe add node type check before checking `.kind`
13 if (node.kind === "method") {
14 return toKeyAlias.increment() + "";
15 } else if (isIdentifier(key)) {
16 alias = key.name;
17 } else if (isStringLiteral(key)) {
18 alias = JSON.stringify(key.value);
19 } else {
20 alias = JSON.stringify(removePropertiesDeep(cloneNode(key)));
21 }
22
23 // @ts-expect-error todo(flow->ts): maybe add node type check before checking `.computed`
24 if (node.computed) {
25 alias = `[${alias}]`;
26 }
27
28 // @ts-expect-error todo(flow->ts): maybe add node type check before checking `.static`
29 if (node.static) {
30 alias = `static:${alias}`;
31 }
32
33 return alias;
34 }
35
36 toKeyAlias.uid = 0;
37
38 toKeyAlias.increment = function () {
39 if (toKeyAlias.uid >= Number.MAX_SAFE_INTEGER) {
40 return (toKeyAlias.uid = 0);
41 } else {
42 return toKeyAlias.uid++;
43 }
44 };