Skip to content

Commit

Permalink
Update: Add metadata to existing rules - Batch 4 (refs eslint#5417)
Browse files Browse the repository at this point in the history
Add metadata to the existing rules. Batch 4.
  • Loading branch information
vitorbal committed Apr 18, 2016
1 parent c81b32d commit 41a67cf
Show file tree
Hide file tree
Showing 40 changed files with 3,126 additions and 2,723 deletions.
146 changes: 78 additions & 68 deletions lib/rules/no-extend-native.js
Expand Up @@ -15,87 +15,97 @@ var globals = require("globals");
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

var config = context.options[0] || {};
var exceptions = config.exceptions || [];
var modifiedBuiltins = Object.keys(globals.builtin).filter(function(builtin) {
return builtin[0].toUpperCase() === builtin[0];
});

if (exceptions.length) {
modifiedBuiltins = modifiedBuiltins.filter(function(builtIn) {
return exceptions.indexOf(builtIn) === -1;
});
}

return {

// handle the Array.prototype.extra style case
"AssignmentExpression": function(node) {
var lhs = node.left,
affectsProto;
module.exports = {
meta: {
docs: {
description: "disallow extending native types",
category: "Best Practices",
recommended: false
},

if (lhs.type !== "MemberExpression" || lhs.object.type !== "MemberExpression") {
return;
schema: [
{
"type": "object",
"properties": {
"exceptions": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true
}
},
"additionalProperties": false
}
]
},

affectsProto = lhs.object.computed ?
lhs.object.property.type === "Literal" && lhs.object.property.value === "prototype" :
lhs.object.property.name === "prototype";
create: function(context) {

if (!affectsProto) {
return;
}
var config = context.options[0] || {};
var exceptions = config.exceptions || [];
var modifiedBuiltins = Object.keys(globals.builtin).filter(function(builtin) {
return builtin[0].toUpperCase() === builtin[0];
});

modifiedBuiltins.forEach(function(builtin) {
if (lhs.object.object.name === builtin) {
context.report(node, builtin + " prototype is read only, properties should not be added.");
}
if (exceptions.length) {
modifiedBuiltins = modifiedBuiltins.filter(function(builtIn) {
return exceptions.indexOf(builtIn) === -1;
});
},
}

// handle the Object.definePropert[y|ies](Array.prototype) case
"CallExpression": function(node) {
return {

var callee = node.callee,
subject,
object;
// handle the Array.prototype.extra style case
"AssignmentExpression": function(node) {
var lhs = node.left,
affectsProto;

// only worry about Object.definePropert[y|ies]
if (callee.type === "MemberExpression" &&
callee.object.name === "Object" &&
(callee.property.name === "defineProperty" || callee.property.name === "defineProperties")) {
if (lhs.type !== "MemberExpression" || lhs.object.type !== "MemberExpression") {
return;
}

// verify the object being added to is a native prototype
subject = node.arguments[0];
object = subject && subject.object;
if (object &&
object.type === "Identifier" &&
(modifiedBuiltins.indexOf(object.name) > -1) &&
subject.property.name === "prototype") {
affectsProto = lhs.object.computed ?
lhs.object.property.type === "Literal" && lhs.object.property.value === "prototype" :
lhs.object.property.name === "prototype";

context.report(node, object.name + " prototype is read only, properties should not be added.");
if (!affectsProto) {
return;
}
}

}
};

};
modifiedBuiltins.forEach(function(builtin) {
if (lhs.object.object.name === builtin) {
context.report(node, builtin + " prototype is read only, properties should not be added.");
}
});
},

// handle the Object.definePropert[y|ies](Array.prototype) case
"CallExpression": function(node) {

var callee = node.callee,
subject,
object;

// only worry about Object.definePropert[y|ies]
if (callee.type === "MemberExpression" &&
callee.object.name === "Object" &&
(callee.property.name === "defineProperty" || callee.property.name === "defineProperties")) {

// verify the object being added to is a native prototype
subject = node.arguments[0];
object = subject && subject.object;
if (object &&
object.type === "Identifier" &&
(modifiedBuiltins.indexOf(object.name) > -1) &&
subject.property.name === "prototype") {

context.report(node, object.name + " prototype is read only, properties should not be added.");
}
}

module.exports.schema = [
{
"type": "object",
"properties": {
"exceptions": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true
}
},
"additionalProperties": false
};

}
];
};

0 comments on commit 41a67cf

Please sign in to comment.