| 2945 | |
| 2946 | /** @internal */ |
| 2947 | export const rename = (ast: AST, mapping: { readonly [K in PropertyKey]?: PropertyKey }): AST => { |
| 2948 | switch (ast._tag) { |
| 2949 | case "TypeLiteral": { |
| 2950 | const propertySignatureTransformations: Array<PropertySignatureTransformation> = [] |
| 2951 | for (const key of Reflect.ownKeys(mapping)) { |
| 2952 | const name = mapping[key] |
| 2953 | if (name !== undefined) { |
| 2954 | propertySignatureTransformations.push( |
| 2955 | new PropertySignatureTransformation( |
| 2956 | key, |
| 2957 | name, |
| 2958 | identity, |
| 2959 | identity |
| 2960 | ) |
| 2961 | ) |
| 2962 | } |
| 2963 | } |
| 2964 | if (propertySignatureTransformations.length === 0) { |
| 2965 | return ast |
| 2966 | } |
| 2967 | return new Transformation( |
| 2968 | ast, |
| 2969 | new TypeLiteral( |
| 2970 | ast.propertySignatures.map((ps) => { |
| 2971 | const name = mapping[ps.name] |
| 2972 | return new PropertySignature( |
| 2973 | name === undefined ? ps.name : name, |
| 2974 | typeAST(ps.type), |
| 2975 | ps.isOptional, |
| 2976 | ps.isReadonly, |
| 2977 | ps.annotations |
| 2978 | ) |
| 2979 | }), |
| 2980 | ast.indexSignatures |
| 2981 | ), |
| 2982 | new TypeLiteralTransformation(propertySignatureTransformations) |
| 2983 | ) |
| 2984 | } |
| 2985 | case "Union": |
| 2986 | return Union.make(ast.types.map((ast) => rename(ast, mapping))) |
| 2987 | case "Suspend": |
| 2988 | return new Suspend(() => rename(ast.f(), mapping)) |
| 2989 | case "Transformation": |
| 2990 | return compose(ast, rename(typeAST(ast), mapping)) |
| 2991 | } |
| 2992 | throw new Error(errors_.getASTUnsupportedRenameSchemaErrorMessage(ast)) |
| 2993 | } |
| 2994 | |
| 2995 | const formatKeyword = (ast: AST): string => Option.getOrElse(getExpected(ast), () => ast._tag) |
| 2996 | |