mirror of
https://github.com/nodejs/node.git
synced 2025-05-19 07:50:07 +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>
42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
/**
|
|
* @fileoverview Rule to flag use of duplicate keys in an object.
|
|
* @author Ian Christian Myers
|
|
* @copyright 2013 Ian Christian Myers. All rights reserved.
|
|
* @copyright 2013 Nicholas C. Zakas. All rights reserved.
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
module.exports = function(context) {
|
|
|
|
return {
|
|
|
|
"ObjectExpression": function(node) {
|
|
|
|
// Object that will be a map of properties--safe because we will
|
|
// prefix all of the keys.
|
|
var nodeProps = Object.create(null);
|
|
|
|
node.properties.forEach(function(property) {
|
|
var keyName = property.key.name || property.key.value,
|
|
key = property.kind + "-" + keyName,
|
|
checkProperty = (!property.computed || property.key.type === "Literal");
|
|
|
|
if (checkProperty) {
|
|
if (nodeProps[key]) {
|
|
context.report(node, "Duplicate key '{{key}}'.", { key: keyName });
|
|
} else {
|
|
nodeProps[key] = true;
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
};
|
|
|
|
};
|