You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.2 KiB
45 lines
1.2 KiB
1 month ago
|
/**
|
||
|
* @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/latest/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" });
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
};
|