From 5b1857d7fb5ff13b3396e1c0160b62ff1db026e6 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 13 Feb 2018 05:26:34 +0100 Subject: [PATCH 1/4] lib,doc: enable no-prototype-builtins This enables the `no-prototype-builtins` eslint rule for /lib and for /doc. --- doc/.eslintrc.yaml | 4 ++++ doc/api/util.md | 2 +- lib/.eslintrc.yaml | 4 ++++ lib/assert.js | 3 ++- lib/internal/async_hooks.js | 4 +++- lib/internal/bootstrap_node.js | 2 ++ lib/internal/crypto/sig.js | 9 +++++---- 7 files changed, 21 insertions(+), 7 deletions(-) diff --git a/doc/.eslintrc.yaml b/doc/.eslintrc.yaml index 8038836fe310db..f759ebe1f1b3dd 100644 --- a/doc/.eslintrc.yaml +++ b/doc/.eslintrc.yaml @@ -12,3 +12,7 @@ rules: no-var: error prefer-const: error prefer-rest-params: error + + # Possible Errors + # http://eslint.org/docs/rules/#possible-errors + no-prototype-builtins: error diff --git a/doc/api/util.md b/doc/api/util.md index b5a3f51ad635f9..2bc3e68a0eba90 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -65,7 +65,7 @@ is wrapped in an `Error` with the original value stored in a field named callbackFunction((err, ret) => { // When the Promise was rejected with `null` it is wrapped with an Error and // the original value is stored in `reason`. - err && err.hasOwnProperty('reason') && err.reason === null; // true + err && Object.prototype.call(err, 'reason') && err.reason === null; // true }); ``` diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index 0b00638e2a638c..467d974141c69c 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -7,3 +7,7 @@ rules: no-let-in-for-declaration: error lowercase-name-for-primitive: error non-ascii-character: error + + # Possible Errors + # http://eslint.org/docs/rules/#possible-errors + no-prototype-builtins: error diff --git a/lib/assert.js b/lib/assert.js index 67c658d9e68bb3..0769e1929ca916 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -31,6 +31,7 @@ const { parseExpressionAt } = require('internal/deps/acorn/dist/acorn'); const { inspect } = require('util'); const { EOL } = require('os'); const nativeModule = require('native_module'); +const { isPrototypeOf } = Object.prototype; // Escape control characters but not \n and \t to keep the line breaks and // indentation intact. @@ -400,7 +401,7 @@ function expectedException(actual, expected, msg) { if (expected.prototype !== undefined && actual instanceof expected) { return true; } - if (Error.isPrototypeOf(expected)) { + if (isPrototypeOf.call(Error, expected)) { return false; } return expected.call({}, actual) === true; diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index d1310ad2cac005..4eeeb7ef9b9bbc 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -82,6 +82,8 @@ const emitDestroyNative = emitHookFactory(destroy_symbol, 'emitDestroyNative'); const emitPromiseResolveNative = emitHookFactory(promise_resolve_symbol, 'emitPromiseResolveNative'); +const { hasOwnProperty } = Object.prototype; + // Setup the callbacks that node::AsyncWrap will call when there are hooks to // process. They use the same functions as the JS embedder API. These callbacks // are setup immediately to prevent async_wrap.setupHooks() from being hijacked @@ -250,7 +252,7 @@ function newUid() { } function getOrSetAsyncId(object) { - if (object.hasOwnProperty(async_id_symbol)) { + if (hasOwnProperty.call(object, async_id_symbol)) { return object[async_id_symbol]; } diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index 0c2109fab36cc0..c92485328d82cb 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -5,6 +5,8 @@ // to the performance of the startup process, many dependencies are invoked // lazily. +/* eslint-disable no-prototype-builtins */ + 'use strict'; (function(process) { diff --git a/lib/internal/crypto/sig.js b/lib/internal/crypto/sig.js index 9f1938290f70cf..b610b867a19126 100644 --- a/lib/internal/crypto/sig.js +++ b/lib/internal/crypto/sig.js @@ -16,6 +16,7 @@ const { const { isArrayBufferView } = require('internal/util/types'); const { Writable } = require('stream'); const { inherits } = require('util'); +const { hasOwnProperty } = Object.prototype; function Sign(algorithm, options) { if (!(this instanceof Sign)) @@ -55,7 +56,7 @@ Sign.prototype.sign = function sign(options, encoding) { // Options specific to RSA var rsaPadding = RSA_PKCS1_PADDING; - if (options.hasOwnProperty('padding')) { + if (hasOwnProperty.call(options, 'padding')) { if (options.padding === options.padding >> 0) { rsaPadding = options.padding; } else { @@ -66,7 +67,7 @@ Sign.prototype.sign = function sign(options, encoding) { } var pssSaltLength = RSA_PSS_SALTLEN_AUTO; - if (options.hasOwnProperty('saltLength')) { + if (hasOwnProperty.call(options, 'saltLength')) { if (options.saltLength === options.saltLength >> 0) { pssSaltLength = options.saltLength; } else { @@ -114,7 +115,7 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) { // Options specific to RSA var rsaPadding = RSA_PKCS1_PADDING; - if (options.hasOwnProperty('padding')) { + if (hasOwnProperty.call(options, 'padding')) { if (options.padding === options.padding >> 0) { rsaPadding = options.padding; } else { @@ -125,7 +126,7 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) { } var pssSaltLength = RSA_PSS_SALTLEN_AUTO; - if (options.hasOwnProperty('saltLength')) { + if (hasOwnProperty.call(options, 'saltLength')) { if (options.saltLength === options.saltLength >> 0) { pssSaltLength = options.saltLength; } else { From c75ca3c57daa03a3f5626b7ab782b01e38cc3ea0 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 13 Feb 2018 12:49:27 +0100 Subject: [PATCH 2/4] fixup --- doc/api/util.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index 2bc3e68a0eba90..cb36cf6bd4d449 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -65,7 +65,9 @@ is wrapped in an `Error` with the original value stored in a field named callbackFunction((err, ret) => { // When the Promise was rejected with `null` it is wrapped with an Error and // the original value is stored in `reason`. - err && Object.prototype.call(err, 'reason') && err.reason === null; // true + console.log(err && Object.prototype.hasOwnProperty.call(err, 'reason') && + err.reason === null); + // Prints: true }); ``` From 104dbf3663117dc28f3912253bbdf8fb380dc6d4 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 13 Feb 2018 13:56:49 +0100 Subject: [PATCH 3/4] fixup typo --- doc/api/util.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index cb36cf6bd4d449..6a49a922664393 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -65,7 +65,7 @@ is wrapped in an `Error` with the original value stored in a field named callbackFunction((err, ret) => { // When the Promise was rejected with `null` it is wrapped with an Error and // the original value is stored in `reason`. - console.log(err && Object.prototype.hasOwnProperty.call(err, 'reason') && + console.log(err && Object.prototype.hasOwnProperty.call(err, 'reason') && err.reason === null); // Prints: true }); From af0f40fe95f9be0eccbcf2a4a28d172d821efbf7 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 13 Feb 2018 16:25:29 +0100 Subject: [PATCH 4/4] fixup --- doc/api/util.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index 6a49a922664393..72b7a61f51e616 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -65,8 +65,7 @@ is wrapped in an `Error` with the original value stored in a field named callbackFunction((err, ret) => { // When the Promise was rejected with `null` it is wrapped with an Error and // the original value is stored in `reason`. - console.log(err && Object.prototype.hasOwnProperty.call(err, 'reason') && - err.reason === null); + console.log(err && err.reason === null); // Prints: true }); ```