1
1
import { FileSystem } from "../adapters/fileSystem" ;
2
2
import { isError } from "../utils" ;
3
3
import * as utils from "tsutils" ;
4
- import * as ts from "typescript" ;
4
+ import ts from "typescript" ;
5
5
import { converters } from "./converters" ;
6
6
import { formatRawTslintRule } from "./formatRawTslintRule" ;
7
7
import { ConversionError } from "../errors/conversionError" ;
@@ -10,7 +10,7 @@ import { ConversionError } from "../errors/conversionError";
10
10
// - https://github.com/Microsoft/TypeScript/issues/21049
11
11
// - https://github.com/palantir/tslint/blob/master/src/enableDisableRules.ts
12
12
export type ConvertCommentsResultsDependencies = {
13
- fileSystem : Pick < FileSystem , "readDir" | "readFile" | "writeFile" > ;
13
+ fileSystem : Pick < FileSystem , "readDir" | "readFile" | "writeFile" | "writeFileSync" > ;
14
14
} ;
15
15
16
16
const tslintRegex : RegExp = new RegExp ( / \s * t s l i n t : ( e n a b l e | d i s a b l e ) (?: - ( l i n e | n e x t - l i n e ) ) ? ( : | \s | $ ) / g) ;
@@ -71,8 +71,14 @@ function splitOnSpaces(str: string): string[] {
71
71
return str . split ( / \s + / ) . filter ( s => s !== "" ) ;
72
72
}
73
73
74
+ interface IReplacement {
75
+ start : number ;
76
+ end : number ;
77
+ replacementText : string ;
78
+ }
79
+
74
80
const replaceComments = async (
75
- dependencies : ConvertCommentsResultsDependencies ,
81
+ _dependencies : ConvertCommentsResultsDependencies ,
76
82
fileName : string ,
77
83
fileContent : string ,
78
84
) => {
@@ -86,14 +92,15 @@ const replaceComments = async (
86
92
// If not, it is `MultiLineCommentTrivia` as this method does check if it is a comment...
87
93
// or that's what I think it does.
88
94
utils . forEachComment ( sourceFile , ( fullText , comment ) => {
95
+ const replacements : IReplacement [ ] = [ ] ;
89
96
const commentText =
90
97
comment . kind === ts . SyntaxKind . SingleLineCommentTrivia
91
- ? fullText . substring ( comment . pos + 2 , comment . end )
92
- : fullText . substring ( comment . pos + 2 , comment . end - 2 ) ;
98
+ ? fullText . substring ( comment . pos + 3 , comment . end )
99
+ : fullText . substring ( comment . pos + 3 , comment . end - 2 ) ;
93
100
94
101
const parsed = parseComment ( commentText ) ;
95
102
if ( parsed !== undefined ) {
96
- const { rulesList, isEnabled , modifier } = parsed ;
103
+ const { rulesList, modifier } = parsed ;
97
104
const switchRange = getSwitchRange ( modifier , comment , sourceFile ) ;
98
105
if ( switchRange !== undefined ) {
99
106
console . log ( "---------- COMMENT TEXT -----------" ) ;
@@ -107,15 +114,37 @@ const replaceComments = async (
107
114
? Array . from ( converters . keys ( ) )
108
115
: rulesList . filter ( ruleKey => converters . has ( ruleKey ) ) ;
109
116
for ( const ruleToSwitch of rulesToSwitch ) {
110
- switchRulename ( ruleToSwitch , isEnabled , switchRange . pos , switchRange . end ) ;
117
+ const transformedRules = switchRule ( ruleToSwitch ) ;
118
+ if ( transformedRules ) {
119
+ replacements . push ( {
120
+ start : switchRange . pos ,
121
+ end : switchRange . end ,
122
+ replacementText : transformedRules . join ( " " ) ,
123
+ } ) ;
124
+ }
111
125
}
112
126
}
113
127
}
128
+ // Reverse the replacement list
129
+ replacements . reverse ( ) ;
130
+
131
+ const newText = getNewText ( fileContent , replacements ) ;
132
+ console . log ( "************** NEW FILE BEING WRITTEN ! **************" ) ;
133
+ console . log ( newText ) ;
134
+ // dependencies.fileSystem.writeFileSync(fileName, newText);
114
135
} ) ;
115
- return await dependencies . fileSystem . writeFile ( fileName , fileContent ) ;
136
+ return true ;
116
137
} ;
117
138
118
- function switchRulename ( ruleName : string , _isEnable : boolean , _start : number , _end : number ) : void {
139
+ function getNewText ( sourceText : string , replacementsInReverse : IReplacement [ ] ) {
140
+ for ( const { start, end, replacementText } of replacementsInReverse ) {
141
+ sourceText = sourceText . slice ( 0 , start ) + replacementText + sourceText . slice ( end ) ;
142
+ }
143
+
144
+ return sourceText ;
145
+ }
146
+
147
+ function switchRule ( ruleName : string ) : string [ ] | null {
119
148
const tslintRuleConverter = converters . get ( ruleName ) ;
120
149
if ( tslintRuleConverter ) {
121
150
const tslintRule = formatRawTslintRule ( ruleName , { ruleName } ) ;
@@ -124,8 +153,10 @@ function switchRulename(ruleName: string, _isEnable: boolean, _start: number, _e
124
153
const eslintRules = conversion . rules . map ( r => r . ruleName ) ;
125
154
console . log ( `Rulename: ${ ruleName } ` ) ;
126
155
console . log ( eslintRules ) ;
156
+ return eslintRules ;
127
157
}
128
158
}
159
+ return null ;
129
160
}
130
161
131
162
function getSwitchRange (
0 commit comments