importName: escapeIdentifier()

in src/module-utils.ts [51:105]


    importName: escapeIdentifier(nonNamespacedPart.replace(/^aws-/g, '').replace(/[^a-z0-9_]/g, '_')),
    moduleName: type.assembly.name,
  };
}

/**
 * Namespaced name inside a module
 */
export function typeNamespacedName(type: reflect.Type): string {
  const parts = analyzeTypeName(type);

  return [
    ...parts.namespaceNameParts,
    parts.simpleName,
  ].join('.');
}

const KEYWORDS = ['function', 'default', 'arguments', 'enum'];

export function escapeIdentifier(ident: string): string {
  return KEYWORDS.includes(ident) ? `${ident}_` : ident;
}

export function moduleReference(type: reflect.Type) {
  const imp = new Import(type);
  return new Code(imp.importName, [imp]);
}

export function typeReference(type: reflect.Type) {
  return Code.concatAll(
    moduleReference(type),
    '.',
    typeNamespacedName(type));
}

/**
 * A type name consists of 4 parts which are all treated differently
 */
interface TypeNameParts {
  readonly assemblyName: string;
  readonly submoduleNameParts: string[];
  readonly namespaceNameParts: string[];
  readonly simpleName: string;
}

function analyzeTypeName(type: reflect.Type): TypeNameParts {
  // Need to divide the namespace into submodule and non-submodule

  // For type 'asm.b.c.d.Type' contains ['asm', 'b', 'c', 'd']
  const nsParts = type.fqn.split('.').slice(0, -1);

  const moduleFqns = new Set(type.assembly.allSubmodules.map((s) => s.fqn));

  let split = nsParts.length;
  while (split > 1 && !moduleFqns.has(nsParts.slice(0, split).join('.'))) {