|
11 | 11 | */
|
12 | 12 | import {existsSync, readFileSync} from 'fs';
|
13 | 13 | import {dirname, join, resolve} from 'path';
|
14 |
| -import {Fix, IOptions, RuleFailure, RuleWalker} from 'tslint'; |
| 14 | +import {IOptions, RuleWalker} from 'tslint'; |
15 | 15 | import * as ts from 'typescript';
|
16 | 16 | import {getLiteralTextWithoutQuotes} from '../typescript/literal';
|
17 | 17 | import {createComponentFile, ExternalResource} from './component-file';
|
@@ -49,7 +49,13 @@ export class ComponentWalker extends RuleWalker {
|
49 | 49 | }
|
50 | 50 |
|
51 | 51 | private _visitDirectiveCallExpression(callExpression: ts.CallExpression) {
|
52 |
| - const directiveMetadata = callExpression.arguments[0] as ts.ObjectLiteralExpression; |
| 52 | + // If the call expressions does not have the correct amount of arguments, we can assume that |
| 53 | + // this call expression is not related to Angular and just uses a similar decorator name. |
| 54 | + if (callExpression.arguments.length !== 1) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const directiveMetadata = this._findMetadataFromExpression(callExpression.arguments[0]); |
53 | 59 |
|
54 | 60 | if (!directiveMetadata) {
|
55 | 61 | return;
|
@@ -130,11 +136,20 @@ export class ComponentWalker extends RuleWalker {
|
130 | 136 | this.visitExternalStylesheet(stylesheetFile);
|
131 | 137 | }
|
132 | 138 |
|
133 |
| - /** Creates a TSLint rule failure for the given external resource. */ |
134 |
| - protected addExternalResourceFailure(file: ExternalResource, message: string, fix?: Fix) { |
135 |
| - const ruleFailure = new RuleFailure(file, file.getStart(), file.getEnd(), |
136 |
| - message, this.getRuleName(), fix); |
| 139 | + /** |
| 140 | + * Recursively searches for the metadata object literal expression inside of a directive call |
| 141 | + * expression. Since expression calls can be nested through *parenthesized* expressions, we |
| 142 | + * need to recursively visit and check every expression inside of a parenthesized expression. |
| 143 | + * |
| 144 | + * e.g. @Component((({myMetadataExpression}))) will return `myMetadataExpression`. |
| 145 | + */ |
| 146 | + private _findMetadataFromExpression(node: ts.Expression): ts.ObjectLiteralExpression | null { |
| 147 | + if (node.kind === ts.SyntaxKind.ObjectLiteralExpression) { |
| 148 | + return node as ts.ObjectLiteralExpression; |
| 149 | + } else if (node.kind === ts.SyntaxKind.ParenthesizedExpression) { |
| 150 | + return this._findMetadataFromExpression((node as ts.ParenthesizedExpression).expression); |
| 151 | + } |
137 | 152 |
|
138 |
| - this.addFailure(ruleFailure); |
| 153 | + return null; |
139 | 154 | }
|
140 | 155 | }
|
0 commit comments