2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Rule to enforce default clauses in switch statements to be last
|
|
|
|
* @author Milos Djermanovic
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
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 clauses in switch statements 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-case-last"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|