Skip to content

feat(eslint-plugin): [no-explicit-any] suggest to replace keyof any with PropertyKey #11032

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 54 additions & 17 deletions packages/eslint-plugin/src/rules/no-explicit-any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export type Options = [
ignoreRestArgs?: boolean;
},
];
export type MessageIds = 'suggestNever' | 'suggestUnknown' | 'unexpectedAny';
export type MessageIds =
| 'suggestNever'
| 'suggestPropertyKey'
| 'suggestUnknown'
| 'unexpectedAny';

export default createRule<Options, MessageIds>({
name: 'no-explicit-any',
Expand All @@ -25,6 +29,8 @@ export default createRule<Options, MessageIds>({
messages: {
suggestNever:
"Use `never` instead, this is useful when instantiating generic type parameters that you don't need to know the type of.",
suggestPropertyKey:
'Use `PropertyKey` instead, this is more explicit than `keyof any`.',
suggestUnknown:
'Use `unknown` instead, this will force you to explicitly, and safely assert the type is correct.',
unexpectedAny: 'Unexpected any. Specify a different type.',
Expand Down Expand Up @@ -170,8 +176,35 @@ export default createRule<Options, MessageIds>({
);
}

/**
* Checks if the node is within a keyof any expression
* @param node the node to be validated.
* @returns true if the node is within a keyof any expression, false otherwise
* @private
*/
function isNodeWithinKeyofAny(node: TSESTree.TSAnyKeyword): boolean {
return (
node.parent.type === AST_NODE_TYPES.TSTypeOperator &&
node.parent.operator === 'keyof'
);
}

/**
* Creates a fixer that replaces a keyof any with PropertyKey
* @param node the node to be fixed.
* @returns a function that will fix the node.
* @private
*/
function createPropertyKeyFixer(node: TSESTree.TSAnyKeyword) {
return (fixer: TSESLint.RuleFixer) => {
return fixer.replaceText(node.parent, 'PropertyKey');
};
}

return {
TSAnyKeyword(node): void {
const isKeyofAny = isNodeWithinKeyofAny(node);

if (ignoreRestArgs && isNodeDescendantOfRestElementInFunction(node)) {
return;
}
Expand All @@ -181,25 +214,29 @@ export default createRule<Options, MessageIds>({
suggest: TSESLint.ReportSuggestionArray<MessageIds> | null;
} = {
fix: null,
suggest: [
{
messageId: 'suggestUnknown',
fix(fixer): TSESLint.RuleFix {
return fixer.replaceText(node, 'unknown');
},
},
{
messageId: 'suggestNever',
fix(fixer): TSESLint.RuleFix {
return fixer.replaceText(node, 'never');
},
},
],
suggest: isKeyofAny
? [
{
messageId: 'suggestPropertyKey',
fix: createPropertyKeyFixer(node),
},
]
: [
{
messageId: 'suggestUnknown',
fix: fixer => fixer.replaceText(node, 'unknown'),
},
{
messageId: 'suggestNever',
fix: fixer => fixer.replaceText(node, 'never'),
},
],
};

if (fixToUnknown) {
fixOrSuggest.fix = (fixer): TSESLint.RuleFix =>
fixer.replaceText(node, 'unknown');
fixOrSuggest.fix = isKeyofAny
? createPropertyKeyFixer(node)
: fixer => fixer.replaceText(node, 'unknown');
}

context.report({
Expand Down
Loading
Loading