Skip to content

Commit bf7c40c

Browse files
BPScottnot-an-aardvark
authored andcommitted
Breaking: Extract showInvisibles and generateDifferences
Move these two functions into a helper module 'prettier-linter-helpers'. If you used those functions then plese reference that package directly. Fixes #101
1 parent 478c7e5 commit bf7c40c

File tree

4 files changed

+15
-185
lines changed

4 files changed

+15
-185
lines changed

eslint-plugin-prettier.js

+8-147
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@
99
// Requirements
1010
// ------------------------------------------------------------------------------
1111

12-
const diff = require('fast-diff');
12+
const {
13+
showInvisibles,
14+
generateDifferences
15+
} = require('prettier-linter-helpers');
1316

1417
// ------------------------------------------------------------------------------
1518
// Constants
1619
// ------------------------------------------------------------------------------
1720

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;
2322

2423
// ------------------------------------------------------------------------------
2524
// Privates
@@ -28,142 +27,6 @@ const OPERATION_REPLACE = 'replace';
2827
// Lazily-loaded Prettier.
2928
let prettier;
3029

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-
16730
// ------------------------------------------------------------------------------
16831
// Rule Definition
16932
// ------------------------------------------------------------------------------
@@ -242,8 +105,6 @@ function reportReplace(context, offset, deleteText, insertText) {
242105
// ------------------------------------------------------------------------------
243106

244107
module.exports = {
245-
showInvisibles,
246-
generateDifferences,
247108
configs: {
248109
recommended: {
249110
extends: ['prettier'],
@@ -354,21 +215,21 @@ module.exports = {
354215

355216
differences.forEach(difference => {
356217
switch (difference.operation) {
357-
case OPERATION_INSERT:
218+
case INSERT:
358219
reportInsert(
359220
context,
360221
difference.offset,
361222
difference.insertText
362223
);
363224
break;
364-
case OPERATION_DELETE:
225+
case DELETE:
365226
reportDelete(
366227
context,
367228
difference.offset,
368229
difference.deleteText
369230
);
370231
break;
371-
case OPERATION_REPLACE:
232+
case REPLACE:
372233
reportReplace(
373234
context,
374235
difference.offset,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"homepage": "https://github.com/prettier/eslint-plugin-prettier#readme",
3030
"dependencies": {
31-
"fast-diff": "^1.1.2"
31+
"prettier-linter-helpers": "^1.0.0"
3232
},
3333
"peerDependencies": {
3434
"prettier": ">= 1.13.0"

test/prettier.js

-37
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
const fs = require('fs');
1616
const path = require('path');
17-
const assert = require('assert');
1817

1918
const eslintPluginPrettier = require('..');
2019

@@ -104,42 +103,6 @@ vueRuleTester.run('prettier', rule, {
104103
]
105104
});
106105

107-
describe('showInvisibles', () => {
108-
it('shows invisibles', () => {
109-
assert.strictEqual(
110-
eslintPluginPrettier.showInvisibles('1 2\n3\t4\r5'),
111-
'1·2⏎3↹4␍5'
112-
);
113-
});
114-
});
115-
116-
describe('generateDifferences', () => {
117-
it('operation: insert', () => {
118-
const differences = eslintPluginPrettier.generateDifferences(
119-
'abc',
120-
'abcdef'
121-
);
122-
assert.deepStrictEqual(differences, [
123-
{ operation: 'insert', offset: 3, insertText: 'def' }
124-
]);
125-
});
126-
it('operation: delete', () => {
127-
const differences = eslintPluginPrettier.generateDifferences(
128-
'abcdef',
129-
'abc'
130-
);
131-
assert.deepStrictEqual(differences, [
132-
{ operation: 'delete', offset: 3, deleteText: 'def' }
133-
]);
134-
});
135-
it('operation: replace', () => {
136-
const differences = eslintPluginPrettier.generateDifferences('abc', 'def');
137-
assert.deepStrictEqual(differences, [
138-
{ operation: 'replace', offset: 0, deleteText: 'abc', insertText: 'def' }
139-
]);
140-
});
141-
});
142-
143106
// ------------------------------------------------------------------------------
144107
// Helpers
145108
// ------------------------------------------------------------------------------

yarn.lock

+6
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,12 @@ prelude-ls@~1.1.2:
671671
version "1.1.2"
672672
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
673673

674+
prettier-linter-helpers@^1.0.0:
675+
version "1.0.0"
676+
resolved "https://registry.yarnpkg.com/prettier-linter-helpers/-/prettier-linter-helpers-1.0.0.tgz#d23d41fe1375646de2d0104d3454a3008802cf7b"
677+
dependencies:
678+
fast-diff "^1.1.2"
679+
674680
prettier@^1.13.0:
675681
version "1.14.3"
676682
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.14.3.tgz#90238dd4c0684b7edce5f83b0fb7328e48bd0895"

0 commit comments

Comments
 (0)