{"version":3,"names":["_isValidIdentifier","require","_index","_default","exports","default","valueToNode","objectToString","Function","call","bind","Object","prototype","toString","isRegExp","value","isPlainObject","proto","getPrototypeOf","undefined","identifier","booleanLiteral","nullLiteral","stringLiteral","result","Number","isFinite","numericLiteral","Math","abs","numerator","isNaN","binaryExpression","is","unaryExpression","pattern","source","flags","exec","regExpLiteral","Array","isArray","arrayExpression","map","props","key","keys","nodeKey","isValidIdentifier","push","objectProperty","objectExpression","Error"],"sources":["../../src/converters/valueToNode.ts"],"sourcesContent":["import isValidIdentifier from \"../validators/isValidIdentifier.ts\";\nimport {\n identifier,\n booleanLiteral,\n nullLiteral,\n stringLiteral,\n numericLiteral,\n regExpLiteral,\n arrayExpression,\n objectProperty,\n objectExpression,\n unaryExpression,\n binaryExpression,\n} from \"../builders/generated/index.ts\";\nimport type * as t from \"../index.ts\";\n\nexport default valueToNode as {\n (value: undefined): t.Identifier; // TODO: This should return \"void 0\"\n (value: boolean): t.BooleanLiteral;\n (value: null): t.NullLiteral;\n (value: string): t.StringLiteral;\n // Infinities and NaN need to use a BinaryExpression; negative values must be wrapped in UnaryExpression\n (value: number): t.NumericLiteral | t.BinaryExpression | t.UnaryExpression;\n (value: RegExp): t.RegExpLiteral;\n (value: ReadonlyArray): t.ArrayExpression;\n\n // this throws with objects that are not plain objects,\n // or if there are non-valueToNode-able values\n (value: object): t.ObjectExpression;\n\n (value: unknown): t.Expression;\n};\n\n// @ts-expect-error: Object.prototype.toString must return a string\nconst objectToString: (value: unknown) => string = Function.call.bind(\n Object.prototype.toString,\n);\n\nfunction isRegExp(value: unknown): value is RegExp {\n return objectToString(value) === \"[object RegExp]\";\n}\n\nfunction isPlainObject(value: unknown): value is object {\n if (\n typeof value !== \"object\" ||\n value === null ||\n Object.prototype.toString.call(value) !== \"[object Object]\"\n ) {\n return false;\n }\n const proto = Object.getPrototypeOf(value);\n // Object.prototype's __proto__ is null. Every other class's __proto__.__proto__ is\n // not null by default. We cannot check if proto === Object.prototype because it\n // could come from another realm.\n return proto === null || Object.getPrototypeOf(proto) === null;\n}\n\nfunction valueToNode(value: unknown): t.Expression {\n // undefined\n if (value === undefined) {\n return identifier(\"undefined\");\n }\n\n // boolean\n if (value === true || value === false) {\n return booleanLiteral(value);\n }\n\n // null\n if (value === null) {\n return nullLiteral();\n }\n\n // strings\n if (typeof value === \"string\") {\n return stringLiteral(value);\n }\n\n // numbers\n if (typeof value === \"number\") {\n let result;\n if (Number.isFinite(value)) {\n result = numericLiteral(Math.abs(value));\n } else {\n let numerator;\n if (Number.isNaN(value)) {\n // NaN\n numerator = numericLiteral(0);\n } else {\n // Infinity / -Infinity\n numerator = numericLiteral(1);\n }\n\n result = binaryExpression(\"/\", numerator, numericLiteral(0));\n }\n\n if (value < 0 || Object.is(value, -0)) {\n result = unaryExpression(\"-\", result);\n }\n\n return result;\n }\n\n // regexes\n if (isRegExp(value)) {\n const pattern = value.source;\n const flags = /\\/([a-z]*)$/.exec(value.toString())[1];\n return regExpLiteral(pattern, flags);\n }\n\n // array\n if (Array.isArray(value)) {\n return arrayExpression(value.map(valueToNode));\n }\n\n // object\n if (isPlainObject(value)) {\n const props = [];\n for (const key of Object.keys(value)) {\n let nodeKey;\n if (isValidIdentifier(key)) {\n nodeKey = identifier(key);\n } else {\n nodeKey = stringLiteral(key);\n }\n props.push(\n objectProperty(\n nodeKey,\n valueToNode(\n // @ts-expect-error key must present in value\n value[key],\n ),\n ),\n );\n }\n return objectExpression(props);\n }\n\n throw new Error(\"don't know how to turn this value into a node\");\n}\n"],"mappings":";;;;;;AAAA,IAAAA,kBAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAYwC,IAAAE,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAGzBC,WAAW;AAkB1B,MAAMC,cAA0C,GAAGC,QAAQ,CAACC,IAAI,CAACC,IAAI,CACnEC,MAAM,CAACC,SAAS,CAACC,QACnB,CAAC;AAED,SAASC,QAAQA,CAACC,KAAc,EAAmB;EACj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