UNPKG

1.6 kBJavaScriptView Raw
1/**
2 * @fileoverview enforce default parameters to be last
3 * @author Chiawen Chen
4 */
5
6"use strict";
7
8module.exports = {
9 meta: {
10 type: "suggestion",
11
12 docs: {
13 description: "enforce default parameters to be last",
14 category: "Best Practices",
15 recommended: false,
16 url: "https://eslint.org/docs/rules/default-param-last"
17 },
18
19 schema: [],
20
21 messages: {
22 shouldBeLast: "Default parameters should be last."
23 }
24 },
25
26 create(context) {
27
28 // eslint-disable-next-line jsdoc/require-description
29 /**
30 * @param {ASTNode} node function node
31 * @returns {void}
32 */
33 function handleFunction(node) {
34 let hasSeenPlainParam = false;
35
36 for (let i = node.params.length - 1; i >= 0; i -= 1) {
37 const param = node.params[i];
38
39 if (
40 param.type !== "AssignmentPattern" &&
41 param.type !== "RestElement"
42 ) {
43 hasSeenPlainParam = true;
44 continue;
45 }
46
47 if (hasSeenPlainParam && param.type === "AssignmentPattern") {
48 context.report({
49 node: param,
50 messageId: "shouldBeLast"
51 });
52 }
53 }
54 }
55
56 return {
57 FunctionDeclaration: handleFunction,
58 FunctionExpression: handleFunction,
59 ArrowFunctionExpression: handleFunction
60 };
61 }
62};