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] handle empty type literal {} #1348

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 33 additions & 19 deletions packages/eslint-plugin/src/rules/ban-types.ts
Expand Up @@ -17,7 +17,7 @@ type Options = [
type MessageIds = 'bannedTypeMessage';

function stringifyTypeName(
node: TSESTree.EntityName,
node: TSESTree.EntityName | TSESTree.TSTypeLiteral,
sourceCode: TSESLint.SourceCode,
): string {
return sourceCode.getText(node).replace(/ /g, '');
Expand Down Expand Up @@ -107,27 +107,41 @@ export default util.createRule<Options, MessageIds>({
},
],
create(context, [{ types: bannedTypes }]) {
return {
TSTypeReference({ typeName }): void {
const name = stringifyTypeName(typeName, context.getSourceCode());
function checkBannedTypes(
typeNode: TSESTree.EntityName | TSESTree.TSTypeLiteral,
): void {
const name = stringifyTypeName(typeNode, context.getSourceCode());

if (name in bannedTypes) {
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: typeName,
messageId: 'bannedTypeMessage',
data: {
name: name,
customMessage,
},
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
fix: fixWith ? fixer => fixer.replaceText(typeName, fixWith) : null,
});
context.report({
node: typeNode,
messageId: 'bannedTypeMessage',
data: {
name,
customMessage,
},
fix: fixWith
? (fixer): TSESLint.RuleFix => fixer.replaceText(typeNode, fixWith)
: null,
});
}
}

return {
TSTypeLiteral(node): void {
if (node.members.length) {
return;
}

checkBannedTypes(node);
},
TSTypeReference({ typeName }): void {
checkBannedTypes(typeName);
},
};
},
Expand Down
27 changes: 27 additions & 0 deletions packages/eslint-plugin/tests/rules/ban-types.test.ts
Expand Up @@ -27,6 +27,8 @@ const options: InferOptionsTypeFromRule<typeof rule> = [
ruleTester.run('ban-types', rule, {
valid: [
'let f = Object();', // Should not fail if there is no options set
'let f: {} = {};',
'let f: { x: number, y: number } = { x: 1, y: 1 };',
{
code: 'let f = Object();',
options,
Expand Down Expand Up @@ -295,5 +297,30 @@ let b: Foo<NS.Good>;
],
options,
},
{
code: `let foo: {} = {};`,
output: `let foo: object = {};`,
options: [
{
types: {
'{}': {
message: 'Use object instead.',
fixWith: 'object',
},
},
},
],
errors: [
{
messageId: 'bannedTypeMessage',
data: {
name: '{}',
customMessage: ' Use object instead.',
},
line: 1,
column: 10,
},
],
},
],
});