chore: implement code fix for 'use-using' rule (#10805)

This commit is contained in:
jrandolf 2023-08-30 10:46:06 +02:00 committed by GitHub
parent 2401bb46b1
commit cb5ab7e02f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,10 +15,6 @@
*/ */
import {ESLintUtils, TSESTree} from '@typescript-eslint/utils'; import {ESLintUtils, TSESTree} from '@typescript-eslint/utils';
import type {
RuleListener,
RuleModule,
} from '@typescript-eslint/utils/ts-eslint';
const usingSymbols = ['ElementHandle', 'JSHandle']; const usingSymbols = ['ElementHandle', 'JSHandle'];
@ -26,18 +22,17 @@ const createRule = ESLintUtils.RuleCreator(name => {
return `https://github.com/puppeteer/puppeteer/tree/main/tools/eslint/${name}.js`; return `https://github.com/puppeteer/puppeteer/tree/main/tools/eslint/${name}.js`;
}); });
const useUsingRule: RuleModule<'useUsing', [], RuleListener> = createRule< const useUsingRule = createRule<[], 'useUsing' | 'useUsingFix'>({
[],
'useUsing'
>({
name: 'use-using', name: 'use-using',
meta: { meta: {
docs: { docs: {
description: "Requires 'using' for element/JS handles.", description: "Requires 'using' for element/JS handles.",
requiresTypeChecking: true, requiresTypeChecking: true,
}, },
hasSuggestions: true,
messages: { messages: {
useUsing: "Use 'using'.", useUsing: "Use 'using'.",
useUsingFix: "Replace with 'using' to ignore.",
}, },
schema: [], schema: [],
type: 'problem', type: 'problem',
@ -49,7 +44,7 @@ const useUsingRule: RuleModule<'useUsing', [], RuleListener> = createRule<
return { return {
VariableDeclaration(node): void { VariableDeclaration(node): void {
if (['using', 'await using'].includes(node.kind)) { if (['using', 'await using'].includes(node.kind) || node.declare) {
return; return;
} }
for (const declaration of node.declarations) { for (const declaration of node.declarations) {
@ -77,6 +72,17 @@ const useUsingRule: RuleModule<'useUsing', [], RuleListener> = createRule<
context.report({ context.report({
node: declaration.id, node: declaration.id,
messageId: 'useUsing', messageId: 'useUsing',
suggest: [
{
messageId: 'useUsingFix',
fix(fixer) {
return fixer.replaceTextRange(
[node.range[0], node.range[0] + node.kind.length],
'using'
);
},
},
],
}); });
} }
} }