From c4a422abade817486d8176ec5b69e0941320c936 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 10 Oct 2018 21:27:12 +0300 Subject: [PATCH] build: fix custom lint rule throwing errors in IDE on invalid files (#13488) Fixes errors being thrown by the decorator validation rule when run in an IDE. The errors can happen, because the rule is run for each key stroke and the user might not be done typing out the definition yet. The two cases where it was throwing were when a decorator is added without being called (e.g. `@Component class Something`) and when defining a new property that doesn't have an initializer yet (e.g. `@Component({selector})`). --- tools/tslint-rules/validateDecoratorsRule.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tools/tslint-rules/validateDecoratorsRule.ts b/tools/tslint-rules/validateDecoratorsRule.ts index c4a7efc970b..b3c49b0e30b 100644 --- a/tools/tslint-rules/validateDecoratorsRule.ts +++ b/tools/tslint-rules/validateDecoratorsRule.ts @@ -58,8 +58,11 @@ class Walker extends Lint.RuleWalker { visitClassDeclaration(node: ts.ClassDeclaration) { if (this._enabled && node.decorators) { node.decorators - .map(decorator => decorator.expression as any) - .filter(expression => expression.arguments.length && expression.arguments[0].properties) + .map(decorator => decorator.expression as ts.CallExpression) + .filter(expression => { + const args = expression.arguments; + return args && args.length && (args[0] as ts.ObjectLiteralExpression).properties; + }) .forEach(expression => this._validatedDecorator(expression)); } @@ -80,11 +83,13 @@ class Walker extends Lint.RuleWalker { } // 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 - })); + const props = decorator.arguments[0].properties + .filter((node: ts.PropertyAssignment) => node.name && node.initializer) + .map((node: ts.PropertyAssignment) => ({ + name: node.name.getText(), + value: node.initializer.getText(), + node + })); // Find all of the required rule properties that are missing from the decorator. const missing = Object.keys(rules.required)