UNPKG

5.35 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var Lint = require("tslint");
14var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
15var AstUtils_1 = require("./utils/AstUtils");
16var METHOD_REGEX = 'method-regex';
17var PRIVATE_METHOD_REGEX = 'private-method-regex';
18var PROTECTED_METHOD_REGEX = 'protected-method-regex';
19var STATIC_METHOD_REGEX = 'static-method-regex';
20var FUNCTION_REGEX = 'function-regex';
21var Rule = (function (_super) {
22 __extends(Rule, _super);
23 function Rule() {
24 return _super !== null && _super.apply(this, arguments) || this;
25 }
26 Rule.prototype.apply = function (sourceFile) {
27 return this.applyWithWalker(new FunctionNameRuleWalker(sourceFile, this.getOptions()));
28 };
29 Rule.metadata = {
30 ruleName: 'function-name',
31 type: 'maintainability',
32 description: 'Applies a naming convention to function names and method names',
33 options: null,
34 optionsDescription: '',
35 typescriptOnly: true,
36 issueClass: 'Non-SDL',
37 issueType: 'Warning',
38 severity: 'Important',
39 level: 'Opportunity for Excellence',
40 group: 'Clarity',
41 commonWeaknessEnumeration: '398, 710'
42 };
43 return Rule;
44}(Lint.Rules.AbstractRule));
45exports.Rule = Rule;
46var FunctionNameRuleWalker = (function (_super) {
47 __extends(FunctionNameRuleWalker, _super);
48 function FunctionNameRuleWalker(sourceFile, options) {
49 var _this = _super.call(this, sourceFile, options) || this;
50 _this.methodRegex = /^[a-z][\w\d]+$/;
51 _this.privateMethodRegex = _this.methodRegex;
52 _this.protectedMethodRegex = _this.privateMethodRegex;
53 _this.staticMethodRegex = /^[A-Z_\d]+$/;
54 _this.functionRegex = /^[a-z][\w\d]+$/;
55 _this.getOptions().forEach(function (opt) {
56 if (typeof (opt) === 'object') {
57 _this.methodRegex = _this.getOptionOrDefault(opt, METHOD_REGEX, _this.methodRegex);
58 _this.privateMethodRegex = _this.getOptionOrDefault(opt, PRIVATE_METHOD_REGEX, _this.privateMethodRegex);
59 _this.protectedMethodRegex = _this.getOptionOrDefault(opt, PROTECTED_METHOD_REGEX, _this.protectedMethodRegex);
60 _this.staticMethodRegex = _this.getOptionOrDefault(opt, STATIC_METHOD_REGEX, _this.staticMethodRegex);
61 _this.functionRegex = _this.getOptionOrDefault(opt, FUNCTION_REGEX, _this.functionRegex);
62 }
63 });
64 return _this;
65 }
66 FunctionNameRuleWalker.prototype.visitMethodDeclaration = function (node) {
67 var name = node.name.getText();
68 if (AstUtils_1.AstUtils.isPrivate(node)) {
69 if (!this.privateMethodRegex.test(name)) {
70 this.addFailureAt(node.name.getStart(), node.name.getWidth(), "Private method name does not match " + this.privateMethodRegex + ": " + name);
71 }
72 }
73 else if (AstUtils_1.AstUtils.isProtected(node)) {
74 if (!this.protectedMethodRegex.test(name)) {
75 this.addFailureAt(node.name.getStart(), node.name.getWidth(), "Protected method name does not match " + this.protectedMethodRegex + ": " + name);
76 }
77 }
78 else if (AstUtils_1.AstUtils.isStatic(node)) {
79 if (!this.staticMethodRegex.test(name)) {
80 this.addFailureAt(node.name.getStart(), node.name.getWidth(), "Static method name does not match " + this.staticMethodRegex + ": " + name);
81 }
82 }
83 else if (!this.methodRegex.test(name)) {
84 this.addFailureAt(node.name.getStart(), node.name.getWidth(), "Method name does not match " + this.methodRegex + ": " + name);
85 }
86 _super.prototype.visitMethodDeclaration.call(this, node);
87 };
88 FunctionNameRuleWalker.prototype.visitFunctionDeclaration = function (node) {
89 if (node.name != null) {
90 var name_1 = node.name.text;
91 if (!this.functionRegex.test(name_1)) {
92 this.addFailureAt(node.name.getStart(), node.name.getWidth(), "Function name does not match " + this.functionRegex + ": " + name_1);
93 }
94 }
95 _super.prototype.visitFunctionDeclaration.call(this, node);
96 };
97 FunctionNameRuleWalker.prototype.getOptionOrDefault = function (option, key, defaultValue) {
98 try {
99 if (option[key] != null) {
100 return new RegExp(option[key]);
101 }
102 }
103 catch (e) {
104 console.error('Could not read ' + key + ' within function-name configuration');
105 }
106 return defaultValue;
107 };
108 return FunctionNameRuleWalker;
109}(ErrorTolerantWalker_1.ErrorTolerantWalker));
110//# sourceMappingURL=functionNameRule.js.map
\No newline at end of file