mirror of https://github.com/nodejs/node.git
54 lines
1.7 KiB
JavaScript
54 lines
1.7 KiB
JavaScript
|
/**
|
||
|
* @fileoverview null objects can be created with `ObjectCreate(null)`, or with
|
||
|
* syntax: `{ __proto__: null }`. This linter rule forces use of
|
||
|
* syntax over ObjectCreate.
|
||
|
* @author Jordan Harband <ljharb@gmail.com>
|
||
|
*/
|
||
|
'use strict';
|
||
|
|
||
|
// Cribbed from `eslint-module-utils/declaredScope`
|
||
|
function declaredScope(context, name) {
|
||
|
const references = context.getScope().references;
|
||
|
const reference = references.find((x) => x.identifier.name === name);
|
||
|
if (!reference) return undefined;
|
||
|
return reference.resolved.scope.type;
|
||
|
}
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// Rule Definition
|
||
|
//------------------------------------------------------------------------------
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
fixable: 'code',
|
||
|
messages: {
|
||
|
error: 'Use `{ __proto__: {{value}} }` instead of `ObjectCreate({{value}})` or `Object.create({{value}})`.',
|
||
|
},
|
||
|
},
|
||
|
create(context) {
|
||
|
return {
|
||
|
/* eslint max-len: 0 */
|
||
|
'CallExpression[arguments.length=1]:matches(\
|
||
|
[callee.type="Identifier"][callee.name="ObjectCreate"],\
|
||
|
[callee.type="MemberExpression"][callee.object.name="Object"][callee.property.name="create"]\
|
||
|
)'(node) {
|
||
|
if (node.callee.type === 'MemberExpression') {
|
||
|
const scope = declaredScope(context, node.callee.object);
|
||
|
if (scope && scope !== 'module' && scope !== 'global') {
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
const value = context.getSourceCode().getText(node.arguments[0]);
|
||
|
context.report({
|
||
|
node,
|
||
|
messageId: 'error',
|
||
|
data: { value },
|
||
|
fix(fixer) {
|
||
|
return fixer.replaceText(node, `{ __proto__: ${value} }`);
|
||
|
},
|
||
|
});
|
||
|
},
|
||
|
};
|
||
|
},
|
||
|
};
|