8000 feat(eslint-plugin): [ban-types] allow banning null and undefined (#821) · webmaster128/typescript-eslint@0b2b887 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0b2b887

Browse files
Validarkbradzacher
andauthored
feat(eslint-plugin): [ban-types] allow banning null and undefined (typescript-eslint#821)
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
1 parent ae2754e commit 0b2b887

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

packages/eslint-plugin/src/rules/ban-types.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ function removeSpaces(str: string): string {
2323
}
2424

2525
function stringifyTypeName(
26-
node: TSESTree.EntityName | TSESTree.TSTypeLiteral,
26+
node:
27+
| TSESTree.EntityName
28+
| TSESTree.TSTypeLiteral
29+
| TSESTree.TSNullKeyword
30+
| TSESTree.TSUndefinedKeyword,
2731
sourceCode: TSESLint.SourceCode,
2832
): string {
2933
return removeSpaces(sourceCode.getText(node));
@@ -113,18 +117,21 @@ export default util.createRule<Options, MessageIds>({
113117
},
114118
],
115119
create(context, [{ types }]) {
116-
const bannedTypes: Types = Object.keys(types).reduce(
117-
(res, type) => ({ ...res, [removeSpaces(type)]: types[type] }),
118-
{},
120+
const bannedTypes = new Map(
121+
Object.entries(types).map(([type, data]) => [removeSpaces(type), data]),
119122
);
120123

121124
function checkBannedTypes(
122-
typeNode: TSESTree.EntityName | TSESTree.TSTypeLiteral,
125+
typeNode:
126+
| TSESTree.EntityName
127+
| TSESTree.TSTypeLiteral
128+
| TSESTree.TSNullKeyword
129+
| TSESTree.TSUndefinedKeyword,
130+
name = stringifyTypeName(typeNode, context.getSourceCode()),
123131
): void {
124-
const name = stringifyTypeName(typeNode, context.getSourceCode());
132+
const bannedType = bannedTypes.get(name);
125133

126-
if (name in bannedTypes) {
127-
const bannedType = bannedTypes[name];
134+
if (bannedType !== undefined) {
128135
const customMessage = getCustomMessage(bannedType);
129136
const fixWith =
130137
bannedType && typeof bannedType === 'object' && bannedType.fixWith;
@@ -144,6 +151,18 @@ export default util.createRule<Options, MessageIds>({
144151
}
145152

146153
return {
154+
...(bannedTypes.has('null') && {
155+
TSNullKeyword(node): void {
156+
checkBannedTypes(node, 'null');
157+
},
158+
}),
159+
160+
...(bannedTypes.has('undefined') && {
161+
TSUndefinedKeyword(node): void {
162+
checkBannedTypes(node, 'undefined');
163+
},
164+
}),
165+
147166
TSTypeLiteral(node): void {
148167
if (node.members.length) {
149168
return;

packages/eslint-plugin/tests/rules/ban-types.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ const options: InferOptionsTypeFromRule<typeof rule> = [
2424
},
2525
];
2626

27+
const options2: InferOptionsTypeFromRule<typeof rule> = [
28+
{
29+
types: {
30+
null: {
31+
message: 'Use undefined instead.',
32+
fixWith: 'undefined',
33+
},
34+
},
35+
},
36+
];
37+
38+
const options3: InferOptionsTypeFromRule<typeof rule> = [
39+
{
40+
types: {
41+
undefined: null,
42+
},
43+
},
44+
];
45+
2746
ruleTester.run('ban-types', rule, {
2847
valid: [
2948
'let f = Object();', // Should not fail if there is no options set
@@ -53,6 +72,14 @@ ruleTester.run('ban-types', rule, {
5372
code: 'let a: NS.Bad._',
5473
options,
5574
},
75+
{
76+
code: 'let a: undefined',
77+
options: options2,
78+
},
79+
{
80+
code: 'let a: null',
81+
options: options3,
82+
},
5683
],
5784
invalid: [
5885
{
@@ -70,6 +97,30 @@ ruleTester.run('ban-types', rule, {
7097
],
7198
options,
7299
},
100+
{
101+
code: 'let a: undefined;',
102+
errors: [
103+
{
104+
messageId: 'bannedTypeMessage',
105+
data: { name: 'undefined', customMessage: '' },
106+
line: 1,
107+
column: 8,
108+
},
109+
],
110+
options: options3,
111+
},
112+
{
113+
code: 'let a: null;',
114+
errors: [
115+
{
116+
messageId: 'bannedTypeMessage',
117+
data: { name: 'null', customMessage: ' Use undefined instead.' },
118+
line: 1,
119+
column: 8,
120+
},
121+
],
122+
options: options2,
123+
},
73124
{
74125
code: 'let aa: Foo;',
75126
errors: [

0 commit comments

Comments
 (0)
0