eslint-plugin-prettier 2.6.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 +30 -0
- package/LICENSE.md +1 -2
- package/README.md +36 -75
- package/eslint-plugin-prettier.js +72 -270
- package/package.json +18 -17
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
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
|
+
|
|
19
|
+
## v2.7.0 (2018-09-26)
|
|
20
|
+
|
|
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))
|
|
22
|
+
* Build: switch to release script package ([047dc8f](https://github.com/prettier/eslint-plugin-prettier/commit/047dc8ffdf006c74267df4902fec684c589dad12))
|
|
23
|
+
|
|
24
|
+
## v2.6.2 (2018-07-06)
|
|
25
|
+
|
|
26
|
+
* Fix: Add representation for \r to showInvisibles ([#100](https://github.com/prettier/eslint-plugin-prettier/issues/100)) ([731bbb5](https://github.com/prettier/eslint-plugin-prettier/commit/731bbb576ce422a5c73a1fa9750aa3466c7da928))
|
|
27
|
+
* Docs: Add clarification about Flow/React support to readme ([#96](https://github.com/prettier/eslint-plugin-prettier/issues/96)) ([977aa77](https://github.com/prettier/eslint-plugin-prettier/commit/977aa77a119f22af3f8ca8d6f47e5bcfcc9e23fb))
|
|
28
|
+
|
|
29
|
+
## v2.6.1 (2018-06-23)
|
|
30
|
+
|
|
31
|
+
* Fix: respect editorconfig ([#92](https://github.com/prettier/eslint-plugin-prettier/issues/92)) ([0b04dd3](https://github.com/prettier/eslint-plugin-prettier/commit/0b04dd362d0d92534a7cf11eaebbab8eb59fc96d))
|
|
32
|
+
|
|
3
33
|
## v2.6.0 (2018-02-02)
|
|
4
34
|
|
|
5
35
|
* Update: Add option to skip loading prettierrc ([#83](https://github.com/prettier/eslint-plugin-prettier/issues/83)) ([9e0fb48](https://github.com/prettier/eslint-plugin-prettier/commit/9e0fb48d077214a81ac549731308ab11512c37cd))
|
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,105 +60,68 @@ 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
|
-
2. Then
|
|
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
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
> 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.
|
|
90
|
-
|
|
91
|
-
* The first option:
|
|
92
|
-
- Objects are passed directly to Prettier as [options](https://prettier.io/docs/en/options.html). Example:
|
|
93
|
-
|
|
94
|
-
```json
|
|
95
|
-
"prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
|
|
96
|
-
```
|
|
83
|
+
3. In order to support special ESLint plugins (e.g. [eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react)), add extra exclusions for the plugins you use like so:
|
|
97
84
|
|
|
98
|
-
|
|
85
|
+
```json
|
|
86
|
+
{
|
|
87
|
+
"extends": [
|
|
88
|
+
"plugin:prettier/recommended",
|
|
89
|
+
"prettier/flowtype",
|
|
90
|
+
"prettier/react",
|
|
91
|
+
"prettier/standard"
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
```
|
|
99
95
|
|
|
100
|
-
|
|
101
|
-
"prettier/prettier": ["error", "fb"]
|
|
102
|
-
```
|
|
96
|
+
For the list of every available exclusion rule set, please see the [readme of eslint-config-prettier](https://github.com/prettier/eslint-config-prettier/blob/master/README.md).
|
|
103
97
|
|
|
104
|
-
|
|
98
|
+
## Options
|
|
105
99
|
|
|
106
|
-
|
|
107
|
-
"prettier/prettier": ["error", {
|
|
108
|
-
"singleQuote": true,
|
|
109
|
-
"trailingComma": "all",
|
|
110
|
-
"bracketSpacing": false,
|
|
111
|
-
"jsxBracketSameLine": true,
|
|
112
|
-
"parser": "flow"
|
|
113
|
-
}]
|
|
114
|
-
```
|
|
115
|
-
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))
|
|
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.
|
|
116
101
|
|
|
117
|
-
|
|
102
|
+
- The first option:
|
|
118
103
|
|
|
119
|
-
-
|
|
104
|
+
- An object representing [options](https://prettier.io/docs/en/options.html) that will be passed into prettier. Example:
|
|
120
105
|
|
|
121
106
|
```json
|
|
122
|
-
"prettier/prettier": ["error",
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
Only files with `@prettier` in the heading docblock will be checked:
|
|
126
|
-
|
|
127
|
-
```js
|
|
128
|
-
/** @prettier */
|
|
129
|
-
|
|
130
|
-
console.log(1 + 2 + 3);
|
|
107
|
+
"prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
|
|
131
108
|
```
|
|
132
109
|
|
|
133
|
-
|
|
110
|
+
NB: This option will merge and override any config set with `.prettierrc` files
|
|
134
111
|
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* @prettier
|
|
138
|
-
*/
|
|
112
|
+
- The second option:
|
|
139
113
|
|
|
140
|
-
console.log(4 + 5 + 6);
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
_This option is useful if you're migrating a large codebase and already use pragmas like `@flow`._
|
|
144
|
-
|
|
145
114
|
- An object with the following options
|
|
146
|
-
|
|
147
|
-
- `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 `@`.
|
|
148
|
-
|
|
149
|
-
```json
|
|
150
|
-
"prettier/prettier": ["error", null, {
|
|
151
|
-
"pragma": "@prettier"
|
|
152
|
-
}]
|
|
153
|
-
```
|
|
154
|
-
|
|
115
|
+
|
|
155
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.
|
|
156
|
-
|
|
117
|
+
|
|
157
118
|
```json
|
|
158
|
-
"prettier/prettier": ["error",
|
|
119
|
+
"prettier/prettier": ["error", {}, {
|
|
159
120
|
"usePrettierrc": false
|
|
160
121
|
}]
|
|
161
122
|
```
|
|
162
123
|
|
|
163
|
-
|
|
124
|
+
- The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.
|
|
164
125
|
|
|
165
126
|
---
|
|
166
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,179 +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
|
-
default:
|
|
104
|
-
ret += str[i];
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
return ret;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Generate results for differences between source code and formatted version.
|
|
113
|
-
* @param {string} source - The original source.
|
|
114
|
-
* @param {string} prettierSource - The Prettier formatted source.
|
|
115
|
-
* @returns {Array} - An array contains { operation, offset, insertText, deleteText }
|
|
116
|
-
*/
|
|
117
|
-
function generateDifferences(source, prettierSource) {
|
|
118
|
-
// fast-diff returns the differences between two texts as a series of
|
|
119
|
-
// INSERT, DELETE or EQUAL operations. The results occur only in these
|
|
120
|
-
// sequences:
|
|
121
|
-
// /-> INSERT -> EQUAL
|
|
122
|
-
// EQUAL | /-> EQUAL
|
|
123
|
-
// \-> DELETE |
|
|
124
|
-
// \-> INSERT -> EQUAL
|
|
125
|
-
// Instead of reporting issues at each INSERT or DELETE, certain sequences
|
|
126
|
-
// are batched together and are reported as a friendlier "replace" operation:
|
|
127
|
-
// - A DELETE immediately followed by an INSERT.
|
|
128
|
-
// - Any number of INSERTs and DELETEs where the joining EQUAL of one's end
|
|
129
|
-
// and another's beginning does not have line endings (i.e. issues that occur
|
|
130
|
-
// on contiguous lines).
|
|
131
|
-
|
|
132
|
-
const results = diff(source, prettierSource);
|
|
133
|
-
const differences = [];
|
|
134
|
-
|
|
135
|
-
const batch = [];
|
|
136
|
-
let offset = 0; // NOTE: INSERT never advances the offset.
|
|
137
|
-
while (results.length) {
|
|
138
|
-
const result = results.shift();
|
|
139
|
-
const op = result[0];
|
|
140
|
-
const text = result[1];
|
|
141
|
-
switch (op) {
|
|
142
|
-
case diff.INSERT:
|
|
143
|
-
case diff.DELETE:
|
|
144
|
-
batch.push(result);
|
|
145
|
-
break;
|
|
146
|
-
case diff.EQUAL:
|
|
147
|
-
if (results.length) {
|
|
148
|
-
if (batch.length) {
|
|
149
|
-
if (LINE_ENDING_RE.test(text)) {
|
|
150
|
-
flush();
|
|
151
|
-
offset += text.length;
|
|
152
|
-
} else {
|
|
153
|
-
batch.push(result);
|
|
154
|
-
}
|
|
155
|
-
} else {
|
|
156
|
-
offset += text.length;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
break;
|
|
160
|
-
default:
|
|
161
|
-
throw new Error(`Unexpected fast-diff operation "${op}"`);
|
|
162
|
-
}
|
|
163
|
-
if (batch.length && !results.length) {
|
|
164
|
-
flush();
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
return differences;
|
|
169
|
-
|
|
170
|
-
function flush() {
|
|
171
|
-
let aheadDeleteText = '';
|
|
172
|
-
let aheadInsertText = '';
|
|
173
|
-
while (batch.length) {
|
|
174
|
-
const next = batch.shift();
|
|
175
|
-
const op = next[0];
|
|
176
|
-
const text = next[1];
|
|
177
|
-
switch (op) {
|
|
178
|
-
case diff.INSERT:
|
|
179
|
-
aheadInsertText += text;
|
|
180
|
-
break;
|
|
181
|
-
case diff.DELETE:
|
|
182
|
-
aheadDeleteText += text;
|
|
183
|
-
break;
|
|
184
|
-
case diff.EQUAL:
|
|
185
|
-
aheadDeleteText += text;
|
|
186
|
-
aheadInsertText += text;
|
|
187
|
-
break;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
if (aheadDeleteText && aheadInsertText) {
|
|
191
|
-
differences.push({
|
|
192
|
-
offset,
|
|
193
|
-
operation: OPERATION_REPLACE,
|
|
194
|
-
insertText: aheadInsertText,
|
|
195
|
-
deleteText: aheadDeleteText
|
|
196
|
-
});
|
|
197
|
-
} else if (!aheadDeleteText && aheadInsertText) {
|
|
198
|
-
differences.push({
|
|
199
|
-
offset,
|
|
200
|
-
operation: OPERATION_INSERT,
|
|
201
|
-
insertText: aheadInsertText
|
|
202
|
-
});
|
|
203
|
-
} else if (aheadDeleteText && !aheadInsertText) {
|
|
204
|
-
differences.push({
|
|
205
|
-
offset,
|
|
206
|
-
operation: OPERATION_DELETE,
|
|
207
|
-
deleteText: aheadDeleteText
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
offset += aheadDeleteText.length;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
30
|
// ------------------------------------------------------------------------------
|
|
215
31
|
// Rule Definition
|
|
216
32
|
// ------------------------------------------------------------------------------
|
|
@@ -223,7 +39,7 @@ function generateDifferences(source, prettierSource) {
|
|
|
223
39
|
* @returns {void}
|
|
224
40
|
*/
|
|
225
41
|
function reportInsert(context, offset, text) {
|
|
226
|
-
const pos = getLocFromIndex(
|
|
42
|
+
const pos = context.getSourceCode().getLocFromIndex(offset);
|
|
227
43
|
const range = [offset, offset];
|
|
228
44
|
context.report({
|
|
229
45
|
message: 'Insert `{{ code }}`',
|
|
@@ -243,8 +59,8 @@ function reportInsert(context, offset, text) {
|
|
|
243
59
|
* @returns {void}
|
|
244
60
|
*/
|
|
245
61
|
function reportDelete(context, offset, text) {
|
|
246
|
-
const start = getLocFromIndex(
|
|
247
|
-
const end = getLocFromIndex(
|
|
62
|
+
const start = context.getSourceCode().getLocFromIndex(offset);
|
|
63
|
+
const end = context.getSourceCode().getLocFromIndex(offset + text.length);
|
|
248
64
|
const range = [offset, offset + text.length];
|
|
249
65
|
context.report({
|
|
250
66
|
message: 'Delete `{{ code }}`',
|
|
@@ -266,8 +82,10 @@ function reportDelete(context, offset, text) {
|
|
|
266
82
|
* @returns {void}
|
|
267
83
|
*/
|
|
268
84
|
function reportReplace(context, offset, deleteText, insertText) {
|
|
269
|
-
const start = getLocFromIndex(
|
|
270
|
-
const end =
|
|
85
|
+
const start = context.getSourceCode().getLocFromIndex(offset);
|
|
86
|
+
const end = context
|
|
87
|
+
.getSourceCode()
|
|
88
|
+
.getLocFromIndex(offset + deleteText.length);
|
|
271
89
|
const range = [offset, offset + deleteText.length];
|
|
272
90
|
context.report({
|
|
273
91
|
message: 'Replace `{{ deleteCode }}` with `{{ insertCode }}`',
|
|
@@ -282,32 +100,11 @@ function reportReplace(context, offset, deleteText, insertText) {
|
|
|
282
100
|
});
|
|
283
101
|
}
|
|
284
102
|
|
|
285
|
-
/**
|
|
286
|
-
* Get the pragma from the ESLint rule context.
|
|
287
|
-
* @param {RuleContext} context - The ESLint rule context.
|
|
288
|
-
* @returns {string|null}
|
|
289
|
-
*/
|
|
290
|
-
function getPragma(context) {
|
|
291
|
-
const pluginOptions = context.options[1];
|
|
292
|
-
|
|
293
|
-
if (!pluginOptions) {
|
|
294
|
-
return null;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
const pragmaRef =
|
|
298
|
-
typeof pluginOptions === 'string' ? pluginOptions : pluginOptions.pragma;
|
|
299
|
-
|
|
300
|
-
// Remove leading @
|
|
301
|
-
return pragmaRef ? pragmaRef.slice(1) : null;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
103
|
// ------------------------------------------------------------------------------
|
|
305
104
|
// Module Definition
|
|
306
105
|
// ------------------------------------------------------------------------------
|
|
307
106
|
|
|
308
107
|
module.exports = {
|
|
309
|
-
showInvisibles,
|
|
310
|
-
generateDifferences,
|
|
311
108
|
configs: {
|
|
312
109
|
recommended: {
|
|
313
110
|
extends: ['prettier'],
|
|
@@ -327,58 +124,26 @@ module.exports = {
|
|
|
327
124
|
schema: [
|
|
328
125
|
// Prettier options:
|
|
329
126
|
{
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
]
|
|
127
|
+
type: 'object',
|
|
128
|
+
properties: {},
|
|
129
|
+
additionalProperties: true
|
|
334
130
|
},
|
|
335
131
|
{
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
{ type: '
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
properties: {
|
|
342
|
-
pragma: { type: 'string', pattern: '^@\\w+$' },
|
|
343
|
-
usePrettierrc: { type: 'boolean' }
|
|
344
|
-
},
|
|
345
|
-
additionalProperties: true
|
|
346
|
-
}
|
|
347
|
-
]
|
|
132
|
+
type: 'object',
|
|
133
|
+
properties: {
|
|
134
|
+
usePrettierrc: { type: 'boolean' }
|
|
135
|
+
},
|
|
136
|
+
additionalProperties: true
|
|
348
137
|
}
|
|
349
138
|
]
|
|
350
139
|
},
|
|
351
140
|
create(context) {
|
|
352
|
-
const pragma = getPragma(context);
|
|
353
141
|
const usePrettierrc =
|
|
354
142
|
!context.options[1] || context.options[1].usePrettierrc !== false;
|
|
355
143
|
const sourceCode = context.getSourceCode();
|
|
144
|
+
const filepath = context.getFilename();
|
|
356
145
|
const source = sourceCode.text;
|
|
357
146
|
|
|
358
|
-
// The pragma is only valid if it is found in a block comment at the very
|
|
359
|
-
// start of the file.
|
|
360
|
-
if (pragma) {
|
|
361
|
-
// ESLint 3.x reports the shebang as a "Line" node, while ESLint 4.x
|
|
362
|
-
// reports it as a "Shebang" node. This works for both versions:
|
|
363
|
-
const hasShebang = source.startsWith('#!');
|
|
364
|
-
const allComments = sourceCode.getAllComments();
|
|
365
|
-
const firstComment = hasShebang ? allComments[1] : allComments[0];
|
|
366
|
-
if (
|
|
367
|
-
!(
|
|
368
|
-
firstComment &&
|
|
369
|
-
firstComment.type === 'Block' &&
|
|
370
|
-
firstComment.loc.start.line === (hasShebang ? 2 : 1) &&
|
|
371
|
-
firstComment.loc.start.column === 0
|
|
372
|
-
)
|
|
373
|
-
) {
|
|
374
|
-
return {};
|
|
375
|
-
}
|
|
376
|
-
const parsed = docblock.parse(firstComment.value);
|
|
377
|
-
if (parsed[pragma] !== '') {
|
|
378
|
-
return {};
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
|
|
382
147
|
if (prettier && prettier.clearConfigCache) {
|
|
383
148
|
prettier.clearConfigCache();
|
|
384
149
|
}
|
|
@@ -390,21 +155,58 @@ module.exports = {
|
|
|
390
155
|
prettier = require('prettier');
|
|
391
156
|
}
|
|
392
157
|
|
|
393
|
-
const eslintPrettierOptions =
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
158
|
+
const eslintPrettierOptions = context.options[0] || {};
|
|
159
|
+
|
|
160
|
+
const prettierRcOptions = usePrettierrc
|
|
161
|
+
? prettier.resolveConfig.sync(filepath, {
|
|
162
|
+
editorconfig: true
|
|
163
|
+
})
|
|
164
|
+
: null;
|
|
165
|
+
|
|
166
|
+
const prettierFileInfo = prettier.getFileInfo.sync(filepath, {
|
|
167
|
+
ignorePath: '.prettierignore'
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Skip if file is ignored using a .prettierignore file
|
|
171
|
+
if (prettierFileInfo.ignored) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const initialOptions = {};
|
|
176
|
+
|
|
177
|
+
// ESLint suppports processors that let you extract and lint JS
|
|
178
|
+
// fragments within a non-JS language. In the cases where prettier
|
|
179
|
+
// supports the same language as a processor, we want to process
|
|
180
|
+
// the provided source code as javascript (as ESLint provides the
|
|
181
|
+
// rules with fragments of JS) instead of guessing the parser
|
|
182
|
+
// based off the filename. Otherwise, for instance, on a .md file we
|
|
183
|
+
// end up trying to run prettier over a fragment of JS using the
|
|
184
|
+
// markdown parser, which throws an error.
|
|
185
|
+
// If we can't infer the parser from from the filename, either
|
|
186
|
+
// because no filename was provided or because there is no parser
|
|
187
|
+
// found for the filename, use javascript.
|
|
188
|
+
// This is added to the options first, so that
|
|
189
|
+
// prettierRcOptions and eslintPrettierOptions can still override
|
|
190
|
+
// the parser.
|
|
191
|
+
//
|
|
192
|
+
// `parserBlocklist` should contain the list of prettier parser
|
|
193
|
+
// names for file types where:
|
|
194
|
+
// * Prettier supports parsing the file type
|
|
195
|
+
// * There is an ESLint processor that extracts JavaScript snippets
|
|
196
|
+
// from the file type.
|
|
197
|
+
const parserBlocklist = [null, 'graphql', 'markdown', 'html'];
|
|
198
|
+
if (
|
|
199
|
+
parserBlocklist.indexOf(prettierFileInfo.inferredParser) !== -1
|
|
200
|
+
) {
|
|
201
|
+
initialOptions.parser = 'babylon';
|
|
202
|
+
}
|
|
203
|
+
|
|
403
204
|
const prettierOptions = Object.assign(
|
|
404
205
|
{},
|
|
206
|
+
initialOptions,
|
|
405
207
|
prettierRcOptions,
|
|
406
208
|
eslintPrettierOptions,
|
|
407
|
-
{ filepath
|
|
209
|
+
{ filepath }
|
|
408
210
|
);
|
|
409
211
|
|
|
410
212
|
const prettierSource = prettier.format(source, prettierOptions);
|
|
@@ -413,21 +215,21 @@ module.exports = {
|
|
|
413
215
|
|
|
414
216
|
differences.forEach(difference => {
|
|
415
217
|
switch (difference.operation) {
|
|
416
|
-
case
|
|
218
|
+
case INSERT:
|
|
417
219
|
reportInsert(
|
|
418
220
|
context,
|
|
419
221
|
difference.offset,
|
|
420
222
|
difference.insertText
|
|
421
223
|
);
|
|
422
224
|
break;
|
|
423
|
-
case
|
|
225
|
+
case DELETE:
|
|
424
226
|
reportDelete(
|
|
425
227
|
context,
|
|
426
228
|
difference.offset,
|
|
427
229
|
difference.deleteText
|
|
428
230
|
);
|
|
429
231
|
break;
|
|
430
|
-
case
|
|
232
|
+
case REPLACE:
|
|
431
233
|
reportReplace(
|
|
432
234
|
context,
|
|
433
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",
|
|
@@ -15,7 +15,9 @@
|
|
|
15
15
|
"main": "eslint-plugin-prettier.js",
|
|
16
16
|
"scripts": {
|
|
17
17
|
"lint": "eslint .",
|
|
18
|
-
"test": "npm run lint && mocha"
|
|
18
|
+
"test": "npm run lint && mocha",
|
|
19
|
+
"format": "yarn run prettier '**/*.{js,json,md,yml}' --write && yarn run lint --fix",
|
|
20
|
+
"generate-release": "node-release-script"
|
|
19
21
|
},
|
|
20
22
|
"repository": {
|
|
21
23
|
"type": "git",
|
|
@@ -26,27 +28,26 @@
|
|
|
26
28
|
},
|
|
27
29
|
"homepage": "https://github.com/prettier/eslint-plugin-prettier#readme",
|
|
28
30
|
"dependencies": {
|
|
29
|
-
"
|
|
30
|
-
"jest-docblock": "^21.0.0"
|
|
31
|
+
"prettier-linter-helpers": "^1.0.0"
|
|
31
32
|
},
|
|
32
33
|
"peerDependencies": {
|
|
33
|
-
"
|
|
34
|
+
"eslint": ">= 5.0.0",
|
|
35
|
+
"prettier": ">= 1.13.0"
|
|
34
36
|
},
|
|
35
37
|
"devDependencies": {
|
|
36
|
-
"
|
|
37
|
-
"eslint
|
|
38
|
-
"eslint-config-
|
|
39
|
-
"eslint-
|
|
40
|
-
"eslint-plugin-
|
|
41
|
-
"eslint-plugin-
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"prettier": "^1.
|
|
45
|
-
"
|
|
46
|
-
"vue-eslint-parser": "^2.0.2"
|
|
38
|
+
"@not-an-aardvark/node-release-script": "^0.1.0",
|
|
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
|
+
"prettier": "^1.13.0",
|
|
47
|
+
"vue-eslint-parser": "^3.2.2"
|
|
47
48
|
},
|
|
48
49
|
"engines": {
|
|
49
|
-
"node": ">=
|
|
50
|
+
"node": ">=6.0.0"
|
|
50
51
|
},
|
|
51
52
|
"license": "MIT"
|
|
52
53
|
}
|