@@ -93,6 +93,7 @@ module.exports = {
9393 result . defStart = node . range [ 0 ] ;
9494 result . defEnd = node . body . range [ 0 ] ;
9595 result . async = node . async || false ;
96+ result . generator = node . generator || false ;
9697 result . body = node . body ;
9798 result . returnType = node . returnType ? sourceCode . getText ( node . returnType ) . replace ( PREFIX_COLON , "" ) : null ;
9899 result . typeParameters = node . typeParameters && node . typeParameters . params && node . typeParameters . params . length ? node . typeParameters . params . map ( t => sourceCode . getText ( t ) ) . join ( ", " ) : null ;
@@ -103,6 +104,7 @@ module.exports = {
103104 result . defStart = parent . range [ 0 ] ;
104105 result . defEnd = node . init . body . range [ 0 ] ;
105106 result . async = node . init . async || false ;
107+ result . generator = node . init . generator || false ;
106108 result . typeAnnotation = node . id . typeAnnotation ? sourceCode . getText ( node . id . typeAnnotation ) . replace ( PREFIX_COLON , "" ) : null ;
107109 result . returnType = node . init . returnType ? sourceCode . getText ( node . init . returnType ) . replace ( PREFIX_COLON , "" ) : null ;
108110 result . typeParameters = node . init . typeParameters && node . init . typeParameters . params && node . init . typeParameters . params . length ? node . init . typeParameters . params . map ( t => sourceCode . getText ( t ) ) . join ( ", " ) : null ;
@@ -121,17 +123,19 @@ module.exports = {
121123 /**
122124 * Generate modified text from analysis data
123125 * @param {Object } data analysis data
124- * @param {string } to convert to
126+ * @param {string } config convert to
125127 * @returns {string } fixed text
126128 */
127- function generateFixedText ( data , to ) {
129+ function generateFixedText ( data , config ) {
130+ const to = data . generator && config !== "declaration" ? "expression" : config ;
131+
128132 switch ( to ) {
129133 case "declaration" :
130- return `${ data . async ? "async " : "" } function${ data . name ? ` ${ data . name } ` : "" } ${ data . typeParameters ? `<${ data . typeParameters } >` : "" } (${ data . params ? data . params : "" } )${ data . returnType ? `: ${ data . returnType } ` : "" } ${ data . body . type !== "BlockStatement" ? `{return ${ data . bodyText } }` : "" } ` ;
134+ return `${ data . async ? "async " : "" } function${ data . generator ? "* " : "" } ${ data . name ? ` ${ data . name } ` : "" } ${ data . typeParameters ? `<${ data . typeParameters } >` : "" } (${ data . params ? data . params : "" } )${ data . returnType ? `: ${ data . returnType } ` : "" } ${ data . body . type !== "BlockStatement" ? `{return ${ data . bodyText } }` : "" } ` ;
131135 case "expression" :
132- return `${ data . kind } ${ data . name } ${ data . typeAnnotation ? `: ${ data . typeAnnotation } ` : "" } = ${ data . async ? "async " : "" } function${ data . typeParameters ? `<${ data . typeParameters } ${ IS_GENERIC_PARSEABLE . test ( data . typeParameters ) ? "" : "," } >` : "" } (${ data . params ? data . params : "" } )${ data . returnType ? `: ${ data . returnType } ` : "" } ${ data . body . type !== "BlockStatement" ? `{return ${ data . bodyText } }` : "" } ` ;
136+ return `${ data . kind } ${ data . name } ${ data . typeAnnotation ? `: ${ data . typeAnnotation } ` : "" } = ${ data . async ? "async " : "" } function${ data . generator ? "* " : "" } ${ data . typeParameters ? `<${ data . typeParameters } ${ IS_GENERIC_PARSEABLE . test ( data . typeParameters ) ? "" : "," } >` : "" } (${ data . params ? data . params : "" } )${ data . returnType ? `: ${ data . returnType } ` : "" } ${ data . body . type !== "BlockStatement" ? `{return ${ data . bodyText } }` : "" } ` ;
133137 case "arrow" :
134- return `${ data . kind } ${ data . name } ${ data . typeAnnotation ? `: ${ data . typeAnnotation } ` : "" } = ${ data . async ? "async " : "" } ${ data . typeParameters ? `<${ data . typeParameters } ${ IS_GENERIC_PARSEABLE . test ( data . typeParameters ) ? "" : "," } >` : "" } (${ data . params ? data . params : "" } )${ data . returnType ? `: ${ data . returnType } ` : "" } => ${ data . body . type !== "BlockStatement" ? data . bodyText : "" } ` ;
138+ return `${ data . kind } ${ data . name } ${ data . typeAnnotation ? `: ${ data . typeAnnotation } ` : "" } = ${ data . async ? "async " : "" } ${ data . generator ? "* " : "" } ${ data . typeParameters ? `<${ data . typeParameters } ${ IS_GENERIC_PARSEABLE . test ( data . typeParameters ) ? "" : "," } >` : "" } (${ data . params ? data . params : "" } )${ data . returnType ? `: ${ data . returnType } ` : "" } => ${ data . body . type !== "BlockStatement" ? data . bodyText : "" } ` ;
135139 default :
136140 return null ;
137141 }
@@ -179,12 +183,13 @@ module.exports = {
179183 FunctionExpression ( node ) {
180184 stack . push ( false ) ;
181185 const isTopLevel = stack . length === 1 ;
186+ const isGenerator = node . generator || false ;
182187
183- if ( ! isTopLevel && ! enforceExpressions && node . parent . type === "VariableDeclarator" ) {
188+ if ( ! isTopLevel && ! enforceExpressions && node . parent . type === "VariableDeclarator" && ( ! enforceArrows || ! isGenerator ) ) {
184189 report ( { node : node . parent , messageId : style , isTopLevel } ) ;
185190 }
186191
187- if ( isTopLevel && ! enforceExpressionTopLevel && node . parent . type === "VariableDeclarator" ) {
192+ if ( isTopLevel && ! enforceExpressionTopLevel && node . parent . type === "VariableDeclarator" && ( ! enforceArrowTopLevel || ! isGenerator ) ) {
188193
189194 report ( { node : node . parent , messageId : `${ topLevelStyle } -in-top-level` , isTopLevel } ) ;
190195 }
0 commit comments