@@ -32,71 +32,26 @@ let prettier;
32
32
// ------------------------------------------------------------------------------
33
33
34
34
/**
35
- * Reports an "Insert ..." issue where text must be inserted.
36
- * @param {RuleContext } context - The ESLint rule context.
37
- * @param {number } offset - The source offset where to insert text.
38
- * @param {string } text - The text to be inserted.
35
+ * Reports a difference.
36
+ * @param {import('eslint').Rule.RuleContext } context - The ESLint rule context.
37
+ * @param {import('prettier-linter-helpers').Difference } difference - The difference object.
39
38
* @returns {void }
40
39
*/
41
- function reportInsert ( context , offset , text ) {
42
- const pos = context . getSourceCode ( ) . getLocFromIndex ( offset ) ;
43
- const range = [ offset , offset ] ;
44
- context . report ( {
45
- message : 'Insert `{{ code }}`' ,
46
- data : { code : showInvisibles ( text ) } ,
47
- loc : { start : pos , end : pos } ,
48
- fix ( fixer ) {
49
- return fixer . insertTextAfterRange ( range , text ) ;
50
- }
51
- } ) ;
52
- }
53
-
54
- /**
55
- * Reports a "Delete ..." issue where text must be deleted.
56
- * @param {RuleContext } context - The ESLint rule context.
57
- * @param {number } offset - The source offset where to delete text.
58
- * @param {string } text - The text to be deleted.
59
- * @returns {void }
60
- */
61
- function reportDelete ( context , offset , text ) {
62
- const start = context . getSourceCode ( ) . getLocFromIndex ( offset ) ;
63
- const end = context . getSourceCode ( ) . getLocFromIndex ( offset + text . length ) ;
64
- const range = [ offset , offset + text . length ] ;
65
- context . report ( {
66
- message : 'Delete `{{ code }}`' ,
67
- data : { code : showInvisibles ( text ) } ,
68
- loc : { start, end } ,
69
- fix ( fixer ) {
70
- return fixer . removeRange ( range ) ;
71
- }
72
- } ) ;
73
- }
74
-
75
- /**
76
- * Reports a "Replace ... with ..." issue where text must be replaced.
77
- * @param {RuleContext } context - The ESLint rule context.
78
- * @param {number } offset - The source offset where to replace deleted text
79
- with inserted text.
80
- * @param {string } deleteText - The text to be deleted.
81
- * @param {string } insertText - The text to be inserted.
82
- * @returns {void }
83
- */
84
- function reportReplace ( context , offset , deleteText , insertText ) {
85
- const start = context . getSourceCode ( ) . getLocFromIndex ( offset ) ;
86
- const end = context
87
- . getSourceCode ( )
88
- . getLocFromIndex ( offset + deleteText . length ) ;
40
+ function reportDifference ( context , difference ) {
41
+ const { operation, offset, deleteText = '' , insertText = '' } = difference ;
89
42
const range = [ offset , offset + deleteText . length ] ;
43
+ const [ start , end ] = range . map ( index =>
44
+ context . getSourceCode ( ) . getLocFromIndex ( index )
45
+ ) ;
46
+
90
47
context . report ( {
91
- message : 'Replace `{{ deleteCode }}` with `{{ insertCode }}`' ,
48
+ messageId : operation ,
92
49
data : {
93
- deleteCode : showInvisibles ( deleteText ) ,
94
- insertCode : showInvisibles ( insertText )
50
+ deleteText : showInvisibles ( deleteText ) ,
51
+ insertText : showInvisibles ( insertText )
95
52
} ,
96
53
loc : { start, end } ,
97
- fix ( fixer ) {
98
- return fixer . replaceTextRange ( range , insertText ) ;
99
- }
54
+ fix : fixer => fixer . replaceTextRange ( range , insertText )
100
55
} ) ;
101
56
}
102
57
@@ -143,7 +98,12 @@ module.exports = {
143
98
} ,
144
99
additionalProperties : true
145
100
}
146
- ]
101
+ ] ,
102
+ messages : {
103
+ [ INSERT ] : 'Insert `{{ insertText }}`' ,
104
+ [ DELETE ] : 'Delete `{{ deleteText }}`' ,
105
+ [ REPLACE ] : 'Replace `{{ deleteText }}` with `{{ insertText }}`'
106
+ }
147
107
} ,
148
108
create ( context ) {
149
109
const usePrettierrc =
@@ -263,32 +223,9 @@ module.exports = {
263
223
if ( source !== prettierSource ) {
264
224
const differences = generateDifferences ( source , prettierSource ) ;
265
225
266
- differences . forEach ( difference => {
267
- switch ( difference . operation ) {
268
- case INSERT :
269
- reportInsert (
270
- context ,
271
- difference . offset ,
272
- difference . insertText
273
- ) ;
274
- break ;
275
- case DELETE :
276
- reportDelete (
277
- context ,
278
- difference . offset ,
279
- difference . deleteText
280
- ) ;
281
- break ;
282
- case REPLACE :
283
- reportReplace (
284
- context ,
285
- difference . offset ,
286
- difference . deleteText ,
287
- difference . insertText
288
- ) ;
289
- break ;
290
- }
291
- } ) ;
226
+ for ( const difference of differences ) {
227
+ reportDifference ( context , difference ) ;
228
+ }
292
229
}
293
230
}
294
231
} ;
0 commit comments