β¬ οΈ Back to Table of Contents
π default-param-last¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 4 |
| π¦ Imports | 3 |
π Table of Contents¶
π οΈ File Location:¶
π packages/eslint-plugin/src/rules/default-param-last.ts
π€ Default Export¶
| Property | Value |
|---|---|
name |
'default-param-last' |
meta.type |
'suggestion' |
meta.docs.description |
'Enforce default parameters to be last' |
meta.docs.extendsBaseRule |
true |
meta.docs.frozen |
true |
meta.messages.shouldBeLast |
'Default parameters should be last.' |
meta.schema |
[] |
defaultOptions |
[] |
Entry point: create β documented under Functions.
π¦ Imports¶
| Name | Source |
|---|---|
TSESTree |
@typescript-eslint/utils |
AST_NODE_TYPES |
@typescript-eslint/utils |
createRule |
../util |
Functions¶
create(context: any): { ArrowFunctionExpression: (node: TSESTree.ArrowFunctionExp⦶
Parameters:
contextany
Returns: { ArrowFunctionExpression: (node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression) => void; FunctionDeclaration: (node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression) => void; FunctionExpression: (node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression) => void; }
Calls:
isOptionalParamisPlainParamcontext.report
Internal Comments:
/**
* checks if node is optional parameter
* @param node the node to be evaluated
* @private
*/
/**
* checks if node is plain parameter
* @param node the node to be evaluated
* @private
*/
Code
create(context) {
/**
* checks if node is optional parameter
* @param node the node to be evaluated
* @private
*/
function isOptionalParam(node: TSESTree.Parameter): boolean {
return (
(node.type === AST_NODE_TYPES.ArrayPattern ||
node.type === AST_NODE_TYPES.AssignmentPattern ||
node.type === AST_NODE_TYPES.Identifier ||
node.type === AST_NODE_TYPES.ObjectPattern ||
node.type === AST_NODE_TYPES.RestElement) &&
node.optional
);
}
/**
* checks if node is plain parameter
* @param node the node to be evaluated
* @private
*/
function isPlainParam(node: TSESTree.Parameter): boolean {
return !(
node.type === AST_NODE_TYPES.AssignmentPattern ||
node.type === AST_NODE_TYPES.RestElement ||
isOptionalParam(node)
);
}
function checkDefaultParamLast(
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
): void {
let hasSeenPlainParam = false;
for (let i = node.params.length - 1; i >= 0; i--) {
const current = node.params[i];
const param =
current.type === AST_NODE_TYPES.TSParameterProperty
? current.parameter
: current;
if (isPlainParam(param)) {
hasSeenPlainParam = true;
continue;
}
if (
hasSeenPlainParam &&
(isOptionalParam(param) ||
param.type === AST_NODE_TYPES.AssignmentPattern)
) {
context.report({ node: current, messageId: 'shouldBeLast' });
}
}
}
return {
ArrowFunctionExpression: checkDefaultParamLast,
FunctionDeclaration: checkDefaultParamLast,
FunctionExpression: checkDefaultParamLast,
};
}
Internal helpers¶
Declared inside another function in this file.
isOptionalParam(node: TSESTree.Parameter): boolean¶
checks if node is optional parameter
Parameters:
nodeany: the node to be evaluated
Tags: @private
Raw JSDoc
Code
function isOptionalParam(node: TSESTree.Parameter): boolean {
return (
(node.type === AST_NODE_TYPES.ArrayPattern ||
node.type === AST_NODE_TYPES.AssignmentPattern ||
node.type === AST_NODE_TYPES.Identifier ||
node.type === AST_NODE_TYPES.ObjectPattern ||
node.type === AST_NODE_TYPES.RestElement) &&
node.optional
);
}
isPlainParam(node: TSESTree.Parameter): boolean¶
checks if node is plain parameter
Parameters:
nodeany: the node to be evaluated
Tags: @private
Raw JSDoc
Calls:
isOptionalParam
Code
checkDefaultParamLast(node: | TSESTree.ArrowFunctionExpression | TSβ¦): void¶
Parameters:
node| TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression
Returns: void
Calls:
isPlainParamisOptionalParamcontext.report
Code
function checkDefaultParamLast(
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
): void {
let hasSeenPlainParam = false;
for (let i = node.params.length - 1; i >= 0; i--) {
const current = node.params[i];
const param =
current.type === AST_NODE_TYPES.TSParameterProperty
? current.parameter
: current;
if (isPlainParam(param)) {
hasSeenPlainParam = true;
continue;
}
if (
hasSeenPlainParam &&
(isOptionalParam(param) ||
param.type === AST_NODE_TYPES.AssignmentPattern)
) {
context.report({ node: current, messageId: 'shouldBeLast' });
}
}
}
Generated by Syntax Scribe