function openSignature()

in packages/jsii-pacmak/lib/targets/python.ts [3126:3184]


function openSignature(
  code: CodeMaker,
  keyword: 'class',
  name: string,
  params: readonly string[],
): void;
function openSignature(
  code: CodeMaker,
  keyword: 'def',
  name: string,
  params: readonly string[],
  returnType: string,
  comment?: string,
): void;
function openSignature(
  code: CodeMaker,
  keyword: 'class' | 'def',
  name: string,
  params: readonly string[],
  returnType?: string,
  lineComment?: string,
) {
  const prefix = `${keyword} ${name}`;
  const suffix = returnType ? ` -> ${returnType}` : '';
  if (params.length === 0) {
    code.openBlock(`${prefix}${returnType ? '()' : ''}${suffix}`);
    return;
  }

  const join = ', ';
  const { elementsSize, joinSize } = totalSizeOf(params, join);

  const hasComments = params.some((param) => /#\s*.+$/.exec(param) != null);

  if (
    !hasComments &&
    TARGET_LINE_LENGTH >
      code.currentIndentLength +
        prefix.length +
        elementsSize +
        joinSize +
        suffix.length +
        2
  ) {
    code.indent(
      `${prefix}(${params.join(join)})${suffix}:${
        lineComment ? `  # ${lineComment}` : ''
      }`,
    );
    return;
  }

  code.indent(`${prefix}(`);
  for (const param of params) {
    code.line(param.replace(/(\s*# .+)?$/, ',$1'));
  }
  code.unindent(false);
  code.indent(`)${suffix}:${lineComment ? `  # ${lineComment}` : ''}`);
}