|
3 | 3 | */
|
4 | 4 | import {existsSync, readFileSync} from 'fs'
|
5 | 5 | import {dirname, join, resolve} from 'path';
|
6 |
| -import {Fix, IOptions, RuleFailure, RuleWalker} from 'tslint'; |
| 6 | +import {IOptions, RuleWalker} from 'tslint'; |
7 | 7 | import * as ts from 'typescript';
|
8 | 8 | import {getLiteralTextWithoutQuotes} from '../typescript/literal';
|
9 | 9 | import {createComponentFile, ExternalResource} from "./component-file";
|
@@ -41,7 +41,13 @@ export class ComponentWalker extends RuleWalker {
|
41 | 41 | }
|
42 | 42 |
|
43 | 43 | private _visitDirectiveCallExpression(callExpression: ts.CallExpression) {
|
44 |
| - const directiveMetadata = callExpression.arguments[0] as ts.ObjectLiteralExpression; |
| 44 | + // If the call expressions does not have the correct amount of arguments, we can assume that |
| 45 | + // this call expression is not related to Angular and just uses a similar decorator name. |
| 46 | + if (callExpression.arguments.length !== 1) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const directiveMetadata = this._findMetadataFromExpression(callExpression.arguments[0]); |
45 | 51 |
|
46 | 52 | if (!directiveMetadata) {
|
47 | 53 | return;
|
@@ -121,11 +127,20 @@ export class ComponentWalker extends RuleWalker {
|
121 | 127 | this.visitExternalStylesheet(stylesheetFile);
|
122 | 128 | }
|
123 | 129 |
|
124 |
| - /** Creates a TSLint rule failure for the given external resource. */ |
125 |
| - protected addExternalResourceFailure(file: ExternalResource, message: string, fix?: Fix) { |
126 |
| - const ruleFailure = new RuleFailure(file, file.getStart(), file.getEnd(), |
127 |
| - message, this.getRuleName(), fix); |
| 130 | + /** |
| 131 | + * Recursively searches for the metadata object literal expression inside of a directive call |
| 132 | + * expression. Since expression calls can be nested through *parenthesized* expressions, we |
| 133 | + * need to recursively visit and check every expression inside of a parenthesized expression. |
| 134 | + * |
| 135 | + * e.g. @Component((({myMetadataExpression}))) will return `myMetadataExpression`. |
| 136 | + */ |
| 137 | + private _findMetadataFromExpression(node: ts.Expression): ts.ObjectLiteralExpression | null { |
| 138 | + if (node.kind === ts.SyntaxKind.ObjectLiteralExpression) { |
| 139 | + return node as ts.ObjectLiteralExpression; |
| 140 | + } else if (node.kind === ts.SyntaxKind.ParenthesizedExpression) { |
| 141 | + return this._findMetadataFromExpression((node as ts.ParenthesizedExpression).expression); |
| 142 | + } |
128 | 143 |
|
129 |
| - this.addFailure(ruleFailure); |
| 144 | + return null; |
130 | 145 | }
|
131 | 146 | }
|
0 commit comments