mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-09-28 21:24:26 +08:00
Update the @typescript-eslint - eslint-plugin and parser to v7.18.0 cia incremental patches. This is the last version before breaking changes need to start be applied to the infrastructure. Needed to update EsLint to v8.56.0 as required by the above major v7. Only one new error was uncovered in test file and fixed. No-Presubmit: true Bug: none Change-Id: I4e4fe265b82deeb49786c5bf3e154dac8fcfecbd Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6054130 Reviewed-by: Danil Somsikov <dsv@chromium.org> Commit-Queue: Nikolay Vitkov <nvitkov@chromium.org>
118 lines
3.5 KiB
JavaScript
118 lines
3.5 KiB
JavaScript
/**
|
|
* @fileoverview Rule to disallow calls to the `Object` constructor without an argument
|
|
* @author Francesco Trotta
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Requirements
|
|
//------------------------------------------------------------------------------
|
|
|
|
const {
|
|
getVariableByName,
|
|
isArrowToken,
|
|
isStartOfExpressionStatement,
|
|
needsPrecedingSemicolon
|
|
} = require("./utils/ast-utils");
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
/** @type {import('../shared/types').Rule} */
|
|
module.exports = {
|
|
meta: {
|
|
type: "suggestion",
|
|
|
|
docs: {
|
|
description: "Disallow calls to the `Object` constructor without an argument",
|
|
recommended: false,
|
|
url: "https://eslint.org/docs/latest/rules/no-object-constructor"
|
|
},
|
|
|
|
hasSuggestions: true,
|
|
|
|
schema: [],
|
|
|
|
messages: {
|
|
preferLiteral: "The object literal notation {} is preferable.",
|
|
useLiteral: "Replace with '{{replacement}}'.",
|
|
useLiteralAfterSemicolon: "Replace with '{{replacement}}', add preceding semicolon."
|
|
}
|
|
},
|
|
|
|
create(context) {
|
|
|
|
const sourceCode = context.sourceCode;
|
|
|
|
/**
|
|
* Determines whether or not an object literal that replaces a specified node needs to be enclosed in parentheses.
|
|
* @param {ASTNode} node The node to be replaced.
|
|
* @returns {boolean} Whether or not parentheses around the object literal are required.
|
|
*/
|
|
function needsParentheses(node) {
|
|
if (isStartOfExpressionStatement(node)) {
|
|
return true;
|
|
}
|
|
|
|
const prevToken = sourceCode.getTokenBefore(node);
|
|
|
|
if (prevToken && isArrowToken(prevToken)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Reports on nodes where the `Object` constructor is called without arguments.
|
|
* @param {ASTNode} node The node to evaluate.
|
|
* @returns {void}
|
|
*/
|
|
function check(node) {
|
|
if (node.callee.type !== "Identifier" || node.callee.name !== "Object" || node.arguments.length) {
|
|
return;
|
|
}
|
|
|
|
const variable = getVariableByName(sourceCode.getScope(node), "Object");
|
|
|
|
if (variable && variable.identifiers.length === 0) {
|
|
let replacement;
|
|
let fixText;
|
|
let messageId = "useLiteral";
|
|
|
|
if (needsParentheses(node)) {
|
|
replacement = "({})";
|
|
if (needsPrecedingSemicolon(sourceCode, node)) {
|
|
fixText = ";({})";
|
|
messageId = "useLiteralAfterSemicolon";
|
|
} else {
|
|
fixText = "({})";
|
|
}
|
|
} else {
|
|
replacement = fixText = "{}";
|
|
}
|
|
|
|
context.report({
|
|
node,
|
|
messageId: "preferLiteral",
|
|
suggest: [
|
|
{
|
|
messageId,
|
|
data: { replacement },
|
|
fix: fixer => fixer.replaceText(node, fixText)
|
|
}
|
|
]
|
|
});
|
|
}
|
|
}
|
|
|
|
return {
|
|
CallExpression: check,
|
|
NewExpression: check
|
|
};
|
|
|
|
}
|
|
};
|