eslint-plugin-prettier 2.6.2 → 3.1.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 CHANGED
@@ -1,5 +1,83 @@
1
1
  # Changelog
2
2
 
3
+ ## v3.1.0 (2019-05-11)
4
+
5
+ * New: Allow options to be passed to prettier.getFileInfo ([#187](https://github.com/prettier/eslint-plugin-prettier/issues/187)) ([21fa69a](https://github.com/prettier/eslint-plugin-prettier/commit/21fa69a8ed3b6acfc5461f6c3332444c21e65e28))
6
+ * build(deps-dev): bump eslint-plugin-eslint-plugin from 2.0.1 to 2.1.0 ([bb597e1](https://github.com/prettier/eslint-plugin-prettier/commit/bb597e14aba46211fd4149d0b0f1bdc51fe76452))
7
+ * build(deps-dev): bump eslint-config-prettier from 4.1.0 to 4.2.0 ([0bb7c1d](https://github.com/prettier/eslint-plugin-prettier/commit/0bb7c1d361b581fddebd64bf172b5dedcad5149c))
8
+ * build(deps-dev): bump vue-eslint-parser from 6.0.3 to 6.0.4 ([2f77df4](https://github.com/prettier/eslint-plugin-prettier/commit/2f77df48f151d4975bbdb29ced8c74a72d011428))
9
+ * build(deps-dev): bump mocha from 6.1.3 to 6.1.4 ([222b87a](https://github.com/prettier/eslint-plugin-prettier/commit/222b87a347331b20b3e7f65dcdfaa491bd277b3a))
10
+ * build(deps-dev): bump prettier from 1.16.4 to 1.17.0 ([58d8ff8](https://github.com/prettier/eslint-plugin-prettier/commit/58d8ff8ab2b1f73c904f5492eb523d7ea585ed8f))
11
+ * build(deps-dev): bump mocha from 6.1.2 to 6.1.3 ([e94e56c](https://github.com/prettier/eslint-plugin-prettier/commit/e94e56c36018aab6e08452fbe05bb16a23d64197))
12
+ * build(deps-dev): bump mocha from 6.1.1 to 6.1.2 ([c02244b](https://github.com/prettier/eslint-plugin-prettier/commit/c02244b197893f4e2a214d43f755b726cecbd03c))
13
+ * build(deps-dev): bump mocha from 6.0.2 to 6.1.1 ([a9a2e4e](https://github.com/prettier/eslint-plugin-prettier/commit/a9a2e4e3c0a243ec73061c10d9c4a5ae0c0e6f68))
14
+ * build(deps-dev): bump eslint from 5.15.3 to 5.16.0 ([073c14c](https://github.com/prettier/eslint-plugin-prettier/commit/073c14c2ae5c43d0939fe6f475561f1cf3d7a3e5))
15
+ * build(deps-dev): bump eslint from 5.15.2 to 5.15.3 ([bda931f](https://github.com/prettier/eslint-plugin-prettier/commit/bda931f4f1344f6927fbfd3a35965d1a4d319642))
16
+ * build(deps-dev): bump eslint from 5.15.1 to 5.15.2 ([19f53d6](https://github.com/prettier/eslint-plugin-prettier/commit/19f53d6a94a701e0aab9630bef93051aec4dfd36))
17
+ * build(deps-dev): bump eslint from 5.15.0 to 5.15.1 ([34b39de](https://github.com/prettier/eslint-plugin-prettier/commit/34b39dec2e6e283da1ca6faa0c636c5361efb5b9))
18
+ * build(deps-dev): bump eslint from 5.14.1 to 5.15.0 ([13bcc66](https://github.com/prettier/eslint-plugin-prettier/commit/13bcc66c120d614c17040e329360510feab47e7d))
19
+ * build(deps-dev): bump eslint-plugin-self from 1.1.0 to 1.2.0 ([5b4adb8](https://github.com/prettier/eslint-plugin-prettier/commit/5b4adb8ce683a93feddad07eda17d99b41849342))
20
+ * build(deps-dev): bump vue-eslint-parser from 6.0.2 to 6.0.3 ([e676cd1](https://github.com/prettier/eslint-plugin-prettier/commit/e676cd19387e70102467d9a82014906561f3c225))
21
+ * build(deps-dev): bump eslint-config-prettier from 4.0.0 to 4.1.0 ([b8a9215](https://github.com/prettier/eslint-plugin-prettier/commit/b8a9215515cdcb75faf212caeb00dfbcae11ee42))
22
+ * build(deps-dev): bump mocha from 6.0.1 to 6.0.2 ([cde36e4](https://github.com/prettier/eslint-plugin-prettier/commit/cde36e4db18ac4442eba3c75a20c1a6605e937d4))
23
+ * build(deps-dev): bump mocha from 6.0.0 to 6.0.1 ([eb39699](https://github.com/prettier/eslint-plugin-prettier/commit/eb39699b9bdf7c406a3134cc26c404947534661d))
24
+ * build(deps-dev): bump mocha from 5.2.0 to 6.0.0 ([5d75421](https://github.com/prettier/eslint-plugin-prettier/commit/5d75421d5e3ee5f8293b47a5825e1f2415f7e6b9))
25
+ * build(deps-dev): bump eslint from 5.14.0 to 5.14.1 ([829156e](https://github.com/prettier/eslint-plugin-prettier/commit/829156e467e53f554691afa687c13715086974f7))
26
+ * build(deps-dev): bump eslint from 5.13.0 to 5.14.0 ([b76d0b4](https://github.com/prettier/eslint-plugin-prettier/commit/b76d0b4471845143630b3603b97607665bf66ca0))
27
+ * build(deps-dev): bump vue-eslint-parser from 6.0.0 to 6.0.2 ([15439e8](https://github.com/prettier/eslint-plugin-prettier/commit/15439e8e0dcfa11a19f0cf249a1f4ad5f7fa5b96))
28
+ * build(deps-dev): bump vue-eslint-parser from 5.0.0 to 6.0.0 ([0ea70e5](https://github.com/prettier/eslint-plugin-prettier/commit/0ea70e5161d315ab93e6c4eb93f76d5304b8c162))
29
+ * build(deps-dev): bump eslint from 5.12.1 to 5.13.0 ([5f18729](https://github.com/prettier/eslint-plugin-prettier/commit/5f18729dbe359fe0df10730fd768a1ca6949b0a2))
30
+ * build(deps-dev): bump prettier from 1.16.3 to 1.16.4 ([ef637fe](https://github.com/prettier/eslint-plugin-prettier/commit/ef637fea4d6028b472cfe56dcb4fe30ee7939e0d))
31
+ * build(deps-dev): bump prettier from 1.16.1 to 1.16.3 ([58ab20c](https://github.com/prettier/eslint-plugin-prettier/commit/58ab20cc03f81a7d668998e64168eef7ad5b4365))
32
+ * build(deps-dev): bump eslint-config-prettier from 3.6.0 to 4.0.0 ([14393bd](https://github.com/prettier/eslint-plugin-prettier/commit/14393bdbcfd6114e810c10b4b7f905485474a36f))
33
+ * build(deps-dev): bump prettier from 1.16.0 to 1.16.1 ([00198b9](https://github.com/prettier/eslint-plugin-prettier/commit/00198b9795d1341f4c4a488c83f656e74f6bfdb0))
34
+ * build(deps-dev): bump prettier from 1.15.3 to 1.16.0 ([7890a87](https://github.com/prettier/eslint-plugin-prettier/commit/7890a876fc1c22b1fdee8724296eaa56eb6df1a3))
35
+ * build(deps-dev): bump eslint from 5.12.0 to 5.12.1 ([92a8984](https://github.com/prettier/eslint-plugin-prettier/commit/92a898470fbd88a4f5f4d8e1b15cf53bd7f8a92e))
36
+ * build(deps-dev): bump eslint-config-prettier from 3.5.0 to 3.6.0 ([5292d12](https://github.com/prettier/eslint-plugin-prettier/commit/5292d127dfd4f90ec6695d4060b5f5447a2c0119))
37
+ * build(deps-dev): bump eslint-config-prettier from 3.4.0 to 3.5.0 ([44a2558](https://github.com/prettier/eslint-plugin-prettier/commit/44a2558820d1c733f1572c98503e7b00b16b3fb7))
38
+ * build(deps-dev): bump eslint-config-prettier from 3.3.0 to 3.4.0 ([425cfce](https://github.com/prettier/eslint-plugin-prettier/commit/425cfce1bb712c96dfdd2292b04d89cceb375681))
39
+ * build(deps-dev): bump eslint from 5.11.1 to 5.12.0 ([3e9aa39](https://github.com/prettier/eslint-plugin-prettier/commit/3e9aa399ee3c0394a397f6ed3f8ec7c5e1597991))
40
+ * build(deps-dev): bump eslint-plugin-node from 8.0.0 to 8.0.1 ([e913afd](https://github.com/prettier/eslint-plugin-prettier/commit/e913afdd7291b5e58adf567f5e7a5bb9257dc9e3))
41
+ * build(deps-dev): bump vue-eslint-parser from 4.0.3 to 5.0.0 ([ecfd5ba](https://github.com/prettier/eslint-plugin-prettier/commit/ecfd5bab7bfb36e0203a334808243cb85aaeb512))
42
+
43
+ ## v3.0.1 (2018-12-28)
44
+
45
+ * 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))
46
+ * build(deps-dev): bump eslint from 5.11.0 to 5.11.1 ([d34daed](https://github.com/prettier/eslint-plugin-prettier/commit/d34daed47fbda09cbd19a73c38323e0aed0c30d5))
47
+ * build(deps-dev): bump eslint from 5.10.0 to 5.11.0 ([7f4f45d](https://github.com/prettier/eslint-plugin-prettier/commit/7f4f45dd132ecd72207b536b86910bebf15693b6))
48
+ * 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))
49
+ * build(deps-dev): bump eslint from 5.9.0 to 5.10.0 ([11e7c44](https://github.com/prettier/eslint-plugin-prettier/commit/11e7c447a8ebcfae213afe6ba872f96adb43e6b9))
50
+ * 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))
51
+ * 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))
52
+ * 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))
53
+ * Upgrade: Bump vue-eslint-parser from 3.2.2 to 3.3.0 ([2379e93](https://github.com/prettier/eslint-plugin-prettier/commit/2379e93c7fb81ddfe306c1fe6a10d1833cfddf2c))
54
+ * Upgrade: Bump eslint-config-prettier from 3.1.0 to 3.3.0 ([3ea0021](https://github.com/prettier/eslint-plugin-prettier/commit/3ea00218961b75e475def14372f9eab0de5ad05d))
55
+ * Upgrade: Bump eslint from 5.8.0 to 5.9.0 ([c774fb8](https://github.com/prettier/eslint-plugin-prettier/commit/c774fb87fe53d19389964883f05e77309b321139))
56
+ * 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))
57
+ * 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))
58
+ * 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))
59
+
60
+ ## v3.0.0 (2018-10-01)
61
+
62
+ * Chore: Add eslint peer-dependency ([d55d79c](https://github.com/prettier/eslint-plugin-prettier/commit/d55d79c6a64f659f405788fc75f344704619979f))
63
+ * Breaking: Extract showInvisibles and generateDifferences ([bf7c40c](https://github.com/prettier/eslint-plugin-prettier/commit/bf7c40c240d9833548a7c9d210a28c90a4f3957b))
64
+ * Breaking: Defining prettier options must use an object ([478c7e5](https://github.com/prettier/eslint-plugin-prettier/commit/478c7e5d2165f3e67e893c9a317b602159eaff9c))
65
+ * Breaking: Drop support for ESLint v3 and v4 ([2326231](https://github.com/prettier/eslint-plugin-prettier/commit/232623179b16b99c0cf89ec9b8ed7660c69b092d))
66
+ * Chore: Update dependencies ([1ec94c8](https://github.com/prettier/eslint-plugin-prettier/commit/1ec94c8e3495f6964588da5264b890cb49616fff))
67
+ * Chore: remove two unused dependencies ([bfe459c](https://github.com/prettier/eslint-plugin-prettier/commit/bfe459c39b742115137e81278f03f8e6abfd7dcf))
68
+ * Chore: Rename test files to keep them sequential ([d38ea52](https://github.com/prettier/eslint-plugin-prettier/commit/d38ea52debdf9da718c60933f42a709fa05f550f))
69
+ * Breaking: Remove pragma support ([3af422c](https://github.com/prettier/eslint-plugin-prettier/commit/3af422c8e301978b611cfc665e052d48c102b443))
70
+ * Breaking: Update minimum required pretter version to 1.13.0 ([29c0506](https://github.com/prettier/eslint-plugin-prettier/commit/29c050605674fda2975b3b620c89a7eb9332641a))
71
+ * Breaking: Drop support for node v4, v7 and v9 ([be460bd](https://github.com/prettier/eslint-plugin-prettier/commit/be460bdd06fafb04442b440efabc7b36b12934a7))
72
+ * Chore: Add vscode config to autoformat on save ([9fac6b4](https://github.com/prettier/eslint-plugin-prettier/commit/9fac6b4039c1983b83073fa7af7864f0d7e1f2d3))
73
+ * Chore: Improve travis matrix ([46d2444](https://github.com/prettier/eslint-plugin-prettier/commit/46d244409e397ba9ff2dea621e99a4ea90e0585b))
74
+ * Chore: Add format script to run prettier ([d46aa6d](https://github.com/prettier/eslint-plugin-prettier/commit/d46aa6dbd8028802121231d3ae0fe3f837bca9ad))
75
+
76
+ ## v2.7.0 (2018-09-26)
77
+
78
+ * 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))
79
+ * Build: switch to release script package ([047dc8f](https://github.com/prettier/eslint-plugin-prettier/commit/047dc8ffdf006c74267df4902fec684c589dad12))
80
+
3
81
  ## v2.6.2 (2018-07-06)
4
82
 
5
83
  * 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))
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,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
- ```sh
66
- npm install --save-dev eslint-config-prettier
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
- ```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
- * Enables `eslint-plugin-prettier`.
82
- * Sets the `prettier/prettier` rule to `"error"`.
83
- * 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
 
@@ -103,79 +99,39 @@ 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
- * The first option:
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
- - 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:
135
105
 
136
106
  ```json
137
- "prettier/prettier": ["error", null, "@prettier"]
107
+ "prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
138
108
  ```
139
109
 
140
- Only files with `@prettier` in the heading docblock will be checked:
141
-
142
- ```js
143
- /** @prettier */
144
-
145
- console.log(1 + 2 + 3);
146
- ```
110
+ NB: This option will merge and override any config set with `.prettierrc` files
147
111
 
148
- Or:
112
+ - The second option:
149
113
 
150
- ```js
151
- /**
152
- * @prettier
153
- */
114
+ - An object with the following options
154
115
 
155
- console.log(4 + 5 + 6);
156
- ```
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.
157
117
 
158
- _This option is useful if you're migrating a large codebase and already use pragmas like `@flow`._
159
-
160
- - 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
118
  ```json
165
- "prettier/prettier": ["error", null, {
166
- "pragma": "@prettier"
119
+ "prettier/prettier": ["error", {}, {
120
+ "usePrettierrc": false
167
121
  }]
168
122
  ```
169
-
170
- - `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
-
123
+
124
+ - `fileInfoOptions`: Options that are passed to [prettier.getFileInfo](https://prettier.io/docs/en/api.html#prettiergetfileinfofilepath-options) to decide whether a file needs to be formatted. Can be used for example to opt-out from ignoring files located in `node_modules` directories.
125
+
172
126
  ```json
173
- "prettier/prettier": ["error", null, {
174
- "usePrettierrc": false
127
+ "prettier/prettier": ["error", {}, {
128
+ "fileInfoOptions": {
129
+ "withNodeModules": true
130
+ }
175
131
  }]
176
132
  ```
177
133
 
178
- * The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.
134
+ - The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.
179
135
 
180
136
  ---
181
137
 
@@ -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,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(context, offset);
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(context, offset);
250
- const end = getLocFromIndex(context, offset + text.length);
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(context, offset);
273
- 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);
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,58 +124,33 @@ module.exports = {
330
124
  schema: [
331
125
  // Prettier options:
332
126
  {
333
- anyOf: [
334
- { enum: [null, 'fb'] },
335
- { type: 'object', properties: {}, additionalProperties: true }
336
- ]
127
+ type: 'object',
128
+ properties: {},
129
+ additionalProperties: true
337
130
  },
338
131
  {
339
- anyOf: [
340
- // Pragma:
341
- { type: 'string', pattern: '^@\\w+$' },
342
- {
132
+ type: 'object',
133
+ properties: {
134
+ usePrettierrc: { type: 'boolean' },
135
+ fileInfoOptions: {
343
136
  type: 'object',
344
- properties: {
345
- pragma: { type: 'string', pattern: '^@\\w+$' },
346
- usePrettierrc: { type: 'boolean' }
347
- },
137
+ properties: {},
348
138
  additionalProperties: true
349
139
  }
350
- ]
140
+ },
141
+ additionalProperties: true
351
142
  }
352
143
  ]
353
144
  },
354
145
  create(context) {
355
- const pragma = getPragma(context);
356
146
  const usePrettierrc =
357
147
  !context.options[1] || context.options[1].usePrettierrc !== false;
148
+ const eslintFileInfoOptions =
149
+ (context.options[1] && context.options[1].fileInfoOptions) || {};
358
150
  const sourceCode = context.getSourceCode();
151
+ const filepath = context.getFilename();
359
152
  const source = sourceCode.text;
360
153
 
361
- // The pragma is only valid if it is found in a block comment at the very
362
- // start of the file.
363
- if (pragma) {
364
- // ESLint 3.x reports the shebang as a "Line" node, while ESLint 4.x
365
- // reports it as a "Shebang" node. This works for both versions:
366
- const hasShebang = source.startsWith('#!');
367
- const allComments = sourceCode.getAllComments();
368
- const firstComment = hasShebang ? allComments[1] : allComments[0];
369
- if (
370
- !(
371
- firstComment &&
372
- firstComment.type === 'Block' &&
373
- firstComment.loc.start.line === (hasShebang ? 2 : 1) &&
374
- firstComment.loc.start.column === 0
375
- )
376
- ) {
377
- return {};
378
- }
379
- const parsed = docblock.parse(firstComment.value);
380
- if (parsed[pragma] !== '') {
381
- return {};
382
- }
383
- }
384
-
385
154
  if (prettier && prettier.clearConfigCache) {
386
155
  prettier.clearConfigCache();
387
156
  }
@@ -393,46 +162,119 @@ module.exports = {
393
162
  prettier = require('prettier');
394
163
  }
395
164
 
396
- const eslintPrettierOptions =
397
- context.options[0] === 'fb'
398
- ? FB_PRETTIER_OPTIONS
399
- : context.options[0];
400
- const prettierRcOptions =
401
- usePrettierrc &&
402
- prettier.resolveConfig &&
403
- prettier.resolveConfig.sync
404
- ? prettier.resolveConfig.sync(context.getFilename(), {
405
- editorconfig: true
406
- })
407
- : null;
165
+ const eslintPrettierOptions = context.options[0] || {};
166
+
167
+ const prettierRcOptions = usePrettierrc
168
+ ? prettier.resolveConfig.sync(filepath, {
169
+ editorconfig: true
170
+ })
171
+ : null;
172
+
173
+ const prettierFileInfo = prettier.getFileInfo.sync(
174
+ filepath,
175
+ Object.assign(
176
+ {},
177
+ { ignorePath: '.prettierignore' },
178
+ eslintFileInfoOptions
179
+ )
180
+ );
181
+
182
+ // Skip if file is ignored using a .prettierignore file
183
+ if (prettierFileInfo.ignored) {
184
+ return;
185
+ }
186
+
187
+ const initialOptions = {};
188
+
189
+ // ESLint suppports processors that let you extract and lint JS
190
+ // fragments within a non-JS language. In the cases where prettier
191
+ // supports the same language as a processor, we want to process
192
+ // the provided source code as javascript (as ESLint provides the
193
+ // rules with fragments of JS) instead of guessing the parser
194
+ // based off the filename. Otherwise, for instance, on a .md file we
195
+ // end up trying to run prettier over a fragment of JS using the
196
+ // markdown parser, which throws an error.
197
+ // If we can't infer the parser from from the filename, either
198
+ // because no filename was provided or because there is no parser
199
+ // found for the filename, use javascript.
200
+ // This is added to the options first, so that
201
+ // prettierRcOptions and eslintPrettierOptions can still override
202
+ // the parser.
203
+ //
204
+ // `parserBlocklist` should contain the list of prettier parser
205
+ // names for file types where:
206
+ // * Prettier supports parsing the file type
207
+ // * There is an ESLint processor that extracts JavaScript snippets
208
+ // from the file type.
209
+ const parserBlocklist = [null, 'graphql', 'markdown', 'html'];
210
+ if (
211
+ parserBlocklist.indexOf(prettierFileInfo.inferredParser) !== -1
212
+ ) {
213
+ initialOptions.parser = 'babylon';
214
+ }
215
+
408
216
  const prettierOptions = Object.assign(
409
217
  {},
218
+ initialOptions,
410
219
  prettierRcOptions,
411
220
  eslintPrettierOptions,
412
- { filepath: context.getFilename() }
221
+ { filepath }
413
222
  );
414
223
 
415
- const prettierSource = prettier.format(source, prettierOptions);
224
+ // prettier.format() may throw a SyntaxError if it cannot parse the
225
+ // source code it is given. Ususally for JS files this isn't a
226
+ // problem as ESLint will report invalid syntax before trying to
227
+ // pass it to the prettier plugin. However this might be a problem
228
+ // for non-JS languages that are handled by a plugin. Notably Vue
229
+ // files throw an error if they contain unclosed elements, such as
230
+ // `<template><div></template>. In this case report an error at the
231
+ // point at which parsing failed.
232
+ let prettierSource;
233
+ try {
234
+ prettierSource = prettier.format(source, prettierOptions);
235
+ } catch (err) {
236
+ if (!(err instanceof SyntaxError)) {
237
+ throw err;
238
+ }
239
+
240
+ let message = 'Parsing error: ' + err.message;
241
+
242
+ // Prettier's message contains a codeframe style preview of the
243
+ // invalid code and the line/column at which the error occured.
244
+ // ESLint shows those pieces of information elsewhere already so
245
+ // remove them from the message
246
+ if (err.codeFrame) {
247
+ message = message.replace(`\n${err.codeFrame}`, '');
248
+ }
249
+ if (err.loc) {
250
+ message = message.replace(/ \(\d+:\d+\)$/, '');
251
+ }
252
+
253
+ context.report({ message, loc: err.loc });
254
+
255
+ return;
256
+ }
257
+
416
258
  if (source !== prettierSource) {
417
259
  const differences = generateDifferences(source, prettierSource);
418
260
 
419
261
  differences.forEach(difference => {
420
262
  switch (difference.operation) {
421
- case OPERATION_INSERT:
263
+ case INSERT:
422
264
  reportInsert(
423
265
  context,
424
266
  difference.offset,
425
267
  difference.insertText
426
268
  );
427
269
  break;
428
- case OPERATION_DELETE:
270
+ case DELETE:
429
271
  reportDelete(
430
272
  context,
431
273
  difference.offset,
432
274
  difference.deleteText
433
275
  );
434
276
  break;
435
- case OPERATION_REPLACE:
277
+ case REPLACE:
436
278
  reportReplace(
437
279
  context,
438
280
  difference.offset,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-prettier",
3
- "version": "2.6.2",
3
+ "version": "3.1.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
- "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": "^4.0.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": "^6.0.0",
46
+ "prettier": "^1.15.3",
47
+ "vue-eslint-parser": "^6.0.0"
47
48
  },
48
49
  "engines": {
49
- "node": ">=4.0.0"
50
+ "node": ">=6.0.0"
50
51
  },
51
52
  "license": "MIT"
52
53
  }