4 * Copyright 2013 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 // tslint:disable object-literal-sort-keys
21 var tsutils = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var utils_1 = require("../utils");
25 var BANNED_KEYWORDS = [
36 var bannedKeywordsSet = new Set(BANNED_KEYWORDS);
37 var bannedKeywordsStr = BANNED_KEYWORDS.map(function (kw) { return "`" + kw + "`"; }).join(", ");
38 var OPTION_CHECK_FORMAT = "check-format";
39 var OPTION_ALLOW_LEADING_UNDERSCORE = "allow-leading-underscore";
40 var OPTION_ALLOW_PASCAL_CASE = "allow-pascal-case";
41 var OPTION_ALLOW_SNAKE_CASE = "allow-snake-case";
42 var OPTION_ALLOW_TRAILING_UNDERSCORE = "allow-trailing-underscore";
43 var OPTION_REQUIRE_CONST_FOR_ALL_CAPS = "require-const-for-all-caps";
44 var OPTION_BAN_KEYWORDS = "ban-keywords";
45 var Rule = /** @class */ (function (_super) {
46 tslib_1.__extends(Rule, _super);
48 return _super !== null && _super.apply(this, arguments) || this;
50 Rule.prototype.apply = function (sourceFile) {
51 return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments));
54 ruleName: "variable-name",
55 description: "Checks variable names for various errors.",
56 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Several arguments may be optionally provided:\n\n * `\"", "\"` enbables enforcement of a certain naming format. By default, the rule only allows only lowerCamelCased or UPPER_CASED variable names.\n * These additional options make the check stricter:\n * `\"", "\"`: enforces that all variables with UPPER_CASED names should be `const`.\n * These additional options make the check more permissive:\n * `\"", "\"` allows underscores at the beginning (only has an effect if \"check-format\" specified)\n * `\"", "\"` allows PascalCase in addition to lowerCamelCase.\n * `\"", "\"` allows snake_case in addition to lowerCamelCase.\n * `\"", "\"` allows underscores at the end. (only has an effect if \"check-format\" specified)\n * `\"", "\"`: disallows the use of certain TypeScript keywords as variable or parameter names.\n * These are: ", ""], ["\n Several arguments may be optionally provided:\n\n * \\`\"", "\"\\` enbables enforcement of a certain naming format. By default, the rule only allows only lowerCamelCased or UPPER_CASED variable names.\n * These additional options make the check stricter:\n * \\`\"", "\"\\`: enforces that all variables with UPPER_CASED names should be \\`const\\`.\n * These additional options make the check more permissive:\n * \\`\"", "\"\\` allows underscores at the beginning (only has an effect if \"check-format\" specified)\n * \\`\"", "\"\\` allows PascalCase in addition to lowerCamelCase.\n * \\`\"", "\"\\` allows snake_case in addition to lowerCamelCase.\n * \\`\"", "\"\\` allows underscores at the end. (only has an effect if \"check-format\" specified)\n * \\`\"", "\"\\`: disallows the use of certain TypeScript keywords as variable or parameter names.\n * These are: ", ""])), OPTION_CHECK_FORMAT, OPTION_REQUIRE_CONST_FOR_ALL_CAPS, OPTION_ALLOW_LEADING_UNDERSCORE, OPTION_ALLOW_PASCAL_CASE, OPTION_ALLOW_SNAKE_CASE, OPTION_ALLOW_TRAILING_UNDERSCORE, OPTION_BAN_KEYWORDS, bannedKeywordsStr),
63 OPTION_ALLOW_LEADING_UNDERSCORE,
64 OPTION_ALLOW_PASCAL_CASE,
65 OPTION_ALLOW_SNAKE_CASE,
66 OPTION_ALLOW_TRAILING_UNDERSCORE,
67 OPTION_REQUIRE_CONST_FOR_ALL_CAPS,
79 "allow-leading-underscore",
85 typescriptOnly: false,
87 Rule.KEYWORD_FAILURE = "variable name clashes with keyword/type";
88 Rule.FAILURE_STRING_CONST = "Only `const` variables may be UPPER_CASE.";
90 }(Lint.Rules.AbstractRule));
92 function parseOptions(ruleArguments) {
93 var banKeywords = hasOption(OPTION_BAN_KEYWORDS);
95 banKeywords: banKeywords,
96 // check variable name formatting by default if no options are specified
97 checkFormat: !banKeywords || hasOption(OPTION_CHECK_FORMAT),
98 leadingUnderscore: hasOption(OPTION_ALLOW_LEADING_UNDERSCORE),
99 trailingUnderscore: hasOption(OPTION_ALLOW_TRAILING_UNDERSCORE),
100 allCapsForConst: hasOption(OPTION_REQUIRE_CONST_FOR_ALL_CAPS),
101 allowPascalCase: hasOption(OPTION_ALLOW_PASCAL_CASE),
102 allowSnakeCase: hasOption(OPTION_ALLOW_SNAKE_CASE),
104 function hasOption(name) {
105 return ruleArguments.indexOf(name) !== -1;
109 var options = ctx.options, sourceFile = ctx.sourceFile;
110 return ts.forEachChild(sourceFile, function cb(node) {
112 case ts.SyntaxKind.BindingElement: {
113 var _a = node, initializer = _a.initializer, name = _a.name, propertyName = _a.propertyName;
114 if (name.kind === ts.SyntaxKind.Identifier) {
115 handleVariableNameKeyword(name);
116 // A destructuring pattern that does not rebind an expression is
117 // always an alias, e.g. `var {Foo} = ...;`. Only check if the name is
118 // rebound (`var {Foo: bar} = ...;`).
119 if (node.parent.kind !== ts.SyntaxKind.ObjectBindingPattern ||
120 propertyName !== undefined) {
121 handleVariableNameFormat(name, initializer);
126 case ts.SyntaxKind.VariableStatement:
127 // skip 'declare' keywords
128 if (tsutils.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword)) {
132 case ts.SyntaxKind.Parameter:
133 case ts.SyntaxKind.PropertyDeclaration:
134 handleDeclaredVariable(node);
136 case ts.SyntaxKind.VariableDeclaration:
137 handleVariableDeclaration(node);
139 return ts.forEachChild(node, cb);
141 function handleDeclaredVariable(node) {
142 var name = node.name, initializer = node.initializer;
143 if (name.kind === ts.SyntaxKind.Identifier) {
144 handleVariableNameFormat(name, initializer);
145 // do not check property declarations for keywords, they are allowed to be
147 if (node.kind !== ts.SyntaxKind.PropertyDeclaration) {
148 handleVariableNameKeyword(name);
152 function handleVariableNameFormat(name, initializer) {
153 if (!options.checkFormat) {
156 var text = name.text;
157 if (initializer !== undefined && isAlias(text, initializer)) {
160 if (text.length !== 0 && !isCamelCase(text, options) && !utils_1.isUpperCase(text)) {
161 ctx.addFailureAtNode(name, formatFailure());
164 function handleVariableNameKeyword(name) {
165 if (options.banKeywords && bannedKeywordsSet.has(name.text)) {
166 ctx.addFailureAtNode(name, Rule.KEYWORD_FAILURE);
169 function handleVariableDeclaration(node) {
170 handleDeclaredVariable(node);
171 if (!ctx.options.allCapsForConst || tsutils.isBindingPattern(node.name)) {
174 var declarationList = node.parent;
175 var text = node.name.text;
176 if (utils_1.isUpperCase(text) && !tsutils.isNodeFlagSet(declarationList, ts.NodeFlags.Const)) {
177 ctx.addFailureAtNode(node, Rule.FAILURE_STRING_CONST);
180 function formatFailure() {
181 var failureMessage = "variable name must be in lowerCamelCase";
182 if (options.allowPascalCase) {
183 failureMessage += ", PascalCase";
185 if (options.allowSnakeCase) {
186 failureMessage += ", snake_case";
188 return failureMessage + " or UPPER_CASE";
191 function isAlias(name, initializer) {
192 switch (initializer.kind) {
193 case ts.SyntaxKind.PropertyAccessExpression:
194 return initializer.name.text === name;
195 case ts.SyntaxKind.Identifier:
196 return initializer.text === name;
201 function isCamelCase(name, options) {
202 var firstCharacter = name[0];
203 var lastCharacter = name[name.length - 1];
204 var middle = name.slice(1, -1);
205 if (!options.leadingUnderscore && firstCharacter === "_") {
208 if (!options.trailingUnderscore && lastCharacter === "_") {
211 if (!options.allowPascalCase && !utils_1.isLowerCase(firstCharacter)) {
214 if (!options.allowSnakeCase && middle.indexOf("_") !== -1) {
219 var templateObject_1;