[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to flag when using constructor without parentheses
|
---|
| 3 | * @author Ilya Volodin
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | "use strict";
|
---|
| 8 |
|
---|
| 9 | //------------------------------------------------------------------------------
|
---|
| 10 | // Requirements
|
---|
| 11 | //------------------------------------------------------------------------------
|
---|
| 12 |
|
---|
| 13 | const astUtils = require("./utils/ast-utils");
|
---|
| 14 |
|
---|
| 15 | //------------------------------------------------------------------------------
|
---|
| 16 | // Helpers
|
---|
| 17 | //------------------------------------------------------------------------------
|
---|
| 18 |
|
---|
| 19 | //------------------------------------------------------------------------------
|
---|
| 20 | // Rule Definition
|
---|
| 21 | //------------------------------------------------------------------------------
|
---|
| 22 |
|
---|
| 23 | /** @type {import('../shared/types').Rule} */
|
---|
| 24 | module.exports = {
|
---|
| 25 | meta: {
|
---|
| 26 | deprecated: true,
|
---|
| 27 | replacedBy: [],
|
---|
| 28 | type: "layout",
|
---|
| 29 |
|
---|
| 30 | docs: {
|
---|
| 31 | description: "Enforce or disallow parentheses when invoking a constructor with no arguments",
|
---|
| 32 | recommended: false,
|
---|
| 33 | url: "https://eslint.org/docs/latest/rules/new-parens"
|
---|
| 34 | },
|
---|
| 35 |
|
---|
| 36 | fixable: "code",
|
---|
| 37 | schema: [
|
---|
| 38 | {
|
---|
| 39 | enum: ["always", "never"]
|
---|
| 40 | }
|
---|
| 41 | ],
|
---|
| 42 | messages: {
|
---|
| 43 | missing: "Missing '()' invoking a constructor.",
|
---|
| 44 | unnecessary: "Unnecessary '()' invoking a constructor with no arguments."
|
---|
| 45 | }
|
---|
| 46 | },
|
---|
| 47 |
|
---|
| 48 | create(context) {
|
---|
| 49 | const options = context.options;
|
---|
| 50 | const always = options[0] !== "never"; // Default is always
|
---|
| 51 |
|
---|
| 52 | const sourceCode = context.sourceCode;
|
---|
| 53 |
|
---|
| 54 | return {
|
---|
| 55 | NewExpression(node) {
|
---|
| 56 | if (node.arguments.length !== 0) {
|
---|
| 57 | return; // if there are arguments, there have to be parens
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | const lastToken = sourceCode.getLastToken(node);
|
---|
| 61 | const hasLastParen = lastToken && astUtils.isClosingParenToken(lastToken);
|
---|
| 62 |
|
---|
| 63 | // `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
|
---|
| 64 | const hasParens = hasLastParen &&
|
---|
| 65 | astUtils.isOpeningParenToken(sourceCode.getTokenBefore(lastToken)) &&
|
---|
| 66 | node.callee.range[1] < node.range[1];
|
---|
| 67 |
|
---|
| 68 | if (always) {
|
---|
| 69 | if (!hasParens) {
|
---|
| 70 | context.report({
|
---|
| 71 | node,
|
---|
| 72 | messageId: "missing",
|
---|
| 73 | fix: fixer => fixer.insertTextAfter(node, "()")
|
---|
| 74 | });
|
---|
| 75 | }
|
---|
| 76 | } else {
|
---|
| 77 | if (hasParens) {
|
---|
| 78 | context.report({
|
---|
| 79 | node,
|
---|
| 80 | messageId: "unnecessary",
|
---|
| 81 | fix: fixer => [
|
---|
| 82 | fixer.remove(sourceCode.getTokenBefore(lastToken)),
|
---|
| 83 | fixer.remove(lastToken),
|
---|
| 84 | fixer.insertTextBefore(node, "("),
|
---|
| 85 | fixer.insertTextAfter(node, ")")
|
---|
| 86 | ]
|
---|
| 87 | });
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | };
|
---|
| 92 | }
|
---|
| 93 | };
|
---|