* Validates that a decorator matches all of the defined rules. * @param decorator Decorator to be checked.
(decorator: ts.Decorator)
| 102 | * @param decorator Decorator to be checked. |
| 103 | */ |
| 104 | private _validateDecorator(decorator: ts.Decorator) { |
| 105 | const expression = decorator.expression; |
| 106 | |
| 107 | if (!expression || !ts.isCallExpression(expression)) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | // Get the rules that are relevant for the current decorator. |
| 112 | const rulesList = this._rules[expression.expression.getText()]; |
| 113 | const args = expression.arguments; |
| 114 | |
| 115 | // Don't do anything if there are no rules. |
| 116 | if (!rulesList) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | for (const rules of rulesList) { |
| 121 | const allPropsRequirement = rules.requiredProps[ALL_PROPS_TOKEN]; |
| 122 | |
| 123 | // If we have a rule that applies to all properties, we just run it through once and we exit. |
| 124 | if (allPropsRequirement) { |
| 125 | const argumentText = args[rules.argument] ? args[rules.argument].getText() : ''; |
| 126 | if (!allPropsRequirement.test(argumentText)) { |
| 127 | this.addFailureAtNode( |
| 128 | expression.parent, |
| 129 | `Expected decorator argument ${rules.argument} ` + `to match "${allPropsRequirement}"`, |
| 130 | ); |
| 131 | } |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if (!args[rules.argument]) { |
| 136 | if (rules.required) { |
| 137 | this.addFailureAtNode( |
| 138 | expression.parent, |
| 139 | `Missing required argument at index ${rules.argument}`, |
| 140 | ); |
| 141 | } |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | if (!ts.isObjectLiteralExpression(args[rules.argument])) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | // Extract the property names and values. |
| 150 | const props: {name: string; value: string; node: ts.PropertyAssignment}[] = []; |
| 151 | |
| 152 | (args[rules.argument] as ts.ObjectLiteralExpression).properties.forEach(prop => { |
| 153 | if (ts.isPropertyAssignment(prop) && prop.name && prop.initializer) { |
| 154 | props.push({ |
| 155 | name: prop.name.getText(), |
| 156 | value: prop.initializer.getText(), |
| 157 | node: prop, |
| 158 | }); |
| 159 | } |
| 160 | }); |
| 161 |
no test coverage detected