Socket
Socket
Sign inDemoInstall

eslint-plugin-unicorn

Package Overview
Dependencies
Maintainers
1
Versions
106
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-unicorn - npm Package Compare versions

Comparing version 12.0.2 to 12.1.0

rules/utils/is-valid-variable-name.js

2

package.json
{
"name": "eslint-plugin-unicorn",
"version": "12.0.2",
"version": "12.1.0",
"description": "Various awesome ESLint rules",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -1,2 +0,1 @@

/* eslint-disable unicorn/expiring-todo-comments */
'use strict';

@@ -192,5 +191,5 @@ const readPkgUp = require('read-pkg-up');

// /*
// * TODO [2000-01-01]: Validate this
// * TODO [2000-01-01]: And this
// * TODO [2000-01-01]: Also this
// * TODO [2999-01-01]: Validate this
// * TODO [2999-01-01]: And this
// * TODO [2999-01-01]: Also this
// */

@@ -197,0 +196,0 @@ .map(comment =>

@@ -37,3 +37,3 @@ 'use strict';

// Strip off backticks
value = value.slice(1, value.length - 1);
value = value.slice(1, -1);
}

@@ -40,0 +40,0 @@

'use strict';
const getDocsUrl = require('./utils/get-docs-url');
const {isValidVariableName} = require('./utils');
const isValidVariableName = require('./utils/is-valid-variable-name');

@@ -5,0 +5,0 @@ const getMethodName = memberExpression => memberExpression.property.name;

@@ -5,5 +5,8 @@ 'use strict';

// Ignore {_,lodash,underscore}.indexOf
const ignoredVariables = new Set(['_', 'lodash', 'underscore']);
const isNegativeOne = (operator, value) => operator === '-' && value === 1;
const isIgnoredTarget = node => node.type === 'Identifier' && ignoredVariables.has(node.name);
const report = (context, node, target, pattern) => {
const report = (context, node, target, argumentsNodes) => {
const sourceCode = context.getSourceCode();

@@ -13,4 +16,10 @@ const memberExpressionNode = target.parent;

const targetSource = sourceCode.getText().slice(memberExpressionNode.range[0], dotToken.range[0]);
const patternSource = sourceCode.getText(pattern);
// Strip default `fromIndex`
if (argumentsNodes.length === 2 && argumentsNodes[1].type === 'Literal' && argumentsNodes[1].value === 0) {
argumentsNodes = argumentsNodes.slice(0, 1);
}
const argumentsSource = argumentsNodes.map(argument => sourceCode.getText(argument));
context.report({

@@ -21,3 +30,3 @@ node,

const isNot = node => ['===', '==', '<'].includes(node.operator) ? '!' : '';
const replacement = `${isNot(node)}${targetSource}.includes(${patternSource})`;
const replacement = `${isNot(node)}${targetSource}.includes(${argumentsSource.join(', ')})`;
return fixer.replaceText(node, replacement);

@@ -32,25 +41,35 @@ }

if (isMethodNamed(left, 'indexOf')) {
const target = left.callee.object;
const pattern = left.arguments[0];
if (!isMethodNamed(left, 'indexOf')) {
return;
}
if (right.type === 'UnaryExpression') {
const {argument} = right;
const target = left.callee.object;
if (argument.type !== 'Literal') {
return false;
}
if (isIgnoredTarget(target)) {
return;
}
const {value} = argument;
const {arguments: argumentsNodes} = left;
if (['!==', '!=', '>', '===', '=='].includes(node.operator) && isNegativeOne(right.operator, value)) {
report(context, node, target, pattern);
}
// Ignore something.indexOf(foo, 0, another)
if (argumentsNodes.length > 2) {
return;
}
if (right.type === 'UnaryExpression') {
const {argument} = right;
if (argument.type !== 'Literal') {
return;
}
if (right.type === 'Literal' && ['>=', '<'].includes(node.operator) && right.value === 0) {
report(context, node, target, pattern);
const {value} = argument;
if (['!==', '!=', '>', '===', '=='].includes(node.operator) && isNegativeOne(right.operator, value)) {
report(context, node, target, argumentsNodes);
}
}
return false;
if (right.type === 'Literal' && ['>=', '<'].includes(node.operator) && right.value === 0) {
report(context, node, target, argumentsNodes);
}

@@ -57,0 +76,0 @@ }

@@ -197,3 +197,3 @@ 'use strict';

const prepareOptions = ({
checkProperties = true,
checkProperties = false,
checkVariables = true,

@@ -651,3 +651,3 @@

if (filenameWithExtension === '<input>' || filenameWithExtension === '<text>') {
return {};
return;
}

@@ -654,0 +654,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc