Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Rule to disallow a duplicate case label. |
| 3 | * @author Dieter Oberkofler |
| 4 | * @author Burak Yigit Kaya |
| 5 | */ |
| 6 | |
| 7 | "use strict"; |
| 8 | |
| 9 | //------------------------------------------------------------------------------ |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 10 | // Requirements |
| 11 | //------------------------------------------------------------------------------ |
| 12 | |
| 13 | const astUtils = require("./utils/ast-utils"); |
| 14 | |
| 15 | //------------------------------------------------------------------------------ |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 16 | // Rule Definition |
| 17 | //------------------------------------------------------------------------------ |
| 18 | |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 19 | /** @type {import('../shared/types').Rule} */ |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 20 | module.exports = { |
| 21 | meta: { |
| 22 | type: "problem", |
| 23 | |
| 24 | docs: { |
| 25 | description: "disallow duplicate case labels", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 26 | recommended: true, |
| 27 | url: "https://eslint.org/docs/rules/no-duplicate-case" |
| 28 | }, |
| 29 | |
| 30 | schema: [], |
| 31 | |
| 32 | messages: { |
| 33 | unexpected: "Duplicate case label." |
| 34 | } |
| 35 | }, |
| 36 | |
| 37 | create(context) { |
| 38 | const sourceCode = context.getSourceCode(); |
| 39 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Determines whether the two given nodes are considered to be equal. |
| 42 | * @param {ASTNode} a First node. |
| 43 | * @param {ASTNode} b Second node. |
| 44 | * @returns {boolean} `true` if the nodes are considered to be equal. |
| 45 | */ |
| 46 | function equal(a, b) { |
| 47 | if (a.type !== b.type) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | return astUtils.equalTokens(a, b, sourceCode); |
| 52 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 53 | return { |
| 54 | SwitchStatement(node) { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 55 | const previousTests = []; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 56 | |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 57 | for (const switchCase of node.cases) { |
| 58 | if (switchCase.test) { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 59 | const test = switchCase.test; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 60 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 61 | if (previousTests.some(previousTest => equal(previousTest, test))) { |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 62 | context.report({ node: switchCase, messageId: "unexpected" }); |
| 63 | } else { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 64 | previousTests.push(test); |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 65 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 66 | } |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 67 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | } |
| 71 | }; |