]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/new-cap.js
ec85c4bf6fd0eb2e878b89a55ac90c5a5648a84a
2 * @fileoverview Tests for new-cap rule.
3 * @author Nicholas C. Zakas
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/new-cap"),
13 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester();
21 ruleTester
.run("new-cap", rule
, {
23 "var x = new Constructor();",
24 "var x = new a.b.Constructor();",
25 "var x = new a.b['Constructor']();",
26 "var x = new a.b[Constructor]();",
27 "var x = new a.b[constructor]();",
28 "var x = new function(){};",
36 "var x = Boolean(42)",
38 "var x = Date.UTC(2000, 0)",
39 "var x = Error('error')",
40 "var x = Function('return 0')",
42 "var x = Object(null)",
45 "var x = Symbol('symbol')",
46 "var x = BigInt('1n')",
49 { code
: "var x = Foo(42)", options
: [{ capIsNew
: false }] },
50 { code
: "var x = bar.Foo(42)", options
: [{ capIsNew
: false }] },
51 { code
: "var x = Foo.bar(42)", options
: [{ capIsNew
: false }] },
52 "var x = bar[Foo](42)",
53 { code
: "var x = bar['Foo'](42)", options
: [{ capIsNew
: false }] },
54 "var x = Foo.bar(42)",
55 { code
: "var x = new foo(42)", options
: [{ newIsCap
: false }] },
56 "var o = { 1: function() {} }; o[1]();",
57 "var o = { 1: function() {} }; new o[1]();",
58 { code
: "var x = Foo(42);", options
: [{ capIsNew
: true, capIsNewExceptions
: ["Foo"] }] },
59 { code
: "var x = Foo(42);", options
: [{ capIsNewExceptionPattern
: "^Foo" }] },
60 { code
: "var x = new foo(42);", options
: [{ newIsCap
: true, newIsCapExceptions
: ["foo"] }] },
61 { code
: "var x = new foo(42);", options
: [{ newIsCapExceptionPattern
: "^foo" }] },
62 { code
: "var x = Object(42);", options
: [{ capIsNewExceptions
: ["Foo"] }] },
64 { code
: "var x = Foo.Bar(42);", options
: [{ capIsNewExceptions
: ["Bar"] }] },
65 { code
: "var x = Foo.Bar(42);", options
: [{ capIsNewExceptions
: ["Foo.Bar"] }] },
67 { code
: "var x = Foo.Bar(42);", options
: [{ capIsNewExceptionPattern
: "^Foo\\.." }] },
68 { code
: "var x = new foo.bar(42);", options
: [{ newIsCapExceptions
: ["bar"] }] },
69 { code
: "var x = new foo.bar(42);", options
: [{ newIsCapExceptions
: ["foo.bar"] }] },
71 { code
: "var x = new foo.bar(42);", options
: [{ newIsCapExceptionPattern
: "^foo\\.." }] },
72 { code
: "var x = new foo.bar(42);", options
: [{ properties
: false }] },
73 { code
: "var x = Foo.bar(42);", options
: [{ properties
: false }] },
74 { code
: "var x = foo.Bar(42);", options
: [{ capIsNew
: false, properties
: false }] }
78 code
: "var x = new c();",
81 type
: "NewExpression",
89 code
: "var x = new φ;",
92 type
: "NewExpression",
100 code
: "var x = new a.b.c;",
103 type
: "NewExpression",
111 code
: "var x = new a.b['c'];",
114 type
: "NewExpression",
122 code
: "var b = Foo();",
125 type
: "CallExpression",
133 code
: "var b = a.Foo();",
136 type
: "CallExpression",
144 code
: "var b = a['Foo']();",
147 type
: "CallExpression",
155 code
: "var b = a.Date.UTC();",
158 type
: "CallExpression",
166 code
: "var b = UTC();",
169 type
: "CallExpression",
177 code
: "var a = B.C();",
181 type
: "CallExpression",
190 code
: "var a = B\n.C();",
194 type
: "CallExpression",
203 code
: "var a = new B.c();",
207 type
: "NewExpression",
216 code
: "var a = new B.\nc();",
220 type
: "NewExpression",
229 code
: "var a = new c();",
233 type
: "NewExpression",
242 code
: "var a = new b[ ( 'foo' ) ]();",
243 parserOptions
: { ecmaVersion
: 6 },
247 type
: "NewExpression",
256 code
: "var a = new b[`foo`];",
257 parserOptions
: { ecmaVersion
: 6 },
261 type
: "NewExpression",
270 code
: "var a = b[`\\\nFoo`]();",
271 parserOptions
: { ecmaVersion
: 6 },
275 type
: "CallExpression",
285 code
: "var x = Foo.Bar(42);",
286 options
: [{ capIsNewExceptions
: ["Foo"] }],
287 errors
: [{ type
: "CallExpression", messageId
: "upper" }]
290 code
: "var x = Bar.Foo(42);",
292 options
: [{ capIsNewExceptionPattern
: "^Foo\\.." }],
293 errors
: [{ type
: "CallExpression", messageId
: "upper" }]
296 code
: "var x = new foo.bar(42);",
297 options
: [{ newIsCapExceptions
: ["foo"] }],
298 errors
: [{ type
: "NewExpression", messageId
: "lower" }]
301 code
: "var x = new bar.foo(42);",
303 options
: [{ newIsCapExceptionPattern
: "^foo\\.." }],
304 errors
: [{ type
: "NewExpression", messageId
: "lower" }]