|
| 1 | +/** |
| 2 | + * @fileoverview Tests for the no-new-native-nonconstructor rule |
| 3 | + * @author Sosuke Suzuki |
| 4 | + */ |
| 5 | + |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | +// Requirements |
| 10 | +//------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const rule = require("../../../lib/rules/no-new-native-nonconstructor"), |
| 13 | + { RuleTester } = require("../../../lib/rule-tester"); |
| 14 | + |
| 15 | +//------------------------------------------------------------------------------ |
| 16 | +// Tests |
| 17 | +//------------------------------------------------------------------------------ |
| 18 | + |
| 19 | +const ruleTester = new RuleTester({ env: { es2022: true } }); |
| 20 | + |
| 21 | +ruleTester.run("no-new-native-nonconstructor", rule, { |
| 22 | + valid: [ |
| 23 | + |
| 24 | + // Symbol |
| 25 | + "var foo = Symbol('foo');", |
| 26 | + "function bar(Symbol) { var baz = new Symbol('baz');}", |
| 27 | + "function Symbol() {} new Symbol();", |
| 28 | + "new foo(Symbol);", |
| 29 | + "new foo(bar, Symbol);", |
| 30 | + |
| 31 | + // BigInt |
| 32 | + "var foo = BigInt(9007199254740991);", |
| 33 | + "function bar(BigInt) { var baz = new BigInt(9007199254740991);}", |
| 34 | + "function BigInt() {} new BigInt();", |
| 35 | + "new foo(BigInt);", |
| 36 | + "new foo(bar, BigInt);" |
| 37 | + ], |
| 38 | + invalid: [ |
| 39 | + |
| 40 | + // Symbol |
| 41 | + { |
| 42 | + code: "var foo = new Symbol('foo');", |
| 43 | + errors: [{ |
| 44 | + message: "`Symbol` cannot be called as a constructor." |
| 45 | + }] |
| 46 | + }, |
| 47 | + { |
| 48 | + code: "function bar() { return function Symbol() {}; } var baz = new Symbol('baz');", |
| 49 | + errors: [{ |
| 50 | + message: "`Symbol` cannot be called as a constructor." |
| 51 | + }] |
| 52 | + }, |
| 53 | + |
| 54 | + // BigInt |
| 55 | + { |
| 56 | + code: "var foo = new BigInt(9007199254740991);", |
| 57 | + errors: [{ |
| 58 | + message: "`BigInt` cannot be called as a constructor." |
| 59 | + }] |
| 60 | + }, |
| 61 | + { |
| 62 | + code: "function bar() { return function BigInt() {}; } var baz = new BigInt(9007199254740991);", |
| 63 | + errors: [{ |
| 64 | + message: "`BigInt` cannot be called as a constructor." |
| 65 | + }] |
| 66 | + } |
| 67 | + ] |
| 68 | +}); |
0 commit comments