]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/new-cap.js
5953c87c24aedcc0717634cfe623d0b911b3820e
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 }] },
79 parserOptions
: { ecmaVersion
: 2020 }
82 code
: "(foo?.bar)();",
83 parserOptions
: { ecmaVersion
: 2020 }
86 code
: "new (foo?.Bar)();",
87 parserOptions
: { ecmaVersion
: 2020 }
90 code
: "(foo?.Bar)();",
91 options
: [{ properties
: false }],
92 parserOptions
: { ecmaVersion
: 2020 }
95 code
: "new (foo?.bar)();",
96 options
: [{ properties
: false }],
97 parserOptions
: { ecmaVersion
: 2020 }
100 code
: "Date?.UTC();",
101 parserOptions
: { ecmaVersion
: 2020 }
104 code
: "(Date?.UTC)();",
105 parserOptions
: { ecmaVersion
: 2020 }
110 code
: "var x = new c();",
113 type
: "NewExpression",
121 code
: "var x = new φ;",
124 type
: "NewExpression",
132 code
: "var x = new a.b.c;",
135 type
: "NewExpression",
143 code
: "var x = new a.b['c'];",
146 type
: "NewExpression",
154 code
: "var b = Foo();",
157 type
: "CallExpression",
165 code
: "var b = a.Foo();",
168 type
: "CallExpression",
176 code
: "var b = a['Foo']();",
179 type
: "CallExpression",
187 code
: "var b = a.Date.UTC();",
190 type
: "CallExpression",
198 code
: "var b = UTC();",
201 type
: "CallExpression",
209 code
: "var a = B.C();",
213 type
: "CallExpression",
222 code
: "var a = B\n.C();",
226 type
: "CallExpression",
235 code
: "var a = new B.c();",
239 type
: "NewExpression",
248 code
: "var a = new B.\nc();",
252 type
: "NewExpression",
261 code
: "var a = new c();",
265 type
: "NewExpression",
274 code
: "var a = new b[ ( 'foo' ) ]();",
275 parserOptions
: { ecmaVersion
: 6 },
279 type
: "NewExpression",
288 code
: "var a = new b[`foo`];",
289 parserOptions
: { ecmaVersion
: 6 },
293 type
: "NewExpression",
302 code
: "var a = b[`\\\nFoo`]();",
303 parserOptions
: { ecmaVersion
: 6 },
307 type
: "CallExpression",
317 code
: "var x = Foo.Bar(42);",
318 options
: [{ capIsNewExceptions
: ["Foo"] }],
319 errors
: [{ type
: "CallExpression", messageId
: "upper" }]
322 code
: "var x = Bar.Foo(42);",
324 options
: [{ capIsNewExceptionPattern
: "^Foo\\.." }],
325 errors
: [{ type
: "CallExpression", messageId
: "upper" }]
328 code
: "var x = new foo.bar(42);",
329 options
: [{ newIsCapExceptions
: ["foo"] }],
330 errors
: [{ type
: "NewExpression", messageId
: "lower" }]
333 code
: "var x = new bar.foo(42);",
335 options
: [{ newIsCapExceptionPattern
: "^foo\\.." }],
336 errors
: [{ type
: "NewExpression", messageId
: "lower" }]
341 code
: "new (foo?.bar)();",
342 parserOptions
: { ecmaVersion
: 2020 },
343 errors
: [{ messageId
: "lower", column
: 11, endColumn
: 14 }]
347 parserOptions
: { ecmaVersion
: 2020 },
348 errors
: [{ messageId
: "upper", column
: 6, endColumn
: 9 }]
351 code
: "(foo?.Bar)();",
352 parserOptions
: { ecmaVersion
: 2020 },
353 errors
: [{ messageId
: "upper", column
: 7, endColumn
: 10 }]