Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 720e811

Browse files
feat(eslint-plugin): [consistent-type-assertions] add suggestions for objectLiteralTypeAssertions (typescript-eslint#6642)
* Add suggestions for objectLiteralTypeAssertions * Add additional test cases * Capture parentheses * Use changes from other PR --------- Co-authored-by: Josh Goldberg <[email protected]>
1 parent dd5167f commit 720e811

File tree

2 files changed

+285
-1
lines changed

2 files changed

+285
-1
lines changed

packages/eslint-plugin/src/rules/consistent-type-assertions.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ type MessageIds =
88
| 'as'
99
| 'angle-bracket'
1010
| 'never'
11-
| 'unexpectedObjectTypeAssertion';
11+
| 'unexpectedObjectTypeAssertion'
12+
| 'replaceObjectTypeAssertionWithAnnotation'
13+
| 'replaceObjectTypeAssertionWithSatisfies';
1214
type OptUnion =
1315
| {
1416
assertionStyle: 'as' | 'angle-bracket';
@@ -24,6 +26,7 @@ export default util.createRule<Options, MessageIds>({
2426
meta: {
2527
type: 'suggestion',
2628
fixable: 'code',
29+
hasSuggestions: true,
2730
docs: {
2831
description: 'Enforce consistent usage of type assertions',
2932
recommended: 'strict',
@@ -33,6 +36,10 @@ export default util.createRule<Options, MessageIds>({
3336
'angle-bracket': "Use '<{{cast}}>' instead of 'as {{cast}}'.",
3437
never: 'Do not use any type assertions.',
3538
unexpectedObjectTypeAssertion: 'Always prefer const x: T = { ... }.',
39+
replaceObjectTypeAssertionWithAnnotation:
40+
'Use const x: {{cast}} = { ... } instead.',
41+
replaceObjectTypeAssertionWithSatisfies:
42+
'Use const x = { ... } satisfies {{cast}} instead.',
3643
},
3744
schema: [
3845
{
@@ -184,9 +191,42 @@ export default util.createRule<Options, MessageIds>({
184191
checkType(node.typeAnnotation) &&
185192
node.expression.type === AST_NODE_TYPES.ObjectExpression
186193
) {
194+
const suggest: TSESLint.ReportSuggestionArray<MessageIds> = [];
195+
if (
196+
node.parent?.type === AST_NODE_TYPES.VariableDeclarator &&
197+
!node.parent.id.typeAnnotation
198+
) {
199+
const { parent } = node;
200+
suggest.push({
201+
messageId: 'replaceObjectTypeAssertionWithAnnotation',
202+
data: { cast: sourceCode.getText(node.typeAnnotation) },
203+
fix: fixer => [
204+
fixer.insertTextAfter(
205+
parent.id,
206+
`: ${sourceCode.getText(node.typeAnnotation)}`,
207+
),
208+
fixer.replaceText(node, getTextWithParentheses(node.expression)),
209+
],
210+
});
211+
}
212+
suggest.push({
213+
messageId: 'replaceObjectTypeAssertionWithSatisfies',
214+
data: { cast: sourceCode.getText(node.typeAnnotation) },
215+
fix: fixer => [
216+
fixer.replaceText(node, getTextWithParentheses(node.expression)),
217+
fixer.insertTextAfter(
218+
node,
219+
` satisfies ${context
220+
.getSourceCode()
221+
.getText(node.typeAnnotation)}`,
222+
),
223+
],
224+
});
225+
187226
context.report({
188227
node,
189228
messageId: 'unexpectedObjectTypeAssertion',
229+
suggest,
190230
});
191231
}
192232
}

0 commit comments

Comments
 (0)