From 7236104b3c8db50591ef225d6e2028fdbf9f82e0 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 13 Jul 2022 11:51:07 +0800 Subject: [PATCH 1/2] fix(eslint-plugin): [no-inferrable] fix optional param to valid code --- .../src/rules/no-inferrable-types.ts | 12 ++++++++++- .../tests/rules/no-inferrable-types.test.ts | 21 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-inferrable-types.ts b/packages/eslint-plugin/src/rules/no-inferrable-types.ts index 9adb4bca9ecd..e32c1d065b2b 100644 --- a/packages/eslint-plugin/src/rules/no-inferrable-types.ts +++ b/packages/eslint-plugin/src/rules/no-inferrable-types.ts @@ -46,6 +46,8 @@ export default util.createRule({ }, ], create(context, [{ ignoreParameters, ignoreProperties }]) { + const sourceCode = context.getSourceCode(); + function isFunctionCall( init: TSESTree.Expression, callName: string, @@ -215,7 +217,15 @@ export default util.createRule({ data: { type, }, - fix: fixer => fixer.remove(typeNode), + *fix(fixer) { + if ( + node.type === AST_NODE_TYPES.AssignmentPattern && + node.left.optional + ) { + yield fixer.remove(sourceCode.getTokenBefore(typeNode)!); + } + yield fixer.remove(typeNode); + }, }); } diff --git a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts index d85806b69c4e..7f06a27c0da7 100644 --- a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts +++ b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts @@ -156,7 +156,26 @@ class Foo { invalid: [ ...invalidTestCases, - + { + // This is invalid TS semantic, but it's trivial to make valid anyway + code: 'const fn = (a?: number = 5) => {};', + output: 'const fn = (a = 5) => {};', + options: [ + { + ignoreParameters: false, + }, + ], + errors: [ + { + messageId: 'noInferrableType', + data: { + type: 'number', + }, + line: 1, + column: 13, + }, + ], + }, { code: "const fn = (a: number = 5, b: boolean = true, c: string = 'foo') => {};", output: "const fn = (a = 5, b = true, c = 'foo') => {};", From 7598abb9f13dc9fabebdd267f7bda2b0519f0e2d Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 14 Jul 2022 11:43:22 +0800 Subject: [PATCH 2/2] Add definite assignment --- .../src/rules/no-inferrable-types.ts | 5 ++-- .../tests/rules/no-inferrable-types.test.ts | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-inferrable-types.ts b/packages/eslint-plugin/src/rules/no-inferrable-types.ts index e32c1d065b2b..e77b694cc707 100644 --- a/packages/eslint-plugin/src/rules/no-inferrable-types.ts +++ b/packages/eslint-plugin/src/rules/no-inferrable-types.ts @@ -219,8 +219,9 @@ export default util.createRule({ }, *fix(fixer) { if ( - node.type === AST_NODE_TYPES.AssignmentPattern && - node.left.optional + (node.type === AST_NODE_TYPES.AssignmentPattern && + node.left.optional) || + (node.type === AST_NODE_TYPES.PropertyDefinition && node.definite) ) { yield fixer.remove(sourceCode.getTokenBefore(typeNode)!); } diff --git a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts index 7f06a27c0da7..cfea578b9f61 100644 --- a/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts +++ b/packages/eslint-plugin/tests/rules/no-inferrable-types.test.ts @@ -176,6 +176,34 @@ class Foo { }, ], }, + { + // This is invalid TS semantic, but it's trivial to make valid anyway + code: ` +class A { + a!: number = 1; +} + `, + output: ` +class A { + a = 1; +} + `, + options: [ + { + ignoreProperties: false, + }, + ], + errors: [ + { + messageId: 'noInferrableType', + data: { + type: 'number', + }, + line: 3, + column: 3, + }, + ], + }, { code: "const fn = (a: number = 5, b: boolean = true, c: string = 'foo') => {};", output: "const fn = (a = 5, b = true, c = 'foo') => {};", pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy