mirror of
https://github.com/nodejs/node.git
synced 2025-05-19 00:47:24 +00:00

PR-URL: https://github.com/iojs/io.js/pull/1539 Fixes: https://github.com/iojs/io.js/issues/1253 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com> Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
/**
|
|
* @fileoverview Rule to disallow a duplicate case label.
|
|
* @author Dieter Oberkofler
|
|
* @copyright 2015 Dieter Oberkofler. All rights reserved.
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
module.exports = function(context) {
|
|
|
|
/**
|
|
* Get a hash value for the node
|
|
* @param {ASTNode} node The node.
|
|
* @returns {string} A hash value for the node.
|
|
* @private
|
|
*/
|
|
function getHash(node) {
|
|
if (node.type === "Literal") {
|
|
return node.type + typeof node.value + node.value;
|
|
} else if (node.type === "Identifier") {
|
|
return node.type + typeof node.name + node.name;
|
|
} else if (node.type === "MemberExpression") {
|
|
return node.type + getHash(node.object) + getHash(node.property);
|
|
}
|
|
}
|
|
|
|
var switchStatement = [];
|
|
|
|
return {
|
|
|
|
"SwitchStatement": function(/*node*/) {
|
|
switchStatement.push({});
|
|
},
|
|
|
|
"SwitchStatement:exit": function(/*node*/) {
|
|
switchStatement.pop();
|
|
},
|
|
|
|
"SwitchCase": function(node) {
|
|
var currentSwitch = switchStatement[switchStatement.length - 1],
|
|
hashValue;
|
|
|
|
if (node.test) {
|
|
hashValue = getHash(node.test);
|
|
if (typeof hashValue !== "undefined" && currentSwitch.hasOwnProperty(hashValue)) {
|
|
context.report(node, "Duplicate case label.");
|
|
} else {
|
|
currentSwitch[hashValue] = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
};
|