UNPKG

8.66 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})();
15var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
16 if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
17 return cooked;
18};
19Object.defineProperty(exports, "__esModule", { value: true });
20var ts = require("typescript");
21var Lint = require("tslint");
22var tsutils = require("tsutils");
23var AstUtils_1 = require("./utils/AstUtils");
24var TypeGuard_1 = require("./utils/TypeGuard");
25var METHOD_REGEX = 'method-regex';
26var PRIVATE_METHOD_REGEX = 'private-method-regex';
27var PROTECTED_METHOD_REGEX = 'protected-method-regex';
28var STATIC_METHOD_REGEX = 'static-method-regex';
29var FUNCTION_REGEX = 'function-regex';
30var VALIDATE_PRIVATE_STATICS_AS_PRIVATE = 'validate-private-statics-as-private';
31var VALIDATE_PRIVATE_STATICS_AS_STATIC = 'validate-private-statics-as-static';
32var VALIDATE_PRIVATE_STATICS_AS_EITHER = 'validate-private-statics-as-either';
33var VALID_ARGS = [VALIDATE_PRIVATE_STATICS_AS_PRIVATE, VALIDATE_PRIVATE_STATICS_AS_STATIC, VALIDATE_PRIVATE_STATICS_AS_EITHER];
34var Rule = (function (_super) {
35 __extends(Rule, _super);
36 function Rule() {
37 return _super !== null && _super.apply(this, arguments) || this;
38 }
39 Rule.prototype.apply = function (sourceFile) {
40 return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.getOptions()));
41 };
42 Rule.prototype.parseOptions = function (options) {
43 var _this = this;
44 var methodRegex = /^[a-z][\w\d]+$/;
45 var privateMethodRegex = methodRegex;
46 var protectedMethodRegex = privateMethodRegex;
47 var staticMethodRegex = /^[A-Z_\d]+$/;
48 var functionRegex = /^[a-z][\w\d]+$/;
49 var validateStatics = VALIDATE_PRIVATE_STATICS_AS_PRIVATE;
50 var ruleArguments = options.ruleArguments;
51 if (ruleArguments.length > 1) {
52 var staticsValidateOption = ruleArguments[1];
53 if (VALID_ARGS.indexOf(staticsValidateOption) > -1) {
54 validateStatics = staticsValidateOption;
55 }
56 }
57 ruleArguments.forEach(function (opt) {
58 if (TypeGuard_1.isObject(opt)) {
59 methodRegex = _this.getOptionOrDefault(opt, METHOD_REGEX, methodRegex);
60 privateMethodRegex = _this.getOptionOrDefault(opt, PRIVATE_METHOD_REGEX, privateMethodRegex);
61 protectedMethodRegex = _this.getOptionOrDefault(opt, PROTECTED_METHOD_REGEX, protectedMethodRegex);
62 staticMethodRegex = _this.getOptionOrDefault(opt, STATIC_METHOD_REGEX, staticMethodRegex);
63 functionRegex = _this.getOptionOrDefault(opt, FUNCTION_REGEX, functionRegex);
64 }
65 });
66 return {
67 validateStatics: validateStatics,
68 methodRegex: methodRegex,
69 privateMethodRegex: privateMethodRegex,
70 protectedMethodRegex: protectedMethodRegex,
71 staticMethodRegex: staticMethodRegex,
72 functionRegex: functionRegex
73 };
74 };
75 Rule.prototype.getOptionOrDefault = function (option, key, defaultValue) {
76 try {
77 var value = option[key];
78 if (value !== undefined && (typeof value === 'string' || value instanceof RegExp)) {
79 return new RegExp(value);
80 }
81 }
82 catch (e) {
83 console.error('Could not read ' + key + ' within function-name configuration');
84 }
85 return defaultValue;
86 };
87 Rule.metadata = {
88 ruleName: 'function-name',
89 type: 'maintainability',
90 description: 'Applies a naming convention to function names and method names',
91 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n Function styles should be consistent throughout the code.\n Users may want functions with multiple descriptors to be validated a certain way.\n An optional argument specifies validation for private static methods:\n * `", "` enforces validation as private.\n * `", "` enforces validation as static.\n * `", "` enforces validation as either.\n "], ["\n Function styles should be consistent throughout the code.\n Users may want functions with multiple descriptors to be validated a certain way.\n An optional argument specifies validation for private static methods:\n * \\`", "\\` enforces validation as private.\n * \\`", "\\` enforces validation as static.\n * \\`", "\\` enforces validation as either.\n "])), VALIDATE_PRIVATE_STATICS_AS_PRIVATE, VALIDATE_PRIVATE_STATICS_AS_STATIC, VALIDATE_PRIVATE_STATICS_AS_EITHER),
92 options: {
93 type: 'array',
94 items: [
95 {
96 type: 'string',
97 enum: [VALIDATE_PRIVATE_STATICS_AS_PRIVATE, VALIDATE_PRIVATE_STATICS_AS_STATIC, VALIDATE_PRIVATE_STATICS_AS_EITHER]
98 }
99 ],
100 minLength: 0,
101 maxLength: 2
102 },
103 optionExamples: [
104 [true, VALIDATE_PRIVATE_STATICS_AS_EITHER],
105 [true, VALIDATE_PRIVATE_STATICS_AS_PRIVATE],
106 [true, VALIDATE_PRIVATE_STATICS_AS_STATIC],
107 [true]
108 ],
109 typescriptOnly: true,
110 issueClass: 'Non-SDL',
111 issueType: 'Warning',
112 severity: 'Important',
113 level: 'Opportunity for Excellence',
114 group: 'Clarity',
115 commonWeaknessEnumeration: '398, 710'
116 };
117 return Rule;
118}(Lint.Rules.AbstractRule));
119exports.Rule = Rule;
120function walk(ctx) {
121 var _a = ctx.options, validateStatics = _a.validateStatics, methodRegex = _a.methodRegex, privateMethodRegex = _a.privateMethodRegex, protectedMethodRegex = _a.protectedMethodRegex, staticMethodRegex = _a.staticMethodRegex, functionRegex = _a.functionRegex;
122 function cb(node) {
123 if (tsutils.isMethodDeclaration(node)) {
124 var name_1 = node.name.getText();
125 if (AstUtils_1.AstUtils.hasComputedName(node)) {
126 }
127 else if (AstUtils_1.AstUtils.isPrivate(node)) {
128 if (!privateMethodRegex.test(name_1) && validateStatics === VALIDATE_PRIVATE_STATICS_AS_PRIVATE) {
129 ctx.addFailureAt(node.name.getStart(), node.name.getWidth(), "Private method name does not match " + privateMethodRegex + ": " + name_1);
130 }
131 }
132 else if (AstUtils_1.AstUtils.isProtected(node)) {
133 if (!protectedMethodRegex.test(name_1) && validateStatics === VALIDATE_PRIVATE_STATICS_AS_PRIVATE) {
134 ctx.addFailureAt(node.name.getStart(), node.name.getWidth(), "Protected method name does not match " + protectedMethodRegex + ": " + name_1);
135 }
136 }
137 else if (AstUtils_1.AstUtils.isStatic(node)) {
138 if (!staticMethodRegex.test(name_1)) {
139 ctx.addFailureAt(node.name.getStart(), node.name.getWidth(), "Static method name does not match " + staticMethodRegex + ": " + name_1);
140 }
141 }
142 else if (!methodRegex.test(name_1)) {
143 ctx.addFailureAt(node.name.getStart(), node.name.getWidth(), "Method name does not match " + methodRegex + ": " + name_1);
144 }
145 }
146 if (tsutils.isFunctionDeclaration(node)) {
147 if (node.name !== undefined) {
148 var name_2 = node.name.text;
149 if (!functionRegex.test(name_2)) {
150 ctx.addFailureAt(node.name.getStart(), node.name.getWidth(), "Function name does not match " + functionRegex + ": " + name_2);
151 }
152 }
153 }
154 return ts.forEachChild(node, cb);
155 }
156 return ts.forEachChild(ctx.sourceFile, cb);
157}
158var templateObject_1;
159//# sourceMappingURL=functionNameRule.js.map
\No newline at end of file