Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: Improve report location for new-cap (refs #12334) (#13136)
  • Loading branch information
mdjermanovic committed Apr 6, 2020
1 parent bfa811c commit af4472f
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/rules/new-cap.js
Expand Up @@ -235,7 +235,7 @@ module.exports = {
callee = callee.property;
}

context.report({ node, loc: callee.loc.start, messageId });
context.report({ node, loc: callee.loc, messageId });
}

//--------------------------------------------------------------------------
Expand Down
170 changes: 156 additions & 14 deletions tests/lib/rules/new-cap.js
Expand Up @@ -74,23 +74,115 @@ ruleTester.run("new-cap", rule, {
{ code: "var x = foo.Bar(42);", options: [{ capIsNew: false, properties: false }] }
],
invalid: [
{ code: "var x = new c();", errors: [{ messageId: "lower", type: "NewExpression" }] },
{ code: "var x = new φ;", errors: [{ messageId: "lower", type: "NewExpression" }] },
{ code: "var x = new a.b.c;", errors: [{ messageId: "lower", type: "NewExpression" }] },
{ code: "var x = new a.b['c'];", errors: [{ messageId: "lower", type: "NewExpression" }] },
{ code: "var b = Foo();", errors: [{ messageId: "upper", type: "CallExpression" }] },
{ code: "var b = a.Foo();", errors: [{ messageId: "upper", type: "CallExpression" }] },
{ code: "var b = a['Foo']();", errors: [{ messageId: "upper", type: "CallExpression" }] },
{ code: "var b = a.Date.UTC();", errors: [{ messageId: "upper", type: "CallExpression" }] },
{ code: "var b = UTC();", errors: [{ messageId: "upper", type: "CallExpression" }] },
{
code: "var x = new c();",
errors: [{
messageId: "lower",
type: "NewExpression",
line: 1,
column: 13,
endLine: 1,
endColumn: 14
}]
},
{
code: "var x = new φ;",
errors: [{
messageId: "lower",
type: "NewExpression",
line: 1,
column: 13,
endLine: 1,
endColumn: 14
}]
},
{
code: "var x = new a.b.c;",
errors: [{
messageId: "lower",
type: "NewExpression",
line: 1,
column: 17,
endLine: 1,
endColumn: 18
}]
},
{
code: "var x = new a.b['c'];",
errors: [{
messageId: "lower",
type: "NewExpression",
line: 1,
column: 17,
endLine: 1,
endColumn: 20
}]
},
{
code: "var b = Foo();",
errors: [{
messageId: "upper",
type: "CallExpression",
line: 1,
column: 9,
endLine: 1,
endColumn: 12
}]
},
{
code: "var b = a.Foo();",
errors: [{
messageId: "upper",
type: "CallExpression",
line: 1,
column: 11,
endLine: 1,
endColumn: 14
}]
},
{
code: "var b = a['Foo']();",
errors: [{
messageId: "upper",
type: "CallExpression",
line: 1,
column: 11,
endLine: 1,
endColumn: 16
}]
},
{
code: "var b = a.Date.UTC();",
errors: [{
messageId: "upper",
type: "CallExpression",
line: 1,
column: 16,
endLine: 1,
endColumn: 19
}]
},
{
code: "var b = UTC();",
errors: [{
messageId: "upper",
type: "CallExpression",
line: 1,
column: 9,
endLine: 1,
endColumn: 12
}]
},
{
code: "var a = B.C();",
errors: [
{
messageId: "upper",
type: "CallExpression",
line: 1,
column: 11
column: 11,
endLine: 1,
endColumn: 12
}
]
},
Expand All @@ -101,7 +193,9 @@ ruleTester.run("new-cap", rule, {
messageId: "upper",
type: "CallExpression",
line: 2,
column: 2
column: 2,
endLine: 2,
endColumn: 3
}
]
},
Expand All @@ -112,7 +206,9 @@ ruleTester.run("new-cap", rule, {
messageId: "lower",
type: "NewExpression",
line: 1,
column: 15
column: 15,
endLine: 1,
endColumn: 16
}
]
},
Expand All @@ -123,7 +219,9 @@ ruleTester.run("new-cap", rule, {
messageId: "lower",
type: "NewExpression",
line: 2,
column: 1
column: 1,
endLine: 2,
endColumn: 2
}
]
},
Expand All @@ -134,7 +232,51 @@ ruleTester.run("new-cap", rule, {
messageId: "lower",
type: "NewExpression",
line: 1,
column: 13
column: 13,
endLine: 1,
endColumn: 14
}
]
},
{
code: "var a = new b[ ( 'foo' ) ]();",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "lower",
type: "NewExpression",
line: 1,
column: 18,
endLine: 1,
endColumn: 23
}
]
},
{
code: "var a = new b[`foo`];",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "lower",
type: "NewExpression",
line: 1,
column: 15,
endLine: 1,
endColumn: 20
}
]
},
{
code: "var a = b[`\\\nFoo`]();",
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "upper",
type: "CallExpression",
line: 1,
column: 11,
endLine: 2,
endColumn: 5
}
]
},
Expand Down

0 comments on commit af4472f

Please sign in to comment.