Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [consistent-type-assertions] addarrayLiteralTypeAssertions
option#6749
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
a920f19
f1e9378
5eada0f
2df3837
f0b4dc6
79772e3
ea3c340
4855d45
24dac63
a150480
fd20281
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -9,12 +9,16 @@ type MessageIds = | ||
| 'angle-bracket' | ||
| 'never' | ||
| 'unexpectedObjectTypeAssertion' | ||
| 'unexpectedArrayTypeAssertion' | ||
| 'replaceObjectTypeAssertionWithAnnotation' | ||
| 'replaceObjectTypeAssertionWithSatisfies' | ||
| 'replaceArrayTypeAssertionWithAnnotation' | ||
| 'replaceArrayTypeAssertionWithSatisfies'; | ||
type OptUnion = | ||
| { | ||
assertionStyle: 'as' | 'angle-bracket'; | ||
objectLiteralTypeAssertions?: 'allow' | 'allow-as-parameter' | 'never'; | ||
arrayLiteralTypeAssertions?: 'allow' | 'never'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
Yeah it feels weird to have one without the other... Could you please? | ||
} | ||
| { | ||
assertionStyle: 'never'; | ||
@@ -36,10 +40,15 @@ export default util.createRule<Options, MessageIds>({ | ||
'angle-bracket': "Use '<{{cast}}>' instead of 'as {{cast}}'.", | ||
never: 'Do not use any type assertions.', | ||
unexpectedObjectTypeAssertion: 'Always prefer const x: T = { ... }.', | ||
unexpectedArrayTypeAssertion: 'Always prefer const x: T[] = [ ... ].', | ||
replaceObjectTypeAssertionWithAnnotation: | ||
'Use const x: {{cast}} = { ... } instead.', | ||
replaceObjectTypeAssertionWithSatisfies: | ||
'Use const x = { ... } satisfies {{cast}} instead.', | ||
replaceArrayTypeAssertionWithAnnotation: | ||
'Use const x: [{cast}] = [ ... ] instead.', | ||
replaceArrayTypeAssertionWithSatisfies: | ||
'Use const x = [ ... ] satisfies [{cast}] instead.', | ||
}, | ||
schema: [ | ||
{ | ||
@@ -63,6 +72,9 @@ export default util.createRule<Options, MessageIds>({ | ||
objectLiteralTypeAssertions: { | ||
enum: ['allow', 'allow-as-parameter', 'never'], | ||
}, | ||
arrayLiteralTypeAssertions: { | ||
enum: ['allow', 'never'], | ||
}, | ||
}, | ||
additionalProperties: false, | ||
required: ['assertionStyle'], | ||
@@ -75,6 +87,7 @@ export default util.createRule<Options, MessageIds>({ | ||
{ | ||
assertionStyle: 'as', | ||
objectLiteralTypeAssertions: 'allow', | ||
arrayLiteralTypeAssertions: 'allow', | ||
}, | ||
], | ||
create(context, [options]) { | ||
@@ -164,7 +177,46 @@ export default util.createRule<Options, MessageIds>({ | ||
} | ||
} | ||
function getReplacementSuggestions( | ||
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression, | ||
annotationMessageId: MessageIds, | ||
satisfiesMessageId: MessageIds, | ||
): TSESLint.ReportSuggestionArray<MessageIds> { | ||
const suggest: TSESLint.ReportSuggestionArray<MessageIds> = []; | ||
if ( | ||
node.parent?.type === AST_NODE_TYPES.VariableDeclarator && | ||
!node.parent.id.typeAnnotation | ||
) { | ||
const { parent } = node; | ||
suggest.push({ | ||
messageId: annotationMessageId, | ||
data: { cast: sourceCode.getText(node.typeAnnotation) }, | ||
fix: fixer => [ | ||
fixer.insertTextAfter( | ||
parent.id, | ||
`: ${sourceCode.getText(node.typeAnnotation)}`, | ||
), | ||
fixer.replaceText(node, getTextWithParentheses(node.expression)), | ||
], | ||
}); | ||
} | ||
suggest.push({ | ||
messageId: satisfiesMessageId, | ||
data: { cast: sourceCode.getText(node.typeAnnotation) }, | ||
fix: fixer => [ | ||
fixer.replaceText(node, getTextWithParentheses(node.expression)), | ||
fixer.insertTextAfter( | ||
node, | ||
` satisfies ${context | ||
.getSourceCode() | ||
.getText(node.typeAnnotation)}`, | ||
), | ||
], | ||
}); | ||
return suggest; | ||
} | ||
function checkExpressionForObjectAssertion( | ||
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression, | ||
): void { | ||
if ( | ||
@@ -191,37 +243,11 @@ export default util.createRule<Options, MessageIds>({ | ||
checkType(node.typeAnnotation) && | ||
node.expression.type === AST_NODE_TYPES.ObjectExpression | ||
) { | ||
const suggest = getReplacementSuggestions( | ||
node, | ||
'replaceObjectTypeAssertionWithAnnotation', | ||
'replaceObjectTypeAssertionWithSatisfies', | ||
); | ||
context.report({ | ||
node, | ||
@@ -231,22 +257,53 @@ export default util.createRule<Options, MessageIds>({ | ||
} | ||
} | ||
function checkExpressionForArrayAssertion( | ||
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression, | ||
): void { | ||
if ( | ||
options.assertionStyle === 'never' || | ||
options.arrayLiteralTypeAssertions === 'allow' || | ||
node.expression.type !== AST_NODE_TYPES.ArrayExpression | ||
) { | ||
return; | ||
} | ||
if ( | ||
checkType(node.typeAnnotation) && | ||
node.expression.type === AST_NODE_TYPES.ArrayExpression | ||
) { | ||
const suggest = getReplacementSuggestions( | ||
node, | ||
'replaceArrayTypeAssertionWithAnnotation', | ||
'replaceArrayTypeAssertionWithSatisfies', | ||
); | ||
context.report({ | ||
node, | ||
messageId: 'unexpectedArrayTypeAssertion', | ||
suggest, | ||
}); | ||
} | ||
} | ||
return { | ||
TSTypeAssertion(node): void { | ||
if (options.assertionStyle !== 'angle-bracket') { | ||
reportIncorrectAssertionType(node); | ||
return; | ||
} | ||
checkExpressionForObjectAssertion(node); | ||
checkExpressionForArrayAssertion(node); | ||
}, | ||
TSAsExpression(node): void { | ||
if (options.assertionStyle !== 'as') { | ||
reportIncorrectAssertionType(node); | ||
return; | ||
} | ||
checkExpressionForObjectAssertion(node); | ||
checkExpressionForArrayAssertion(node); | ||
}, | ||
}; | ||
}, | ||
Uh oh!
There was an error while loading.Please reload this page.