eslint-plugin-prettier 3.1.4 → 3.4.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,36 @@
1
1
  # Changelog
2
2
 
3
+ ## v3.4.0 (2021-04-15)
4
+
5
+ * feat: support processor virtual filename ([#401](git@github.com:prettier/eslint-plugin-prettier/issues/401)) ([ee0ccc6](git@github.com:prettier/eslint-plugin-prettier/commit/ee0ccc6ac06d13cd546e78b444e53164f59eb27f))
6
+ * Simplify report logic ([#380](git@github.com:prettier/eslint-plugin-prettier/issues/380)) ([d993f24](git@github.com:prettier/eslint-plugin-prettier/commit/d993f247b5661683af031ab3b93955a0dfe448fa))
7
+ * Update: README.md ([#375](git@github.com:prettier/eslint-plugin-prettier/issues/375)) ([3ea4242](git@github.com:prettier/eslint-plugin-prettier/commit/3ea4242a8d4acdb76eb7e7dca9e44d3e87db70e3))
8
+
9
+ ## v3.3.1 (2021-01-04)
10
+
11
+ * fix: add eslint-config-prettier as an optional peer dependency ([#374](git@github.com:prettier/eslint-plugin-prettier/issues/374)) ([d59df27](git@github.com:prettier/eslint-plugin-prettier/commit/d59df27890aaffec9e528ceb3155831a0261848d))
12
+ * build(deps-dev): bump eslint from 7.16.0 to 7.17.0 ([b87985d](git@github.com:prettier/eslint-plugin-prettier/commit/b87985d8b1986743374b56691bcc1633df8f4eae))
13
+ * build(deps-dev): bump eslint from 7.15.0 to 7.16.0 ([11e427e](git@github.com:prettier/eslint-plugin-prettier/commit/11e427e5d6cedeb26e3e03c8143be3496a24955a))
14
+
15
+ ## v3.3.0 (2020-12-13)
16
+
17
+ * Minor: Perf improvement: Do not clear the config cache on each run ([#368](git@github.com:prettier/eslint-plugin-prettier/issues/368)) ([1b90ea7](git@github.com:prettier/eslint-plugin-prettier/commit/1b90ea752636959babb27ebca5d67093c346dab9))
18
+ * Add peerDependenciesMeta block ([#367](git@github.com:prettier/eslint-plugin-prettier/issues/367)) ([86608d5](git@github.com:prettier/eslint-plugin-prettier/commit/86608d5084692ab0d1f2f49a3df4909d04c39ae7))
19
+ * build(deps-dev): bump eslint from 7.14.0 to 7.15.0 ([885f484](git@github.com:prettier/eslint-plugin-prettier/commit/885f48405e0fc9f312acdd3e3487c824bd59c102))
20
+ * build(deps-dev): bump eslint from 7.3.1 to 7.14.0 ([cebc80b](git@github.com:prettier/eslint-plugin-prettier/commit/cebc80b39d3d09f957a73536e54f6d8dd4567080))
21
+
22
+ ## v3.2.0 (2020-12-03)
23
+
24
+ * Skip CI for eslint 6 + node 8 ([#364](git@github.com:prettier/eslint-plugin-prettier/issues/364)) ([f8f08e4](git@github.com:prettier/eslint-plugin-prettier/commit/f8f08e483522d74bc4dd93d9813914aa7ba9314b))
25
+ * Turn off problematic rules in recommended config (prepare for next eslint-config-prettier version) ([#360](git@github.com:prettier/eslint-plugin-prettier/issues/360)) ([a1e5591](git@github.com:prettier/eslint-plugin-prettier/commit/a1e559112073eedfb0dd2041b9c2f6ef775844ec))
26
+ * Create dependabot.yml ([f58b6c7](git@github.com:prettier/eslint-plugin-prettier/commit/f58b6c7c356a37b437593cd6ff8d1dca1c437b13))
27
+ * docs(README): fix prettier getFileInfo link ([#335](git@github.com:prettier/eslint-plugin-prettier/issues/335)) ([5a690f1](git@github.com:prettier/eslint-plugin-prettier/commit/5a690f14d793ba5a08c55287fa3d6338dcda21ba))
28
+ * build(deps-dev): bump eslint-plugin-eslint-plugin from 2.2.2 to 2.3.0 ([8614c45](git@github.com:prettier/eslint-plugin-prettier/commit/8614c458ed284bc126034d432b49b07d7d67ef06))
29
+ * build(deps-dev): bump eslint from 7.3.0 to 7.3.1 ([12d9ed8](git@github.com:prettier/eslint-plugin-prettier/commit/12d9ed877aacfad2c27f01161cc2eb28a445725f))
30
+ * build(deps-dev): bump eslint from 7.2.0 to 7.3.0 ([5a6f42e](git@github.com:prettier/eslint-plugin-prettier/commit/5a6f42e4eda871a294da1eb55f214c475450faa6))
31
+ * chore: update CI badge in readme ([5012b66](git@github.com:prettier/eslint-plugin-prettier/commit/5012b665f981edbc21feaaccb3cd297f49ca40d3))
32
+ * Use Github Actions for CI ([#305](git@github.com:prettier/eslint-plugin-prettier/issues/305)) ([41eb64f](git@github.com:prettier/eslint-plugin-prettier/commit/41eb64fda33663ed1c43a85218f390c6cd4b6191))
33
+
3
34
  ## v3.1.4 (2020-06-14)
4
35
 
5
36
  * Avoid clearing Prettier cache when not using prettierrc ([#303](git@github.com:prettier/eslint-plugin-prettier/issues/303)) ([3c8e2d9](git@github.com:prettier/eslint-plugin-prettier/commit/3c8e2d9871d86a82b10fe3d54f32bb5a54f2913b))
package/README.md CHANGED
@@ -1,7 +1,11 @@
1
- # eslint-plugin-prettier [![Build Status](https://travis-ci.org/prettier/eslint-plugin-prettier.svg?branch=master)](https://travis-ci.org/prettier/eslint-plugin-prettier)
1
+ # eslint-plugin-prettier [![Build Status](https://github.com/prettier/eslint-plugin-prettier/workflows/CI/badge.svg?branch=master)](https://github.com/prettier/eslint-plugin-prettier/actions?query=workflow%3ACI+branch%3Amaster)
2
2
 
3
3
  Runs [Prettier](https://github.com/prettier/prettier) as an [ESLint](http://eslint.org) rule and reports differences as individual ESLint issues.
4
4
 
5
+ If your desired formatting does not match Prettier’s output, you should use a different tool such as [prettier-eslint](https://github.com/prettier/prettier-eslint) instead.
6
+
7
+ Please read [Integrating with linters](https://prettier.io/docs/en/integrating-with-linters.html) before installing.
8
+
5
9
  ## Sample
6
10
 
7
11
  ```js
@@ -52,11 +56,9 @@ Then, in your `.eslintrc.json`:
52
56
 
53
57
  ## Recommended Configuration
54
58
 
55
- This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect patterns in the AST. (If another active ESLint rule disagrees with `prettier` about how code should be formatted, it will be impossible to avoid lint errors.) You can use [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) to disable all formatting-related ESLint rules.
59
+ This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect potential bugs. (If another active ESLint rule disagrees with `prettier` about how code should be formatted, it will be impossible to avoid lint errors.) You can use [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) to disable all formatting-related ESLint rules.
56
60
 
57
- If your desired formatting does not match the `prettier` output, you should use a different tool such as [prettier-eslint](https://github.com/prettier/prettier-eslint) instead.
58
-
59
- To integrate this plugin with `eslint-config-prettier`, you can use the `"recommended"` configuration:
61
+ This plugin ships with a `plugin:prettier/recommended` config that sets up both the plugin and `eslint-config-prettier` in one go.
60
62
 
61
63
  1. In addition to the above installation instructions, install `eslint-config-prettier`:
62
64
 
@@ -64,7 +66,7 @@ To integrate this plugin with `eslint-config-prettier`, you can use the `"recomm
64
66
  npm install --save-dev eslint-config-prettier
65
67
  ```
66
68
 
67
- 2. Then you need to add `plugin:prettier/recommended` as the last extension in your `.eslintrc.json`:
69
+ 2. Then you need to add `plugin:prettier/recommended` as the _last_ extension in your `.eslintrc.json`:
68
70
 
69
71
  ```json
70
72
  {
@@ -72,28 +74,36 @@ To integrate this plugin with `eslint-config-prettier`, you can use the `"recomm
72
74
  }
73
75
  ```
74
76
 
75
- This does three things:
76
-
77
- - Enables `eslint-plugin-prettier`.
78
- - Sets the `prettier/prettier` rule to `"error"`.
79
- - Extends the `eslint-config-prettier` configuration.
77
+ You can then set Prettier's own options inside a `.prettierrc` file.
80
78
 
81
- You can then set Prettier's own options inside a `.prettierrc` file.
82
-
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:
79
+ Exactly what does `plugin:prettier/recommended` do? Well, this is what it expands to:
84
80
 
85
81
  ```json
86
82
  {
87
- "extends": [
88
- "plugin:prettier/recommended",
89
- "prettier/flowtype",
90
- "prettier/react",
91
- "prettier/standard"
92
- ]
83
+ "extends": ["prettier"],
84
+ "plugins": ["prettier"],
85
+ "rules": {
86
+ "prettier/prettier": "error",
87
+ "arrow-body-style": "off",
88
+ "prefer-arrow-callback": "off"
89
+ }
93
90
  }
94
91
  ```
95
92
 
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).
93
+ - `"extends": ["prettier"]` enables the config from `eslint-config-prettier`, which turns off some ESLint rules that conflict with Prettier.
94
+ - `"plugins": ["prettier"]` registers this plugin.
95
+ - `"prettier/prettier": "error"` turns on the rule provided by this plugin, which runs Prettier from within ESLint.
96
+ - `"arrow-body-style": "off"` and `"prefer-arrow-callback": "off"` turns off two ESLint core rules that unfortunately are problematic with this plugin – see the next section.
97
+
98
+ ## `arrow-body-style` and `prefer-arrow-callback` issue
99
+
100
+ If you use [arrow-body-style](https://eslint.org/docs/rules/arrow-body-style) or [prefer-arrow-callback](https://eslint.org/docs/rules/prefer-arrow-callback) together with the `prettier/prettier` rule from this plugin, you can in some cases end up with invalid code due to a bug in ESLint’s autofix – see [issue #65](https://github.com/prettier/eslint-plugin-prettier/issues/65).
101
+
102
+ For this reason, it’s recommended to turn off these rules. The `plugin:prettier/recommended` config does that for you.
103
+
104
+ You _can_ still use these rules together with this plugin if you want, because the bug does not occur _all the time._ But if you do, you need to keep in mind that you might end up with invalid code, where you manually have to insert a missing closing parenthesis to get going again.
105
+
106
+ If you’re fixing large of amounts of previously unformatted code, consider temporarily disabling the `prettier/prettier` rule and running `eslint --fix` and `prettier --write` separately.
97
107
 
98
108
  ## Options
99
109
 
@@ -121,7 +131,7 @@ For the list of every available exclusion rule set, please see the [readme of es
121
131
  }]
122
132
  ```
123
133
 
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.
134
+ - `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
135
 
126
136
  ```json
127
137
  "prettier/prettier": ["error", {}, {
@@ -9,6 +9,9 @@
9
9
  // Requirements
10
10
  // ------------------------------------------------------------------------------
11
11
 
12
+ const fs = require('fs');
13
+ const path = require('path');
14
+
12
15
  const {
13
16
  showInvisibles,
14
17
  generateDifferences
@@ -25,6 +28,9 @@ const { INSERT, DELETE, REPLACE } = generateDifferences;
25
28
  // ------------------------------------------------------------------------------
26
29
 
27
30
  // Lazily-loaded Prettier.
31
+ /**
32
+ * @type {import('prettier')}
33
+ */
28
34
  let prettier;
29
35
 
30
36
  // ------------------------------------------------------------------------------
@@ -32,72 +38,49 @@ let prettier;
32
38
  // ------------------------------------------------------------------------------
33
39
 
34
40
  /**
35
- * Reports an "Insert ..." issue where text must be inserted.
36
- * @param {RuleContext} context - The ESLint rule context.
37
- * @param {number} offset - The source offset where to insert text.
38
- * @param {string} text - The text to be inserted.
41
+ * Reports a difference.
42
+ * @param {import('eslint').Rule.RuleContext} context - The ESLint rule context.
43
+ * @param {import('prettier-linter-helpers').Difference} difference - The difference object.
39
44
  * @returns {void}
40
45
  */
41
- function reportInsert(context, offset, text) {
42
- const pos = context.getSourceCode().getLocFromIndex(offset);
43
- const range = [offset, offset];
44
- context.report({
45
- message: 'Insert `{{ code }}`',
46
- data: { code: showInvisibles(text) },
47
- loc: { start: pos, end: pos },
48
- fix(fixer) {
49
- return fixer.insertTextAfterRange(range, text);
50
- }
51
- });
52
- }
46
+ function reportDifference(context, difference) {
47
+ const { operation, offset, deleteText = '', insertText = '' } = difference;
48
+ const range = [offset, offset + deleteText.length];
49
+ const [start, end] = range.map(index =>
50
+ context.getSourceCode().getLocFromIndex(index)
51
+ );
53
52
 
54
- /**
55
- * Reports a "Delete ..." issue where text must be deleted.
56
- * @param {RuleContext} context - The ESLint rule context.
57
- * @param {number} offset - The source offset where to delete text.
58
- * @param {string} text - The text to be deleted.
59
- * @returns {void}
60
- */
61
- function reportDelete(context, offset, text) {
62
- const start = context.getSourceCode().getLocFromIndex(offset);
63
- const end = context.getSourceCode().getLocFromIndex(offset + text.length);
64
- const range = [offset, offset + text.length];
65
53
  context.report({
66
- message: 'Delete `{{ code }}`',
67
- data: { code: showInvisibles(text) },
54
+ messageId: operation,
55
+ data: {
56
+ deleteText: showInvisibles(deleteText),
57
+ insertText: showInvisibles(insertText)
58
+ },
68
59
  loc: { start, end },
69
- fix(fixer) {
70
- return fixer.removeRange(range);
71
- }
60
+ fix: fixer => fixer.replaceTextRange(range, insertText)
72
61
  });
73
62
  }
74
63
 
75
64
  /**
76
- * Reports a "Replace ... with ..." issue where text must be replaced.
77
- * @param {RuleContext} context - The ESLint rule context.
78
- * @param {number} offset - The source offset where to replace deleted text
79
- with inserted text.
80
- * @param {string} deleteText - The text to be deleted.
81
- * @param {string} insertText - The text to be inserted.
82
- * @returns {void}
65
+ * Given a filepath, get the nearest path that is a regular file.
66
+ * The filepath provided by eslint may be a virtual filepath rather than a file
67
+ * on disk. This attempts to transform a virtual path into an on-disk path
68
+ * @param {string} filepath
69
+ * @returns {string}
83
70
  */
84
- function reportReplace(context, offset, deleteText, insertText) {
85
- const start = context.getSourceCode().getLocFromIndex(offset);
86
- const end = context
87
- .getSourceCode()
88
- .getLocFromIndex(offset + deleteText.length);
89
- const range = [offset, offset + deleteText.length];
90
- context.report({
91
- message: 'Replace `{{ deleteCode }}` with `{{ insertCode }}`',
92
- data: {
93
- deleteCode: showInvisibles(deleteText),
94
- insertCode: showInvisibles(insertText)
95
- },
96
- loc: { start, end },
97
- fix(fixer) {
98
- return fixer.replaceTextRange(range, insertText);
71
+ function getOnDiskFilepath(filepath) {
72
+ try {
73
+ if (fs.statSync(filepath).isFile()) {
74
+ return filepath;
99
75
  }
100
- });
76
+ } catch (err) {
77
+ // https://github.com/eslint/eslint/issues/11989
78
+ if (err.code === 'ENOTDIR') {
79
+ return getOnDiskFilepath(path.dirname(filepath));
80
+ }
81
+ }
82
+
83
+ return filepath;
101
84
  }
102
85
 
103
86
  // ------------------------------------------------------------------------------
@@ -110,7 +93,9 @@ module.exports = {
110
93
  extends: ['prettier'],
111
94
  plugins: ['prettier'],
112
95
  rules: {
113
- 'prettier/prettier': 'error'
96
+ 'prettier/prettier': 'error',
97
+ 'arrow-body-style': 'off',
98
+ 'prefer-arrow-callback': 'off'
114
99
  }
115
100
  }
116
101
  },
@@ -141,7 +126,12 @@ module.exports = {
141
126
  },
142
127
  additionalProperties: true
143
128
  }
144
- ]
129
+ ],
130
+ messages: {
131
+ [INSERT]: 'Insert `{{ insertText }}`',
132
+ [DELETE]: 'Delete `{{ deleteText }}`',
133
+ [REPLACE]: 'Replace `{{ deleteText }}` with `{{ insertText }}`'
134
+ }
145
135
  },
146
136
  create(context) {
147
137
  const usePrettierrc =
@@ -150,17 +140,16 @@ module.exports = {
150
140
  (context.options[1] && context.options[1].fileInfoOptions) || {};
151
141
  const sourceCode = context.getSourceCode();
152
142
  const filepath = context.getFilename();
143
+ // Processors that extract content from a file, such as the markdown
144
+ // plugin extracting fenced code blocks may choose to specify virtual
145
+ // file paths. If this is the case then we need to resolve prettier
146
+ // config and file info using the on-disk path instead of the virtual
147
+ // path.
148
+ // See https://github.com/eslint/eslint/issues/11989 for ideas around
149
+ // being able to get this value directly from eslint in the future.
150
+ const onDiskFilepath = getOnDiskFilepath(filepath);
153
151
  const source = sourceCode.text;
154
152
 
155
- // This allows long-running ESLint processes (e.g. vscode-eslint) to
156
- // pick up changes to .prettierrc without restarting the editor. This
157
- // will invalidate the prettier plugin cache on every file as well which
158
- // will make ESLint very slow, so it would probably be a good idea to
159
- // find a better way to do this.
160
- if (usePrettierrc && prettier && prettier.clearConfigCache) {
161
- prettier.clearConfigCache();
162
- }
163
-
164
153
  return {
165
154
  Program() {
166
155
  if (!prettier) {
@@ -171,13 +160,13 @@ module.exports = {
171
160
  const eslintPrettierOptions = context.options[0] || {};
172
161
 
173
162
  const prettierRcOptions = usePrettierrc
174
- ? prettier.resolveConfig.sync(filepath, {
163
+ ? prettier.resolveConfig.sync(onDiskFilepath, {
175
164
  editorconfig: true
176
165
  })
177
166
  : null;
178
167
 
179
168
  const prettierFileInfo = prettier.getFileInfo.sync(
180
- filepath,
169
+ onDiskFilepath,
181
170
  Object.assign(
182
171
  {},
183
172
  { resolveConfig: true, ignorePath: '.prettierignore' },
@@ -192,7 +181,7 @@ module.exports = {
192
181
 
193
182
  const initialOptions = {};
194
183
 
195
- // ESLint suppports processors that let you extract and lint JS
184
+ // ESLint supports processors that let you extract and lint JS
196
185
  // fragments within a non-JS language. In the cases where prettier
197
186
  // supports the same language as a processor, we want to process
198
187
  // the provided source code as javascript (as ESLint provides the
@@ -200,9 +189,14 @@ module.exports = {
200
189
  // based off the filename. Otherwise, for instance, on a .md file we
201
190
  // end up trying to run prettier over a fragment of JS using the
202
191
  // markdown parser, which throws an error.
203
- // If we can't infer the parser from from the filename, either
204
- // because no filename was provided or because there is no parser
205
- // found for the filename, use javascript.
192
+ // Processors may set virtual filenames for these extracted blocks.
193
+ // If they do so then we want to trust the file extension they
194
+ // provide, and no override is needed.
195
+ // If the processor does not set any virtual filename (signified by
196
+ // `filepath` and `onDiskFilepath` being equal) AND we can't
197
+ // infer the parser from the filename, either because no filename
198
+ // was provided or because there is no parser found for the
199
+ // filename, use javascript.
206
200
  // This is added to the options first, so that
207
201
  // prettierRcOptions and eslintPrettierOptions can still override
208
202
  // the parser.
@@ -214,6 +208,7 @@ module.exports = {
214
208
  // from the file type.
215
209
  const parserBlocklist = [null, 'graphql', 'markdown', 'html'];
216
210
  if (
211
+ filepath === onDiskFilepath &&
217
212
  parserBlocklist.indexOf(prettierFileInfo.inferredParser) !== -1
218
213
  ) {
219
214
  // Prettier v1.16.0 renamed the `babylon` parser to `babel`
@@ -234,7 +229,7 @@ module.exports = {
234
229
  );
235
230
 
236
231
  // prettier.format() may throw a SyntaxError if it cannot parse the
237
- // source code it is given. Ususally for JS files this isn't a
232
+ // source code it is given. Usually for JS files this isn't a
238
233
  // problem as ESLint will report invalid syntax before trying to
239
234
  // pass it to the prettier plugin. However this might be a problem
240
235
  // for non-JS languages that are handled by a plugin. Notably Vue
@@ -252,7 +247,7 @@ module.exports = {
252
247
  let message = 'Parsing error: ' + err.message;
253
248
 
254
249
  // Prettier's message contains a codeframe style preview of the
255
- // invalid code and the line/column at which the error occured.
250
+ // invalid code and the line/column at which the error occurred.
256
251
  // ESLint shows those pieces of information elsewhere already so
257
252
  // remove them from the message
258
253
  if (err.codeFrame) {
@@ -270,32 +265,9 @@ module.exports = {
270
265
  if (source !== prettierSource) {
271
266
  const differences = generateDifferences(source, prettierSource);
272
267
 
273
- differences.forEach(difference => {
274
- switch (difference.operation) {
275
- case INSERT:
276
- reportInsert(
277
- context,
278
- difference.offset,
279
- difference.insertText
280
- );
281
- break;
282
- case DELETE:
283
- reportDelete(
284
- context,
285
- difference.offset,
286
- difference.deleteText
287
- );
288
- break;
289
- case REPLACE:
290
- reportReplace(
291
- context,
292
- difference.offset,
293
- difference.deleteText,
294
- difference.insertText
295
- );
296
- break;
297
- }
298
- });
268
+ for (const difference of differences) {
269
+ reportDifference(context, difference);
270
+ }
299
271
  }
300
272
  }
301
273
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-prettier",
3
- "version": "3.1.4",
3
+ "version": "3.4.0",
4
4
  "description": "Runs prettier as an eslint rule",
5
5
  "keywords": [
6
6
  "eslint",
@@ -46,6 +46,11 @@
46
46
  "prettier": "^1.15.3",
47
47
  "vue-eslint-parser": "^6.0.0"
48
48
  },
49
+ "peerDependenciesMeta": {
50
+ "eslint-config-prettier": {
51
+ "optional": true
52
+ }
53
+ },
49
54
  "engines": {
50
55
  "node": ">=6.0.0"
51
56
  },