2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview enforce default parameters to be last
|
|
|
|
* @author Chiawen Chen
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
/** @type {import('../shared/types').Rule} */
|
2024-08-09 12:04:48 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
type: "suggestion",
|
|
|
|
|
|
|
|
docs: {
|
2024-08-21 06:34:30 +00:00
|
|
|
description: "Enforce default parameters to be last",
|
2024-08-09 12:04:48 +00:00
|
|
|
recommended: false,
|
2024-08-21 06:34:30 +00:00
|
|
|
url: "https://eslint.org/docs/latest/rules/default-param-last"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
schema: [],
|
|
|
|
|
|
|
|
messages: {
|
|
|
|
shouldBeLast: "Default parameters should be last."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
create(context) {
|
|
|
|
|
|
|
|
/**
|
2024-08-21 06:34:30 +00:00
|
|
|
* Handler for function contexts.
|
2024-08-09 12:04:48 +00:00
|
|
|
* @param {ASTNode} node function node
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function handleFunction(node) {
|
|
|
|
let hasSeenPlainParam = false;
|
|
|
|
|
|
|
|
for (let i = node.params.length - 1; i >= 0; i -= 1) {
|
|
|
|
const param = node.params[i];
|
|
|
|
|
|
|
|
if (
|
|
|
|
param.type !== "AssignmentPattern" &&
|
|
|
|
param.type !== "RestElement"
|
|
|
|
) {
|
|
|
|
hasSeenPlainParam = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasSeenPlainParam && param.type === "AssignmentPattern") {
|
|
|
|
context.report({
|
|
|
|
node: param,
|
|
|
|
messageId: "shouldBeLast"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
FunctionDeclaration: handleFunction,
|
|
|
|
FunctionExpression: handleFunction,
|
|
|
|
ArrowFunctionExpression: handleFunction
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|