How to use jsii-reflect - 10 common examples

To help you get started, we’ve selected a few jsii-reflect examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aws / jsii / packages / jsii-diff / lib / classes-ifaces.ts View on Github external
violator: original,
        message: `was ${origQual}, is now ${updQual}`
      });
    }
  }

  if (original.variadic && !updated.variadic) {
    // Once variadic, can never be made non-variadic anymore (because I could always have been passing N+1 arguments)
    context.mismatches.report({
      ruleKey: 'changed-variadic',
      violator: original,
      message: 'used to be variadic, not variadic anymore.'
    });
  }

  if (reflect.isMethod(original) && reflect.isMethod(updated)) {
    const retAna = isCompatibleReturnType(original.returns, updated.returns);
    if (!retAna.success) {
      context.mismatches.report({
        ruleKey: 'change-return-type',
        violator: original,
        message: `returns ${describeOptionalValueMatchingFailure(original.returns, updated.returns, retAna)}`
      });
    }
  }

  // Check that every original parameter can still be mapped to a parameter in the updated method
  original.parameters.forEach((param, i) => {
    const updatedParam = findParam(updated.parameters, i);
    if (updatedParam === undefined) {
      context.mismatches.report({
        ruleKey: 'removed-argument',
github aws / jsii / packages / jsii-diff / lib / classes-ifaces.ts View on Github external
// JSII assembler has already taken care of inheritance here
    if (original.initializer && updated.initializer) {
      compareMethod(original.initializer, updated.initializer, context);
    }
  }

  if (original.docs.subclassable && !updated.docs.subclassable) {
    context.mismatches.report({
      ruleKey: 'remove-subclassable',
      message: 'has gone from @subclassable to non-@subclassable',
      violator: original,
    });
  }

  for (const [origMethod, updatedElement] of memberPairs(original, original.allMethods, updated, context)) {
    if (reflect.isMethod(origMethod) && reflect.isMethod(updatedElement)) {
      compareMethod(origMethod, updatedElement, context);
    }
  }

  for (const [origProp, updatedElement] of memberPairs(original, original.allProperties, updated, context)) {
    if (reflect.isProperty(origProp) && reflect.isProperty(updatedElement)) {
      compareProperty(origProp, updatedElement, context);
    }
  }

  // You cannot have added abstract members to the class/interface, as they are
  // an added burden on potential implementors.
  //
  // Only for types that are explicitly marked as intended to be subclassed by customers.
  if (subclassableType(original)) {
    noNewAbstractMembers(original, updated, context);
github aws / jsii / packages / jsii-diff / lib / classes-ifaces.ts View on Github external
function compareMethod(
  original: T,
  updated: T,
  context: ComparisonContext) {
  compareStabilities(original, updated, context);

  // Type guards on original are duplicated on updated to help tsc... They are required to be the same type by the declaration.
  if (reflect.isMethod(original) && reflect.isMethod(updated)) {
    if (original.static !== updated.static) {
      const origQual = original.static ? 'static' : 'not static';
      const updQual = updated.static ? 'static' : 'not static';
      context.mismatches.report({
        ruleKey: 'changed-static',
        violator: original,
        message: `was ${origQual}, is now ${updQual}.`
      });
    }

    if (original.async !== updated.async) {
      const origQual = original.async ? 'asynchronous' : 'synchronous';
      const updQual = updated.async ? 'asynchronous' : 'synchronous';
      context.mismatches.report({
        ruleKey: 'changed-async',
        violator: original,
github aws / aws-cdk / tools / awslint / bin / awslint.ts View on Github external
async function loadModule(dir: string) {
  const ts = new reflect.TypeSystem();
  await ts.load(dir, { validate: false }); // Don't validate to save 66% of execution time (20s vs 1min).
  // We run 'awslint' during build time, assemblies are guaranteed to be ok.

  if (ts.roots.length !== 1) {
    throw new Error(`Expecting only a single root assembly`);
  }

  return ts.roots[0];
}
github aws / aws-cdk / packages / decdk / lib / jsii2schema.ts View on Github external
if (typeOrTypeRef.type) {
      type = typeOrTypeRef.type;
    } else {
      return false;
    }
  }

  // if it is an interface, it should extend cdk.IConstruct
  if (type instanceof jsiiReflect.InterfaceType) {
    const constructIface = type.system.findFqn('@aws-cdk/core.IConstruct');
    return type.extends(constructIface);
  }

  // if it is a class, it should extend cdk.Construct
  if (type instanceof jsiiReflect.ClassType) {
    const constructClass = type.system.findFqn('@aws-cdk/core.Construct');
    return type.extends(constructClass);
  }

  return false;
}
github aws / aws-cdk / packages / decdk / lib / jsii2schema.ts View on Github external
return isConstruct(typeOrTypeRef.mapOfType);
    }

    if (typeOrTypeRef.unionOfTypes) {
      return typeOrTypeRef.unionOfTypes.some(x => isConstruct(x));
    }

    if (typeOrTypeRef.type) {
      type = typeOrTypeRef.type;
    } else {
      return false;
    }
  }

  // if it is an interface, it should extend cdk.IConstruct
  if (type instanceof jsiiReflect.InterfaceType) {
    const constructIface = type.system.findFqn('@aws-cdk/core.IConstruct');
    return type.extends(constructIface);
  }

  // if it is a class, it should extend cdk.Construct
  if (type instanceof jsiiReflect.ClassType) {
    const constructClass = type.system.findFqn('@aws-cdk/core.Construct');
    return type.extends(constructClass);
  }

  return false;
}
github aws / aws-cdk / packages / decdk / lib / declarative-stack.ts View on Github external
throw new ValidationError(`Failed to deserialize union. Errors: \n  ${errors.map(e => e.message).join('\n  ')}`);
  }

  const enm = deconstructEnum(stack, typeRef, key, value);
  if (enm) {
    return enm;
  }

  // if this is an interface, deserialize each property
  const ifc = deconstructInterface(stack, typeRef, key, value);
  if (ifc) {
    return ifc;
  }

  // if this is an enum type, use the name to dereference
  if (typeRef.type instanceof reflect.EnumType) {
    const enumType = resolveType(typeRef.type.fqn);
    return enumType[value];
  }

  if (typeRef.primitive) {
    return value;
  }

  const enumLike = deconstructEnumLike(stack, typeRef, value);
  if (enumLike) {
    return enumLike;
  }

  const asType = deconstructType(stack, typeRef, value);
  if (asType) {
    return asType;
github aws / aws-cdk / packages / decdk / lib / declarative-stack.ts View on Github external
// we are promised there are no conflicts
      const kwargs = deserializeValue(stack, p.type, p.optional, p.name, parameters);
      args.push(kwargs);
    } else {
      const val = parameters[p.name];
      if (val === undefined && !p.optional) {
        throw new Error(`Missing required parameter '${p.name}' for ${method.parentType.fqn}.${method.name}`);
      }

      if (val !== undefined) {
        args.push(deserializeValue(stack, p.type, p.optional, p.name, val));
      }
    }
  }

  if (reflect.Initializer.isInitializer(method)) {
    return new typeClass(...args);
  }

  const methodFn: (...args: any[]) => any = typeClass[method.name];
  if (!methodFn) {
    throw new Error(`Cannot find method named ${method.name} in ${typeClass.fqn}`);
  }

  return methodFn.apply(typeClass, args);
}
github aws / jsii / packages / jsii-diff / lib / classes-ifaces.ts View on Github external
if (original.docs.subclassable && !updated.docs.subclassable) {
    context.mismatches.report({
      ruleKey: 'remove-subclassable',
      message: 'has gone from @subclassable to non-@subclassable',
      violator: original,
    });
  }

  for (const [origMethod, updatedElement] of memberPairs(original, original.allMethods, updated, context)) {
    if (reflect.isMethod(origMethod) && reflect.isMethod(updatedElement)) {
      compareMethod(origMethod, updatedElement, context);
    }
  }

  for (const [origProp, updatedElement] of memberPairs(original, original.allProperties, updated, context)) {
    if (reflect.isProperty(origProp) && reflect.isProperty(updatedElement)) {
      compareProperty(origProp, updatedElement, context);
    }
  }

  // You cannot have added abstract members to the class/interface, as they are
  // an added burden on potential implementors.
  //
  // Only for types that are explicitly marked as intended to be subclassed by customers.
  if (subclassableType(original)) {
    noNewAbstractMembers(original, updated, context);
  }
}
github aws / aws-cdk / packages / decdk / lib / declarative-stack.ts View on Github external
throw new ValidationError('Schema validation errors:\n  ' + result.errors.map(e => `"${e.property}" ${e.message}`).join('\n  '));
    }

    // Replace every resource that starts with CDK::
    for (const [logicalId, resourceProps] of Object.entries(template.Resources || {})) {
      const rprops: any = resourceProps;
      if (!rprops.Type) {
        throw new Error('Resource is missing type: ' + JSON.stringify(resourceProps));
      }

      if (isCfnResourceType(rprops.Type)) {
        continue;
      }

      const typeInfo = typeSystem.findFqn(rprops.Type + 'Props');
      const typeRef = new reflect.TypeReference(typeSystem, typeInfo);
      const Ctor = resolveType(rprops.Type);

      // Changing working directory if needed, such that relative paths in the template are resolved relative to the
      // template's location, and not to the current process' CWD.
      _cwd(props.workingDirectory, () =>
        new Ctor(this, logicalId, deserializeValue(this, typeRef, true, 'Properties', rprops.Properties)));

      delete template.Resources[logicalId];
    }

    delete template.$schema;

    // Add an Include construct with what's left of the template
    new cdk.CfnInclude(this, 'Include', { template });

    // replace all "Fn::GetAtt" with tokens that resolve correctly both for