mirror of
https://github.com/nodejs/node.git
synced 2026-12-06 10:18:46 -05:00
PR-URL: https://github.com/nodejs/node/pull/40394 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
/**
|
|
* @fileoverview Rule to enforce default clauses in switch statements to be last
|
|
* @author Milos Djermanovic
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
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" });
|
|
}
|
|
}
|
|
};
|
|
}
|
|
};
|