1 | /**
|
2 | * @fileoverview Rule to flag when using constructor without parentheses
|
3 | * @author Ilya Volodin
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | //------------------------------------------------------------------------------
|
9 | // Requirements
|
10 | //------------------------------------------------------------------------------
|
11 |
|
12 | const astUtils = require("../ast-utils");
|
13 |
|
14 | //------------------------------------------------------------------------------
|
15 | // Helpers
|
16 | //------------------------------------------------------------------------------
|
17 |
|
18 | //------------------------------------------------------------------------------
|
19 | // Rule Definition
|
20 | //------------------------------------------------------------------------------
|
21 |
|
22 | module.exports = {
|
23 | meta: {
|
24 | docs: {
|
25 | description: "require parentheses when invoking a constructor with no arguments",
|
26 | category: "Stylistic Issues",
|
27 | recommended: false,
|
28 | url: "https://eslint.org/docs/rules/new-parens"
|
29 | },
|
30 |
|
31 | schema: [],
|
32 |
|
33 | fixable: "code"
|
34 | },
|
35 |
|
36 | create(context) {
|
37 | const sourceCode = context.getSourceCode();
|
38 |
|
39 | return {
|
40 | NewExpression(node) {
|
41 | if (node.arguments.length !== 0) {
|
42 | return; // shortcut: if there are arguments, there have to be parens
|
43 | }
|
44 |
|
45 | const lastToken = sourceCode.getLastToken(node);
|
46 | const hasLastParen = lastToken && astUtils.isClosingParenToken(lastToken);
|
47 | const hasParens = hasLastParen && astUtils.isOpeningParenToken(sourceCode.getTokenBefore(lastToken));
|
48 |
|
49 | if (!hasParens) {
|
50 | context.report({
|
51 | node,
|
52 | message: "Missing '()' invoking a constructor.",
|
53 | fix: fixer => fixer.insertTextAfter(node, "()")
|
54 | });
|
55 | }
|
56 | }
|
57 | };
|
58 | }
|
59 | };
|