1
1
/* @internal */
2
2
namespace ts {
3
- export function computeSuggestionDiagnostics ( sourceFile : SourceFile , program : Program ) : DiagnosticWithLocation [ ] {
4
- program . getSemanticDiagnostics ( sourceFile ) ;
5
- const checker = program . getDiagnosticsProducingTypeChecker ( ) ;
3
+ export function computeSuggestionDiagnostics ( sourceFile : SourceFile , program : Program , cancellationToken : CancellationToken ) : DiagnosticWithLocation [ ] {
4
+ program . getSemanticDiagnostics ( sourceFile , cancellationToken ) ;
6
5
const diags : DiagnosticWithLocation [ ] = [ ] ;
7
6
8
7
if ( sourceFile . commonJsModuleIndicator &&
@@ -13,6 +12,25 @@ namespace ts {
13
12
14
13
const isJsFile = isSourceFileJavaScript ( sourceFile ) ;
15
14
15
+ check ( sourceFile ) ;
16
+
17
+ if ( getAllowSyntheticDefaultImports ( program . getCompilerOptions ( ) ) ) {
18
+ for ( const moduleSpecifier of sourceFile . imports ) {
19
+ const importNode = importFromModuleSpecifier ( moduleSpecifier ) ;
20
+ const name = importNameForConvertToDefaultImport ( importNode ) ;
21
+ if ( ! name ) continue ;
22
+ const module = getResolvedModule ( sourceFile , moduleSpecifier . text ) ;
23
+ const resolvedFile = module && program . getSourceFile ( module . resolvedFileName ) ;
24
+ if ( resolvedFile && resolvedFile . externalModuleIndicator && isExportAssignment ( resolvedFile . externalModuleIndicator ) && resolvedFile . externalModuleIndicator . isExportEquals ) {
25
+ diags . push ( createDiagnosticForNode ( name , Diagnostics . Import_may_be_converted_to_a_default_import ) ) ;
26
+ }
27
+ }
28
+ }
29
+
30
+ addRange ( diags , sourceFile . bindSuggestionDiagnostics ) ;
31
+ addRange ( diags , program . getSuggestionDiagnostics ( sourceFile , cancellationToken ) ) ;
32
+ return diags . sort ( ( d1 , d2 ) => d1 . start - d2 . start ) ;
33
+
16
34
function check ( node : Node ) {
17
35
if ( isJsFile ) {
18
36
switch ( node . kind ) {
@@ -25,52 +43,33 @@ namespace ts {
25
43
break ;
26
44
}
27
45
}
28
- // falls through if no diagnostic was created
46
+ // falls through if no diagnostic was created
29
47
case SyntaxKind . FunctionDeclaration :
30
48
const symbol = node . symbol ;
31
49
if ( symbol . members && ( symbol . members . size > 0 ) ) {
32
50
diags . push ( createDiagnosticForNode ( isVariableDeclaration ( node . parent ) ? node . parent . name : node , Diagnostics . This_constructor_function_may_be_converted_to_a_class_declaration ) ) ;
33
51
}
34
52
break ;
35
- }
36
- }
37
-
38
- if ( ! isJsFile && codefix . parameterShouldGetTypeFromJSDoc ( node ) ) {
39
- diags . push ( createDiagnosticForNode ( node . name || node , Diagnostics . JSDoc_types_may_be_moved_to_TypeScript_types ) ) ;
53
+ }
40
54
}
41
-
42
- node . forEachChild ( check ) ;
43
- }
44
- check ( sourceFile ) ;
45
-
46
- if ( ! isJsFile ) {
47
- for ( const statement of sourceFile . statements ) {
48
- if ( isVariableStatement ( statement ) &&
49
- statement . declarationList . flags & NodeFlags . Const &&
50
- statement . declarationList . declarations . length === 1 ) {
51
- const init = statement . declarationList . declarations [ 0 ] . initializer ;
55
+ else {
56
+ if ( isVariableStatement ( node ) &&
57
+ node . parent === sourceFile &&
58
+ node . declarationList . flags & NodeFlags . Const &&
59
+ node . declarationList . declarations . length === 1 ) {
60
+ const init = node . declarationList . declarations [ 0 ] . initializer ;
52
61
if ( init && isRequireCall ( init , /*checkArgumentIsStringLiteralLike*/ true ) ) {
53
62
diags . push ( createDiagnosticForNode ( init , Diagnostics . require_call_may_be_converted_to_an_import ) ) ;
54
63
}
55
64
}
56
- }
57
- }
58
65
59
- if ( getAllowSyntheticDefaultImports ( program . getCompilerOptions ( ) ) ) {
60
- for ( const moduleSpecifier of sourceFile . imports ) {
61
- const importNode = importFromModuleSpecifier ( moduleSpecifier ) ;
62
- const name = importNameForConvertToDefaultImport ( importNode ) ;
63
- if ( ! name ) continue ;
64
- const module = getResolvedModule ( sourceFile , moduleSpecifier . text ) ;
65
- const resolvedFile = module && program . getSourceFile ( module . resolvedFileName ) ;
66
- if ( resolvedFile && resolvedFile . externalModuleIndicator && isExportAssignment ( resolvedFile . externalModuleIndicator ) && resolvedFile . externalModuleIndicator . isExportEquals ) {
67
- diags . push ( createDiagnosticForNode ( name , Diagnostics . Import_may_be_converted_to_a_default_import ) ) ;
66
+ if ( codefix . parameterShouldGetTypeFromJSDoc ( node ) ) {
67
+ diags . push ( createDiagnosticForNode ( node . name || node , Diagnostics . JSDoc_types_may_be_moved_to_TypeScript_types ) ) ;
68
68
}
69
69
}
70
- }
71
70
72
- addRange ( diags , sourceFile . bindSuggestionDiagnostics ) ;
73
- return diags . concat ( checker . getSuggestionDiagnostics ( sourceFile ) ) . sort ( ( d1 , d2 ) => d1 . start - d2 . start ) ;
71
+ node . forEachChild ( check ) ;
72
+ }
74
73
}
75
74
76
75
// convertToEs6Module only works on top-level, so don't trigger it if commonjs code only appears in nested scopes.
0 commit comments