Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(eslint-plugin): [no-inferrable] fix optional param to valid code#5342

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
13 changes: 12 additions & 1 deletionpackages/eslint-plugin/src/rules/no-inferrable-types.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,6 +46,8 @@ export default util.createRule<Options, MessageIds>({
},
],
create(context, [{ ignoreParameters, ignoreProperties }]) {
const sourceCode = context.getSourceCode();

function isFunctionCall(
init: TSESTree.Expression,
callName: string,
Expand DownExpand Up@@ -215,7 +217,16 @@ export default util.createRule<Options, MessageIds>({
data: {
type,
},
fix: fixer => fixer.remove(typeNode),
*fix(fixer) {
if (
(node.type === AST_NODE_TYPES.AssignmentPattern &&
node.left.optional) ||
(node.type === AST_NODE_TYPES.PropertyDefinition && node.definite)
) {
yield fixer.remove(sourceCode.getTokenBefore(typeNode)!);
}
yield fixer.remove(typeNode);
},
});
}

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -156,7 +156,54 @@ class Foo {

invalid: [
...invalidTestCases,

{
// This is invalid TS semantic, but it's trivial to make valid anyway
code: 'const fn = (a?: number = 5) => {};',
Copy link
Collaborator

@armano2armano2Jul 13, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

not sure if we want to handle this in here, but there is one more use case that is broken.

playground

classTest{x!:number=5abstracty!:number=5}

maybe something like this could be better:

if(node.type===AST_NODE_TYPES.AssignmentPattern&&node.left.optional){yieldfixer.remove(sourceCode.getTokenBefore(typeNode)!);}elseif((node.type===AST_NODE_TYPES.TSAbstractPropertyDefinition||node.type===AST_NODE_TYPES.PropertyDefinition)&&node.definite){yieldfixer.remove(sourceCode.getTokenBefore(typeNode)!);}

we can make separate ticket for this, if you like


there is also

classTest2{constructor(privatea:number=5){}}

witch is currently not reported

Copy link
MemberAuthor

@Josh-CenaJosh-CenaJul 14, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'll add definite assignment to this PR, but theprivate one seems tangential to this. Does this one also seem worth reporting?

classTest2{x?:number=5;}

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Also, theabstract y!: number = 5 is too messed up to be autofixed. The initializer needs to be removed as well. It isn't even reported by the rule under the status quo, so I'll just ignore that.

output: 'const fn = (a = 5) => {};',
options: [
{
ignoreParameters: false,
},
],
errors: [
{
messageId: 'noInferrableType',
data: {
type: 'number',
},
line: 1,
column: 13,
},
],
},
{
// 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') => {};",
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp