UNPKG

5.22 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var ts = require("typescript");
17var Lint = require("tslint");
18var tsutils = require("tsutils");
19var FAILURE_STRING = 'Unnecessary method override. A method that only calls super can be removed: ';
20var Rule = (function (_super) {
21 __extends(Rule, _super);
22 function Rule() {
23 return _super !== null && _super.apply(this, arguments) || this;
24 }
25 Rule.prototype.apply = function (sourceFile) {
26 return this.applyWithFunction(sourceFile, walk);
27 };
28 Rule.metadata = {
29 ruleName: 'no-unnecessary-override',
30 type: 'maintainability',
31 description: 'Do not write a method that only calls super() on the parent method with the same arguments.',
32 options: null,
33 optionsDescription: '',
34 typescriptOnly: true,
35 issueClass: 'Non-SDL',
36 issueType: 'Warning',
37 severity: 'Moderate',
38 level: 'Opportunity for Excellence',
39 group: 'Correctness',
40 commonWeaknessEnumeration: '398, 710'
41 };
42 return Rule;
43}(Lint.Rules.AbstractRule));
44exports.Rule = Rule;
45function walk(ctx) {
46 function getSingleStatement(block) {
47 if (block.statements.length === 1) {
48 return block.statements[0];
49 }
50 return undefined;
51 }
52 function isMatchingArgumentList(node, statement) {
53 var call = getCallExpressionFromStatement(statement);
54 if (call === undefined) {
55 return false;
56 }
57 if (call.arguments.length === 0 && node.parameters.length === 0) {
58 return true;
59 }
60 if (call.arguments.length !== node.parameters.length) {
61 return false;
62 }
63 var allParameters = node.parameters;
64 for (var i = 0; i < allParameters.length; i++) {
65 var parameter = allParameters[i];
66 var argument = call.arguments[i];
67 if (!tsutils.isIdentifier(argument)) {
68 return false;
69 }
70 if (!tsutils.isIdentifier(parameter.name)) {
71 return false;
72 }
73 var argumentName = argument.text;
74 var parameterName = parameter.name.text;
75 if (argumentName !== parameterName) {
76 return false;
77 }
78 }
79 return true;
80 }
81 function isSuperCall(node, statement) {
82 var call = getCallExpressionFromStatement(statement);
83 if (call === undefined) {
84 return false;
85 }
86 if (!tsutils.isPropertyAccessExpression(call.expression)) {
87 return false;
88 }
89 var propAccess = call.expression;
90 if (propAccess.expression.kind !== ts.SyntaxKind.SuperKeyword) {
91 return false;
92 }
93 var declaredMethodName = getMethodName(node);
94 var methodName = propAccess.name.text;
95 return methodName === declaredMethodName;
96 }
97 function getCallExpressionFromStatement(statement) {
98 var expression;
99 if (tsutils.isExpressionStatement(statement)) {
100 expression = statement.expression;
101 }
102 else if (tsutils.isReturnStatement(statement)) {
103 expression = statement.expression;
104 if (expression === undefined) {
105 return undefined;
106 }
107 }
108 else {
109 return undefined;
110 }
111 if (!tsutils.isCallExpression(expression)) {
112 return undefined;
113 }
114 var call = expression;
115 if (!tsutils.isPropertyAccessExpression(call.expression)) {
116 return undefined;
117 }
118 return call;
119 }
120 function getMethodName(node) {
121 var nameNode = node.name;
122 if (tsutils.isIdentifier(nameNode)) {
123 return nameNode.text;
124 }
125 return '<unknown>';
126 }
127 function cb(node) {
128 if (tsutils.isMethodDeclaration(node)) {
129 if (node.body !== undefined) {
130 var statement = getSingleStatement(node.body);
131 if (statement !== undefined) {
132 if (isSuperCall(node, statement) && isMatchingArgumentList(node, statement)) {
133 ctx.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING + getMethodName(node));
134 }
135 }
136 }
137 }
138 return ts.forEachChild(node, cb);
139 }
140 return ts.forEachChild(ctx.sourceFile, cb);
141}
142//# sourceMappingURL=noUnnecessaryOverrideRule.js.map
\No newline at end of file