2023-01-10 05:38:36 +00:00
|
|
|
/**
|
|
|
|
* @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`
|
2024-05-09 15:27:39 +00:00
|
|
|
function declaredScope(context, node, name) {
|
|
|
|
const references = context.sourceCode.getScope(node).references;
|
2023-01-10 05:38:36 +00:00
|
|
|
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 {
|
2024-04-15 15:08:10 +00:00
|
|
|
/* eslint @stylistic/js/max-len: 0 */
|
2023-01-10 05:38:36 +00:00
|
|
|
'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') {
|
2024-05-09 15:27:39 +00:00
|
|
|
const scope = declaredScope(context, node, node.callee.object);
|
2023-01-10 05:38:36 +00:00
|
|
|
if (scope && scope !== 'module' && scope !== 'global') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-05-09 15:27:39 +00:00
|
|
|
const value = context.sourceCode.getText(node.arguments[0]);
|
2023-01-10 05:38:36 +00:00
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: 'error',
|
|
|
|
data: { value },
|
|
|
|
fix(fixer) {
|
|
|
|
return fixer.replaceText(node, `{ __proto__: ${value} }`);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|