Skip to content

Commit df218a2

Browse files
authored
Improve consistent-function-scoping report location (#778)
1 parent f7127da commit df218a2

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

rules/consistent-function-scoping.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const {getFunctionNameWithKind} = require('eslint-utils');
2+
const {getFunctionHeadLocation, getFunctionNameWithKind} = require('eslint-utils');
33
const getDocumentationUrl = require('./utils/get-documentation-url');
44
const getReferences = require('./utils/get-references');
55

@@ -166,6 +166,7 @@ const create = context => {
166166
if (!hasJsx && !checkNode(node, scopeManager)) {
167167
context.report({
168168
node,
169+
loc: getFunctionHeadLocation(node, sourceCode),
169170
messageId: MESSAGE_ID,
170171
data: {
171172
functionNameWithKind: getFunctionNameWithKind(node)

test/consistent-function-scoping.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ const typescriptRuleTester = avaRuleTester(test, {
1919

2020
const MESSAGE_ID = 'consistent-function-scoping';
2121

22-
const createError = functionNameWithKind => ({
22+
const createError = (functionNameWithKind, loc) => ({
2323
messageId: MESSAGE_ID,
2424
data: {
2525
functionNameWithKind
26-
}
26+
},
27+
...loc
2728
});
2829

2930
ruleTester.run('consistent-function-scoping', rule, {
@@ -488,34 +489,34 @@ ruleTester.run('consistent-function-scoping', rule, {
488489
`,
489490
errors: [createError('function \'doBar\'')]
490491
},
491-
// Function kinds and names
492+
// Function kinds and names, loc
492493
{
493494
code: 'function foo() { function bar() {} }',
494-
errors: [createError('function \'bar\'')]
495+
errors: [createError('function \'bar\'', {line: 1, column: 18, endLine: 1, endColumn: 30})]
495496
},
496497
{
497498
code: 'function foo() { async function bar() {} }',
498-
errors: [createError('async function \'bar\'')]
499+
errors: [createError('async function \'bar\'', {line: 1, column: 18, endLine: 1, endColumn: 36})]
499500
},
500501
{
501502
code: 'function foo() { function* bar() {} }',
502-
errors: [createError('generator function \'bar\'')]
503+
errors: [createError('generator function \'bar\'', {line: 1, column: 18, endLine: 1, endColumn: 31})]
503504
},
504505
{
505506
code: 'function foo() { async function* bar() {} }',
506-
errors: [createError('async generator function \'bar\'')]
507+
errors: [createError('async generator function \'bar\'', {line: 1, column: 18, endLine: 1, endColumn: 37})]
507508
},
508509
{
509510
code: 'function foo() { const bar = () => {} }',
510-
errors: [createError('arrow function \'bar\'')]
511+
errors: [createError('arrow function \'bar\'', {line: 1, column: 33, endLine: 1, endColumn: 35})]
511512
},
512513
{
513514
code: 'const doFoo = () => bar => bar;',
514-
errors: [createError('arrow function')]
515+
errors: [createError('arrow function', {line: 1, column: 25, endLine: 1, endColumn: 27})]
515516
},
516517
{
517518
code: 'function foo() { const bar = async () => {} }',
518-
errors: [createError('async arrow function \'bar\'')]
519+
errors: [createError('async arrow function \'bar\'', {line: 1, column: 39, endLine: 1, endColumn: 41})]
519520
},
520521
// Actual message
521522
{

0 commit comments

Comments
 (0)