@@ -85,57 +85,48 @@ module.exports = {
8585 } ,
8686 create ( context ) {
8787 return {
88- Program ( node ) {
88+ Program ( program ) {
8989 let regexMatch ;
90- for ( const comment of node . comments ?? [ ] ) {
90+ for ( const comment of program . comments ?? [ ] ) {
9191 let found = 0 ;
92+ /** @type {Rule.Node } */
93+ // @ts -expect-error -- comments are not considered nodes but this still works
94+ const node = comment ;
9295 /** @type {Scope.Scope["block"] } */
9396 while ( ( regexMatch = deprecatedCommentRegex . exec ( comment . value ) ) ) {
9497 const match = regexMatch ;
9598 if ( found === 1 ) {
9699 context . report ( {
97- // @ts -expect-error -- comments are not considered nodes but this still works
98- node : comment ,
100+ node,
99101 messageId : messageIds . doubleDeprecation ,
100102 } ) ;
101103 }
102104 found ++ ;
103105 if ( ! match . groups ?. description ) {
104106 context . report ( {
105- // @ts -expect-error -- comments are not considered nodes but this still works
106- node : comment ,
107+ node,
107108 messageId : messageIds . noDescription ,
108109 } ) ;
109110 continue ; // no point in applying any fixes after this - the description will have to be added manually anyway.
110111 }
111112
113+ let noVersion = false ;
114+ let addDate = false ;
115+ let oldDate = false ;
116+ let addSeparator = false ;
117+ let removeSeparator = false ;
118+ let badDescription = false ;
112119 if ( ! match . groups ?. version ) {
113120 // the version is missing
114121 if ( context . options [ 0 ] ?. addVersion || match . groups ?. when ) {
115122 // either the options require having a version, or the deprecation date is already there, in both cases the version should also be there.
116- context . report ( {
117- // @ts -expect-error -- comments are not considered nodes but this still works
118- node : comment ,
119- messageId : messageIds . noVersion ,
120- fix : ( ( ) => {
121- if ( context . options [ 0 ] ?. addVersion )
122- // only if the addVersion option is enabled, add the current version. Otherwise, log a problem without a fix.
123- return ( fixer ) =>
124- wrapComment (
125- fixer ,
126- comment ,
127- // prettier-ignore
128- `${ comment . value . substring ( 0 , match . index ) } @deprecated in ${ context . options [ 0 ] . addVersion } ${ comment . value . substring ( match . index + "@deprecated" . length ) } ` ,
129- ) ;
130- } ) ( ) ,
131- } ) ;
123+ noVersion = true ;
132124 }
133125 }
134126
135127 if ( match . groups ?. version ?. includes ( "x" ) ) {
136128 context . report ( {
137- // @ts -expect-error -- comments are not considered nodes but this still works
138- node : comment ,
129+ node,
139130 messageId : messageIds . badVersion ,
140131 } ) ;
141132 }
@@ -144,100 +135,49 @@ module.exports = {
144135 const currentDate = now . toFormat ( "yyyy-MM-dd" ) ;
145136 const targetDate = now . plus ( { year : 1 } ) . toFormat ( "yyyy-MM-dd" ) ;
146137
147- // This will not work if the version is missing. However, eslint should run the rule in multiple passes if any fixes are applied.
148- // So if the above fix is applied, next time it runs, the version will be there.
149138 if ( context . options [ 0 ] ?. addVersion && ! match . groups ?. when ) {
150139 // add date
151- context . report ( {
152- // @ts -expect-error -- comments are not considered nodes but this still works
153- node : comment ,
154- messageId : messageIds . noDate ,
155- fix : ( ( ) => {
156- if ( match . indices ?. groups ?. version === undefined ) return undefined ;
157- const versionIndices = match . indices . groups . version ;
158- return ( fixer ) =>
159- wrapComment (
160- fixer ,
161- comment ,
162- // prettier-ignore
163- `${ comment . value . substring ( 0 , versionIndices [ 1 ] ) } - ${ regexParts . notUntil } ${ targetDate } ${ comment . value . substring ( versionIndices [ 1 ] ) } ` ,
164- ) ;
165- } ) ( ) ,
166- } ) ;
140+ addDate = true ;
167141 }
168142
169143 if ( context . options [ 0 ] ?. removeOldDates && match . groups ?. date && match . groups . date < currentDate ) {
170144 // remove old date
171- context . report ( {
172- // @ts -expect-error -- comments are not considered nodes but this still works
173- node : comment ,
174- messageId : messageIds . oldDate ,
175- fix : ( ( ) => {
176- if ( match . indices ?. groups ?. when === undefined ) return undefined ;
177- const whenIndices = match . indices . groups . when ;
178- return ( fixer ) =>
179- wrapComment (
180- fixer ,
181- comment ,
182- `${ comment . value . substring ( 0 , whenIndices [ 0 ] ) } ${ regexParts . expired } ${ comment . value . substring ( whenIndices [ 1 ] ) } ` ,
183- ) ;
184- } ) ( ) ,
185- } ) ;
145+ oldDate = true ;
186146 }
187147
188- if (
189- ( ( match . groups ?. version || match . groups ?. when ) && ! match . groups ?. separator && match . groups ?. description ) ||
190- ( ! ( match . groups ?. version || match . groups ?. when ) && match . groups ?. separator && match . groups ?. description )
191- ) {
192- // add/remove separator
193- context . report ( {
194- // @ts -expect-error -- comments are not considered nodes but this still works
195- node : comment ,
196- messageId : messageIds . noSeparator ,
197- fix : ( ( ) => {
198- if ( match . indices ?. groups ?. description === undefined ) return undefined ;
199- const descriptionIndices = match . indices . groups . description ;
200- if ( match . groups ?. separator )
201- return ( fixer ) =>
202- wrapComment (
203- fixer ,
204- comment ,
205- `${ comment . value . substring ( 0 , descriptionIndices [ 0 ] ) . trimEnd ( ) . replace ( / \. $ / , "" ) } ${ firstUpper (
206- comment . value . substring ( descriptionIndices [ 0 ] ) ,
207- ) } `,
208- ) ;
209- else
210- return ( fixer ) =>
211- wrapComment (
212- fixer ,
213- comment ,
214- `${ comment . value . substring ( 0 , descriptionIndices [ 0 ] ) . trimEnd ( ) } . ${ firstUpper (
215- comment . value . substring ( descriptionIndices [ 0 ] ) ,
216- ) } `,
217- ) ;
218- } ) ( ) ,
219- } ) ;
220- } else if ( match . groups ?. description && ! validDescriptionRegex . test ( match . groups . description ) ) {
221- const description = match . groups . description ;
222- context . report ( {
223- // @ts -expect-error -- comments are not considered nodes but this still works
224- node : comment ,
225- messageId : messageIds . badDescription ,
226- fix : ( ( ) => {
227- const descriptionIndices = match . indices ?. groups ?. description ;
228- if ( ! descriptionIndices ) return undefined ;
229- if ( description . startsWith ( "-" ) )
230- return ( fixer ) =>
231- wrapComment (
232- fixer ,
233- comment ,
234- `${ comment . value . substring ( 0 , descriptionIndices [ 0 ] ) } ${ firstUpper (
235- comment . value . substring ( descriptionIndices [ 0 ] ) . replace ( / ^ - \s * / , "" ) ,
236- ) } `,
237- ) ;
238- } ) ( ) ,
239- } ) ;
240- }
148+ if ( match . groups ?. description && ! validDescriptionRegex . test ( match . groups . description ) ) badDescription = true ;
149+
150+ if ( ( match . groups ?. version || match . groups ?. when ) && ! match . groups ?. separator && match . groups ?. description ) addSeparator = true ;
151+ else if ( ! ( match . groups ?. version || match . groups ?. when ) && match . groups ?. separator && match . groups ?. description ) removeSeparator = true ;
152+
153+ const oldDescription = match . groups ?. description ?. trimStart ( ) ?? "" ;
154+ const newDescription = firstUpper ( oldDescription . replace ( / ^ - \s * / , "" ) ) ;
155+ const newComment =
156+ comment . value . substring ( 0 , match . index ) +
157+ "@deprecated" +
158+ ( noVersion && context . options [ 0 ] ?. addVersion
159+ ? ` in ${ context . options [ 0 ] . addVersion } `
160+ : match . groups ?. version
161+ ? ` in ${ match . groups . version } `
162+ : "" ) +
163+ ( addDate
164+ ? ` - ${ regexParts . notUntil } ${ targetDate } `
165+ : oldDate
166+ ? ` - ${ regexParts . expired } `
167+ : match . groups ?. when
168+ ? ` - ${ match . groups . when } `
169+ : "" ) +
170+ ( addDate || oldDate || match . groups ?. when || noVersion || match . groups ?. version ? "." : "" ) +
171+ ` ${ newDescription } ` +
172+ comment . value . substring ( match . index + match [ 0 ] . length ) ;
173+
174+ /** @param {Rule.RuleFixer } fixer */
175+ const fix = newComment !== comment . value ? ( fixer ) => wrapComment ( fixer , comment , newComment ) : undefined ;
176+ if ( noVersion ) context . report ( { node, messageId : messageIds . noVersion , fix } ) ;
177+ if ( addDate ) context . report ( { node, messageId : messageIds . noDate , fix } ) ;
178+ if ( oldDate ) context . report ( { node, messageId : messageIds . oldDate , fix } ) ;
179+ if ( addSeparator || removeSeparator ) context . report ( { node, messageId : messageIds . noSeparator , fix } ) ;
180+ if ( badDescription ) context . report ( { node, messageId : messageIds . badDescription , fix } ) ;
241181 }
242182 }
243183 } ,
0 commit comments