Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit 726a65c

Browse files
authored
Fix up variable-name rule metadata (#4731)
1 parent 8ef652e commit 726a65c

File tree

5 files changed

+51
-23
lines changed

5 files changed

+51
-23
lines changed

src/language/rule/rule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface IRuleMetadata {
7373
* Examples of what a standard config for the rule might look like.
7474
* Using a string[] here is deprecated. Write the options as a JSON object instead.
7575
*/
76-
optionExamples?: Array<true | any[]> | string[];
76+
optionExamples?: Array<true | any[]> | string[] | Array<{ options: any }>;
7777

7878
/**
7979
* An explanation of why the rule is useful.

src/rules/variableNameRule.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,30 @@ const BANNED_KEYWORDS = [
3737
const bannedKeywordsSet = new Set(BANNED_KEYWORDS);
3838
const bannedKeywordsStr = BANNED_KEYWORDS.map(kw => `\`${kw}\``).join(", ");
3939

40-
const OPTION_LEADING_UNDERSCORE = "allow-leading-underscore";
41-
const OPTION_TRAILING_UNDERSCORE = "allow-trailing-underscore";
42-
const OPTION_BAN_KEYWORDS = "ban-keywords";
4340
const OPTION_CHECK_FORMAT = "check-format";
44-
const OPTION_REQUIRE_CONT_FOR_ALL_CAPS = "require-const-for-all-caps";
41+
const OPTION_ALLOW_LEADING_UNDERSCORE = "allow-leading-underscore";
4542
const OPTION_ALLOW_PASCAL_CASE = "allow-pascal-case";
4643
const OPTION_ALLOW_SNAKE_CASE = "allow-snake-case";
44+
const OPTION_ALLOW_TRAILING_UNDERSCORE = "allow-trailing-underscore";
45+
const OPTION_REQUIRE_CONST_FOR_ALL_CAPS = "require-const-for-all-caps";
46+
47+
const OPTION_BAN_KEYWORDS = "ban-keywords";
4748

4849
export class Rule extends Lint.Rules.AbstractRule {
4950
public static metadata: Lint.IRuleMetadata = {
5051
ruleName: "variable-name",
5152
description: "Checks variable names for various errors.",
5253
optionsDescription: Lint.Utils.dedent`
53-
Six arguments may be optionally provided:
54-
55-
* \`"${OPTION_CHECK_FORMAT}"\`: allows only lowerCamelCased or UPPER_CASED variable names
56-
* \`"${OPTION_LEADING_UNDERSCORE}"\` allows underscores at the beginning (only has an effect if "check-format" specified)
57-
* \`"${OPTION_TRAILING_UNDERSCORE}"\` allows underscores at the end. (only has an effect if "check-format" specified)
58-
* \`"${OPTION_REQUIRE_CONT_FOR_ALL_CAPS}"\`: enforces that all variables with UPPER_CASED names should be \`const\`.
59-
* \`"${OPTION_ALLOW_PASCAL_CASE}"\` allows PascalCase in addition to lowerCamelCase.
60-
* \`"${OPTION_ALLOW_SNAKE_CASE}"\` allows snake_case in addition to lowerCamelCase.
54+
Several arguments may be optionally provided:
55+
56+
* \`"${OPTION_CHECK_FORMAT}"\` enbables enforcement of a certain naming format. By default, the rule only allows only lowerCamelCased or UPPER_CASED variable names.
57+
* These additional options make the check stricter:
58+
* \`"${OPTION_REQUIRE_CONST_FOR_ALL_CAPS}"\`: enforces that all variables with UPPER_CASED names should be \`const\`.
59+
* These additional options make the check more permissive:
60+
* \`"${OPTION_ALLOW_LEADING_UNDERSCORE}"\` allows underscores at the beginning (only has an effect if "check-format" specified)
61+
* \`"${OPTION_ALLOW_PASCAL_CASE}"\` allows PascalCase in addition to lowerCamelCase.
62+
* \`"${OPTION_ALLOW_SNAKE_CASE}"\` allows snake_case in addition to lowerCamelCase.
63+
* \`"${OPTION_ALLOW_TRAILING_UNDERSCORE}"\` allows underscores at the end. (only has an effect if "check-format" specified)
6164
* \`"${OPTION_BAN_KEYWORDS}"\`: disallows the use of certain TypeScript keywords as variable or parameter names.
6265
* These are: ${bannedKeywordsStr}`,
6366
options: {
@@ -66,17 +69,27 @@ export class Rule extends Lint.Rules.AbstractRule {
6669
type: "string",
6770
enum: [
6871
OPTION_CHECK_FORMAT,
69-
OPTION_LEADING_UNDERSCORE,
70-
OPTION_TRAILING_UNDERSCORE,
72+
OPTION_ALLOW_LEADING_UNDERSCORE,
7173
OPTION_ALLOW_PASCAL_CASE,
7274
OPTION_ALLOW_SNAKE_CASE,
75+
OPTION_ALLOW_TRAILING_UNDERSCORE,
76+
OPTION_REQUIRE_CONST_FOR_ALL_CAPS,
7377
OPTION_BAN_KEYWORDS,
7478
],
7579
},
7680
minLength: 0,
7781
maxLength: 6,
7882
},
79-
optionExamples: [[true, "ban-keywords", "check-format", "allow-leading-underscore"]],
83+
optionExamples: [
84+
{
85+
options: [
86+
"ban-keywords",
87+
"check-format",
88+
"allow-leading-underscore",
89+
"allow-pascal-case",
90+
],
91+
},
92+
],
8093
type: "style",
8194
typescriptOnly: false,
8295
};
@@ -105,9 +118,9 @@ function parseOptions(ruleArguments: string[]): Options {
105118
banKeywords,
106119
// check variable name formatting by default if no options are specified
107120
checkFormat: !banKeywords || hasOption(OPTION_CHECK_FORMAT),
108-
leadingUnderscore: hasOption(OPTION_LEADING_UNDERSCORE),
109-
trailingUnderscore: hasOption(OPTION_TRAILING_UNDERSCORE),
110-
allCapsForConst: hasOption(OPTION_REQUIRE_CONT_FOR_ALL_CAPS),
121+
leadingUnderscore: hasOption(OPTION_ALLOW_LEADING_UNDERSCORE),
122+
trailingUnderscore: hasOption(OPTION_ALLOW_TRAILING_UNDERSCORE),
123+
allCapsForConst: hasOption(OPTION_REQUIRE_CONST_FOR_ALL_CAPS),
111124
allowPascalCase: hasOption(OPTION_ALLOW_PASCAL_CASE),
112125
allowSnakeCase: hasOption(OPTION_ALLOW_SNAKE_CASE),
113126
};
@@ -125,8 +138,9 @@ function walk(ctx: Lint.WalkContext<Options>): void {
125138
const { initializer, name, propertyName } = node as ts.BindingElement;
126139
if (name.kind === ts.SyntaxKind.Identifier) {
127140
handleVariableNameKeyword(name);
128-
// A destructuring pattern that does not rebind an expression is always an alias, e.g. `var {Foo} = ...;`.
129-
// Only check if the name is rebound (`var {Foo: bar} = ...;`).
141+
// A destructuring pattern that does not rebind an expression is
142+
// always an alias, e.g. `var {Foo} = ...;`. Only check if the name is
143+
// rebound (`var {Foo: bar} = ...;`).
130144
if (
131145
node.parent.kind !== ts.SyntaxKind.ObjectBindingPattern ||
132146
propertyName !== undefined
@@ -163,7 +177,8 @@ function walk(ctx: Lint.WalkContext<Options>): void {
163177

164178
if (name.kind === ts.SyntaxKind.Identifier) {
165179
handleVariableNameFormat(name, initializer);
166-
// do not check property declarations for keywords, they are allowed to be keywords
180+
// do not check property declarations for keywords, they are allowed to be
181+
// keywords
167182
if (node.kind !== ts.SyntaxKind.PropertyDeclaration) {
168183
handleVariableNameKeyword(name);
169184
}

test/rules/no-null-undefined-union/test.ts.lint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[typescript]: >= 2.4.0
1+
[typescript]: >= 2.4.0 < 3.5.0
22

33
// Catches explicit union types.
44

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[typescript]: >= 3.5.0
2+
3+
type SomeType =
4+
5+
| null
6+
~~~~~~
7+
| undefined
8+
~~~~~~~~~~~~~~~
9+
| boolean;
10+
~~~~~~~~~~~~~ [0]
11+
12+
[0]: Union type cannot include both 'null' and 'undefined'.

tslint-vscode.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"no-floating-promises": false,
1010
"no-for-in-array": false,
1111
"no-inferred-empty-object-type": false,
12+
"no-restricted-globals": false,
1213
"no-unnecessary-type-assertion": false,
1314
"no-unsafe-any": false,
1415
"prefer-readonly": false,

0 commit comments

Comments
 (0)