private _validatedDecorator()

in tools/tslint-rules/validateDecoratorsRule.ts [63:95]


  private _validatedDecorator(decorator: any) {
    // Get the rules that are relevant for the current decorator.
    const rules = this._rules[decorator.expression.getText()];

    // Don't do anything if there are no rules.
    if (!rules) {
      return;
    }

    // Extract the property names and values.
    const props = decorator.arguments[0].properties.map((node: ts.PropertyAssignment) => ({
      name: node.name.getText(),
      value: node.initializer.getText(),
      node
    }));

    // Find all of the rule properties that are missing from the decorator.
    const missing = Object.keys(rules).filter(key => !props.find((prop: any) => prop.name === key));

    if (missing.length) {
      // Exit early if any of the properties are missing.
      this.addFailureAtNode(decorator.parent, 'Missing required properties: ' + missing.join(', '));
    } else {
      // If all the necessary properties are defined, ensure that they match the pattern.
      props
        .filter((prop: any) => rules[prop.name])
        .filter((prop: any) => !rules[prop.name].test(prop.value))
        .forEach((prop: any) => {
          this.addFailureAtNode(prop.node,
              `Invalid value for property. Expected value to match "${rules[prop.name]}".`);
        });
    }
  }