Skip to content

Commit

Permalink
Improve consistent-function-scoping report location (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jun 21, 2020
1 parent f7127da commit df218a2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
3 changes: 2 additions & 1 deletion rules/consistent-function-scoping.js
@@ -1,5 +1,5 @@
'use strict';
const {getFunctionNameWithKind} = require('eslint-utils');
const {getFunctionHeadLocation, getFunctionNameWithKind} = require('eslint-utils');
const getDocumentationUrl = require('./utils/get-documentation-url');
const getReferences = require('./utils/get-references');

Expand Down Expand Up @@ -166,6 +166,7 @@ const create = context => {
if (!hasJsx && !checkNode(node, scopeManager)) {
context.report({
node,
loc: getFunctionHeadLocation(node, sourceCode),
messageId: MESSAGE_ID,
data: {
functionNameWithKind: getFunctionNameWithKind(node)
Expand Down
21 changes: 11 additions & 10 deletions test/consistent-function-scoping.js
Expand Up @@ -19,11 +19,12 @@ const typescriptRuleTester = avaRuleTester(test, {

const MESSAGE_ID = 'consistent-function-scoping';

const createError = functionNameWithKind => ({
const createError = (functionNameWithKind, loc) => ({
messageId: MESSAGE_ID,
data: {
functionNameWithKind
}
},
...loc
});

ruleTester.run('consistent-function-scoping', rule, {
Expand Down Expand Up @@ -488,34 +489,34 @@ ruleTester.run('consistent-function-scoping', rule, {
`,
errors: [createError('function \'doBar\'')]
},
// Function kinds and names
// Function kinds and names, loc
{
code: 'function foo() { function bar() {} }',
errors: [createError('function \'bar\'')]
errors: [createError('function \'bar\'', {line: 1, column: 18, endLine: 1, endColumn: 30})]
},
{
code: 'function foo() { async function bar() {} }',
errors: [createError('async function \'bar\'')]
errors: [createError('async function \'bar\'', {line: 1, column: 18, endLine: 1, endColumn: 36})]
},
{
code: 'function foo() { function* bar() {} }',
errors: [createError('generator function \'bar\'')]
errors: [createError('generator function \'bar\'', {line: 1, column: 18, endLine: 1, endColumn: 31})]
},
{
code: 'function foo() { async function* bar() {} }',
errors: [createError('async generator function \'bar\'')]
errors: [createError('async generator function \'bar\'', {line: 1, column: 18, endLine: 1, endColumn: 37})]
},
{
code: 'function foo() { const bar = () => {} }',
errors: [createError('arrow function \'bar\'')]
errors: [createError('arrow function \'bar\'', {line: 1, column: 33, endLine: 1, endColumn: 35})]
},
{
code: 'const doFoo = () => bar => bar;',
errors: [createError('arrow function')]
errors: [createError('arrow function', {line: 1, column: 25, endLine: 1, endColumn: 27})]
},
{
code: 'function foo() { const bar = async () => {} }',
errors: [createError('async arrow function \'bar\'')]
errors: [createError('async arrow function \'bar\'', {line: 1, column: 39, endLine: 1, endColumn: 41})]
},
// Actual message
{
Expand Down

0 comments on commit df218a2

Please sign in to comment.