@@ -17,22 +17,35 @@ function create(context) {
1717 return ;
1818 }
1919
20- if ( ! ( node . init && ( node . init . type === 'ArrowFunctionExpression' || node . init . type === 'FunctionExpression' ) ) ) {
21- return ;
20+ const sourceCode = context . getSourceCode ( ) ;
21+
22+ if ( node . init && node . init . type === 'ArrowFunctionExpression' ) {
23+ const functionName = node . id . name ;
24+ const functionText = sourceCode . getText ( node . init ) ;
25+
26+ context . report ( {
27+ node : node . init ,
28+ message : 'Top-level functions must be named/regular functions.' ,
29+ fix ( fixer ) {
30+ const fixedCode = `function ${ functionName } ${ functionText . slice ( functionText . indexOf ( '(' ) ) } ` ;
31+ return fixer . replaceText ( node . parent , fixedCode ) ;
32+ } ,
33+ } ) ;
2234 }
2335
24- const sourceCode = context . getSourceCode ( ) ;
25- const functionText = sourceCode . getText ( node . init ) ;
26- const functionName = node . id . name ;
36+ if ( node . init && node . init . type === 'FunctionExpression' ) {
37+ const functionName = node . id . name ;
38+ const functionText = sourceCode . getText ( node . init ) ;
2739
28- context . report ( {
29- node : node . init ,
30- message : 'Top-level functions must be named/regular functions.' ,
31- fix ( fixer ) {
32- const fixedCode = `function ${ functionName } ${ functionText . slice ( functionText . indexOf ( '(' ) ) } ` ;
33- return fixer . replaceText ( node , fixedCode ) ;
34- } ,
35- } ) ;
40+ context . report ( {
41+ node : node . init ,
42+ message : 'Top-level functions must be named/regular functions.' ,
43+ fix ( fixer ) {
44+ const fixedCode = `function ${ functionName } ${ functionText . slice ( functionText . indexOf ( '(' ) ) } ` ;
45+ return fixer . replaceText ( node . parent , fixedCode ) ;
46+ } ,
47+ } ) ;
48+ }
3649 } ,
3750
3851 FunctionDeclaration ( node ) {
@@ -48,6 +61,7 @@ function create(context) {
4861 const functionName = 'defaultFunction' ;
4962 const sourceCode = context . getSourceCode ( ) ;
5063 const functionText = sourceCode . getText ( node ) ;
64+
5165 const fixedCode = functionText . replace ( 'function (' , `function ${ functionName } (` ) ;
5266
5367 return fixer . replaceText ( node , fixedCode ) ;
0 commit comments