Skip to content

Commit c4a422a

Browse files
crisbetoandrewseguin
authored andcommitted
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})`).
1 parent e085664 commit c4a422a

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

tools/tslint-rules/validateDecoratorsRule.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ class Walker extends Lint.RuleWalker {
5858
visitClassDeclaration(node: ts.ClassDeclaration) {
5959
if (this._enabled && node.decorators) {
6060
node.decorators
61-
.map(decorator => decorator.expression as any)
62-
.filter(expression => expression.arguments.length && expression.arguments[0].properties)
61+
.map(decorator => decorator.expression as ts.CallExpression)
62+
.filter(expression => {
63+
const args = expression.arguments;
64+
return args && args.length && (args[0] as ts.ObjectLiteralExpression).properties;
65+
})
6366
.forEach(expression => this._validatedDecorator(expression));
6467
}
6568

@@ -80,11 +83,13 @@ class Walker extends Lint.RuleWalker {
8083
}
8184

8285
// Extract the property names and values.
83-
const props = decorator.arguments[0].properties.map((node: ts.PropertyAssignment) => ({
84-
name: node.name.getText(),
85-
value: node.initializer.getText(),
86-
node
87-
}));
86+
const props = decorator.arguments[0].properties
87+
.filter((node: ts.PropertyAssignment) => node.name && node.initializer)
88+
.map((node: ts.PropertyAssignment) => ({
89+
name: node.name.getText(),
90+
value: node.initializer.getText(),
91+
node
92+
}));
8893

8994
// Find all of the required rule properties that are missing from the decorator.
9095
const missing = Object.keys(rules.required)

0 commit comments

Comments
 (0)