eslint-plugin-unicorn
Advanced tools
Comparing version 2.1.2 to 3.0.0
12
index.js
@@ -17,3 +17,3 @@ 'use strict'; | ||
rules: { | ||
'unicorn/catch-error-name': ['error', {name: 'err'}], | ||
'unicorn/catch-error-name': ['off', {name: 'err'}], | ||
'unicorn/explicit-length-check': 'error', | ||
@@ -25,9 +25,13 @@ 'unicorn/filename-case': ['error', {case: 'kebabCase'}], | ||
'unicorn/number-literal-case': 'error', | ||
'unicorn/escape-case': 'off', | ||
'unicorn/escape-case': 'error', | ||
'unicorn/no-array-instanceof': 'error', | ||
'unicorn/no-new-buffer': 'error', | ||
'unicorn/no-hex-escape': 'off', | ||
'unicorn/no-hex-escape': 'error', | ||
'unicorn/custom-error-definition': 'error', | ||
'unicorn/prefer-starts-ends-with': 'error', | ||
'unicorn/prefer-type-error': 'error' | ||
'unicorn/prefer-type-error': 'error', | ||
'unicorn/no-fn-reference-in-iterator': 'error', | ||
'unicorn/import-index': 'error', | ||
'unicorn/new-for-builtins': 'error', | ||
'unicorn/regex-shorthand': 'error' | ||
} | ||
@@ -34,0 +38,0 @@ } |
147
package.json
{ | ||
"name": "eslint-plugin-unicorn", | ||
"version": "2.1.2", | ||
"description": "Various awesome ESLint rules", | ||
"license": "MIT", | ||
"repository": "sindresorhus/eslint-plugin-unicorn", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"maintainers": [ | ||
{ | ||
"name": "James Talmage", | ||
"email": "james@talmage.io", | ||
"url": "github.com/jamestalmage" | ||
}, | ||
{ | ||
"name": "Jeroen Engels", | ||
"email": "jfm.engels@gmail.com", | ||
"url": "github.com/jfmengels" | ||
}, | ||
{ | ||
"name": "Sam Verschueren", | ||
"email": "sam.verschueren@gmail.com", | ||
"url": "github.com/SamVerschueren" | ||
} | ||
], | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && nyc ava", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"rules" | ||
], | ||
"keywords": [ | ||
"eslint", | ||
"eslintplugin", | ||
"eslint-plugin", | ||
"unicorn", | ||
"linter", | ||
"lint", | ||
"style", | ||
"xo" | ||
], | ||
"dependencies": { | ||
"import-modules": "^1.1.0", | ||
"lodash.camelcase": "^4.1.1", | ||
"lodash.kebabcase": "^4.0.1", | ||
"lodash.snakecase": "^4.0.1", | ||
"lodash.upperfirst": "^4.2.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"coveralls": "^2.11.9", | ||
"eslint": "^4.0.0", | ||
"eslint-ava-rule-tester": "^2.0.0", | ||
"nyc": "^10.0.0", | ||
"pify": "^2.3.0", | ||
"xo": "*" | ||
}, | ||
"peerDependencies": { | ||
"eslint": ">=3.6" | ||
} | ||
"name": "eslint-plugin-unicorn", | ||
"version": "3.0.0", | ||
"description": "Various awesome ESLint rules", | ||
"license": "MIT", | ||
"repository": "sindresorhus/eslint-plugin-unicorn", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"maintainers": [ | ||
{ | ||
"name": "James Talmage", | ||
"email": "james@talmage.io", | ||
"url": "github.com/jamestalmage" | ||
}, | ||
{ | ||
"name": "Jeroen Engels", | ||
"email": "jfm.engels@gmail.com", | ||
"url": "github.com/jfmengels" | ||
}, | ||
{ | ||
"name": "Sam Verschueren", | ||
"email": "sam.verschueren@gmail.com", | ||
"url": "github.com/SamVerschueren" | ||
} | ||
], | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && nyc ava", | ||
"integration": "./test/integration/test.js", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"rules" | ||
], | ||
"keywords": [ | ||
"eslint", | ||
"eslintplugin", | ||
"eslint-plugin", | ||
"unicorn", | ||
"linter", | ||
"lint", | ||
"style", | ||
"xo" | ||
], | ||
"dependencies": { | ||
"clean-regexp": "^1.0.0", | ||
"eslint-ast-utils": "^1.0.0", | ||
"import-modules": "^1.1.0", | ||
"lodash.camelcase": "^4.1.1", | ||
"lodash.kebabcase": "^4.0.1", | ||
"lodash.snakecase": "^4.0.1", | ||
"lodash.upperfirst": "^4.2.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"chalk": "^2.0.1", | ||
"coveralls": "^3.0.0", | ||
"del": "^3.0.0", | ||
"eslint": "^4.0.0", | ||
"eslint-ava-rule-tester": "^2.0.0", | ||
"execa": "^0.8.0", | ||
"listr": "^0.13.0", | ||
"nyc": "^11.0.3", | ||
"pify": "^3.0.0", | ||
"tempy": "^0.2.1", | ||
"xo": "*" | ||
}, | ||
"peerDependencies": { | ||
"eslint": ">=4" | ||
}, | ||
"ava": { | ||
"files": [ | ||
"test/*.js" | ||
] | ||
} | ||
} |
@@ -49,3 +49,7 @@ # eslint-plugin-unicorn [![Build Status](https://travis-ci.org/sindresorhus/eslint-plugin-unicorn.svg?branch=master)](https://travis-ci.org/sindresorhus/eslint-plugin-unicorn) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/eslint-plugin-unicorn/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/eslint-plugin-unicorn?branch=master) | ||
"unicorn/prefer-starts-ends-with": "error", | ||
"unicorn/prefer-type-error": "error" | ||
"unicorn/prefer-type-error": "error", | ||
"unicorn/no-fn-reference-in-iterator": "error", | ||
"unicorn/import-index": "error", | ||
"unicorn/new-for-builtins": "error", | ||
"unicorn/regex-shorthand": "error" | ||
} | ||
@@ -60,3 +64,3 @@ } | ||
- [catch-error-name](docs/rules/catch-error-name.md) - Enforce a specific parameter name in catch clauses. | ||
- [explicit-length-check](docs/rules/explicit-length-check.md) - Enforce explicitly comparing the `length` property of a value. | ||
- [explicit-length-check](docs/rules/explicit-length-check.md) - Enforce explicitly comparing the `length` property of a value. *(partly fixable)* | ||
- [filename-case](docs/rules/filename-case.md) - Enforce a case style for filenames. | ||
@@ -74,2 +78,6 @@ - [no-abusive-eslint-disable](docs/rules/no-abusive-eslint-disable.md) - Enforce specifying rules to disable in `eslint-disable` comments. | ||
- [prefer-type-error](docs/rules/prefer-type-error.md) - Enforce throwing `TypeError` in type checking conditions. *(fixable)* | ||
- [no-fn-reference-in-iterator](docs/rules/no-fn-reference-in-iterator.md) - Prevents passing a function reference directly to iterator methods. *(fixable)* | ||
- [import-index](docs/rules/import-index.md) - Enforce importing index files with `.`. *(fixable)* | ||
- [new-for-builtins](docs/rules/new-for-builtins.md) - Enforce the use of `new` for all builtins, except `String`, `Number` and `Boolean`. *(fixable)* | ||
- [regex-shorthand](docs/rules/regex-shorthand.md) - Enforce the use of regex shorthands to improve readability. *(fixable)* | ||
@@ -76,0 +84,0 @@ |
'use strict'; | ||
const astUtils = require('eslint-ast-utils'); | ||
@@ -26,2 +27,13 @@ // Matches someObj.then([FunctionExpression | ArrowFunctionExpression]) | ||
function indexifyName(name, scope) { | ||
const variables = scope.variableScope.set; | ||
let index = 1; | ||
while (variables.has(index === 1 ? name : name + index)) { | ||
index++; | ||
} | ||
return name + (index === 1 ? '' : index); | ||
} | ||
const create = context => { | ||
@@ -41,6 +53,8 @@ const opts = context.options[0]; | ||
function popAndReport(node) { | ||
if (!stack.pop()) { | ||
const value = stack.pop(); | ||
if (value !== true) { | ||
context.report({ | ||
node, | ||
message: `The catch parameter should be named \`${name}\`.` | ||
message: `The catch parameter should be named \`${value || name}\`.` | ||
}); | ||
@@ -54,3 +68,10 @@ } | ||
const params = node.arguments[0].params; | ||
push(params.length === 0 || params[0].name === name); | ||
if (params.length > 0 && params[0].name === '_') { | ||
push(!astUtils.containsIdentifier('_', node.arguments[0].body)); | ||
return; | ||
} | ||
const errName = indexifyName(name, context.getScope()); | ||
push(params.length === 0 || params[0].name === errName || errName); | ||
} | ||
@@ -64,3 +85,9 @@ }, | ||
CatchClause: node => { | ||
push(node.param.name === name); | ||
if (node.param.name === '_') { | ||
push(!astUtils.someContainIdentifier('_', node.body.body)); | ||
return; | ||
} | ||
const errName = indexifyName(name, context.getScope()); | ||
push(node.param.name === errName || errName); | ||
}, | ||
@@ -67,0 +94,0 @@ 'CatchClause:exit': node => { |
'use strict'; | ||
const escapeWithLowercase = /\\(x[a-f0-9]{2}|u[a-f0-9]{4}|u\{([0-9a-f]{1,})\}|c[a-z])/; | ||
const hasLowercaseCharacter = /[a-z].*?[a-z]/; | ||
const escapeWithLowercase = /((?:^|[^\\])(?:\\\\)*)\\(x[a-f\d]{2}|u[a-f\d]{4}|u\{(?:[a-f\d]{1,})\}|c[a-z])/; | ||
const hasLowercaseCharacter = /[a-z]+/; | ||
const message = 'Use uppercase characters for the value of the escape sequence.'; | ||
@@ -10,4 +10,5 @@ | ||
if (results) { | ||
const fixedEscape = results[0].slice(0, 2) + results[0].slice(2).toUpperCase(); | ||
return value.slice(0, results.index) + fixedEscape + value.slice(results.index + results[0].length); | ||
const prefix = results[1].length + 1; | ||
const fixedEscape = results[2].slice(0, 1) + results[2].slice(1).toUpperCase(); | ||
return value.slice(0, results.index + prefix) + fixedEscape + value.slice(results.index + results[0].length); | ||
} | ||
@@ -21,3 +22,9 @@ | ||
Literal(node) { | ||
if (typeof node.value === 'string' && node.raw.match(escapeWithLowercase) && node.raw.match(hasLowercaseCharacter)) { | ||
if (typeof node.value !== 'string') { | ||
return; | ||
} | ||
const matches = node.raw.match(escapeWithLowercase); | ||
if (matches && matches[2].slice(1).match(hasLowercaseCharacter)) { | ||
context.report({ | ||
@@ -31,3 +38,9 @@ node, | ||
TemplateElement(node) { | ||
if (typeof node.value.raw === 'string' && node.value.raw.match(escapeWithLowercase) && node.value.raw.match(hasLowercaseCharacter)) { | ||
if (typeof node.value.raw !== 'string') { | ||
return; | ||
} | ||
const matches = node.value.raw.match(escapeWithLowercase); | ||
if (matches && matches[2].slice(1).match(hasLowercaseCharacter)) { | ||
context.report({ | ||
@@ -34,0 +47,0 @@ node, |
'use strict'; | ||
const operatorTypes = { | ||
gt: ['>'], | ||
gte: ['>='], | ||
ne: ['!==', '!='] | ||
}; | ||
function reportError(context, node, message, fixDetails) { | ||
context.report({ | ||
node, | ||
message, | ||
fix: fixDetails && (fixer => { | ||
return fixer.replaceText(node, | ||
`${context.getSourceCode().getText(fixDetails.node)} ${fixDetails.operator} ${fixDetails.value}` | ||
); | ||
}) | ||
}); | ||
} | ||
function checkZeroType(context, node) { | ||
if (node.operator === '<' && node.right.value === 1) { | ||
reportError(context, | ||
node, | ||
'Zero `.length` should be compared with `=== 0`.', | ||
{ | ||
node: node.left, | ||
operator: '===', | ||
value: 0 | ||
} | ||
); | ||
} | ||
} | ||
function checkNonZeroType(context, node, type) { | ||
const value = node.right.value; | ||
const operator = node.operator; | ||
switch (type) { | ||
case 'greater-than': | ||
if ((operatorTypes.gte.indexOf(operator) !== -1 && value === 1) || | ||
(operatorTypes.ne.indexOf(operator) !== -1 && value === 0) | ||
) { | ||
reportError(context, | ||
node, | ||
'Non-zero `.length` should be compared with `> 0`.', | ||
{ | ||
node: node.left, | ||
operator: '>', | ||
value: 0 | ||
} | ||
); | ||
} | ||
break; | ||
case 'greater-than-or-equal': | ||
if ((operatorTypes.gt.indexOf(operator) !== -1 && value === 0) || | ||
(operatorTypes.ne.indexOf(operator) !== -1 && value === 0) | ||
) { | ||
reportError(context, | ||
node, | ||
'Non-zero `.length` should be compared with `>= 1`.', | ||
{ | ||
node: node.left, | ||
operator: '>=', | ||
value: 1 | ||
} | ||
); | ||
} | ||
break; | ||
case 'not-equal': | ||
if ((operatorTypes.gt.indexOf(operator) !== -1 && value === 0) || | ||
(operatorTypes.gte.indexOf(operator) !== -1 && value === 1) | ||
) { | ||
reportError(context, | ||
node, | ||
'Non-zero `.length` should be compared with `!== 0`.', | ||
{ | ||
node: node.left, | ||
operator: '!==', | ||
value: 0 | ||
} | ||
); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
function checkBinaryExpression(context, node, options) { | ||
if (node.right.type === 'Literal' && | ||
node.left.type === 'MemberExpression' && | ||
node.left.property.type === 'Identifier' && | ||
node.left.property.name === 'length' | ||
) { | ||
checkZeroType(context, node); | ||
checkNonZeroType(context, node, options['non-zero']); | ||
} | ||
} | ||
function checkExpression(context, node) { | ||
@@ -15,2 +113,7 @@ if (node.type === 'LogicalExpression') { | ||
if (node.type === 'BinaryExpression') { | ||
checkBinaryExpression(context, node, context.options[0] || {}); | ||
return; | ||
} | ||
if (node.type === 'MemberExpression' && | ||
@@ -20,6 +123,3 @@ node.property.type === 'Identifier' && | ||
) { | ||
context.report({ | ||
node, | ||
message: '`length` property should be compared to a value.' | ||
}); | ||
reportError(context, node, '`length` property should be compared to a value.'); | ||
} | ||
@@ -36,5 +136,21 @@ } | ||
const schema = [{ | ||
type: 'object', | ||
properties: { | ||
'non-zero': { | ||
enum: [ | ||
'not-equal', | ||
'greater-than', | ||
'greater-than-or-equal' | ||
] | ||
} | ||
} | ||
}]; | ||
module.exports = { | ||
create, | ||
meta: {} | ||
meta: { | ||
fixable: 'code', | ||
schema | ||
} | ||
}; |
@@ -5,3 +5,3 @@ /* eslint-disable unicorn/no-hex-escape */ | ||
function checkEscape(context, node, value) { | ||
const fixedValue = typeof value === 'string' ? value.replace(/\\x/g, '\\u00') : value; | ||
const fixedValue = typeof value === 'string' ? value.replace(/((?:^|[^\\])(?:\\\\)*)\\x/g, '$1\\u00') : value; | ||
@@ -8,0 +8,0 @@ if (value !== fixedValue) { |
@@ -6,16 +6,14 @@ 'use strict'; | ||
return { | ||
NewExpression: node => { | ||
if (node.callee.name === 'Buffer') { | ||
const method = inferMethod(node.arguments); | ||
const range = [ | ||
node.start, | ||
node.callee.end | ||
]; | ||
'NewExpression[callee.name="Buffer"]': node => { | ||
const method = inferMethod(node.arguments); | ||
const range = [ | ||
node.start, | ||
node.callee.end | ||
]; | ||
context.report({ | ||
node, | ||
message: `\`new Buffer()\` is deprecated, use \`Buffer.${method}()\` instead.`, | ||
fix: fixer => fixer.replaceTextRange(range, `Buffer.${method}`) | ||
}); | ||
} | ||
context.report({ | ||
node, | ||
message: `\`new Buffer()\` is deprecated, use \`Buffer.${method}()\` instead.`, | ||
fix: fixer => fixer.replaceTextRange(range, `Buffer.${method}`) | ||
}); | ||
} | ||
@@ -22,0 +20,0 @@ }; |
Sorry, the diff of this file is not supported yet
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
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
34514
22
1034
117
8
12
+ Addedclean-regexp@^1.0.0
+ Addedeslint-ast-utils@^1.0.0
+ Addedclean-regexp@1.0.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedeslint-ast-utils@1.1.0(transitive)
+ Addedlodash.get@4.4.2(transitive)
+ Addedlodash.zip@4.2.0(transitive)