Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [ban-types] allow banning null and undefined #821

Merged
merged 10 commits into from Feb 12, 2020
67 changes: 48 additions & 19 deletions packages/eslint-plugin/src/rules/ban-types.ts
Expand Up @@ -107,27 +107,56 @@ export default util.createRule<Options, MessageIds>({
},
],
create(context, [{ types: bannedTypes }]) {
return {
TSTypeReference({ typeName }) {
const name = stringifyTypeName(typeName, context.getSourceCode());
function reportBannedType(
node:
| TSESTree.EntityName
| TSESTree.TSNullKeyword
| TSESTree.TSUndefinedKeyword,
name: string,
) {
const bannedType = bannedTypes[name];
const customMessage = getCustomMessage(bannedType);
const fixWith =
bannedType && typeof bannedType === 'object' && bannedType.fixWith;

if (name in bannedTypes) {
const bannedType = bannedTypes[name];
const customMessage = getCustomMessage(bannedType);
const fixWith =
bannedType && typeof bannedType === 'object' && bannedType.fixWith;
context.report({
node: node,
messageId: 'bannedTypeMessage',
data: {
name,
customMessage,
},
fix: fixWith ? fixer => fixer.replaceText(node, fixWith) : null,
});
}

context.report({
node: typeName,
messageId: 'bannedTypeMessage',
data: {
name: name,
customMessage,
},
fix: fixWith ? fixer => fixer.replaceText(typeName, fixWith) : null,
});
let trackedBannedTypes = 0;
const bannedTypeNames = Object.keys(bannedTypes);
const ruleListener: TSESLint.RuleListener = {};

if ('null' in bannedTypes) {
ruleListener.TSNullKeyword = typeName => {
reportBannedType(typeName, 'null');
};
++trackedBannedTypes;
}

if ('undefined' in bannedTypes) {
ruleListener.TSUndefinedKeyword = typeName => {
reportBannedType(typeName, 'undefined');
};
++trackedBannedTypes;
}

if (bannedTypeNames.length > trackedBannedTypes) {
ruleListener.TSTypeReference = ({ typeName }) => {
const name = stringifyTypeName(typeName, context.getSourceCode());
if (name in bannedTypes) {
reportBannedType(typeName, name);
}
},
};
};
}

return ruleListener;
},
});
51 changes: 51 additions & 0 deletions packages/eslint-plugin/tests/rules/ban-types.test.ts
Expand Up @@ -23,6 +23,25 @@ const options: InferOptionsTypeFromRule<typeof rule> = [
},
},
];

const options2: InferOptionsTypeFromRule<typeof rule> = [
{
types: {
'null': {
message: 'Use undefined instead.',
fixWith: 'undefined',
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
];

const options3: InferOptionsTypeFromRule<typeof rule> = [
{
types: {
'undefined': null,
},
},
];

ruleTester.run('ban-types', rule, {
valid: [
Expand Down Expand Up @@ -51,6 +70,14 @@ ruleTester.run('ban-types', rule, {
code: 'let a: NS.Bad._',
options,
},
{
code: 'let a: undefined',
options: options2,
},
{
code: 'let a: null',
options: options3,
},
],
invalid: [
{
Expand All @@ -68,6 +95,30 @@ ruleTester.run('ban-types', rule, {
],
options,
},
{
code: 'let a: undefined;',
errors: [
{
messageId: 'bannedTypeMessage',
data: { name: 'undefined', customMessage: '' },
line: 1,
column: 8,
},
],
options: options3,
},
{
code: 'let a: null;',
errors: [
{
messageId: 'bannedTypeMessage',
data: { name: 'null', customMessage: ' Use undefined instead.' },
line: 1,
column: 8,
},
],
options: options2,
},
{
code: 'let aa: Foo;',
errors: [
Expand Down