2020-09-29 18:29:40 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview We shouldn't use global built-in object for security and
|
2022-02-08 16:41:40 +00:00
|
|
|
* performance reason. This linter rule reports replaceable codes
|
2020-09-29 18:29:40 +00:00
|
|
|
* that can be replaced with primordials.
|
|
|
|
* @author Leko <leko.noor@gmail.com>
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function toPrimordialsName(obj, prop) {
|
|
|
|
return obj + toUcFirst(prop);
|
|
|
|
}
|
|
|
|
|
|
|
|
function toUcFirst(str) {
|
|
|
|
return str[0].toUpperCase() + str.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isTarget(map, varName) {
|
|
|
|
return map.has(varName);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isIgnored(map, varName, propName) {
|
2020-11-07 16:29:12 +00:00
|
|
|
return map.get(varName)?.get(propName)?.ignored ?? false;
|
2020-09-29 18:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getReportName({ name, parentName, into }) {
|
|
|
|
if (into) {
|
|
|
|
return toPrimordialsName(into, name);
|
|
|
|
}
|
|
|
|
if (parentName) {
|
|
|
|
return toPrimordialsName(parentName, name);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get identifier of object spread assignment
|
|
|
|
*
|
|
|
|
* code: 'const { ownKeys } = Reflect;'
|
|
|
|
* argument: 'ownKeys'
|
|
|
|
* return: 'Reflect'
|
|
|
|
*/
|
|
|
|
function getDestructuringAssignmentParent(scope, node) {
|
|
|
|
const declaration = scope.set.get(node.name);
|
|
|
|
if (
|
|
|
|
!declaration ||
|
|
|
|
!declaration.defs ||
|
|
|
|
declaration.defs.length === 0 ||
|
|
|
|
declaration.defs[0].type !== 'Variable' ||
|
|
|
|
!declaration.defs[0].node.init
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-11-04 17:04:27 +00:00
|
|
|
return declaration.defs[0].node.init;
|
2020-09-29 18:29:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 18:42:02 +00:00
|
|
|
const parentSelectors = [
|
|
|
|
// We want to select identifiers that refer to other references, not the ones
|
|
|
|
// that create a new reference.
|
|
|
|
'ClassDeclaration',
|
|
|
|
'FunctionDeclaration',
|
|
|
|
'LabeledStatement',
|
|
|
|
'MemberExpression',
|
|
|
|
'MethodDefinition',
|
|
|
|
'SwitchCase',
|
|
|
|
'VariableDeclarator',
|
|
|
|
];
|
|
|
|
const identifierSelector = parentSelectors.map((selector) => `[type!=${selector}]`).join('') + '>Identifier';
|
2020-09-29 18:29:40 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
messages: {
|
2022-12-18 16:39:39 +00:00
|
|
|
error: 'Use `const { {{name}} } = primordials;` instead of the global.',
|
2024-10-17 10:37:54 +00:00
|
|
|
errorPolyfill: 'Use `const { {{name}} } = require("internal/util");` instead of the primordial.',
|
2022-12-18 16:39:39 +00:00
|
|
|
},
|
2024-05-09 15:27:39 +00:00
|
|
|
schema: {
|
|
|
|
type: 'array',
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
type: 'object',
|
|
|
|
required: ['name'],
|
|
|
|
properties: {
|
|
|
|
name: { type: 'string' },
|
|
|
|
ignore: {
|
|
|
|
type: 'array',
|
|
|
|
items: { type: 'string' },
|
|
|
|
},
|
|
|
|
into: { type: 'string' },
|
2024-10-17 10:37:54 +00:00
|
|
|
polyfilled: {
|
|
|
|
type: 'array',
|
|
|
|
items: { type: 'string' },
|
|
|
|
},
|
2024-05-09 15:27:39 +00:00
|
|
|
},
|
|
|
|
additionalProperties: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-09-29 18:29:40 +00:00
|
|
|
},
|
|
|
|
create(context) {
|
2024-05-09 15:27:39 +00:00
|
|
|
const globalScope = context.sourceCode.scopeManager.globalScope;
|
2020-11-07 16:29:12 +00:00
|
|
|
|
|
|
|
const nameMap = new Map();
|
|
|
|
const renameMap = new Map();
|
2024-10-17 10:37:54 +00:00
|
|
|
const polyfilledSet = new Set();
|
2020-11-07 16:29:12 +00:00
|
|
|
|
|
|
|
for (const option of context.options) {
|
|
|
|
const names = option.ignore || [];
|
|
|
|
nameMap.set(
|
2020-09-29 18:29:40 +00:00
|
|
|
option.name,
|
2022-12-18 16:39:39 +00:00
|
|
|
new Map(names.map((name) => [name, { ignored: true }])),
|
2020-11-07 16:29:12 +00:00
|
|
|
);
|
|
|
|
if (option.into) {
|
|
|
|
renameMap.set(option.name, option.into);
|
|
|
|
}
|
2024-10-17 10:37:54 +00:00
|
|
|
if (option.polyfilled) {
|
|
|
|
for (const propertyName of option.polyfilled) {
|
|
|
|
polyfilledSet.add(`${option.name}${propertyName[0].toUpperCase()}${propertyName.slice(1)}`);
|
|
|
|
}
|
|
|
|
}
|
2020-11-07 16:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 18:29:40 +00:00
|
|
|
let reported;
|
|
|
|
|
|
|
|
return {
|
|
|
|
Program() {
|
2020-11-07 16:29:12 +00:00
|
|
|
reported = new Set();
|
2020-09-29 18:29:40 +00:00
|
|
|
},
|
|
|
|
[identifierSelector](node) {
|
2022-02-18 18:42:02 +00:00
|
|
|
if (node.parent.type === 'Property' && node.parent.key === node) {
|
|
|
|
// If the identifier is the key for this property declaration, it
|
|
|
|
// can't be referring to a primordials member.
|
|
|
|
return;
|
|
|
|
}
|
2020-09-29 18:29:40 +00:00
|
|
|
if (reported.has(node.range[0])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const name = node.name;
|
2021-11-04 17:04:27 +00:00
|
|
|
const parent = getDestructuringAssignmentParent(
|
2024-05-09 15:27:39 +00:00
|
|
|
context.sourceCode.getScope(node),
|
2022-12-18 16:39:39 +00:00
|
|
|
node,
|
2020-09-29 18:29:40 +00:00
|
|
|
);
|
2021-11-04 17:04:27 +00:00
|
|
|
const parentName = parent?.name;
|
2023-02-17 18:29:42 +00:00
|
|
|
if (!isTarget(nameMap, name) && (!isTarget(nameMap, parentName) || isIgnored(nameMap, parentName, name))) {
|
2020-09-29 18:29:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-07 16:29:12 +00:00
|
|
|
const defs = globalScope.set.get(name)?.defs;
|
2020-09-29 18:29:40 +00:00
|
|
|
if (parentName && isTarget(nameMap, parentName)) {
|
2021-11-04 17:04:27 +00:00
|
|
|
if (defs?.[0].name.name !== 'primordials' &&
|
|
|
|
!reported.has(parent.range[0]) &&
|
|
|
|
parent.parent?.id?.type !== 'Identifier') {
|
2020-11-07 16:29:12 +00:00
|
|
|
reported.add(node.range[0]);
|
2020-09-29 18:29:40 +00:00
|
|
|
const into = renameMap.get(name);
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: 'error',
|
|
|
|
data: {
|
2022-12-18 16:39:39 +00:00
|
|
|
name: getReportName({ into, parentName, name }),
|
|
|
|
},
|
2020-09-29 18:29:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (defs.length === 0 || defs[0].node.init.name !== 'primordials') {
|
2020-11-07 16:29:12 +00:00
|
|
|
reported.add(node.range[0]);
|
2020-09-29 18:29:40 +00:00
|
|
|
const into = renameMap.get(name);
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: 'error',
|
|
|
|
data: {
|
2022-12-18 16:39:39 +00:00
|
|
|
name: getReportName({ into, parentName, name }),
|
|
|
|
},
|
2020-09-29 18:29:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
MemberExpression(node) {
|
|
|
|
const obj = node.object.name;
|
|
|
|
const prop = node.property.name;
|
|
|
|
if (!prop || !isTarget(nameMap, obj) || isIgnored(nameMap, obj, prop)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const variables =
|
2024-05-09 15:27:39 +00:00
|
|
|
context.sourceCode.scopeManager.getDeclaredVariables(node);
|
2020-09-29 18:29:40 +00:00
|
|
|
if (variables.length === 0) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: 'error',
|
|
|
|
data: {
|
|
|
|
name: toPrimordialsName(obj, prop),
|
2022-12-18 16:39:39 +00:00
|
|
|
},
|
2020-09-29 18:29:40 +00:00
|
|
|
});
|
|
|
|
}
|
2021-11-04 17:04:27 +00:00
|
|
|
},
|
|
|
|
VariableDeclarator(node) {
|
|
|
|
const name = node.init?.name;
|
2024-10-17 10:37:54 +00:00
|
|
|
if (name === 'primordials' && node.id.type === 'ObjectPattern') {
|
|
|
|
const name = node.id.properties.find(({ key }) => polyfilledSet.has(key.name))?.key.name;
|
|
|
|
if (name) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: 'errorPolyfill',
|
|
|
|
data: { name },
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 17:04:27 +00:00
|
|
|
if (name !== undefined && isTarget(nameMap, name) &&
|
|
|
|
node.id.type === 'Identifier' &&
|
|
|
|
!globalScope.set.get(name)?.defs.length) {
|
|
|
|
reported.add(node.init.range[0]);
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: 'error',
|
|
|
|
data: { name },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2020-09-29 18:29:40 +00:00
|
|
|
};
|
2022-12-18 16:39:39 +00:00
|
|
|
},
|
2020-09-29 18:29:40 +00:00
|
|
|
};
|