eslint-plugin-no-use-extend-native
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -9,5 +9,10 @@ /* eslint no-var: 0 */ | ||
}, | ||
rulesConfig: { | ||
'no-use-extend-native': 2 | ||
configs: { | ||
recommended: { | ||
plugins: ['no-use-extend-native'], | ||
rules: { | ||
'no-use-extend-native/no-use-extend-native': 2 | ||
} | ||
} | ||
} | ||
} |
{ | ||
"name": "eslint-plugin-no-use-extend-native", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "ESLint plugin to prevent use of extended native objects", | ||
@@ -11,3 +11,13 @@ "scripts": { | ||
}, | ||
"repository": "dustinspecker/eslint-plugin-no-use-extend-native", | ||
"ava": { | ||
"require": [ | ||
"esm" | ||
] | ||
}, | ||
"repository": "https://github.com/dustinspecker/eslint-plugin-no-use-extend-native", | ||
"bugs": "https://github.com/dustinspecker/eslint-plugin-no-use-extend-native/issues", | ||
"homepage": "https://github.com/dustinspecker/eslint-plugin-no-use-extend-native", | ||
"engines": { | ||
"node": ">=6.0.0" | ||
}, | ||
"keywords": [ | ||
@@ -21,2 +31,5 @@ "eslint", | ||
"author": "Dustin Specker", | ||
"contributors": [ | ||
"Brett Zamir" | ||
], | ||
"license": "MIT", | ||
@@ -34,13 +47,14 @@ "files": [ | ||
"devDependencies": { | ||
"ava": "^0.25.0", | ||
"babel-cli": "^6.7.7", | ||
"babel-preset-es2015": "^6.6.0", | ||
"coveralls": "^3.0.0", | ||
"eslint": "^4.13.1", | ||
"eslint-ava-rule-tester": "^2.0.0", | ||
"@babel/cli": "^7.8.4", | ||
"@babel/preset-env": "^7.9.5", | ||
"ava": "^3.6.0", | ||
"coveralls": "^3.0.11", | ||
"eslint": "^6.8.0", | ||
"eslint-ava-rule-tester": "^4.0.0", | ||
"eslint-config-dustinspecker": "^5.0.0", | ||
"eslint-path-formatter": "^0.1.1", | ||
"eslint-plugin-new-with-error": "^1.1.0", | ||
"nyc": "^11.4.1" | ||
"eslint-plugin-new-with-error": "^2.0.0", | ||
"esm": "^3.2.25", | ||
"nyc": "^15.0.1" | ||
} | ||
} |
@@ -36,3 +36,3 @@ # eslint-plugin-no-use-extend-native | ||
To modify the single rule, `no-use-extend-native`, add the rule to your `.eslintrc` as such: | ||
To modify the single rule, `no-use-extend-native`, add the rule to your `.eslintrc.*` as such: | ||
```javascript | ||
@@ -44,3 +44,3 @@ { | ||
rules: { | ||
'no-use-extend-native/no-use-extend-native': 0 | ||
'no-use-extend-native/no-use-extend-native': 1 | ||
} | ||
@@ -52,5 +52,14 @@ } | ||
If you want the default, you can also just use the following instead of | ||
all of the above: | ||
```javascript | ||
{ | ||
extends: ['plugin:no-use-extend-native/recommended'] | ||
} | ||
``` | ||
With this plugin enabled, ESLint will find issues with using extended native objects: | ||
```javascript | ||
var colors = require('colors'); | ||
const colors = require('colors'); | ||
console.log('unicorn'.green); | ||
@@ -57,0 +66,0 @@ // => ESLint will give an error stating 'Avoid using extended native objects' |
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _isGetSetProp = _interopRequireDefault(require("is-get-set-prop")); | ||
var _isGetSetProp = require('is-get-set-prop'); | ||
var _isJsType = _interopRequireDefault(require("is-js-type")); | ||
var _isGetSetProp2 = _interopRequireDefault(_isGetSetProp); | ||
var _isObjProp = _interopRequireDefault(require("is-obj-prop")); | ||
var _isJsType = require('is-js-type'); | ||
var _isProtoProp = _interopRequireDefault(require("is-proto-prop")); | ||
var _isJsType2 = _interopRequireDefault(_isJsType); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _isObjProp = require('is-obj-prop'); | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
var _isObjProp2 = _interopRequireDefault(_isObjProp); | ||
var _isProtoProp = require('is-proto-prop'); | ||
var _isProtoProp2 = _interopRequireDefault(_isProtoProp); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -37,3 +29,2 @@ * Return type of value of left or right | ||
}; | ||
/** | ||
@@ -44,7 +35,9 @@ * Returns type of binary expression result | ||
*/ | ||
var binaryExpressionProduces = function binaryExpressionProduces(o) { | ||
var leftType = o.left.type === 'BinaryExpression' ? binaryExpressionProduces(o.left) : getType(o.left); | ||
var rightType = o.right.type === 'BinaryExpression' ? binaryExpressionProduces(o.right) : getType(o.right); | ||
var isRegExp = leftType === rightType && leftType === 'RegExp'; | ||
var isRegExp = leftType === rightType && leftType === 'RegExp'; | ||
if (leftType === 'String' || rightType === 'String' || isRegExp) { | ||
@@ -60,3 +53,2 @@ return 'String'; | ||
}; | ||
/** | ||
@@ -67,5 +59,6 @@ * Returns the JS type and property name | ||
*/ | ||
var getJsTypeAndPropertyName = function getJsTypeAndPropertyName(node) { | ||
var propertyName = void 0, | ||
jsType = void 0; | ||
var propertyName, jsType; | ||
@@ -76,8 +69,11 @@ switch (node.object.type) { | ||
break; | ||
case 'Literal': | ||
jsType = getType(node.object); | ||
break; | ||
case 'BinaryExpression': | ||
jsType = binaryExpressionProduces(node.object); | ||
break; | ||
case 'Identifier': | ||
@@ -90,3 +86,5 @@ if (node.property.name === 'prototype' && node.parent.property) { | ||
} | ||
break; | ||
default: | ||
@@ -97,4 +95,6 @@ jsType = node.object.type.replace('Expression', ''); | ||
propertyName = propertyName || node.property.name || node.property.value; | ||
return { propertyName: propertyName, jsType: jsType }; | ||
return { | ||
propertyName: propertyName, | ||
jsType: jsType | ||
}; | ||
}; | ||
@@ -104,6 +104,4 @@ | ||
var isExpression = usageType === 'ExpressionStatement' || usageType === 'MemberExpression'; | ||
return isExpression && !(0, _isGetSetProp2.default)(jsType, propertyName) && !(0, _isProtoProp2.default)(jsType, propertyName) && !(0, _isObjProp2.default)(jsType, propertyName); | ||
return isExpression && !(0, _isGetSetProp["default"])(jsType, propertyName) && !(0, _isProtoProp["default"])(jsType, propertyName) && !(0, _isObjProp["default"])(jsType, propertyName); | ||
}; | ||
/** | ||
@@ -116,4 +114,6 @@ * Determine if a jsType's usage of propertyName is valid | ||
*/ | ||
var isInvalid = function isInvalid(jsType, propertyName, usageType) { | ||
if (typeof propertyName !== 'string' || typeof jsType !== 'string' || !(0, _isJsType2.default)(jsType)) { | ||
if (typeof propertyName !== 'string' || typeof jsType !== 'string' || !(0, _isJsType["default"])(jsType)) { | ||
return false; | ||
@@ -123,37 +123,38 @@ } | ||
var unknownGetterSetterOrjsTypeExpressed = isUnkownGettSetterOrJsTypeExpressed(jsType, propertyName, usageType); | ||
var isFunctionCall = usageType === 'CallExpression'; | ||
var getterSetterCalledAsFunction = isFunctionCall && (0, _isGetSetProp2.default)(jsType, propertyName); | ||
var unknownjsTypeCalledAsFunction = isFunctionCall && !(0, _isProtoProp2.default)(jsType, propertyName) && !(0, _isObjProp2.default)(jsType, propertyName); | ||
var getterSetterCalledAsFunction = isFunctionCall && (0, _isGetSetProp["default"])(jsType, propertyName); | ||
var unknownjsTypeCalledAsFunction = isFunctionCall && !(0, _isProtoProp["default"])(jsType, propertyName) && !(0, _isObjProp["default"])(jsType, propertyName); | ||
return unknownGetterSetterOrjsTypeExpressed || getterSetterCalledAsFunction || unknownjsTypeCalledAsFunction; | ||
}; | ||
module.exports = function (context) { | ||
return { | ||
MemberExpression: function MemberExpression(node) { | ||
/* eslint complexity: [2, 9] */ | ||
if (node.computed && node.property.type === 'Identifier') { | ||
/** | ||
* handles cases like {}[i][j] | ||
* not enough information to identify type of variable in computed properties | ||
* so ignore false positives by not performing any checks | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: 'problem' | ||
}, | ||
create: function create(context) { | ||
return { | ||
MemberExpression: function MemberExpression(node) { | ||
/* eslint complexity: [2, 9] */ | ||
if (node.computed && node.property.type === 'Identifier') { | ||
/** | ||
* handles cases like {}[i][j] | ||
* not enough information to identify type of variable in computed properties | ||
* so ignore false positives by not performing any checks | ||
*/ | ||
return; | ||
} | ||
return; | ||
} | ||
var isArgToParent = node.parent.arguments && node.parent.arguments.indexOf(node) > -1; | ||
var usageType = isArgToParent ? node.type : node.parent.type; | ||
var isArgToParent = node.parent.arguments && node.parent.arguments.indexOf(node) > -1; | ||
var usageType = isArgToParent ? node.type : node.parent.type; | ||
var _getJsTypeAndProperty = getJsTypeAndPropertyName(node), | ||
propertyName = _getJsTypeAndProperty.propertyName, | ||
jsType = _getJsTypeAndProperty.jsType; | ||
var _getJsTypeAndProperty = getJsTypeAndPropertyName(node), | ||
propertyName = _getJsTypeAndProperty.propertyName, | ||
jsType = _getJsTypeAndProperty.jsType; | ||
if (isInvalid(jsType, propertyName, usageType) && isInvalid('Function', propertyName, usageType)) { | ||
context.report(node, 'Avoid using extended native objects'); | ||
if (isInvalid(jsType, propertyName, usageType) && isInvalid('Function', propertyName, usageType)) { | ||
context.report(node, 'Avoid using extended native objects'); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
} | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
11899
135
1
92
11