2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Rule to flag when using constructor without parentheses
|
|
|
|
* @author Ilya Volodin
|
2024-08-21 06:34:30 +00:00
|
|
|
* @deprecated in ESLint v8.53.0
|
2024-08-09 12:04:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Requirements
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const astUtils = require("./utils/ast-utils");
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Helpers
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
/** @type {import('../shared/types').Rule} */
|
2024-08-09 12:04:48 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: {
|
2024-08-21 06:34:30 +00:00
|
|
|
deprecated: true,
|
|
|
|
replacedBy: [],
|
2024-08-09 12:04:48 +00:00
|
|
|
type: "layout",
|
|
|
|
|
|
|
|
docs: {
|
2024-08-21 06:34:30 +00:00
|
|
|
description: "Enforce or disallow parentheses when invoking a constructor with no arguments",
|
2024-08-09 12:04:48 +00:00
|
|
|
recommended: false,
|
2024-08-21 06:34:30 +00:00
|
|
|
url: "https://eslint.org/docs/latest/rules/new-parens"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fixable: "code",
|
2024-08-21 06:34:30 +00:00
|
|
|
schema: [
|
|
|
|
{
|
|
|
|
enum: ["always", "never"]
|
|
|
|
}
|
|
|
|
],
|
2024-08-09 12:04:48 +00:00
|
|
|
messages: {
|
|
|
|
missing: "Missing '()' invoking a constructor.",
|
|
|
|
unnecessary: "Unnecessary '()' invoking a constructor with no arguments."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
create(context) {
|
|
|
|
const options = context.options;
|
|
|
|
const always = options[0] !== "never"; // Default is always
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
const sourceCode = context.sourceCode;
|
2024-08-09 12:04:48 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
NewExpression(node) {
|
|
|
|
if (node.arguments.length !== 0) {
|
|
|
|
return; // if there are arguments, there have to be parens
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastToken = sourceCode.getLastToken(node);
|
|
|
|
const hasLastParen = lastToken && astUtils.isClosingParenToken(lastToken);
|
|
|
|
|
|
|
|
// `hasParens` is true only if the new expression ends with its own parens, e.g., new new foo() does not end with its own parens
|
|
|
|
const hasParens = hasLastParen &&
|
|
|
|
astUtils.isOpeningParenToken(sourceCode.getTokenBefore(lastToken)) &&
|
|
|
|
node.callee.range[1] < node.range[1];
|
|
|
|
|
|
|
|
if (always) {
|
|
|
|
if (!hasParens) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: "missing",
|
|
|
|
fix: fixer => fixer.insertTextAfter(node, "()")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (hasParens) {
|
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: "unnecessary",
|
|
|
|
fix: fixer => [
|
|
|
|
fixer.remove(sourceCode.getTokenBefore(lastToken)),
|
|
|
|
fixer.remove(lastToken),
|
|
|
|
fixer.insertTextBefore(node, "("),
|
|
|
|
fixer.insertTextAfter(node, ")")
|
|
|
|
]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|