Skip to content

Commit

Permalink
Chore: enable no-prototype-builtins in codebase (fixes #10660) (#10664)
Browse files Browse the repository at this point in the history
* Chore: enable no-prototype-builtins in codebase

* Chore: fix hasown linting errors

* Chore: hasOwn => Object.prototype.hasOwnProperty

* Chore: rm hasOwn

* Chore: add tests
  • Loading branch information
aladdin-add committed Aug 11, 2018
1 parent 137140f commit 6492233
Show file tree
Hide file tree
Showing 30 changed files with 66 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Makefile.js
Expand Up @@ -871,7 +871,7 @@ target.checkRuleFiles = function() {
* @private
*/
function isInConfig() {
return eslintRecommended.hasOwnProperty(basename);
return Object.prototype.hasOwnProperty.call(eslintRecommended, basename);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/cli-engine.js
Expand Up @@ -30,7 +30,6 @@ const fs = require("fs"),
pkg = require("../package.json");

const debug = require("debug")("eslint:cli-engine");

const resolver = new ModuleResolver();

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -469,7 +468,7 @@ class CLIEngine {
* @returns {void}
*/
static outputFixes(report) {
report.results.filter(result => result.hasOwnProperty("output")).forEach(result => {
report.results.filter(result => Object.prototype.hasOwnProperty.call(result, "output")).forEach(result => {
fs.writeFileSync(result.filePath, result.output);
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/comma-style.js
Expand Up @@ -58,7 +58,7 @@ module.exports = {
NewExpression: true
};

if (context.options.length === 2 && context.options[1].hasOwnProperty("exceptions")) {
if (context.options.length === 2 && Object.prototype.hasOwnProperty.call(context.options[1], "exceptions")) {
const keys = Object.keys(context.options[1].exceptions);

for (let i = 0; i < keys.length; i++) {
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/complexity.js
Expand Up @@ -61,10 +61,10 @@ module.exports = {
const option = context.options[0];
let THRESHOLD = 20;

if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
THRESHOLD = option.maximum;
}
if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
THRESHOLD = option.max;
}
if (typeof option === "number") {
Expand Down
8 changes: 6 additions & 2 deletions lib/rules/indent.js
Expand Up @@ -1333,15 +1333,19 @@ module.exports = {
node.expressions.forEach((expression, index) => {
const previousQuasi = node.quasis[index];
const nextQuasi = node.quasis[index + 1];
const tokenToAlignFrom = previousQuasi.loc.start.line === previousQuasi.loc.end.line ? sourceCode.getFirstToken(previousQuasi) : null;
const tokenToAlignFrom = previousQuasi.loc.start.line === previousQuasi.loc.end.line
? sourceCode.getFirstToken(previousQuasi)
: null;

offsets.setDesiredOffsets([previousQuasi.range[1], nextQuasi.range[0]], tokenToAlignFrom, 1);
offsets.setDesiredOffset(sourceCode.getFirstToken(nextQuasi), tokenToAlignFrom, 0);
});
},

VariableDeclaration(node) {
const variableIndent = options.VariableDeclarator.hasOwnProperty(node.kind) ? options.VariableDeclarator[node.kind] : DEFAULT_VARIABLE_INDENT;
const variableIndent = Object.prototype.hasOwnProperty.call(options.VariableDeclarator, node.kind)
? options.VariableDeclarator[node.kind]
: DEFAULT_VARIABLE_INDENT;

if (node.declarations[node.declarations.length - 1].loc.start.line > node.loc.start.line) {

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/line-comment-position.js
Expand Up @@ -62,7 +62,7 @@ module.exports = {
above = !options.position || options.position === "above";
ignorePattern = options.ignorePattern;

if (options.hasOwnProperty("applyDefaultIgnorePatterns")) {
if (Object.prototype.hasOwnProperty.call(options, "applyDefaultIgnorePatterns")) {
applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns !== false;
} else {
applyDefaultIgnorePatterns = options.applyDefaultPatterns !== false;
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/max-depth.js
Expand Up @@ -54,10 +54,10 @@ module.exports = {
option = context.options[0];
let maxDepth = 4;

if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
maxDepth = option.maximum;
}
if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
maxDepth = option.max;
}
if (typeof option === "number") {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/max-lines.js
Expand Up @@ -56,7 +56,7 @@ module.exports = {
const option = context.options[0];
let max = 300;

if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
max = option.max;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/max-nested-callbacks.js
Expand Up @@ -52,10 +52,10 @@ module.exports = {
const option = context.options[0];
let THRESHOLD = 10;

if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
THRESHOLD = option.maximum;
}
if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
THRESHOLD = option.max;
}
if (typeof option === "number") {
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/max-params.js
Expand Up @@ -57,10 +57,10 @@ module.exports = {
const option = context.options[0];
let numParams = 3;

if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
numParams = option.maximum;
}
if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
numParams = option.max;
}
if (typeof option === "number") {
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/max-statements.js
Expand Up @@ -73,10 +73,10 @@ module.exports = {
topLevelFunctions = [];
let maxStatements = 10;

if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
maxStatements = option.maximum;
}
if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
maxStatements = option.max;
}
if (typeof option === "number") {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-restricted-globals.js
Expand Up @@ -94,7 +94,7 @@ module.exports = {
* @private
*/
function isRestricted(name) {
return restrictedGlobalMessages.hasOwnProperty(name);
return Object.prototype.hasOwnProperty.call(restrictedGlobalMessages, name);
}

return {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-restricted-imports.js
Expand Up @@ -83,7 +83,7 @@ module.exports = {
const options = Array.isArray(context.options) ? context.options : [];
const isPathAndPatternsObject =
typeof options[0] === "object" &&
(options[0].hasOwnProperty("paths") || options[0].hasOwnProperty("patterns"));
(Object.prototype.hasOwnProperty.call(options[0], "paths") || Object.prototype.hasOwnProperty.call(options[0], "patterns"));

const restrictedPaths = (isPathAndPatternsObject ? options[0].paths : context.options) || [];
const restrictedPatterns = (isPathAndPatternsObject ? options[0].patterns : []) || [];
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-restricted-modules.js
Expand Up @@ -77,7 +77,7 @@ module.exports = {
const options = Array.isArray(context.options) ? context.options : [];
const isPathAndPatternsObject =
typeof options[0] === "object" &&
(options[0].hasOwnProperty("paths") || options[0].hasOwnProperty("patterns"));
(Object.prototype.hasOwnProperty.call(options[0], "paths") || Object.prototype.hasOwnProperty.call(options[0], "patterns"));

const restrictedPaths = (isPathAndPatternsObject ? options[0].paths : context.options) || [];
const restrictedPatterns = (isPathAndPatternsObject ? options[0].patterns : []) || [];
Expand Down
12 changes: 6 additions & 6 deletions lib/rules/one-var.js
Expand Up @@ -74,19 +74,19 @@ module.exports = {
options.let = { uninitialized: mode, initialized: mode };
options.const = { uninitialized: mode, initialized: mode };
} else if (typeof mode === "object") { // options configuration is an object
if (mode.hasOwnProperty("separateRequires")) {
if (Object.prototype.hasOwnProperty.call(mode, "separateRequires")) {
options.separateRequires = !!mode.separateRequires;
}
if (mode.hasOwnProperty("var")) {
if (Object.prototype.hasOwnProperty.call(mode, "var")) {
options.var = { uninitialized: mode.var, initialized: mode.var };
}
if (mode.hasOwnProperty("let")) {
if (Object.prototype.hasOwnProperty.call(mode, "let")) {
options.let = { uninitialized: mode.let, initialized: mode.let };
}
if (mode.hasOwnProperty("const")) {
if (Object.prototype.hasOwnProperty.call(mode, "const")) {
options.const = { uninitialized: mode.const, initialized: mode.const };
}
if (mode.hasOwnProperty("uninitialized")) {
if (Object.prototype.hasOwnProperty.call(mode, "uninitialized")) {
if (!options.var) {
options.var = {};
}
Expand All @@ -100,7 +100,7 @@ module.exports = {
options.let.uninitialized = mode.uninitialized;
options.const.uninitialized = mode.uninitialized;
}
if (mode.hasOwnProperty("initialized")) {
if (Object.prototype.hasOwnProperty.call(mode, "initialized")) {
if (!options.var) {
options.var = {};
}
Expand Down
12 changes: 6 additions & 6 deletions lib/rules/padded-blocks.js
Expand Up @@ -58,13 +58,13 @@ module.exports = {
options.switches = shouldHavePadding;
options.classes = shouldHavePadding;
} else {
if (config.hasOwnProperty("blocks")) {
if (Object.prototype.hasOwnProperty.call(config, "blocks")) {
options.blocks = config.blocks === "always";
}
if (config.hasOwnProperty("switches")) {
if (Object.prototype.hasOwnProperty.call(config, "switches")) {
options.switches = config.switches === "always";
}
if (config.hasOwnProperty("classes")) {
if (Object.prototype.hasOwnProperty.call(config, "classes")) {
options.classes = config.classes === "always";
}
}
Expand Down Expand Up @@ -225,7 +225,7 @@ module.exports = {

const rule = {};

if (options.hasOwnProperty("switches")) {
if (Object.prototype.hasOwnProperty.call(options, "switches")) {
rule.SwitchStatement = function(node) {
if (node.cases.length === 0) {
return;
Expand All @@ -234,7 +234,7 @@ module.exports = {
};
}

if (options.hasOwnProperty("blocks")) {
if (Object.prototype.hasOwnProperty.call(options, "blocks")) {
rule.BlockStatement = function(node) {
if (node.body.length === 0) {
return;
Expand All @@ -243,7 +243,7 @@ module.exports = {
};
}

if (options.hasOwnProperty("classes")) {
if (Object.prototype.hasOwnProperty.call(options, "classes")) {
rule.ClassBody = function(node) {
if (node.body.length === 0) {
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-reflect.js
Expand Up @@ -98,7 +98,7 @@ module.exports = {
CallExpression(node) {
const methodName = (node.callee.property || {}).name;
const isReflectCall = (node.callee.object || {}).name === "Reflect";
const hasReflectSubsitute = reflectSubsitutes.hasOwnProperty(methodName);
const hasReflectSubsitute = Object.prototype.hasOwnProperty.call(reflectSubsitutes, methodName);
const userConfiguredException = exceptions.indexOf(methodName) !== -1;

if (hasReflectSubsitute && !isReflectCall && !userConfiguredException) {
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/semi-spacing.js
Expand Up @@ -46,10 +46,10 @@ module.exports = {
requireSpaceAfter = true;

if (typeof config === "object") {
if (config.hasOwnProperty("before")) {
if (Object.prototype.hasOwnProperty.call(config, "before")) {
requireSpaceBefore = config.before;
}
if (config.hasOwnProperty("after")) {
if (Object.prototype.hasOwnProperty.call(config, "after")) {
requireSpaceAfter = config.after;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/space-unary-ops.js
Expand Up @@ -72,7 +72,7 @@ module.exports = {
* @returns {boolean} Whether or not an override has been provided for the operator
*/
function overrideExistsForOperator(operator) {
return options.overrides && options.overrides.hasOwnProperty(operator);
return options.overrides && Object.prototype.hasOwnProperty.call(options.overrides, operator);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/valid-jsdoc.js
Expand Up @@ -322,7 +322,7 @@ module.exports = {
}

// check tag preferences
if (prefer.hasOwnProperty(tag.title) && tag.title !== prefer[tag.title]) {
if (Object.prototype.hasOwnProperty.call(prefer, tag.title) && tag.title !== prefer[tag.title]) {
const entireTagRange = getAbsoluteRange(jsdocNode, tag);

context.report({
Expand Down
10 changes: 5 additions & 5 deletions lib/testers/rule-tester.js
Expand Up @@ -533,19 +533,19 @@ class RuleTester {
assert.strictEqual(message.nodeType, error.type, `Error type should be ${error.type}, found ${message.nodeType}`);
}

if (error.hasOwnProperty("line")) {
if (Object.prototype.hasOwnProperty.call(error, "line")) {
assert.strictEqual(message.line, error.line, `Error line should be ${error.line}`);
}

if (error.hasOwnProperty("column")) {
if (Object.prototype.hasOwnProperty.call(error, "column")) {
assert.strictEqual(message.column, error.column, `Error column should be ${error.column}`);
}

if (error.hasOwnProperty("endLine")) {
if (Object.prototype.hasOwnProperty.call(error, "endLine")) {
assert.strictEqual(message.endLine, error.endLine, `Error endLine should be ${error.endLine}`);
}

if (error.hasOwnProperty("endColumn")) {
if (Object.prototype.hasOwnProperty.call(error, "endColumn")) {
assert.strictEqual(message.endColumn, error.endColumn, `Error endColumn should be ${error.endColumn}`);
}
} else {
Expand All @@ -556,7 +556,7 @@ class RuleTester {
}
}

if (item.hasOwnProperty("output")) {
if (Object.prototype.hasOwnProperty.call(item, "output")) {
if (item.output === null) {
assert.strictEqual(
messages.filter(message => message.fix).length,
Expand Down
4 changes: 2 additions & 2 deletions lib/util/file-finder.js
Expand Up @@ -89,7 +89,7 @@ class FileFinder {
? path.resolve(this.cwd, relativeDirectory)
: this.cwd;

if (cache.hasOwnProperty(initialDirectory)) {
if (Object.prototype.hasOwnProperty.call(cache, initialDirectory)) {
yield* cache[initialDirectory];
return; // to avoid doing the normal loop afterwards
}
Expand Down Expand Up @@ -130,7 +130,7 @@ class FileFinder {
return;
}

} while (!cache.hasOwnProperty(directory));
} while (!Object.prototype.hasOwnProperty.call(cache, directory));

// Add what has been cached previously to the cache of each directory searched.
for (let i = 0; i < searched; i++) {
Expand Down
4 changes: 2 additions & 2 deletions lib/util/lint-result-cache.js
Expand Up @@ -109,7 +109,7 @@ class LintResultCache {
* @returns {void}
*/
setCachedLintResults(filePath, result) {
if (result && result.hasOwnProperty("output")) {
if (result && Object.prototype.hasOwnProperty.call(result, "output")) {
return;
}

Expand All @@ -125,7 +125,7 @@ class LintResultCache {
* In `getCachedLintResults`, if source is explicitly null, we will
* read the file from the filesystem to set the value again.
*/
if (resultToSerialize.hasOwnProperty("source")) {
if (Object.prototype.hasOwnProperty.call(resultToSerialize, "source")) {
resultToSerialize.source = null;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/util/source-code-fixer.js
Expand Up @@ -107,7 +107,7 @@ SourceCodeFixer.applyFixes = function(sourceText, messages, shouldFix) {
}

messages.forEach(problem => {
if (problem.hasOwnProperty("fix")) {
if (Object.prototype.hasOwnProperty.call(problem, "fix")) {
fixes.push(problem);
} else {
remainingMessages.push(problem);
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config-eslint/default.yml
Expand Up @@ -88,6 +88,7 @@ rules:
no-path-concat: "error"
no-process-exit: "error"
no-proto: "error"
no-prototype-builtins: "error"
no-redeclare: "error"
no-restricted-properties: [
"error",
Expand Down
2 changes: 1 addition & 1 deletion tests/bench/large.js
Expand Up @@ -50435,7 +50435,7 @@ assert.ifError = function(err) { if (err) {throw err;}};
var objectKeys = Object.keys || function (obj) {
var keys = [];
for (var key in obj) {
if (hasOwn.call(obj, key)) keys.push(key);
if (Object.prototype.hasOwnProperty.call(obj, key)) keys.push(key);
}
return keys;
};
Expand Down

0 comments on commit 6492233

Please sign in to comment.