github-pages-deploy-action/node_modules/tslint/lib/rules/variableNameRule.js

220 lines
10 KiB
JavaScript
Raw Normal View History

2019-11-19 20:48:29 +08:00
"use strict";
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
// tslint:disable object-literal-sort-keys
var tsutils = require("tsutils");
var ts = require("typescript");
var Lint = require("../index");
var utils_1 = require("../utils");
var BANNED_KEYWORDS = [
"any",
"Number",
"number",
"String",
"string",
"Boolean",
"boolean",
"Undefined",
"undefined",
];
var bannedKeywordsSet = new Set(BANNED_KEYWORDS);
var bannedKeywordsStr = BANNED_KEYWORDS.map(function (kw) { return "`" + kw + "`"; }).join(", ");
var OPTION_CHECK_FORMAT = "check-format";
var OPTION_ALLOW_LEADING_UNDERSCORE = "allow-leading-underscore";
var OPTION_ALLOW_PASCAL_CASE = "allow-pascal-case";
var OPTION_ALLOW_SNAKE_CASE = "allow-snake-case";
var OPTION_ALLOW_TRAILING_UNDERSCORE = "allow-trailing-underscore";
var OPTION_REQUIRE_CONST_FOR_ALL_CAPS = "require-const-for-all-caps";
var OPTION_BAN_KEYWORDS = "ban-keywords";
var Rule = /** @class */ (function (_super) {
tslib_1.__extends(Rule, _super);
function Rule() {
return _super !== null && _super.apply(this, arguments) || this;
}
Rule.prototype.apply = function (sourceFile) {
return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments));
};
Rule.metadata = {
ruleName: "variable-name",
description: "Checks variable names for various errors.",
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),
options: {
type: "array",
items: {
type: "string",
enum: [
OPTION_CHECK_FORMAT,
OPTION_ALLOW_LEADING_UNDERSCORE,
OPTION_ALLOW_PASCAL_CASE,
OPTION_ALLOW_SNAKE_CASE,
OPTION_ALLOW_TRAILING_UNDERSCORE,
OPTION_REQUIRE_CONST_FOR_ALL_CAPS,
OPTION_BAN_KEYWORDS,
],
},
minLength: 0,
maxLength: 6,
},
optionExamples: [
{
options: [
"ban-keywords",
"check-format",
"allow-leading-underscore",
"allow-pascal-case",
],
},
],
type: "style",
typescriptOnly: false,
};
Rule.KEYWORD_FAILURE = "variable name clashes with keyword/type";
Rule.FAILURE_STRING_CONST = "Only `const` variables may be UPPER_CASE.";
return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
function parseOptions(ruleArguments) {
var banKeywords = hasOption(OPTION_BAN_KEYWORDS);
return {
banKeywords: banKeywords,
// check variable name formatting by default if no options are specified
checkFormat: !banKeywords || hasOption(OPTION_CHECK_FORMAT),
leadingUnderscore: hasOption(OPTION_ALLOW_LEADING_UNDERSCORE),
trailingUnderscore: hasOption(OPTION_ALLOW_TRAILING_UNDERSCORE),
allCapsForConst: hasOption(OPTION_REQUIRE_CONST_FOR_ALL_CAPS),
allowPascalCase: hasOption(OPTION_ALLOW_PASCAL_CASE),
allowSnakeCase: hasOption(OPTION_ALLOW_SNAKE_CASE),
};
function hasOption(name) {
return ruleArguments.indexOf(name) !== -1;
}
}
function walk(ctx) {
var options = ctx.options, sourceFile = ctx.sourceFile;
return ts.forEachChild(sourceFile, function cb(node) {
switch (node.kind) {
case ts.SyntaxKind.BindingElement: {
var _a = node, initializer = _a.initializer, name = _a.name, propertyName = _a.propertyName;
if (name.kind === ts.SyntaxKind.Identifier) {
handleVariableNameKeyword(name);
// A destructuring pattern that does not rebind an expression is
// always an alias, e.g. `var {Foo} = ...;`. Only check if the name is
// rebound (`var {Foo: bar} = ...;`).
if (node.parent.kind !== ts.SyntaxKind.ObjectBindingPattern ||
propertyName !== undefined) {
handleVariableNameFormat(name, initializer);
}
}
break;
}
case ts.SyntaxKind.VariableStatement:
// skip 'declare' keywords
if (tsutils.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword)) {
return;
}
break;
case ts.SyntaxKind.Parameter:
case ts.SyntaxKind.PropertyDeclaration:
handleDeclaredVariable(node);
break;
case ts.SyntaxKind.VariableDeclaration:
handleVariableDeclaration(node);
}
return ts.forEachChild(node, cb);
});
function handleDeclaredVariable(node) {
var name = node.name, initializer = node.initializer;
if (name.kind === ts.SyntaxKind.Identifier) {
handleVariableNameFormat(name, initializer);
// do not check property declarations for keywords, they are allowed to be
// keywords
if (node.kind !== ts.SyntaxKind.PropertyDeclaration) {
handleVariableNameKeyword(name);
}
}
}
function handleVariableNameFormat(name, initializer) {
if (!options.checkFormat) {
return;
}
var text = name.text;
if (initializer !== undefined && isAlias(text, initializer)) {
return;
}
if (text.length !== 0 && !isCamelCase(text, options) && !utils_1.isUpperCase(text)) {
ctx.addFailureAtNode(name, formatFailure());
}
}
function handleVariableNameKeyword(name) {
if (options.banKeywords && bannedKeywordsSet.has(name.text)) {
ctx.addFailureAtNode(name, Rule.KEYWORD_FAILURE);
}
}
function handleVariableDeclaration(node) {
handleDeclaredVariable(node);
if (!ctx.options.allCapsForConst || tsutils.isBindingPattern(node.name)) {
return;
}
var declarationList = node.parent;
var text = node.name.text;
if (utils_1.isUpperCase(text) && !tsutils.isNodeFlagSet(declarationList, ts.NodeFlags.Const)) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING_CONST);
}
}
function formatFailure() {
var failureMessage = "variable name must be in lowerCamelCase";
if (options.allowPascalCase) {
failureMessage += ", PascalCase";
}
if (options.allowSnakeCase) {
failureMessage += ", snake_case";
}
return failureMessage + " or UPPER_CASE";
}
}
function isAlias(name, initializer) {
switch (initializer.kind) {
case ts.SyntaxKind.PropertyAccessExpression:
return initializer.name.text === name;
case ts.SyntaxKind.Identifier:
return initializer.text === name;
default:
return false;
}
}
function isCamelCase(name, options) {
var firstCharacter = name[0];
var lastCharacter = name[name.length - 1];
var middle = name.slice(1, -1);
if (!options.leadingUnderscore && firstCharacter === "_") {
return false;
}
if (!options.trailingUnderscore && lastCharacter === "_") {
return false;
}
if (!options.allowPascalCase && !utils_1.isLowerCase(firstCharacter)) {
return false;
}
if (!options.allowSnakeCase && middle.indexOf("_") !== -1) {
return false;
}
return true;
}
var templateObject_1;