Refresh new-cap tests

This commit is contained in:
Brian Ng 2018-04-18 20:33:36 -05:00
parent 5b5d9a5dec
commit ae68b5bf24

View File

@ -37,36 +37,46 @@ ruleTester.run('babel/new-cap', rule, {
"var x = Symbol('symbol')", "var x = Symbol('symbol')",
"var x = _();", "var x = _();",
"var x = $();", "var x = $();",
{ code: "var x = Foo(42)", options: [{"capIsNew": false}] }, { code: "var x = Foo(42)", options: [{ capIsNew: false }] },
{ code: "var x = bar.Foo(42)", options: [{"capIsNew": false}] }, { code: "var x = bar.Foo(42)", options: [{ capIsNew: false }] },
{ code: "var x = Foo.bar(42)", options: [{ capIsNew: false }] },
"var x = bar[Foo](42)", "var x = bar[Foo](42)",
{code: "var x = bar['Foo'](42)", options: [{"capIsNew": false}] }, { code: "var x = bar['Foo'](42)", options: [{ capIsNew: false }] },
"var x = Foo.bar(42)", "var x = Foo.bar(42)",
{ code: "var x = new foo(42)", options: [{"newIsCap": false}] }, { code: "var x = new foo(42)", options: [{ newIsCap: false }] },
"var o = { 1: function() {} }; o[1]();", "var o = { 1: function() {} }; o[1]();",
"var o = { 1: function() {} }; new o[1]();", "var o = { 1: function() {} }; new o[1]();",
{ code: "var x = Foo(42);", options: [{ capIsNew: true, capIsNewExceptions: ["Foo"] }] }, { code: "var x = Foo(42);", options: [{ capIsNew: true, capIsNewExceptions: ["Foo"] }] },
{ code: "var x = Foo(42);", options: [{ capIsNewExceptionPattern: "^Foo" }] },
{ code: "var x = new foo(42);", options: [{ newIsCap: true, newIsCapExceptions: ["foo"] }] }, { code: "var x = new foo(42);", options: [{ newIsCap: true, newIsCapExceptions: ["foo"] }] },
{ code: "var x = new foo(42);", options: [{ newIsCapExceptionPattern: "^foo" }] },
{ code: "var x = Object(42);", options: [{ capIsNewExceptions: ["Foo"] }] }, { code: "var x = Object(42);", options: [{ capIsNewExceptions: ["Foo"] }] },
{ code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Bar"] }] }, { code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Bar"] }] },
{ code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Foo.Bar"] }] }, { code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Foo.Bar"] }] },
{ code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptionPattern: "^Foo\\.." }] },
{ code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["bar"] }] }, { code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["bar"] }] },
{ code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["foo.bar"] }] }, { code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["foo.bar"] }] },
{ code: "var x = new foo.bar(42);", options: [{ newIsCapExceptionPattern: "^foo\\.." }] },
{ code: "var x = new foo.bar(42);", options: [{ properties: false }] },
{ code: "var x = Foo.bar(42);", options: [{ properties: false }] },
{ code: "var x = foo.Bar(42);", options: [{ capIsNew: false, properties: false }] },
// Babel-specific test cases. // Babel-specific test cases.
{ code: "@MyDecorator(123) class MyClass{}", parser: "babel-eslint" }, { code: "@MyDecorator(123) class MyClass{}", parser: "babel-eslint" },
], ],
invalid: [ invalid: [
{ code: "var x = new c();", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression"}] }, { code: "var x = new c();", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] },
{ code: "var x = new φ;", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression"}] }, { code: "var x = new φ;", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] },
{ code: "var x = new a.b.c;", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression"}] }, { code: "var x = new a.b.c;", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] },
{ code: "var x = new a.b['c'];", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression"}] }, { code: "var x = new a.b['c'];", errors: [{ message: "A constructor name should not start with a lowercase letter.", type: "NewExpression" }] },
{ 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"}] }, { 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" }] },
{ 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"}] }, { 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" }] },
{ 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"}] }, { 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" }] },
{ 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"}] }, { 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" }] },
{ 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"}] }, { 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" }] },
{ {
code: "var a = B.C();", code: "var a = B.C();",
errors: [ errors: [
@ -125,13 +135,25 @@ ruleTester.run('babel/new-cap', rule, {
{ {
code: "var x = Foo.Bar(42);", code: "var x = Foo.Bar(42);",
options: [{capIsNewExceptions: ["Foo"]}], options: [{ capIsNewExceptions: ["Foo"] }],
errors: [{type: "CallExpression", message: "A function with a name starting with an uppercase letter should only be used as a constructor."}] errors: [{ type: "CallExpression", message: "A function with a name starting with an uppercase letter should only be used as a constructor." }]
},
{
code: "var x = Bar.Foo(42);",
options: [{ capIsNewExceptionPattern: "^Foo\\.." }],
errors: [{ type: "CallExpression", message: "A function with a name starting with an uppercase letter should only be used as a constructor." }]
}, },
{ {
code: "var x = new foo.bar(42);", code: "var x = new foo.bar(42);",
options: [{newIsCapExceptions: ["foo"]}], options: [{ newIsCapExceptions: ["foo"] }],
errors: [{type: "NewExpression", message: "A constructor name should not start with a lowercase letter."}] errors: [{ type: "NewExpression", message: "A constructor name should not start with a lowercase letter." }]
},
{
code: "var x = new bar.foo(42);",
options: [{ newIsCapExceptionPattern: "^foo\\.." }],
errors: [{ type: "NewExpression", message: "A constructor name should not start with a lowercase letter." }]
} }
] ]
}); });