1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | module.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 |
|
29 | |
30 |
|
31 |
|
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 | };
|