.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / variableNameRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
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 = [
26     "any",
27     "Number",
28     "number",
29     "String",
30     "string",
31     "Boolean",
32     "boolean",
33     "Undefined",
34     "undefined",
35 ];
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);
47     function Rule() {
48         return _super !== null && _super.apply(this, arguments) || this;
49     }
50     Rule.prototype.apply = function (sourceFile) {
51         return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments));
52     };
53     Rule.metadata = {
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),
57         options: {
58             type: "array",
59             items: {
60                 type: "string",
61                 enum: [
62                     OPTION_CHECK_FORMAT,
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,
68                     OPTION_BAN_KEYWORDS,
69                 ],
70             },
71             minLength: 0,
72             maxLength: 6,
73         },
74         optionExamples: [
75             {
76                 options: [
77                     "ban-keywords",
78                     "check-format",
79                     "allow-leading-underscore",
80                     "allow-pascal-case",
81                 ],
82             },
83         ],
84         type: "style",
85         typescriptOnly: false,
86     };
87     Rule.KEYWORD_FAILURE = "variable name clashes with keyword/type";
88     Rule.FAILURE_STRING_CONST = "Only `const` variables may be UPPER_CASE.";
89     return Rule;
90 }(Lint.Rules.AbstractRule));
91 exports.Rule = Rule;
92 function parseOptions(ruleArguments) {
93     var banKeywords = hasOption(OPTION_BAN_KEYWORDS);
94     return {
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),
103     };
104     function hasOption(name) {
105         return ruleArguments.indexOf(name) !== -1;
106     }
107 }
108 function walk(ctx) {
109     var options = ctx.options, sourceFile = ctx.sourceFile;
110     return ts.forEachChild(sourceFile, function cb(node) {
111         switch (node.kind) {
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);
122                     }
123                 }
124                 break;
125             }
126             case ts.SyntaxKind.VariableStatement:
127                 // skip 'declare' keywords
128                 if (tsutils.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword)) {
129                     return;
130                 }
131                 break;
132             case ts.SyntaxKind.Parameter:
133             case ts.SyntaxKind.PropertyDeclaration:
134                 handleDeclaredVariable(node);
135                 break;
136             case ts.SyntaxKind.VariableDeclaration:
137                 handleVariableDeclaration(node);
138         }
139         return ts.forEachChild(node, cb);
140     });
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
146             // keywords
147             if (node.kind !== ts.SyntaxKind.PropertyDeclaration) {
148                 handleVariableNameKeyword(name);
149             }
150         }
151     }
152     function handleVariableNameFormat(name, initializer) {
153         if (!options.checkFormat) {
154             return;
155         }
156         var text = name.text;
157         if (initializer !== undefined && isAlias(text, initializer)) {
158             return;
159         }
160         if (text.length !== 0 && !isCamelCase(text, options) && !utils_1.isUpperCase(text)) {
161             ctx.addFailureAtNode(name, formatFailure());
162         }
163     }
164     function handleVariableNameKeyword(name) {
165         if (options.banKeywords && bannedKeywordsSet.has(name.text)) {
166             ctx.addFailureAtNode(name, Rule.KEYWORD_FAILURE);
167         }
168     }
169     function handleVariableDeclaration(node) {
170         handleDeclaredVariable(node);
171         if (!ctx.options.allCapsForConst || tsutils.isBindingPattern(node.name)) {
172             return;
173         }
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);
178         }
179     }
180     function formatFailure() {
181         var failureMessage = "variable name must be in lowerCamelCase";
182         if (options.allowPascalCase) {
183             failureMessage += ", PascalCase";
184         }
185         if (options.allowSnakeCase) {
186             failureMessage += ", snake_case";
187         }
188         return failureMessage + " or UPPER_CASE";
189     }
190 }
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;
197         default:
198             return false;
199     }
200 }
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 === "_") {
206         return false;
207     }
208     if (!options.trailingUnderscore && lastCharacter === "_") {
209         return false;
210     }
211     if (!options.allowPascalCase && !utils_1.isLowerCase(firstCharacter)) {
212         return false;
213     }
214     if (!options.allowSnakeCase && middle.indexOf("_") !== -1) {
215         return false;
216     }
217     return true;
218 }
219 var templateObject_1;