β¬ οΈ Back to Table of Contents
π prefer-reduce-type-parameter¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 3 |
| π¦ Imports | 6 |
| π Type Aliases | 1 |
π Table of Contents¶
π οΈ File Location:¶
π packages/eslint-plugin/src/rules/prefer-reduce-type-parameter.ts
π€ Default Export¶
| Property | Value |
|---|---|
name |
'prefer-reduce-type-parameter' |
meta.type |
'problem' |
meta.docs.description |
'Enforce using type parameter when calling Array#reduce instead of using a type assertion' |
meta.docs.recommended |
'strict' |
meta.docs.requiresTypeChecking |
true |
meta.fixable |
'code' |
meta.messages.preferTypeParameter |
'Unnecessary assertion: Array#reduce accepts a type parameter for the default value.' |
meta.schema |
[] |
defaultOptions |
[] |
Entry point: create β documented under Functions.
π¦ Imports¶
| Name | Source |
|---|---|
TSESTree |
@typescript-eslint/utils |
createRule |
../util |
getConstrainedTypeAtLocation |
../util |
getParserServices |
../util |
isStaticMemberAccessOfValue |
../util |
isTypeAssertion |
../util |
Functions¶
create(context: any): { 'CallExpression > MemberExpression.callee'(callee: Member⦶
Parameters:
contextany
Returns: { 'CallExpression > MemberExpression.callee'(callee: MemberExpressionWithCallExpressionParent): void; }
Calls:
getParserServices (from ../util)services.program.getTypeCheckertsutils .unionConstituents(type) .everytsutils .intersectionConstituents(unionPart) .everychecker.isArrayTypechecker.isTupleTypeisStaticMemberAccessOfValue (from ../util)isTypeAssertion (from ../util)services.getTypeAtLocationchecker.isTypeAssignableTogetConstrainedTypeAtLocation (from ../util)isArrayTypecontext.reportfixer.removeRangefixes.pushfixer.insertTextAftercontext.sourceCode.getText
Internal Comments:
// don't report this if the resulting fix will be a type error
// Get the symbol of the `reduce` method. (x2)
// Check the owner type of the `reduce` method.
Code
create(context) {
const services = getParserServices(context);
const checker = services.program.getTypeChecker();
function isArrayType(type: ts.Type): boolean {
return tsutils
.unionConstituents(type)
.every(unionPart =>
tsutils
.intersectionConstituents(unionPart)
.every(t => checker.isArrayType(t) || checker.isTupleType(t)),
);
}
return {
'CallExpression > MemberExpression.callee'(
callee: MemberExpressionWithCallExpressionParent,
): void {
if (!isStaticMemberAccessOfValue(callee, context, 'reduce')) {
return;
}
const [, secondArg] = callee.parent.arguments;
if (callee.parent.arguments.length < 2) {
return;
}
if (isTypeAssertion(secondArg)) {
const initializerType = services.getTypeAtLocation(
secondArg.expression,
);
const assertedType = services.getTypeAtLocation(
secondArg.typeAnnotation,
);
const isAssertionNecessary = !checker.isTypeAssignableTo(
initializerType,
assertedType,
);
// don't report this if the resulting fix will be a type error
if (isAssertionNecessary) {
return;
}
} else {
return;
}
// Get the symbol of the `reduce` method.
const calleeObjType = getConstrainedTypeAtLocation(
services,
callee.object,
);
// Check the owner type of the `reduce` method.
if (isArrayType(calleeObjType)) {
context.report({
node: secondArg,
messageId: 'preferTypeParameter',
fix: fixer => {
const fixes = [
fixer.removeRange([
secondArg.range[0],
secondArg.expression.range[0],
]),
fixer.removeRange([
secondArg.expression.range[1],
secondArg.range[1],
]),
];
if (!callee.parent.typeArguments) {
fixes.push(
fixer.insertTextAfter(
callee,
`<${context.sourceCode.getText(secondArg.typeAnnotation)}>`,
),
);
}
return fixes;
},
});
return;
}
},
};
}
Internal helpers¶
Declared inside another function in this file.
isArrayType(type: ts.Type): boolean¶
Parameters:
typets.Type
Returns: boolean
Calls:
tsutils .unionConstituents(type) .everytsutils .intersectionConstituents(unionPart) .everychecker.isArrayTypechecker.isTupleType
Code
fix(fixer: any): any[]¶
Parameters:
fixerany
Returns: any[]
Calls:
fixer.removeRangefixes.pushfixer.insertTextAftercontext.sourceCode.getText
Code
fixer => {
const fixes = [
fixer.removeRange([
secondArg.range[0],
secondArg.expression.range[0],
]),
fixer.removeRange([
secondArg.expression.range[1],
secondArg.range[1],
]),
];
if (!callee.parent.typeArguments) {
fixes.push(
fixer.insertTextAfter(
callee,
`<${context.sourceCode.getText(secondArg.typeAnnotation)}>`,
),
);
}
return fixes;
}
Type Aliases¶
MemberExpressionWithCallExpressionParent¶
type MemberExpressionWithCallExpressionParent = {
parent: TSESTree.CallExpression;
} & TSESTree.MemberExpression;
Generated by Syntax Scribe