@@ -17,16 +17,16 @@ const tslintRegex = new RegExp(/\s*tslint:(enable|disable)(?:-(line|next-line))?
17
17
18
18
export const convertComments = async ( dependencies : ConvertCommentsResultsDependencies ) => {
19
19
// TODO: Remove console logs
20
- console . log ( "Started" ) ;
21
20
const filenames = await dependencies . fileSystem . readDir ( "./" , { withFileTypes : true } ) ;
22
21
if ( ! isError ( filenames ) ) {
23
22
const filteredFilenames : string [ ] = filenames
24
- . filter ( fileEnt => fileEnt . isFile ( ) )
25
- . map ( fileEnt => fileEnt . name ) ;
23
+ . filter ( ( fileEnt ) => fileEnt . isFile ( ) )
24
+ . map ( ( fileEnt ) => fileEnt . name ) ;
26
25
// TODO: Remove console logs
27
- console . log ( "Filenames filtered" ) ;
28
- console . log ( filteredFilenames ) ;
29
26
for ( const filename of filteredFilenames ) {
27
+ // todo: cli should pass glob/wildcard
28
+ if ( ! filename . includes ( "index" ) ) continue ;
29
+
30
30
const fileContent : string | Error = await dependencies . fileSystem . readFile ( filename ) ;
31
31
if ( ! isError ( fileContent ) ) {
32
32
const writeFileRes = await replaceComments ( dependencies , filename , fileContent ) ;
@@ -40,7 +40,9 @@ export const convertComments = async (dependencies: ConvertCommentsResultsDepend
40
40
return Error ( "Failed to convert file comments" ) ;
41
41
} ;
42
42
43
- type Modifier = "line" | "next-line" | undefined ;
43
+ type CommentKind = ts . SyntaxKind . MultiLineCommentTrivia | ts . SyntaxKind . SingleLineCommentTrivia ;
44
+
45
+ type Modifier = "line" | "next-line" | undefined ; // todo: split "line" into "block-start" and "block-end"?
44
46
function parseComment (
45
47
commentText : string ,
46
48
) : { rulesList : string [ ] | "all" ; isEnabled : boolean ; modifier : Modifier } | undefined {
@@ -67,7 +69,7 @@ function parseComment(
67
69
}
68
70
69
71
function splitOnSpaces ( str : string ) : string [ ] {
70
- return str . split ( / \s + / ) . filter ( s => s !== "" ) ;
72
+ return str . split ( / \s + / ) . filter ( ( s ) => s !== "" ) ;
71
73
}
72
74
73
75
type IReplacement = {
@@ -92,34 +94,34 @@ const replaceComments = async (
92
94
// or that's what I think it does.
93
95
utils . forEachComment ( sourceFile , ( fullText , comment ) => {
94
96
const replacements : IReplacement [ ] = [ ] ;
95
- const commentText =
96
- comment . kind === ts . SyntaxKind . SingleLineCommentTrivia
97
- ? fullText . substring ( comment . pos + 3 , comment . end )
98
- : fullText . substring ( comment . pos + 3 , comment . end - 2 ) ;
97
+ const commentText = ( comment . kind === ts . SyntaxKind . SingleLineCommentTrivia
98
+ ? fullText . substring ( comment . pos + 2 , comment . end )
99
+ : fullText . substring ( comment . pos + 2 , comment . end - 2 )
100
+ ) . trim ( ) ;
99
101
100
102
const parsed = parseComment ( commentText ) ;
101
103
if ( parsed !== undefined ) {
102
104
const { rulesList, modifier } = parsed ;
103
105
const switchRange = getSwitchRange ( modifier , comment , sourceFile ) ;
106
+ console . log ( { modifier, comment } ) ;
104
107
if ( switchRange !== undefined ) {
105
108
// Extra log to check what is going on
106
- console . log ( "----------- COMMENT TEXT -----------" ) ;
107
- console . log ( commentText ) ;
108
- console . log ( "----------- PARSED DATA -----------" ) ;
109
- console . log ( parsed ) ;
110
- console . log ( "----------- SWITCH RANGE -----------" ) ;
111
- console . log ( switchRange ) ;
112
109
const rulesToSwitch =
113
110
rulesList === "all"
114
111
? Array . from ( rulesConverters . keys ( ) )
115
- : rulesList . filter ( ruleKey => rulesConverters . has ( ruleKey ) ) ;
112
+ : rulesList . filter ( ( ruleKey ) => rulesConverters . has ( ruleKey ) ) ;
113
+ console . log ( { rulesToSwitch } ) ;
116
114
for ( const ruleToSwitch of rulesToSwitch ) {
117
115
const transformedRules = switchRule ( ruleToSwitch ) ;
118
116
if ( transformedRules ) {
119
117
replacements . push ( {
120
- start : switchRange . pos ,
121
- end : switchRange . end ,
122
- replacementText : transformedRules . join ( " " ) ,
118
+ start : comment . pos ,
119
+ end : comment . end ,
120
+ replacementText : createReplacementText (
121
+ comment . kind ,
122
+ modifier ,
123
+ transformedRules ,
124
+ ) ,
123
125
} ) ;
124
126
}
125
127
}
@@ -130,17 +132,28 @@ const replaceComments = async (
130
132
131
133
const newText = getNewText ( fileContent , replacements ) ;
132
134
// Check the output before writing to file.
133
- console . log ( "" ) ;
134
- console . log ( "************** NEW FILE BEING WRITTEN ! **************" ) ;
135
- console . log ( newText ) ;
136
135
// Write the file with the changes.
137
136
// At the moment,
138
137
_dependencies . fileSystem . writeFileSync ( fileName , newText ) ;
139
138
} ) ;
140
139
return true ;
141
140
} ;
142
141
142
+ function createReplacementText (
143
+ commentKind : CommentKind ,
144
+ _modifier : Modifier ,
145
+ transformedRules : string [ ] ,
146
+ ) {
147
+ const directive = "eslint-disable-next-line" ; // todo: map from modifier
148
+ const [ left , right ] =
149
+ commentKind === ts . SyntaxKind . SingleLineCommentTrivia ? [ "// " , "" ] : [ "/* " , " */" ] ;
150
+
151
+ // todo: no transformed rules?
152
+ return [ left , directive , " " , transformedRules . join ( " " ) , right ] . join ( "" ) ;
153
+ }
154
+
143
155
function getNewText ( sourceText : string , replacementsInReverse : IReplacement [ ] ) {
156
+ console . log ( { replacementsInReverse } ) ;
144
157
for ( const { start, end, replacementText } of replacementsInReverse ) {
145
158
sourceText = sourceText . slice ( 0 , start ) + replacementText + sourceText . slice ( end ) ;
146
159
}
@@ -154,9 +167,7 @@ function switchRule(ruleName: string): string[] | null {
154
167
const tslintRule = formatRawTslintRule ( ruleName , { ruleName } ) ;
155
168
const conversion = tslintRuleConverter ( tslintRule ) ;
156
169
if ( ! ( conversion instanceof ConversionError ) ) {
157
- const eslintRules = conversion . rules . map ( r => r . ruleName ) ;
158
- console . log ( `Rulename: ${ ruleName } ` ) ;
159
- console . log ( eslintRules ) ;
170
+ const eslintRules = conversion . rules . map ( ( r ) => r . ruleName ) ;
160
171
return eslintRules ;
161
172
}
162
173
}
0 commit comments