public process()

in packages/jsii/lib/transforms/deprecation-warnings.ts [24:134]


  public process(assembly: Assembly, projectInfo: ProjectInfo) {
    const projectRoot = projectInfo.projectRoot;
    const functionDeclarations: ts.FunctionDeclaration[] = [];

    const types = assembly.types ?? {};
    for (const type of Object.values(types)) {
      const statements: ts.Statement[] = [];
      let isEmpty = true;

      // This will add the parameter to the set of visited objects, to prevent infinite recursion
      statements.push(
        ts.createExpressionStatement(
          ts.createCall(
            ts.createIdentifier(`${VISITED_OBJECTS_SET_NAME}.add`),
            [],
            [ts.createIdentifier(PARAMETER_NAME)],
          ),
        ),
      );

      if (spec.isDeprecated(type) && spec.isEnumType(type)) {
        // The type is deprecated
        statements.push(
          createWarningFunctionCall(type.fqn, type.docs?.deprecated),
        );
        isEmpty = false;
      }

      if (spec.isEnumType(type) && type.locationInModule?.filename) {
        statements.push(
          createEnumRequireStatement(type.locationInModule?.filename),
        );
        statements.push(createDuplicateEnumValuesCheck(type));

        for (const member of Object.values(type.members ?? [])) {
          if (spec.isDeprecated(member)) {
            // The enum member is deprecated
            const condition = ts.createIdentifier(
              `${PARAMETER_NAME} === ${LOCAL_ENUM_NAMESPACE}.${type.name}.${member.name}`,
            );

            statements.push(
              createWarningFunctionCall(
                `${type.fqn}#${member.name}`,
                member.docs?.deprecated,
                condition,
              ),
            );
            isEmpty = false;
          }
        }
      } else if (spec.isInterfaceType(type) && type.datatype) {
        const { statementsByProp, excludedProps } = processInterfaceType(
          type,
          types,
          assembly,
          projectInfo,
        );

        for (const [name, statement] of statementsByProp.entries()) {
          if (!excludedProps.has(name)) {
            statements.push(statement);
            isEmpty = false;
          }
        }
      }
      statements.push(
        ts.createExpressionStatement(
          ts.createCall(
            ts.createIdentifier(`${VISITED_OBJECTS_SET_NAME}.delete`),
            [],
            [ts.createIdentifier(PARAMETER_NAME)],
          ),
        ),
      );

      const parameter = ts.createParameter(
        undefined,
        undefined,
        undefined,
        PARAMETER_NAME,
      );
      const functionName = fnName(type.fqn);
      const functionDeclaration = ts.createFunctionDeclaration(
        undefined,
        undefined,
        undefined,
        functionName,
        undefined,
        [parameter],
        undefined,
        createFunctionBlock(isEmpty ? [] : statements),
      );
      functionDeclarations.push(functionDeclaration);
    }
    this.transformers = {
      before: [
        (context) => {
          const transformer = new Transformer(
            this.typeChecker,
            context,
            projectRoot,
            this.buildTypeIndex(assembly),
            assembly,
          );
          return transformer.transform.bind(transformer);
        },
      ],
    };
    generateWarningsFile(projectRoot, functionDeclarations);
  }