-
-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
619 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"cSpell.words": [ | ||
"unmark" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Prefer `Object.hasOwn(…)` over `Object.prototype.hasOwnProperty.call(…)` | ||
|
||
[`Object.hasOwn(…)`](https://github.com/tc39/proposal-accessible-object-hasownproperty) is more accessible than `Object.prototype.hasOwnProperty.call(…)`. | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
const hasProperty = Object.prototype.hasOwnProperty.call(object, property); | ||
``` | ||
|
||
```js | ||
const hasProperty = {}.hasOwnProperty.call(object, property); | ||
``` | ||
|
||
```js | ||
const hasProperty = lodash.has(object, property); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const hasProperty = Object.hasOwn(object, property); | ||
``` | ||
|
||
## Options | ||
|
||
Type: `object` | ||
|
||
### functions | ||
|
||
Type: `string[]` | ||
|
||
You can also check custom functions that indicating the object has the specified property as its own property. | ||
|
||
`_.has()`, `lodash.has()`, and `underscore.has()` are checked by default. | ||
|
||
Example: | ||
|
||
```js | ||
{ | ||
'unicorn/prefer-object-has-own': [ | ||
'error', | ||
{ | ||
functions: [ | ||
'has', | ||
'utils.has', | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
```js | ||
// eslint unicorn/prefer-object-has-own: ["error", {"functions": ["utils.has"]}] | ||
const hasProperty = utils.has(object, property); // Fails | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'use strict'; | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
const {isNodeMatchesNameOrPath} = require('./utils/is-node-matches'); | ||
const { | ||
objectPrototypeMethodSelector, | ||
methodCallSelector, | ||
callExpressionSelector | ||
} = require('./selectors'); | ||
|
||
const MESSAGE_ID = 'prefer-object-has-own'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Use `Object.hasOwn(…)` instead of `{{description}}`.' | ||
}; | ||
|
||
const objectPrototypeHasOwnProperty = [ | ||
methodCallSelector({name: 'call', length: 2}), | ||
' > ', | ||
objectPrototypeMethodSelector({ | ||
path: 'object', | ||
name: 'hasOwnProperty' | ||
}), | ||
'.callee' | ||
].join(''); | ||
|
||
const lodashHasFunctions = [ | ||
'_.has', | ||
'lodash.has', | ||
'underscore.has' | ||
]; | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = context => { | ||
const {functions: configFunctions} = { | ||
functions: [], | ||
...context.options[0] | ||
}; | ||
const functions = [...configFunctions, ...lodashHasFunctions]; | ||
|
||
return { | ||
[objectPrototypeHasOwnProperty](node) { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID, | ||
data: {description: 'Object.prototype.hasOwnProperty.call(…)'}, | ||
/** @param {import('eslint').Rule.RuleFixer} fixer */ | ||
fix: fixer => fixer.replaceText(node, 'Object.hasOwn') | ||
}); | ||
}, | ||
[callExpressionSelector({length: 2})]({callee: node}) { | ||
const matchedFunction = functions.find(nameOrPath => isNodeMatchesNameOrPath(node, nameOrPath)); | ||
if (!matchedFunction) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID, | ||
data: {description: `${matchedFunction.trim()}.call(…)`}, | ||
/** @param {import('eslint').Rule.RuleFixer} fixer */ | ||
fix: fixer => fixer.replaceText(node, 'Object.hasOwn') | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
const schema = [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
functions: { | ||
type: 'array', | ||
uniqueItems: true | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
]; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Prefer `Object.hasOwn(…)` over `Object.prototype.hasOwnProperty.call(…)`.', | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'code', | ||
schema, | ||
messages | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
function emptyObjectSelector(path) { | ||
const prefix = `${path}.`; | ||
return [ | ||
`[${prefix}type="ObjectExpression"]`, | ||
`[${prefix}properties.length=0]` | ||
].join(''); | ||
} | ||
|
||
module.exports = emptyObjectSelector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
const matches = require('./matches-any'); | ||
const memberExpressionSelector = require('./member-expression-selector'); | ||
const emptyArraySelector = require('./empty-array-selector'); | ||
const emptyObjectSelector = require('./empty-object-selector'); | ||
|
||
function prototypeMethodSelector(options) { | ||
const { | ||
object, | ||
name, | ||
names, | ||
path | ||
} = { | ||
path: '', | ||
name: '', | ||
...options | ||
}; | ||
|
||
const objectPath = path ? `${path}.object` : 'object'; | ||
|
||
const prototypeSelectors = [ | ||
memberExpressionSelector({path: objectPath, name: 'prototype', object}) | ||
]; | ||
|
||
switch (object) { | ||
case 'Array': | ||
// `[].method` or `Array.prototype.method` | ||
prototypeSelectors.push(emptyArraySelector(objectPath)); | ||
break; | ||
case 'Object': | ||
// `{}.method` or `Object.prototype.method` | ||
prototypeSelectors.push(emptyObjectSelector(objectPath)); | ||
break; | ||
// No default | ||
} | ||
|
||
return [ | ||
memberExpressionSelector({ | ||
path, | ||
name, | ||
names | ||
}), | ||
matches(prototypeSelectors) | ||
].join(''); | ||
} | ||
|
||
const arrayPrototypeMethodSelector = options => prototypeMethodSelector({...options, object: 'Array'}); | ||
const objectPrototypeMethodSelector = options => prototypeMethodSelector({...options, object: 'Object'}); | ||
|
||
module.exports = { | ||
arrayPrototypeMethodSelector, | ||
objectPrototypeMethodSelector | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
'const hasProperty = Object.hasOwn(object, property);', | ||
|
||
// CallExpression | ||
'Object.prototype.hasOwnProperty.call', | ||
'({}).hasOwnProperty.call', | ||
'foo.call(Object.prototype.hasOwnProperty)', | ||
'foo.call(Object.prototype.hasOwnProperty.call)', | ||
|
||
// Arguments | ||
'Object.prototype.hasOwnProperty.call(object)', | ||
'Object.prototype.hasOwnProperty.call()', | ||
'Object.prototype.hasOwnProperty.call(object, property, extraArgument)', | ||
'Object.prototype.hasOwnProperty.call(...[object, property])', | ||
'({}).hasOwnProperty.call(object)', | ||
'({}).hasOwnProperty.call()', | ||
'({}).hasOwnProperty.call(object, property, extraArgument)', | ||
'({}).hasOwnProperty.call(...[object, property])', | ||
|
||
// Optional | ||
'Object.prototype.hasOwnProperty.call?.(object, property)', | ||
'Object.prototype.hasOwnProperty?.call(object, property)', | ||
'Object.prototype?.hasOwnProperty.call(object, property)', | ||
'Object?.prototype.hasOwnProperty.call(object, property)', | ||
'({}).hasOwnProperty.call?.(object, property)', | ||
'({}).hasOwnProperty?.call(object, property)', | ||
'({})?.hasOwnProperty.call(object, property)', | ||
|
||
// Computed | ||
'Object.prototype.hasOwnProperty[call](object, property)', | ||
'Object.prototype[hasOwnProperty].call(object, property)', | ||
'Object[prototype].hasOwnProperty.call(object, property)', | ||
'({}).hasOwnProperty[call](object, property)', | ||
'({})[hasOwnProperty].call(object, property)', | ||
|
||
// Names | ||
'Object.prototype.hasOwnProperty.notCall(object, property)', | ||
'Object.prototype.notHasOwnProperty.call(object, property)', | ||
'Object.notPrototype.hasOwnProperty.call(object, property)', | ||
'notObject.prototype.hasOwnProperty.call(object, property)', | ||
'({}).hasOwnProperty.notCall(object, property)', | ||
'({}).notHasOwnProperty.call(object, property)', | ||
|
||
// Empty object | ||
'({notEmpty}).hasOwnProperty.call(object, property)', | ||
'([]).hasOwnProperty.call(object, property)' | ||
], | ||
invalid: [ | ||
'const hasProperty = Object.prototype.hasOwnProperty.call(object, property);', | ||
'const hasProperty = Object.prototype.hasOwnProperty.call(object, property,);', | ||
'const hasProperty = (( Object.prototype.hasOwnProperty.call(object, property) ));', | ||
'const hasProperty = (( Object.prototype.hasOwnProperty.call ))(object, property);', | ||
'const hasProperty = (( Object.prototype.hasOwnProperty )).call(object, property);', | ||
'const hasProperty = (( Object.prototype )).hasOwnProperty.call(object, property);', | ||
'const hasProperty = (( Object )).prototype.hasOwnProperty.call(object, property);', | ||
'const hasProperty = {}.hasOwnProperty.call(object, property);', | ||
'const hasProperty = (( {}.hasOwnProperty.call(object, property) ));', | ||
'const hasProperty = (( {}.hasOwnProperty.call ))(object, property);', | ||
'const hasProperty = (( {}.hasOwnProperty )).call(object, property);', | ||
'const hasProperty = (( {} )).hasOwnProperty.call(object, property);' | ||
] | ||
}); | ||
|
||
// `functions` | ||
test.snapshot({ | ||
valid: [ | ||
'_.has(object)', | ||
'_.has()', | ||
'_.has(object, property, extraArgument)', | ||
'_.has', | ||
'_.has?.(object, property)', | ||
'_?.has(object, property)', | ||
'foo.has(object, property)', | ||
'foo._.has(object, property)' | ||
], | ||
invalid: [ | ||
'_.has(object, property)', | ||
'lodash.has(object, property)', | ||
'underscore.has(object, property)', | ||
{ | ||
code: '_.has(object, property)', | ||
options: [{functions: ['utils.has']}] | ||
}, | ||
{ | ||
code: 'utils.has(object, property)', | ||
options: [{functions: ['utils.has']}] | ||
} | ||
] | ||
}); |
Oops, something went wrong.