9
9
// Requirements
10
10
// ------------------------------------------------------------------------------
11
11
12
- const diff = require ( 'fast-diff' ) ;
12
+ const {
13
+ showInvisibles,
14
+ generateDifferences
15
+ } = require ( 'prettier-linter-helpers' ) ;
13
16
14
17
// ------------------------------------------------------------------------------
15
18
// Constants
16
19
// ------------------------------------------------------------------------------
17
20
18
- const LINE_ENDING_RE = / \r \n | [ \r \n \u2028 \u2029 ] / ;
19
-
20
- const OPERATION_INSERT = 'insert' ;
21
- const OPERATION_DELETE = 'delete' ;
22
- const OPERATION_REPLACE = 'replace' ;
21
+ const { INSERT , DELETE , REPLACE } = generateDifferences ;
23
22
24
23
// ------------------------------------------------------------------------------
25
24
// Privates
@@ -28,142 +27,6 @@ const OPERATION_REPLACE = 'replace';
28
27
// Lazily-loaded Prettier.
29
28
let prettier ;
30
29
31
- // ------------------------------------------------------------------------------
32
- // Helpers
33
- // ------------------------------------------------------------------------------
34
-
35
- /**
36
- * Converts invisible characters to a commonly recognizable visible form.
37
- * @param {string } str - The string with invisibles to convert.
38
- * @returns {string } The converted string.
39
- */
40
- function showInvisibles ( str ) {
41
- let ret = '' ;
42
- for ( let i = 0 ; i < str . length ; i ++ ) {
43
- switch ( str [ i ] ) {
44
- case ' ' :
45
- ret += '·' ; // Middle Dot, \u00B7
46
- break ;
47
- case '\n' :
48
- ret += '⏎' ; // Return Symbol, \u23ce
49
- break ;
50
- case '\t' :
51
- ret += '↹' ; // Left Arrow To Bar Over Right Arrow To Bar, \u21b9
52
- break ;
53
- case '\r' :
54
- ret += '␍' ; // Carriage Return Symbol, \u240D
55
- break ;
56
- default :
57
- ret += str [ i ] ;
58
- break ;
59
- }
60
- }
61
- return ret ;
62
- }
63
-
64
- /**
65
- * Generate results for differences between source code and formatted version.
66
- * @param {string } source - The original source.
67
- * @param {string } prettierSource - The Prettier formatted source.
68
- * @returns {Array } - An array contains { operation, offset, insertText, deleteText }
69
- */
70
- function generateDifferences ( source , prettierSource ) {
71
- // fast-diff returns the differences between two texts as a series of
72
- // INSERT, DELETE or EQUAL operations. The results occur only in these
73
- // sequences:
74
- // /-> INSERT -> EQUAL
75
- // EQUAL | /-> EQUAL
76
- // \-> DELETE |
77
- // \-> INSERT -> EQUAL
78
- // Instead of reporting issues at each INSERT or DELETE, certain sequences
79
- // are batched together and are reported as a friendlier "replace" operation:
80
- // - A DELETE immediately followed by an INSERT.
81
- // - Any number of INSERTs and DELETEs where the joining EQUAL of one's end
82
- // and another's beginning does not have line endings (i.e. issues that occur
83
- // on contiguous lines).
84
-
85
- const results = diff ( source , prettierSource ) ;
86
- const differences = [ ] ;
87
-
88
- const batch = [ ] ;
89
- let offset = 0 ; // NOTE: INSERT never advances the offset.
90
- while ( results . length ) {
91
- const result = results . shift ( ) ;
92
- const op = result [ 0 ] ;
93
- const text = result [ 1 ] ;
94
- switch ( op ) {
95
- case diff . INSERT :
96
- case diff . DELETE :
97
- batch . push ( result ) ;
98
- break ;
99
- case diff . EQUAL :
100
- if ( results . length ) {
101
- if ( batch . length ) {
102
- if ( LINE_ENDING_RE . test ( text ) ) {
103
- flush ( ) ;
104
- offset += text . length ;
105
- } else {
106
- batch . push ( result ) ;
107
- }
108
- } else {
109
- offset += text . length ;
110
- }
111
- }
112
- break ;
113
- default :
114
- throw new Error ( `Unexpected fast-diff operation "${ op } "` ) ;
115
- }
116
- if ( batch . length && ! results . length ) {
117
- flush ( ) ;
118
- }
119
- }
120
-
121
- return differences ;
122
-
123
- function flush ( ) {
124
- let aheadDeleteText = '' ;
125
- let aheadInsertText = '' ;
126
- while ( batch . length ) {
127
- const next = batch . shift ( ) ;
128
- const op = next [ 0 ] ;
129
- const text = next [ 1 ] ;
130
- switch ( op ) {
131
- case diff . INSERT :
132
- aheadInsertText += text ;
133
- break ;
134
- case diff . DELETE :
135
- aheadDeleteText += text ;
136
- break ;
137
- case diff . EQUAL :
138
- aheadDeleteText += text ;
139
- aheadInsertText += text ;
140
- break ;
141
- }
142
- }
143
- if ( aheadDeleteText && aheadInsertText ) {
144
- differences . push ( {
145
- offset,
146
- operation : OPERATION_REPLACE ,
147
- insertText : aheadInsertText ,
148
- deleteText : aheadDeleteText
149
- } ) ;
150
- } else if ( ! aheadDeleteText && aheadInsertText ) {
151
- differences . push ( {
152
- offset,
153
- operation : OPERATION_INSERT ,
154
- insertText : aheadInsertText
155
- } ) ;
156
- } else if ( aheadDeleteText && ! aheadInsertText ) {
157
- differences . push ( {
158
- offset,
159
- operation : OPERATION_DELETE ,
160
- deleteText : aheadDeleteText
161
- } ) ;
162
- }
163
- offset += aheadDeleteText . length ;
164
- }
165
- }
166
-
167
30
// ------------------------------------------------------------------------------
168
31
// Rule Definition
169
32
// ------------------------------------------------------------------------------
@@ -242,8 +105,6 @@ function reportReplace(context, offset, deleteText, insertText) {
242
105
// ------------------------------------------------------------------------------
243
106
244
107
module . exports = {
245
- showInvisibles,
246
- generateDifferences,
247
108
configs : {
248
109
recommended : {
249
110
extends : [ 'prettier' ] ,
@@ -354,21 +215,21 @@ module.exports = {
354
215
355
216
differences . forEach ( difference => {
356
217
switch ( difference . operation ) {
357
- case OPERATION_INSERT :
218
+ case INSERT :
358
219
reportInsert (
359
220
context ,
360
221
difference . offset ,
361
222
difference . insertText
362
223
) ;
363
224
break ;
364
- case OPERATION_DELETE :
225
+ case DELETE :
365
226
reportDelete (
366
227
context ,
367
228
difference . offset ,
368
229
difference . deleteText
369
230
) ;
370
231
break ;
371
- case OPERATION_REPLACE :
232
+ case REPLACE :
372
233
reportReplace (
373
234
context ,
374
235
difference . offset ,
0 commit comments