eslint-plugin-prettier 2.6.1 → 3.0.1

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 CHANGED
@@ -1,5 +1,48 @@
1
1
  # Changelog
2
2
 
3
+ ## v3.0.1 (2018-12-28)
4
+
5
+ * Catch and format SyntaxErrors as eslint violations ([#141](https://github.com/prettier/eslint-plugin-prettier/issues/141)) ([4a0e57d](https://github.com/prettier/eslint-plugin-prettier/commit/4a0e57ddcc0fa2ae8e8f7d8b65ddc4ac93d9f474))
6
+ * build(deps-dev): bump eslint from 5.11.0 to 5.11.1 ([d34daed](https://github.com/prettier/eslint-plugin-prettier/commit/d34daed47fbda09cbd19a73c38323e0aed0c30d5))
7
+ * build(deps-dev): bump eslint from 5.10.0 to 5.11.0 ([7f4f45d](https://github.com/prettier/eslint-plugin-prettier/commit/7f4f45dd132ecd72207b536b86910bebf15693b6))
8
+ * build(deps-dev): bump eslint-plugin-eslint-plugin from 2.0.0 to 2.0.1 ([5be3bcf](https://github.com/prettier/eslint-plugin-prettier/commit/5be3bcfce11b741cd35c92b9c972e457a4038766))
9
+ * build(deps-dev): bump eslint from 5.9.0 to 5.10.0 ([11e7c44](https://github.com/prettier/eslint-plugin-prettier/commit/11e7c447a8ebcfae213afe6ba872f96adb43e6b9))
10
+ * build(deps-dev): bump eslint-plugin-eslint-plugin from 1.4.1 to 2.0.0 ([9e5bf14](https://github.com/prettier/eslint-plugin-prettier/commit/9e5bf140451f82a36c78042315a9f88a12cfe45f))
11
+ * build(deps-dev): bump vue-eslint-parser from 4.0.2 to 4.0.3 ([234583a](https://github.com/prettier/eslint-plugin-prettier/commit/234583a19a97ecd1f996542ccb1178a26d20c0fd))
12
+ * build(deps-dev): bump vue-eslint-parser from 3.3.0 to 4.0.2 ([8675d57](https://github.com/prettier/eslint-plugin-prettier/commit/8675d5713f5171981119b89c2e8a58fda6b81259))
13
+ * Upgrade: Bump vue-eslint-parser from 3.2.2 to 3.3.0 ([2379e93](https://github.com/prettier/eslint-plugin-prettier/commit/2379e93c7fb81ddfe306c1fe6a10d1833cfddf2c))
14
+ * Upgrade: Bump eslint-config-prettier from 3.1.0 to 3.3.0 ([3ea0021](https://github.com/prettier/eslint-plugin-prettier/commit/3ea00218961b75e475def14372f9eab0de5ad05d))
15
+ * Upgrade: Bump eslint from 5.8.0 to 5.9.0 ([c774fb8](https://github.com/prettier/eslint-plugin-prettier/commit/c774fb87fe53d19389964883f05e77309b321139))
16
+ * build(deps-dev): bump eslint-plugin-node from 7.0.1 to 8.0.0 ([#121](https://github.com/prettier/eslint-plugin-prettier/issues/121)) ([2a4fba0](https://github.com/prettier/eslint-plugin-prettier/commit/2a4fba01222f62a576da48478e3dcd832e3bff7e))
17
+ * build(deps-dev): bump eslint-plugin-eslint-plugin from 1.4.0 to 1.4.1 ([#120](https://github.com/prettier/eslint-plugin-prettier/issues/120)) ([29caa29](https://github.com/prettier/eslint-plugin-prettier/commit/29caa299612db8af7a188749a5dd8b9827f51a67))
18
+ * build(deps-dev): bump eslint from 5.6.0 to 5.8.0 ([#119](https://github.com/prettier/eslint-plugin-prettier/issues/119)) ([2836350](https://github.com/prettier/eslint-plugin-prettier/commit/2836350829dc3c19b4c1ebca33a3a7905c1b28a5))
19
+
20
+ ## v3.0.0 (2018-10-01)
21
+
22
+ * Chore: Add eslint peer-dependency ([d55d79c](https://github.com/prettier/eslint-plugin-prettier/commit/d55d79c6a64f659f405788fc75f344704619979f))
23
+ * Breaking: Extract showInvisibles and generateDifferences ([bf7c40c](https://github.com/prettier/eslint-plugin-prettier/commit/bf7c40c240d9833548a7c9d210a28c90a4f3957b))
24
+ * Breaking: Defining prettier options must use an object ([478c7e5](https://github.com/prettier/eslint-plugin-prettier/commit/478c7e5d2165f3e67e893c9a317b602159eaff9c))
25
+ * Breaking: Drop support for ESLint v3 and v4 ([2326231](https://github.com/prettier/eslint-plugin-prettier/commit/232623179b16b99c0cf89ec9b8ed7660c69b092d))
26
+ * Chore: Update dependencies ([1ec94c8](https://github.com/prettier/eslint-plugin-prettier/commit/1ec94c8e3495f6964588da5264b890cb49616fff))
27
+ * Chore: remove two unused dependencies ([bfe459c](https://github.com/prettier/eslint-plugin-prettier/commit/bfe459c39b742115137e81278f03f8e6abfd7dcf))
28
+ * Chore: Rename test files to keep them sequential ([d38ea52](https://github.com/prettier/eslint-plugin-prettier/commit/d38ea52debdf9da718c60933f42a709fa05f550f))
29
+ * Breaking: Remove pragma support ([3af422c](https://github.com/prettier/eslint-plugin-prettier/commit/3af422c8e301978b611cfc665e052d48c102b443))
30
+ * Breaking: Update minimum required pretter version to 1.13.0 ([29c0506](https://github.com/prettier/eslint-plugin-prettier/commit/29c050605674fda2975b3b620c89a7eb9332641a))
31
+ * Breaking: Drop support for node v4, v7 and v9 ([be460bd](https://github.com/prettier/eslint-plugin-prettier/commit/be460bdd06fafb04442b440efabc7b36b12934a7))
32
+ * Chore: Add vscode config to autoformat on save ([9fac6b4](https://github.com/prettier/eslint-plugin-prettier/commit/9fac6b4039c1983b83073fa7af7864f0d7e1f2d3))
33
+ * Chore: Improve travis matrix ([46d2444](https://github.com/prettier/eslint-plugin-prettier/commit/46d244409e397ba9ff2dea621e99a4ea90e0585b))
34
+ * Chore: Add format script to run prettier ([d46aa6d](https://github.com/prettier/eslint-plugin-prettier/commit/d46aa6dbd8028802121231d3ae0fe3f837bca9ad))
35
+
36
+ ## v2.7.0 (2018-09-26)
37
+
38
+ * 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))
39
+ * Build: switch to release script package ([047dc8f](https://github.com/prettier/eslint-plugin-prettier/commit/047dc8ffdf006c74267df4902fec684c589dad12))
40
+
41
+ ## v2.6.2 (2018-07-06)
42
+
43
+ * 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))
44
+ * 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))
45
+
3
46
  ## v2.6.1 (2018-06-23)
4
47
 
5
48
  * Fix: respect editorconfig ([#92](https://github.com/prettier/eslint-plugin-prettier/issues/92)) ([0b04dd3](https://github.com/prettier/eslint-plugin-prettier/commit/0b04dd362d0d92534a7cf11eaebbab8eb59fc96d))
package/LICENSE.md CHANGED
@@ -1,5 +1,4 @@
1
- The MIT License (MIT)
2
- =====================
1
+ # The MIT License (MIT)
3
2
 
4
3
  Copyright © 2017 Andres Suarez and Teddy Katz
5
4
 
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
- ```sh
66
- npm install --save-dev eslint-config-prettier
67
- ```
63
+ ```sh
64
+ npm install --save-dev eslint-config-prettier
65
+ ```
68
66
 
69
- 2. Then all you need in your `.eslintrc.json` is:
67
+ 2. Then you need to add `plugin:prettier/recommended` as the last extension in your `.eslintrc.json`:
70
68
 
71
- ```json
72
- {
73
- "extends": [
74
- "plugin:prettier/recommended"
75
- ]
76
- }
77
- ```
69
+ ```json
70
+ {
71
+ "extends": ["plugin:prettier/recommended"]
72
+ }
73
+ ```
78
74
 
79
75
  This does three things:
80
76
 
81
- 1. Enables `eslint-plugin-prettier`.
82
- 2. Sets the `prettier/prettier` rule to `"error"`.
83
- 3. Extends the `eslint-config-prettier` configuration.
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
- ## Options
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
- - Or the string `"fb"` may be used to set "Facebook style" defaults:
85
+ ```json
86
+ {
87
+ "extends": [
88
+ "plugin:prettier/recommended",
89
+ "prettier/flowtype",
90
+ "prettier/react",
91
+ "prettier/standard"
92
+ ]
93
+ }
94
+ ```
99
95
 
100
- ```json
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
- Equivalent to:
98
+ ## Options
105
99
 
106
- ```json
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
- * The second option:
102
+ - The first option:
118
103
 
119
- - 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 `@`. Example:
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", null, "@prettier"]
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
- Or:
110
+ NB: This option will merge and override any config set with `.prettierrc` files
134
111
 
135
- ```js
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", null, {
119
+ "prettier/prettier": ["error", {}, {
159
120
  "usePrettierrc": false
160
121
  }]
161
122
  ```
162
123
 
163
- * The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.
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 diff = require('fast-diff');
13
- const docblock = require('jest-docblock');
12
+ const {
13
+ showInvisibles,
14
+ generateDifferences
15
+ } = require('prettier-linter-helpers');
14
16
 
15
17
  // ------------------------------------------------------------------------------
16
18
  // Constants
17
19
  // ------------------------------------------------------------------------------
18
20
 
19
- // Preferred Facebook style.
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(context, offset);
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(context, offset);
247
- const end = getLocFromIndex(context, offset + text.length);
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(context, offset);
270
- const end = getLocFromIndex(context, offset + deleteText.length);
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
- anyOf: [
331
- { enum: [null, 'fb'] },
332
- { type: 'object', properties: {}, additionalProperties: true }
333
- ]
127
+ type: 'object',
128
+ properties: {},
129
+ additionalProperties: true
334
130
  },
335
131
  {
336
- anyOf: [
337
- // Pragma:
338
- { type: 'string', pattern: '^@\\w+$' },
339
- {
340
- type: 'object',
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,46 +155,114 @@ module.exports = {
390
155
  prettier = require('prettier');
391
156
  }
392
157
 
393
- const eslintPrettierOptions =
394
- context.options[0] === 'fb'
395
- ? FB_PRETTIER_OPTIONS
396
- : context.options[0];
397
- const prettierRcOptions =
398
- usePrettierrc &&
399
- prettier.resolveConfig &&
400
- prettier.resolveConfig.sync
401
- ? prettier.resolveConfig.sync(context.getFilename(), {
402
- editorconfig: true
403
- })
404
- : null;
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
+
405
204
  const prettierOptions = Object.assign(
406
205
  {},
206
+ initialOptions,
407
207
  prettierRcOptions,
408
208
  eslintPrettierOptions,
409
- { filepath: context.getFilename() }
209
+ { filepath }
410
210
  );
411
211
 
412
- const prettierSource = prettier.format(source, prettierOptions);
212
+ // prettier.format() may throw a SyntaxError if it cannot parse the
213
+ // source code it is given. Ususally for JS files this isn't a
214
+ // problem as ESLint will report invalid syntax before trying to
215
+ // pass it to the prettier plugin. However this might be a problem
216
+ // for non-JS languages that are handled by a plugin. Notably Vue
217
+ // files throw an error if they contain unclosed elements, such as
218
+ // `<template><div></template>. In this case report an error at the
219
+ // point at which parsing failed.
220
+ let prettierSource;
221
+ try {
222
+ prettierSource = prettier.format(source, prettierOptions);
223
+ } catch (err) {
224
+ if (!(err instanceof SyntaxError)) {
225
+ throw err;
226
+ }
227
+
228
+ let message = 'Parsing error: ' + err.message;
229
+
230
+ // Prettier's message contains a codeframe style preview of the
231
+ // invalid code and the line/column at which the error occured.
232
+ // ESLint shows those pieces of information elsewhere already so
233
+ // remove them from the message
234
+ if (err.codeFrame) {
235
+ message = message.replace(`\n${err.codeFrame}`, '');
236
+ }
237
+ if (err.loc) {
238
+ message = message.replace(/ \(\d+:\d+\)$/, '');
239
+ }
240
+
241
+ context.report({ message, loc: err.loc });
242
+
243
+ return;
244
+ }
245
+
413
246
  if (source !== prettierSource) {
414
247
  const differences = generateDifferences(source, prettierSource);
415
248
 
416
249
  differences.forEach(difference => {
417
250
  switch (difference.operation) {
418
- case OPERATION_INSERT:
251
+ case INSERT:
419
252
  reportInsert(
420
253
  context,
421
254
  difference.offset,
422
255
  difference.insertText
423
256
  );
424
257
  break;
425
- case OPERATION_DELETE:
258
+ case DELETE:
426
259
  reportDelete(
427
260
  context,
428
261
  difference.offset,
429
262
  difference.deleteText
430
263
  );
431
264
  break;
432
- case OPERATION_REPLACE:
265
+ case REPLACE:
433
266
  reportReplace(
434
267
  context,
435
268
  difference.offset,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-prettier",
3
- "version": "2.6.1",
3
+ "version": "3.0.1",
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
- "fast-diff": "^1.1.1",
30
- "jest-docblock": "^21.0.0"
31
+ "prettier-linter-helpers": "^1.0.0"
31
32
  },
32
33
  "peerDependencies": {
33
- "prettier": ">= 0.11.0"
34
+ "eslint": ">= 5.0.0",
35
+ "prettier": ">= 1.13.0"
34
36
  },
35
37
  "devDependencies": {
36
- "eslint": "^3.14.1",
37
- "eslint-config-not-an-aardvark": "^2.0.0",
38
- "eslint-config-prettier": "^1.3.0",
39
- "eslint-plugin-eslint-plugin": "^0.7.1",
40
- "eslint-plugin-node": "^4.2.2",
41
- "eslint-plugin-self": "^1.0.1",
42
- "mocha": "^3.1.2",
43
- "moment": "^2.18.1",
44
- "prettier": "^1.10.2",
45
- "semver": "^5.3.0",
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": "^2.0.0",
43
+ "eslint-plugin-node": "^8.0.0",
44
+ "eslint-plugin-self": "^1.1.0",
45
+ "mocha": "^5.2.0",
46
+ "prettier": "^1.15.3",
47
+ "vue-eslint-parser": "^4.0.2"
47
48
  },
48
49
  "engines": {
49
- "node": ">=4.0.0"
50
+ "node": ">=6.0.0"
50
51
  },
51
52
  "license": "MIT"
52
53
  }