eslint-plugin-prettier 2.7.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/LICENSE.md +1 -2
- package/README.md +21 -75
- package/eslint-plugin-prettier.js +32 -281
- package/package.json +14 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## v3.0.0 (2018-10-01)
|
|
4
|
+
|
|
5
|
+
* Chore: Add eslint peer-dependency ([d55d79c](https://github.com/prettier/eslint-plugin-prettier/commit/d55d79c6a64f659f405788fc75f344704619979f))
|
|
6
|
+
* Breaking: Extract showInvisibles and generateDifferences ([bf7c40c](https://github.com/prettier/eslint-plugin-prettier/commit/bf7c40c240d9833548a7c9d210a28c90a4f3957b))
|
|
7
|
+
* Breaking: Defining prettier options must use an object ([478c7e5](https://github.com/prettier/eslint-plugin-prettier/commit/478c7e5d2165f3e67e893c9a317b602159eaff9c))
|
|
8
|
+
* Breaking: Drop support for ESLint v3 and v4 ([2326231](https://github.com/prettier/eslint-plugin-prettier/commit/232623179b16b99c0cf89ec9b8ed7660c69b092d))
|
|
9
|
+
* Chore: Update dependencies ([1ec94c8](https://github.com/prettier/eslint-plugin-prettier/commit/1ec94c8e3495f6964588da5264b890cb49616fff))
|
|
10
|
+
* Chore: remove two unused dependencies ([bfe459c](https://github.com/prettier/eslint-plugin-prettier/commit/bfe459c39b742115137e81278f03f8e6abfd7dcf))
|
|
11
|
+
* Chore: Rename test files to keep them sequential ([d38ea52](https://github.com/prettier/eslint-plugin-prettier/commit/d38ea52debdf9da718c60933f42a709fa05f550f))
|
|
12
|
+
* Breaking: Remove pragma support ([3af422c](https://github.com/prettier/eslint-plugin-prettier/commit/3af422c8e301978b611cfc665e052d48c102b443))
|
|
13
|
+
* Breaking: Update minimum required pretter version to 1.13.0 ([29c0506](https://github.com/prettier/eslint-plugin-prettier/commit/29c050605674fda2975b3b620c89a7eb9332641a))
|
|
14
|
+
* Breaking: Drop support for node v4, v7 and v9 ([be460bd](https://github.com/prettier/eslint-plugin-prettier/commit/be460bdd06fafb04442b440efabc7b36b12934a7))
|
|
15
|
+
* Chore: Add vscode config to autoformat on save ([9fac6b4](https://github.com/prettier/eslint-plugin-prettier/commit/9fac6b4039c1983b83073fa7af7864f0d7e1f2d3))
|
|
16
|
+
* Chore: Improve travis matrix ([46d2444](https://github.com/prettier/eslint-plugin-prettier/commit/46d244409e397ba9ff2dea621e99a4ea90e0585b))
|
|
17
|
+
* Chore: Add format script to run prettier ([d46aa6d](https://github.com/prettier/eslint-plugin-prettier/commit/d46aa6dbd8028802121231d3ae0fe3f837bca9ad))
|
|
18
|
+
|
|
3
19
|
## v2.7.0 (2018-09-26)
|
|
4
20
|
|
|
5
21
|
* Update: Support prettierignore and custom processors ([#111](https://github.com/prettier/eslint-plugin-prettier/issues/111)) ([38537ba](https://github.com/prettier/eslint-plugin-prettier/commit/38537ba35fc9152852c3b91f3041d72556b43013))
|
package/LICENSE.md
CHANGED
package/README.md
CHANGED
|
@@ -43,9 +43,7 @@ Then, in your `.eslintrc.json`:
|
|
|
43
43
|
|
|
44
44
|
```json
|
|
45
45
|
{
|
|
46
|
-
"plugins": [
|
|
47
|
-
"prettier"
|
|
48
|
-
],
|
|
46
|
+
"plugins": ["prettier"],
|
|
49
47
|
"rules": {
|
|
50
48
|
"prettier/prettier": "error"
|
|
51
49
|
}
|
|
@@ -62,25 +60,23 @@ To integrate this plugin with `eslint-config-prettier`, you can use the `"recomm
|
|
|
62
60
|
|
|
63
61
|
1. In addition to the above installation instructions, install `eslint-config-prettier`:
|
|
64
62
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
63
|
+
```sh
|
|
64
|
+
npm install --save-dev eslint-config-prettier
|
|
65
|
+
```
|
|
68
66
|
|
|
69
67
|
2. Then you need to add `plugin:prettier/recommended` as the last extension in your `.eslintrc.json`:
|
|
70
68
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
```
|
|
69
|
+
```json
|
|
70
|
+
{
|
|
71
|
+
"extends": ["plugin:prettier/recommended"]
|
|
72
|
+
}
|
|
73
|
+
```
|
|
78
74
|
|
|
79
75
|
This does three things:
|
|
80
76
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
77
|
+
- Enables `eslint-plugin-prettier`.
|
|
78
|
+
- Sets the `prettier/prettier` rule to `"error"`.
|
|
79
|
+
- Extends the `eslint-config-prettier` configuration.
|
|
84
80
|
|
|
85
81
|
You can then set Prettier's own options inside a `.prettierrc` file.
|
|
86
82
|
|
|
@@ -103,79 +99,29 @@ For the list of every available exclusion rule set, please see the [readme of es
|
|
|
103
99
|
|
|
104
100
|
> Note: While it is possible to pass options to Prettier via your ESLint configuration file, it is not recommended because editor extensions such as `prettier-atom` and `prettier-vscode` **will** read [`.prettierrc`](https://prettier.io/docs/en/configuration.html), but **won't** read settings from ESLint, which can lead to an inconsistent experience.
|
|
105
101
|
|
|
106
|
-
|
|
107
|
-
- Objects are passed directly to Prettier as [options](https://prettier.io/docs/en/options.html). Example:
|
|
108
|
-
|
|
109
|
-
```json
|
|
110
|
-
"prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
- Or the string `"fb"` may be used to set "Facebook style" defaults:
|
|
114
|
-
|
|
115
|
-
```json
|
|
116
|
-
"prettier/prettier": ["error", "fb"]
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
Equivalent to:
|
|
120
|
-
|
|
121
|
-
```json
|
|
122
|
-
"prettier/prettier": ["error", {
|
|
123
|
-
"singleQuote": true,
|
|
124
|
-
"trailingComma": "all",
|
|
125
|
-
"bracketSpacing": false,
|
|
126
|
-
"jsxBracketSameLine": true,
|
|
127
|
-
"parser": "flow"
|
|
128
|
-
}]
|
|
129
|
-
```
|
|
130
|
-
NB: This option will merge and override any config set with `.prettierrc` files (for Prettier < 1.7.0, [config files are ignored](https://github.com/prettier/eslint-plugin-prettier/issues/46))
|
|
131
|
-
|
|
132
|
-
* The second option:
|
|
102
|
+
- The first option:
|
|
133
103
|
|
|
134
|
-
-
|
|
104
|
+
- An object representing [options](https://prettier.io/docs/en/options.html) that will be passed into prettier. Example:
|
|
135
105
|
|
|
136
106
|
```json
|
|
137
|
-
"prettier/prettier": ["error",
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
Only files with `@prettier` in the heading docblock will be checked:
|
|
141
|
-
|
|
142
|
-
```js
|
|
143
|
-
/** @prettier */
|
|
144
|
-
|
|
145
|
-
console.log(1 + 2 + 3);
|
|
107
|
+
"prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
|
|
146
108
|
```
|
|
147
109
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
```js
|
|
151
|
-
/**
|
|
152
|
-
* @prettier
|
|
153
|
-
*/
|
|
110
|
+
NB: This option will merge and override any config set with `.prettierrc` files
|
|
154
111
|
|
|
155
|
-
|
|
156
|
-
```
|
|
112
|
+
- The second option:
|
|
157
113
|
|
|
158
|
-
_This option is useful if you're migrating a large codebase and already use pragmas like `@flow`._
|
|
159
|
-
|
|
160
114
|
- An object with the following options
|
|
161
|
-
|
|
162
|
-
- `pragma`: Also sets the aforementioned `pragma`: a string with a pragma that triggers this rule. By default, this rule applies to all files. However, if you set a pragma (this option), only files with that pragma in the heading docblock will be checked. All pragmas must start with `@`.
|
|
163
|
-
|
|
164
|
-
```json
|
|
165
|
-
"prettier/prettier": ["error", null, {
|
|
166
|
-
"pragma": "@prettier"
|
|
167
|
-
}]
|
|
168
|
-
```
|
|
169
|
-
|
|
115
|
+
|
|
170
116
|
- `usePrettierrc`: Enables loading of the Prettier configuration file, (default: `true`). May be useful if you are using multiple tools that conflict with each other, or do not wish to mix your ESLint settings with your Prettier configuration.
|
|
171
|
-
|
|
117
|
+
|
|
172
118
|
```json
|
|
173
|
-
"prettier/prettier": ["error",
|
|
119
|
+
"prettier/prettier": ["error", {}, {
|
|
174
120
|
"usePrettierrc": false
|
|
175
121
|
}]
|
|
176
122
|
```
|
|
177
123
|
|
|
178
|
-
|
|
124
|
+
- The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.
|
|
179
125
|
|
|
180
126
|
---
|
|
181
127
|
|
|
@@ -9,27 +9,16 @@
|
|
|
9
9
|
// Requirements
|
|
10
10
|
// ------------------------------------------------------------------------------
|
|
11
11
|
|
|
12
|
-
const
|
|
13
|
-
|
|
12
|
+
const {
|
|
13
|
+
showInvisibles,
|
|
14
|
+
generateDifferences
|
|
15
|
+
} = require('prettier-linter-helpers');
|
|
14
16
|
|
|
15
17
|
// ------------------------------------------------------------------------------
|
|
16
18
|
// Constants
|
|
17
19
|
// ------------------------------------------------------------------------------
|
|
18
20
|
|
|
19
|
-
|
|
20
|
-
const FB_PRETTIER_OPTIONS = {
|
|
21
|
-
singleQuote: true,
|
|
22
|
-
trailingComma: 'all',
|
|
23
|
-
bracketSpacing: false,
|
|
24
|
-
jsxBracketSameLine: true,
|
|
25
|
-
parser: 'flow'
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const LINE_ENDING_RE = /\r\n|[\r\n\u2028\u2029]/;
|
|
29
|
-
|
|
30
|
-
const OPERATION_INSERT = 'insert';
|
|
31
|
-
const OPERATION_DELETE = 'delete';
|
|
32
|
-
const OPERATION_REPLACE = 'replace';
|
|
21
|
+
const { INSERT, DELETE, REPLACE } = generateDifferences;
|
|
33
22
|
|
|
34
23
|
// ------------------------------------------------------------------------------
|
|
35
24
|
// Privates
|
|
@@ -38,182 +27,6 @@ const OPERATION_REPLACE = 'replace';
|
|
|
38
27
|
// Lazily-loaded Prettier.
|
|
39
28
|
let prettier;
|
|
40
29
|
|
|
41
|
-
// ------------------------------------------------------------------------------
|
|
42
|
-
// Helpers
|
|
43
|
-
// ------------------------------------------------------------------------------
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Gets the location of a given index in the source code for a given context.
|
|
47
|
-
* @param {RuleContext} context - The ESLint rule context.
|
|
48
|
-
* @param {number} index - An index in the source code.
|
|
49
|
-
* @returns {Object} An object containing numeric `line` and `column` keys.
|
|
50
|
-
*/
|
|
51
|
-
function getLocFromIndex(context, index) {
|
|
52
|
-
// If `sourceCode.getLocFromIndex` is available from ESLint, use it - added
|
|
53
|
-
// in ESLint 3.16.0.
|
|
54
|
-
const sourceCode = context.getSourceCode();
|
|
55
|
-
if (typeof sourceCode.getLocFromIndex === 'function') {
|
|
56
|
-
return sourceCode.getLocFromIndex(index);
|
|
57
|
-
}
|
|
58
|
-
const text = sourceCode.getText();
|
|
59
|
-
if (typeof index !== 'number') {
|
|
60
|
-
throw new TypeError('Expected `index` to be a number.');
|
|
61
|
-
}
|
|
62
|
-
if (index < 0 || index > text.length) {
|
|
63
|
-
throw new RangeError('Index out of range.');
|
|
64
|
-
}
|
|
65
|
-
// Loosely based on
|
|
66
|
-
// https://github.com/eslint/eslint/blob/18a519fa/lib/ast-utils.js#L408-L438
|
|
67
|
-
const lineEndingPattern = /\r\n|[\r\n\u2028\u2029]/g;
|
|
68
|
-
let offset = 0;
|
|
69
|
-
let line = 0;
|
|
70
|
-
let match;
|
|
71
|
-
while ((match = lineEndingPattern.exec(text))) {
|
|
72
|
-
const next = match.index + match[0].length;
|
|
73
|
-
if (index < next) {
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
line++;
|
|
77
|
-
offset = next;
|
|
78
|
-
}
|
|
79
|
-
return {
|
|
80
|
-
line: line + 1,
|
|
81
|
-
column: index - offset
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Converts invisible characters to a commonly recognizable visible form.
|
|
87
|
-
* @param {string} str - The string with invisibles to convert.
|
|
88
|
-
* @returns {string} The converted string.
|
|
89
|
-
*/
|
|
90
|
-
function showInvisibles(str) {
|
|
91
|
-
let ret = '';
|
|
92
|
-
for (let i = 0; i < str.length; i++) {
|
|
93
|
-
switch (str[i]) {
|
|
94
|
-
case ' ':
|
|
95
|
-
ret += '·'; // Middle Dot, \u00B7
|
|
96
|
-
break;
|
|
97
|
-
case '\n':
|
|
98
|
-
ret += '⏎'; // Return Symbol, \u23ce
|
|
99
|
-
break;
|
|
100
|
-
case '\t':
|
|
101
|
-
ret += '↹'; // Left Arrow To Bar Over Right Arrow To Bar, \u21b9
|
|
102
|
-
break;
|
|
103
|
-
case '\r':
|
|
104
|
-
ret += '␍'; // Carriage Return Symbol, \u240D
|
|
105
|
-
break;
|
|
106
|
-
default:
|
|
107
|
-
ret += str[i];
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return ret;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Generate results for differences between source code and formatted version.
|
|
116
|
-
* @param {string} source - The original source.
|
|
117
|
-
* @param {string} prettierSource - The Prettier formatted source.
|
|
118
|
-
* @returns {Array} - An array contains { operation, offset, insertText, deleteText }
|
|
119
|
-
*/
|
|
120
|
-
function generateDifferences(source, prettierSource) {
|
|
121
|
-
// fast-diff returns the differences between two texts as a series of
|
|
122
|
-
// INSERT, DELETE or EQUAL operations. The results occur only in these
|
|
123
|
-
// sequences:
|
|
124
|
-
// /-> INSERT -> EQUAL
|
|
125
|
-
// EQUAL | /-> EQUAL
|
|
126
|
-
// \-> DELETE |
|
|
127
|
-
// \-> INSERT -> EQUAL
|
|
128
|
-
// Instead of reporting issues at each INSERT or DELETE, certain sequences
|
|
129
|
-
// are batched together and are reported as a friendlier "replace" operation:
|
|
130
|
-
// - A DELETE immediately followed by an INSERT.
|
|
131
|
-
// - Any number of INSERTs and DELETEs where the joining EQUAL of one's end
|
|
132
|
-
// and another's beginning does not have line endings (i.e. issues that occur
|
|
133
|
-
// on contiguous lines).
|
|
134
|
-
|
|
135
|
-
const results = diff(source, prettierSource);
|
|
136
|
-
const differences = [];
|
|
137
|
-
|
|
138
|
-
const batch = [];
|
|
139
|
-
let offset = 0; // NOTE: INSERT never advances the offset.
|
|
140
|
-
while (results.length) {
|
|
141
|
-
const result = results.shift();
|
|
142
|
-
const op = result[0];
|
|
143
|
-
const text = result[1];
|
|
144
|
-
switch (op) {
|
|
145
|
-
case diff.INSERT:
|
|
146
|
-
case diff.DELETE:
|
|
147
|
-
batch.push(result);
|
|
148
|
-
break;
|
|
149
|
-
case diff.EQUAL:
|
|
150
|
-
if (results.length) {
|
|
151
|
-
if (batch.length) {
|
|
152
|
-
if (LINE_ENDING_RE.test(text)) {
|
|
153
|
-
flush();
|
|
154
|
-
offset += text.length;
|
|
155
|
-
} else {
|
|
156
|
-
batch.push(result);
|
|
157
|
-
}
|
|
158
|
-
} else {
|
|
159
|
-
offset += text.length;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
break;
|
|
163
|
-
default:
|
|
164
|
-
throw new Error(`Unexpected fast-diff operation "${op}"`);
|
|
165
|
-
}
|
|
166
|
-
if (batch.length && !results.length) {
|
|
167
|
-
flush();
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return differences;
|
|
172
|
-
|
|
173
|
-
function flush() {
|
|
174
|
-
let aheadDeleteText = '';
|
|
175
|
-
let aheadInsertText = '';
|
|
176
|
-
while (batch.length) {
|
|
177
|
-
const next = batch.shift();
|
|
178
|
-
const op = next[0];
|
|
179
|
-
const text = next[1];
|
|
180
|
-
switch (op) {
|
|
181
|
-
case diff.INSERT:
|
|
182
|
-
aheadInsertText += text;
|
|
183
|
-
break;
|
|
184
|
-
case diff.DELETE:
|
|
185
|
-
aheadDeleteText += text;
|
|
186
|
-
break;
|
|
187
|
-
case diff.EQUAL:
|
|
188
|
-
aheadDeleteText += text;
|
|
189
|
-
aheadInsertText += text;
|
|
190
|
-
break;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
if (aheadDeleteText && aheadInsertText) {
|
|
194
|
-
differences.push({
|
|
195
|
-
offset,
|
|
196
|
-
operation: OPERATION_REPLACE,
|
|
197
|
-
insertText: aheadInsertText,
|
|
198
|
-
deleteText: aheadDeleteText
|
|
199
|
-
});
|
|
200
|
-
} else if (!aheadDeleteText && aheadInsertText) {
|
|
201
|
-
differences.push({
|
|
202
|
-
offset,
|
|
203
|
-
operation: OPERATION_INSERT,
|
|
204
|
-
insertText: aheadInsertText
|
|
205
|
-
});
|
|
206
|
-
} else if (aheadDeleteText && !aheadInsertText) {
|
|
207
|
-
differences.push({
|
|
208
|
-
offset,
|
|
209
|
-
operation: OPERATION_DELETE,
|
|
210
|
-
deleteText: aheadDeleteText
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
offset += aheadDeleteText.length;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
30
|
// ------------------------------------------------------------------------------
|
|
218
31
|
// Rule Definition
|
|
219
32
|
// ------------------------------------------------------------------------------
|
|
@@ -226,7 +39,7 @@ function generateDifferences(source, prettierSource) {
|
|
|
226
39
|
* @returns {void}
|
|
227
40
|
*/
|
|
228
41
|
function reportInsert(context, offset, text) {
|
|
229
|
-
const pos = getLocFromIndex(
|
|
42
|
+
const pos = context.getSourceCode().getLocFromIndex(offset);
|
|
230
43
|
const range = [offset, offset];
|
|
231
44
|
context.report({
|
|
232
45
|
message: 'Insert `{{ code }}`',
|
|
@@ -246,8 +59,8 @@ function reportInsert(context, offset, text) {
|
|
|
246
59
|
* @returns {void}
|
|
247
60
|
*/
|
|
248
61
|
function reportDelete(context, offset, text) {
|
|
249
|
-
const start = getLocFromIndex(
|
|
250
|
-
const end = getLocFromIndex(
|
|
62
|
+
const start = context.getSourceCode().getLocFromIndex(offset);
|
|
63
|
+
const end = context.getSourceCode().getLocFromIndex(offset + text.length);
|
|
251
64
|
const range = [offset, offset + text.length];
|
|
252
65
|
context.report({
|
|
253
66
|
message: 'Delete `{{ code }}`',
|
|
@@ -269,8 +82,10 @@ function reportDelete(context, offset, text) {
|
|
|
269
82
|
* @returns {void}
|
|
270
83
|
*/
|
|
271
84
|
function reportReplace(context, offset, deleteText, insertText) {
|
|
272
|
-
const start = getLocFromIndex(
|
|
273
|
-
const end =
|
|
85
|
+
const start = context.getSourceCode().getLocFromIndex(offset);
|
|
86
|
+
const end = context
|
|
87
|
+
.getSourceCode()
|
|
88
|
+
.getLocFromIndex(offset + deleteText.length);
|
|
274
89
|
const range = [offset, offset + deleteText.length];
|
|
275
90
|
context.report({
|
|
276
91
|
message: 'Replace `{{ deleteCode }}` with `{{ insertCode }}`',
|
|
@@ -285,32 +100,11 @@ function reportReplace(context, offset, deleteText, insertText) {
|
|
|
285
100
|
});
|
|
286
101
|
}
|
|
287
102
|
|
|
288
|
-
/**
|
|
289
|
-
* Get the pragma from the ESLint rule context.
|
|
290
|
-
* @param {RuleContext} context - The ESLint rule context.
|
|
291
|
-
* @returns {string|null}
|
|
292
|
-
*/
|
|
293
|
-
function getPragma(context) {
|
|
294
|
-
const pluginOptions = context.options[1];
|
|
295
|
-
|
|
296
|
-
if (!pluginOptions) {
|
|
297
|
-
return null;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
const pragmaRef =
|
|
301
|
-
typeof pluginOptions === 'string' ? pluginOptions : pluginOptions.pragma;
|
|
302
|
-
|
|
303
|
-
// Remove leading @
|
|
304
|
-
return pragmaRef ? pragmaRef.slice(1) : null;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
103
|
// ------------------------------------------------------------------------------
|
|
308
104
|
// Module Definition
|
|
309
105
|
// ------------------------------------------------------------------------------
|
|
310
106
|
|
|
311
107
|
module.exports = {
|
|
312
|
-
showInvisibles,
|
|
313
|
-
generateDifferences,
|
|
314
108
|
configs: {
|
|
315
109
|
recommended: {
|
|
316
110
|
extends: ['prettier'],
|
|
@@ -330,59 +124,26 @@ module.exports = {
|
|
|
330
124
|
schema: [
|
|
331
125
|
// Prettier options:
|
|
332
126
|
{
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
]
|
|
127
|
+
type: 'object',
|
|
128
|
+
properties: {},
|
|
129
|
+
additionalProperties: true
|
|
337
130
|
},
|
|
338
131
|
{
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
{ type: '
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
properties: {
|
|
345
|
-
pragma: { type: 'string', pattern: '^@\\w+$' },
|
|
346
|
-
usePrettierrc: { type: 'boolean' }
|
|
347
|
-
},
|
|
348
|
-
additionalProperties: true
|
|
349
|
-
}
|
|
350
|
-
]
|
|
132
|
+
type: 'object',
|
|
133
|
+
properties: {
|
|
134
|
+
usePrettierrc: { type: 'boolean' }
|
|
135
|
+
},
|
|
136
|
+
additionalProperties: true
|
|
351
137
|
}
|
|
352
138
|
]
|
|
353
139
|
},
|
|
354
140
|
create(context) {
|
|
355
|
-
const pragma = getPragma(context);
|
|
356
141
|
const usePrettierrc =
|
|
357
142
|
!context.options[1] || context.options[1].usePrettierrc !== false;
|
|
358
143
|
const sourceCode = context.getSourceCode();
|
|
359
144
|
const filepath = context.getFilename();
|
|
360
145
|
const source = sourceCode.text;
|
|
361
146
|
|
|
362
|
-
// The pragma is only valid if it is found in a block comment at the very
|
|
363
|
-
// start of the file.
|
|
364
|
-
if (pragma) {
|
|
365
|
-
// ESLint 3.x reports the shebang as a "Line" node, while ESLint 4.x
|
|
366
|
-
// reports it as a "Shebang" node. This works for both versions:
|
|
367
|
-
const hasShebang = source.startsWith('#!');
|
|
368
|
-
const allComments = sourceCode.getAllComments();
|
|
369
|
-
const firstComment = hasShebang ? allComments[1] : allComments[0];
|
|
370
|
-
if (
|
|
371
|
-
!(
|
|
372
|
-
firstComment &&
|
|
373
|
-
firstComment.type === 'Block' &&
|
|
374
|
-
firstComment.loc.start.line === (hasShebang ? 2 : 1) &&
|
|
375
|
-
firstComment.loc.start.column === 0
|
|
376
|
-
)
|
|
377
|
-
) {
|
|
378
|
-
return {};
|
|
379
|
-
}
|
|
380
|
-
const parsed = docblock.parse(firstComment.value);
|
|
381
|
-
if (parsed[pragma] !== '') {
|
|
382
|
-
return {};
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
147
|
if (prettier && prettier.clearConfigCache) {
|
|
387
148
|
prettier.clearConfigCache();
|
|
388
149
|
}
|
|
@@ -394,27 +155,17 @@ module.exports = {
|
|
|
394
155
|
prettier = require('prettier');
|
|
395
156
|
}
|
|
396
157
|
|
|
397
|
-
const eslintPrettierOptions =
|
|
398
|
-
context.options[0] === 'fb'
|
|
399
|
-
? FB_PRETTIER_OPTIONS
|
|
400
|
-
: context.options[0];
|
|
158
|
+
const eslintPrettierOptions = context.options[0] || {};
|
|
401
159
|
|
|
402
|
-
const prettierRcOptions =
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
editorconfig: true
|
|
408
|
-
})
|
|
409
|
-
: null;
|
|
160
|
+
const prettierRcOptions = usePrettierrc
|
|
161
|
+
? prettier.resolveConfig.sync(filepath, {
|
|
162
|
+
editorconfig: true
|
|
163
|
+
})
|
|
164
|
+
: null;
|
|
410
165
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
? prettier.getFileInfo.sync(filepath, {
|
|
415
|
-
ignorePath: '.prettierignore'
|
|
416
|
-
})
|
|
417
|
-
: { ignored: false, inferredParser: null };
|
|
166
|
+
const prettierFileInfo = prettier.getFileInfo.sync(filepath, {
|
|
167
|
+
ignorePath: '.prettierignore'
|
|
168
|
+
});
|
|
418
169
|
|
|
419
170
|
// Skip if file is ignored using a .prettierignore file
|
|
420
171
|
if (prettierFileInfo.ignored) {
|
|
@@ -464,21 +215,21 @@ module.exports = {
|
|
|
464
215
|
|
|
465
216
|
differences.forEach(difference => {
|
|
466
217
|
switch (difference.operation) {
|
|
467
|
-
case
|
|
218
|
+
case INSERT:
|
|
468
219
|
reportInsert(
|
|
469
220
|
context,
|
|
470
221
|
difference.offset,
|
|
471
222
|
difference.insertText
|
|
472
223
|
);
|
|
473
224
|
break;
|
|
474
|
-
case
|
|
225
|
+
case DELETE:
|
|
475
226
|
reportDelete(
|
|
476
227
|
context,
|
|
477
228
|
difference.offset,
|
|
478
229
|
difference.deleteText
|
|
479
230
|
);
|
|
480
231
|
break;
|
|
481
|
-
case
|
|
232
|
+
case REPLACE:
|
|
482
233
|
reportReplace(
|
|
483
234
|
context,
|
|
484
235
|
difference.offset,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-prettier",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Runs prettier as an eslint rule",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"scripts": {
|
|
17
17
|
"lint": "eslint .",
|
|
18
18
|
"test": "npm run lint && mocha",
|
|
19
|
+
"format": "yarn run prettier '**/*.{js,json,md,yml}' --write && yarn run lint --fix",
|
|
19
20
|
"generate-release": "node-release-script"
|
|
20
21
|
},
|
|
21
22
|
"repository": {
|
|
@@ -27,28 +28,26 @@
|
|
|
27
28
|
},
|
|
28
29
|
"homepage": "https://github.com/prettier/eslint-plugin-prettier#readme",
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"
|
|
31
|
-
"jest-docblock": "^21.0.0"
|
|
31
|
+
"prettier-linter-helpers": "^1.0.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"
|
|
34
|
+
"eslint": ">= 5.0.0",
|
|
35
|
+
"prettier": ">= 1.13.0"
|
|
35
36
|
},
|
|
36
37
|
"devDependencies": {
|
|
37
38
|
"@not-an-aardvark/node-release-script": "^0.1.0",
|
|
38
|
-
"eslint": "^
|
|
39
|
-
"eslint-config-not-an-aardvark": "^2.
|
|
40
|
-
"eslint-config-prettier": "^1.
|
|
41
|
-
"eslint-plugin-eslint-plugin": "^
|
|
42
|
-
"eslint-plugin-node": "^
|
|
43
|
-
"eslint-plugin-self": "^1.0
|
|
44
|
-
"mocha": "^
|
|
45
|
-
"moment": "^2.18.1",
|
|
39
|
+
"eslint": "^5.6.0",
|
|
40
|
+
"eslint-config-not-an-aardvark": "^2.1.0",
|
|
41
|
+
"eslint-config-prettier": "^3.1.0",
|
|
42
|
+
"eslint-plugin-eslint-plugin": "^1.4.0",
|
|
43
|
+
"eslint-plugin-node": "^7.0.1",
|
|
44
|
+
"eslint-plugin-self": "^1.1.0",
|
|
45
|
+
"mocha": "^5.2.0",
|
|
46
46
|
"prettier": "^1.13.0",
|
|
47
|
-
"
|
|
48
|
-
"vue-eslint-parser": "^2.0.2"
|
|
47
|
+
"vue-eslint-parser": "^3.2.2"
|
|
49
48
|
},
|
|
50
49
|
"engines": {
|
|
51
|
-
"node": ">=
|
|
50
|
+
"node": ">=6.0.0"
|
|
52
51
|
},
|
|
53
52
|
"license": "MIT"
|
|
54
53
|
}
|