@@ -244,43 +244,52 @@ namespace ts.refactor.extractSymbol {
244244 return { targetRange : { range : statements , facts : rangeFacts , declarations } } ;
245245 }
246246
247- // We have a single node (start)
248- let node = start ;
247+ if ( isReturnStatement ( start ) && ! start . expression ) {
248+ // Makes no sense to extract an expression-less return statement.
249+ return { errors : [ createFileDiagnostic ( sourceFile , span . start , length , Messages . CannotExtractRange ) ] } ;
250+ }
249251
250- if ( isReturnStatement ( start ) ) {
251- if ( ! start . expression ) {
252- // Makes no sense to extract an expression-less return statement.
253- return { errors : [ createFileDiagnostic ( sourceFile , span . start , length , Messages . CannotExtractRange ) ] } ;
254- }
252+ // We have a single node (start)
253+ const node = refineNode ( start ) ;
255254
256- node = start . expression ;
255+ const errors = checkRootNode ( node ) || checkNode ( node ) ;
256+ if ( errors ) {
257+ return { errors } ;
257258 }
258- else if ( isVariableStatement ( start ) ) {
259- let numInitializers = 0 ;
260- let lastInitializer : Expression | undefined = undefined ;
261- for ( const declaration of start . declarationList . declarations ) {
262- if ( declaration . initializer ) {
263- numInitializers ++ ;
264- lastInitializer = declaration . initializer ;
259+ return { targetRange : { range : getStatementOrExpressionRange ( node ) , facts : rangeFacts , declarations } } ;
260+
261+ /**
262+ * Attempt to refine the extraction node (generally, by shrinking it) to produce better results.
263+ * @param node The unrefined extraction node.
264+ */
265+ function refineNode ( node : Node ) {
266+ if ( isReturnStatement ( node ) ) {
267+ if ( node . expression ) {
268+ return node . expression ;
265269 }
266270 }
267-
268- if ( numInitializers === 1 ) {
269- node = lastInitializer ;
271+ else if ( isVariableStatement ( node ) ) {
272+ let numInitializers = 0 ;
273+ let lastInitializer : Expression | undefined = undefined ;
274+ for ( const declaration of node . declarationList . declarations ) {
275+ if ( declaration . initializer ) {
276+ numInitializers ++ ;
277+ lastInitializer = declaration . initializer ;
278+ }
279+ }
280+ if ( numInitializers === 1 ) {
281+ return lastInitializer ;
282+ }
283+ // No special handling if there are multiple initializers.
270284 }
271- // No special handling if there are multiple initializers.
272- }
273- else if ( isVariableDeclaration ( node ) ) {
274- if ( node . initializer ) {
275- node = node . initializer ;
285+ else if ( isVariableDeclaration ( node ) ) {
286+ if ( node . initializer ) {
287+ return node . initializer ;
288+ }
276289 }
277- }
278290
279- const errors = checkRootNode ( node ) || checkNode ( node ) ;
280- if ( errors ) {
281- return { errors } ;
291+ return node ;
282292 }
283- return { targetRange : { range : getStatementOrExpressionRange ( node ) , facts : rangeFacts , declarations } } ;
284293
285294 function checkRootNode ( node : Node ) : Diagnostic [ ] | undefined {
286295 if ( isIdentifier ( isExpressionStatement ( node ) ? node . expression : node ) ) {
0 commit comments