default-case-last.js
1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* @fileoverview Rule to enforce default clauses in switch statements to be last
* @author Milos Djermanovic
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('../shared/types').Rule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "Enforce default clauses in switch statements to be last",
recommended: false,
url: "https://eslint.org/docs/rules/default-case-last"
},
schema: [],
messages: {
notLast: "Default clause should be the last clause."
}
},
create(context) {
return {
SwitchStatement(node) {
const cases = node.cases,
indexOfDefault = cases.findIndex(c => c.test === null);
if (indexOfDefault !== -1 && indexOfDefault !== cases.length - 1) {
const defaultClause = cases[indexOfDefault];
context.report({ node: defaultClause, messageId: "notLast" });
}
}
};
}
};