Skip to content

Commit b37bda2

Browse files
committed
fixup: add support for Object.create(null)
1 parent 0c92b54 commit b37bda2

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

tools/eslint-rules/prefer-proto.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
*/
77
'use strict';
88

9+
// Cribbed from `eslint-module-utils/declaredScope`
10+
function declaredScope(context, name) {
11+
const references = context.getScope().references;
12+
const reference = references.find((x) => x.identifier.name === name);
13+
if (!reference) return undefined;
14+
return reference.resolved.scope.type;
15+
}
16+
917
//------------------------------------------------------------------------------
1018
// Rule Definition
1119
//------------------------------------------------------------------------------
@@ -19,8 +27,16 @@ module.exports = {
1927
},
2028
create(context) {
2129
return {
22-
// eslint-disable-next-line max-len
23-
'CallExpression[callee.name="ObjectCreate"][arguments.0.type="Literal"][arguments.0.value=null][arguments.length=1]'(node) {
30+
/* eslint max-len: 0 */
31+
'CallExpression[callee.type="Identifier"][callee.name="ObjectCreate"][arguments.0.type="Literal"][arguments.0.value=null][arguments.length=1],\
32+
CallExpression[callee.type="MemberExpression"][callee.object.name="Object"][callee.property.name="create"][arguments.0.type="Literal"][arguments.0.value=null][arguments.length=1]\
33+
'(node) {
34+
if (node.callee.type === 'MemberExpression') {
35+
const scope = declaredScope(context, node.callee.object);
36+
if (scope !== 'module' && scope !== 'global') {
37+
return;
38+
}
39+
}
2440
context.report({
2541
node,
2642
messageId: 'error',

0 commit comments

Comments
 (0)