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 | },
|
29 |
|
30 | schema: [],
|
31 |
|
32 | fixable: "code"
|
33 | },
|
34 |
|
35 | create(context) {
|
36 | const sourceCode = context.getSourceCode();
|
37 |
|
38 | return {
|
39 | NewExpression(node) {
|
40 | if (node.arguments.length !== 0) {
|
41 | return; // shortcut: if there are arguments, there have to be parens
|
42 | }
|
43 |
|
44 | const lastToken = sourceCode.getLastToken(node);
|
45 | const hasLastParen = lastToken && astUtils.isClosingParenToken(lastToken);
|
46 | const hasParens = hasLastParen && astUtils.isOpeningParenToken(sourceCode.getTokenBefore(lastToken));
|
47 |
|
48 | if (!hasParens) {
|
49 | context.report({
|
50 | node,
|
51 | message: "Missing '()' invoking a constructor.",
|
52 | fix: fixer => fixer.insertTextAfter(node, "()")
|
53 | });
|
54 | }
|
55 | }
|
56 | };
|
57 | }
|
58 | };
|