@@ -4,7 +4,7 @@ import ts from 'typescript';
4
4
import { readFileSync , writeFileSync } from 'fs' ;
5
5
import { resolve , dirname } from 'path' ;
6
6
7
- const [ , , fileName , errorCode ] = process . argv ;
7
+ const [ , , fileName , errorCode ] = process . argv ;
8
8
9
9
if ( ! fileName ) {
10
10
console . error ( 'Usage: node apply-ts-codefixes.js <file-path> [error-code]' ) ;
@@ -33,8 +33,8 @@ if (!sourceFile) {
33
33
34
34
const diagnostics = [
35
35
...program . getSemanticDiagnostics ( sourceFile ) ,
36
- ...program . getSyntacticDiagnostics ( sourceFile )
37
- ] . filter ( d => ! errorCode || d . code === parseInt ( errorCode ) ) ;
36
+ ...program . getSyntacticDiagnostics ( sourceFile ) ,
37
+ ] . filter ( ( d ) => ! errorCode || d . code === parseInt ( errorCode ) ) ;
38
38
39
39
if ( ! diagnostics . length ) {
40
40
console . log ( 'No applicable TypeScript diagnostics found' ) ;
@@ -46,8 +46,10 @@ const languageService = ts.createLanguageService({
46
46
getCompilationSettings : ( ) => options ,
47
47
getScriptFileNames : ( ) => [ resolvedFileName ] ,
48
48
getScriptVersion : ( ) => '1' ,
49
- getScriptSnapshot : ( name ) => name === resolvedFileName ?
50
- ts . ScriptSnapshot . fromString ( readFileSync ( name , 'utf-8' ) ) : undefined ,
49
+ getScriptSnapshot : ( name ) =>
50
+ name === resolvedFileName
51
+ ? ts . ScriptSnapshot . fromString ( readFileSync ( name , 'utf-8' ) )
52
+ : undefined ,
51
53
getCurrentDirectory : ( ) => process . cwd ( ) ,
52
54
getDefaultLibFileName : ( options ) => ts . getDefaultLibFilePath ( options ) ,
53
55
readFile : ts . sys . readFile ,
@@ -67,12 +69,12 @@ for (const diagnostic of diagnostics) {
67
69
{ } ,
68
70
{ }
69
71
) ;
70
-
72
+
71
73
if ( fixes . length > 0 ) {
72
74
console . log ( `Found fix for TS${ diagnostic . code } : ${ diagnostic . messageText } ` ) ;
73
75
console . log ( ` Fix: ${ fixes [ 0 ] ?. description } ` ) ;
74
-
75
- allChanges . push ( ...( fixes [ 0 ] ?. changes . flatMap ( c => c . textChanges ) || [ ] ) ) ;
76
+
77
+ allChanges . push ( ...( fixes [ 0 ] ?. changes . flatMap ( ( c ) => c . textChanges ) || [ ] ) ) ;
76
78
}
77
79
}
78
80
}
@@ -86,11 +88,12 @@ if (!allChanges.length) {
86
88
let content = readFileSync ( resolvedFileName , 'utf-8' ) ;
87
89
allChanges
88
90
. sort ( ( a , b ) => b . span . start - a . span . start )
89
- . forEach ( change => {
90
- content = content . slice ( 0 , change . span . start ) +
91
- change . newText +
92
- content . slice ( change . span . start + change . span . length ) ;
91
+ . forEach ( ( change ) => {
92
+ content =
93
+ content . slice ( 0 , change . span . start ) +
94
+ change . newText +
95
+ content . slice ( change . span . start + change . span . length ) ;
93
96
} ) ;
94
97
95
98
writeFileSync ( resolvedFileName , content ) ;
96
- console . log ( `Applied ${ allChanges . length } TypeScript code fixes` ) ;
99
+ console . log ( `Applied ${ allChanges . length } TypeScript code fixes` ) ;
0 commit comments