@@ -141,6 +141,7 @@ namespace ts.refactor.extractSymbol {
141
141
export const CannotExtractAmbientBlock = createMessage ( "Cannot extract code from ambient contexts" ) ;
142
142
export const CannotAccessVariablesFromNestedScopes = createMessage ( "Cannot access variables from nested scopes" ) ;
143
143
export const CannotExtractToOtherFunctionLike = createMessage ( "Cannot extract method to a function-like scope that is not a function" ) ;
144
+ export const CannotExtractToJSClass = createMessage ( "Cannot extract constant to a class scope in JS" ) ;
144
145
}
145
146
146
147
enum RangeFacts {
@@ -866,21 +867,17 @@ namespace ts.refactor.extractSymbol {
866
867
const changeTracker = textChanges . ChangeTracker . fromContext ( context ) ;
867
868
868
869
if ( isClassLike ( scope ) ) {
869
- // always create private method in TypeScript files
870
+ Debug . assert ( ! isJS ) ; // See CannotExtractToJSClass
870
871
const modifiers : Modifier [ ] = [ ] ;
871
- if ( ! isJS ) {
872
- modifiers . push ( createToken ( SyntaxKind . PrivateKeyword ) ) ;
873
- }
872
+ modifiers . push ( createToken ( SyntaxKind . PrivateKeyword ) ) ;
874
873
if ( rangeFacts & RangeFacts . InStaticRegion ) {
875
874
modifiers . push ( createToken ( SyntaxKind . StaticKeyword ) ) ;
876
875
}
877
- if ( ! isJS ) {
878
- modifiers . push ( createToken ( SyntaxKind . ReadonlyKeyword ) ) ;
879
- }
876
+ modifiers . push ( createToken ( SyntaxKind . ReadonlyKeyword ) ) ;
880
877
881
878
const newVariable = createProperty (
882
879
/*decorators*/ undefined ,
883
- modifiers . length ? modifiers : undefined ,
880
+ modifiers ,
884
881
localNameText ,
885
882
/*questionToken*/ undefined ,
886
883
variableType ,
@@ -1195,20 +1192,29 @@ namespace ts.refactor.extractSymbol {
1195
1192
const constantErrorsPerScope : Diagnostic [ ] [ ] = [ ] ;
1196
1193
const visibleDeclarationsInExtractedRange : Symbol [ ] = [ ] ;
1197
1194
1198
- const expressionDiagnostics =
1195
+ const expressionDiagnostic =
1199
1196
isReadonlyArray ( targetRange . range ) && ! ( targetRange . range . length === 1 && isExpressionStatement ( targetRange . range [ 0 ] ) )
1200
- ? ( ( start , end ) => [ createFileDiagnostic ( sourceFile , start , end - start , Messages . ExpressionExpected ) ] ) ( firstOrUndefined ( targetRange . range ) . getStart ( ) , lastOrUndefined ( targetRange . range ) . end )
1201
- : [ ] ;
1197
+ ? ( ( start , end ) => createFileDiagnostic ( sourceFile , start , end - start , Messages . ExpressionExpected ) ) ( firstOrUndefined ( targetRange . range ) . getStart ( ) , lastOrUndefined ( targetRange . range ) . end )
1198
+ : undefined ;
1202
1199
1203
1200
// initialize results
1204
1201
for ( const scope of scopes ) {
1205
1202
usagesPerScope . push ( { usages : createMap < UsageEntry > ( ) , typeParameterUsages : createMap < TypeParameter > ( ) , substitutions : createMap < Expression > ( ) } ) ;
1206
1203
substitutionsPerScope . push ( createMap < Expression > ( ) ) ;
1204
+
1207
1205
functionErrorsPerScope . push (
1208
1206
isFunctionLikeDeclaration ( scope ) && scope . kind !== SyntaxKind . FunctionDeclaration
1209
1207
? [ createDiagnosticForNode ( scope , Messages . CannotExtractToOtherFunctionLike ) ]
1210
1208
: [ ] ) ;
1211
- constantErrorsPerScope . push ( expressionDiagnostics ) ;
1209
+
1210
+ const constantErrors = [ ] ;
1211
+ if ( expressionDiagnostic ) {
1212
+ constantErrors . push ( expressionDiagnostic ) ;
1213
+ }
1214
+ if ( isClassLike ( scope ) && isInJavaScriptFile ( scope ) ) {
1215
+ constantErrors . push ( createDiagnosticForNode ( scope , Messages . CannotExtractToJSClass ) ) ;
1216
+ }
1217
+ constantErrorsPerScope . push ( constantErrors ) ;
1212
1218
}
1213
1219
1214
1220
const seenUsages = createMap < Usage > ( ) ;
0 commit comments