UNPKG

7.35 kBJavaScriptView Raw
1/* eslint-disable */
2
3/**
4 * @fileoverview Tests for new-cap rule.
5 * @author Nicholas C. Zakas
6 */
7
8var rule = require('../../rules/new-cap'),
9 RuleTester = require('../RuleTester');
10
11var ruleTester = new RuleTester();
12ruleTester.run('babel/new-cap', rule, {
13 valid: [
14 // Original test cases.
15 "var x = new Constructor();",
16 "var x = new a.b.Constructor();",
17 "var x = new a.b['Constructor']();",
18 "var x = new a.b[Constructor]();",
19 "var x = new a.b[constructor]();",
20 "var x = new function(){};",
21 "var x = new _;",
22 "var x = new $;",
23 "var x = new Σ;",
24 "var x = new _x;",
25 "var x = new $x;",
26 "var x = new this;",
27 "var x = Array(42)",
28 "var x = Boolean(42)",
29 "var x = Date(42)",
30 "var x = Date.UTC(2000, 0)",
31 "var x = Error('error')",
32 "var x = Function('return 0')",
33 "var x = Number(42)",
34 "var x = Object(null)",
35 "var x = RegExp(42)",
36 "var x = String(42)",
37 "var x = Symbol('symbol')",
38 "var x = _();",
39 "var x = $();",
40 { code: "var x = Foo(42)", options: [{ capIsNew: false }] },
41 { code: "var x = bar.Foo(42)", options: [{ capIsNew: false }] },
42 { code: "var x = Foo.bar(42)", options: [{ capIsNew: false }] },
43 "var x = bar[Foo](42)",
44 { code: "var x = bar['Foo'](42)", options: [{ capIsNew: false }] },
45 "var x = Foo.bar(42)",
46 { code: "var x = new foo(42)", options: [{ newIsCap: false }] },
47 "var o = { 1: function() {} }; o[1]();",
48 "var o = { 1: function() {} }; new o[1]();",
49 { code: "var x = Foo(42);", options: [{ capIsNew: true, capIsNewExceptions: ["Foo"] }] },
50 { code: "var x = Foo(42);", options: [{ capIsNewExceptionPattern: "^Foo" }] },
51 { code: "var x = new foo(42);", options: [{ newIsCap: true, newIsCapExceptions: ["foo"] }] },
52 { code: "var x = new foo(42);", options: [{ newIsCapExceptionPattern: "^foo" }] },
53 { code: "var x = Object(42);", options: [{ capIsNewExceptions: ["Foo"] }] },
54
55 { code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Bar"] }] },
56 { code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Foo.Bar"] }] },
57
58 { code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptionPattern: "^Foo\\.." }] },
59 { code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["bar"] }] },
60 { code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["foo.bar"] }] },
61
62 { code: "var x = new foo.bar(42);", options: [{ newIsCapExceptionPattern: "^foo\\.." }] },
63 { code: "var x = new foo.bar(42);", options: [{ properties: false }] },
64 { code: "var x = Foo.bar(42);", options: [{ properties: false }] },
65 { code: "var x = foo.Bar(42);", options: [{ capIsNew: false, properties: false }] },
66
67 // Babel-specific test cases.
68 { code: "@MyDecorator(123) class MyClass{}", parser: "babel-eslint" },
69 ],
70 invalid: [
71 { code: "var x = new c();", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] },
72 { code: "var x = new φ;", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] },
73 { code: "var x = new a.b.c;", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] },
74 { code: "var x = new a.b['c'];", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] },
75 { code: "var b = Foo();", errors: [{ message: "A function with a name starting with an uppercase letter should only be used as a constructor.", type: "CallExpression" }] },
76 { code: "var b = a.Foo();", errors: [{ message: "A function with a name starting with an uppercase letter should only be used as a constructor.", type: "CallExpression" }] },
77 { code: "var b = a['Foo']();", errors: [{ message: "A function with a name starting with an uppercase letter should only be used as a constructor.", type: "CallExpression" }] },
78 { code: "var b = a.Date.UTC();", errors: [{ message: "A function with a name starting with an uppercase letter should only be used as a constructor.", type: "CallExpression" }] },
79 { code: "var b = UTC();", errors: [{ message: "A function with a name starting with an uppercase letter should only be used as a constructor.", type: "CallExpression" }] },
80 {
81 code: "var a = B.C();",
82 errors: [
83 {
84 message: "A function with a name starting with an uppercase letter should only be used as a constructor.",
85 type: "CallExpression",
86 line: 1,
87 column: 11
88 }
89 ]
90 },
91 {
92 code: "var a = B\n.C();",
93 errors: [
94 {
95 message: "A function with a name starting with an uppercase letter should only be used as a constructor.",
96 type: "CallExpression",
97 line: 2,
98 column: 2
99 }
100 ]
101 },
102 {
103 code: "var a = new B.c();",
104 errors: [
105 {
106 message: "A constructor name should not start with a lowercase letter.",
107 type: "NewExpression",
108 line: 1,
109 column: 15
110 }
111 ]
112 },
113 {
114 code: "var a = new B.\nc();",
115 errors: [
116 {
117 message: "A constructor name should not start with a lowercase letter.",
118 type: "NewExpression",
119 line: 2,
120 column: 1
121 }
122 ]
123 },
124 {
125 code: "var a = new c();",
126 errors: [
127 {
128 message: "A constructor name should not start with a lowercase letter.",
129 type: "NewExpression",
130 line: 1,
131 column: 13
132 }
133 ]
134 },
135
136 {
137 code: "var x = Foo.Bar(42);",
138 options: [{ capIsNewExceptions: ["Foo"] }],
139 errors: [{ type: "CallExpression", message: "A function with a name starting with an uppercase letter should only be used as a constructor." }]
140 },
141 {
142 code: "var x = Bar.Foo(42);",
143
144 options: [{ capIsNewExceptionPattern: "^Foo\\.." }],
145 errors: [{ type: "CallExpression", message: "A function with a name starting with an uppercase letter should only be used as a constructor." }]
146 },
147 {
148 code: "var x = new foo.bar(42);",
149 options: [{ newIsCapExceptions: ["foo"] }],
150 errors: [{ type: "NewExpression", message: "A constructor name should not start with a lowercase letter." }]
151 },
152 {
153 code: "var x = new bar.foo(42);",
154
155 options: [{ newIsCapExceptionPattern: "^foo\\.." }],
156 errors: [{ type: "NewExpression", message: "A constructor name should not start with a lowercase letter." }]
157 }
158 ]
159});