Skip to content

Commit 38537ba

Browse files
BPScottnot-an-aardvark
authored andcommittedSep 26, 2018
Update: Support prettierignore and custom processors (#111)
* Ignore files in .prettierignore Fixes #88 * Force the babylon parser when parsing not-js files Forcing the parser stops errors that are caused by trying to run a JS fragment through the graphql / markdown parsers. The 'html' parser has not yet been released yet, but will be in Prettier 1.15. This uses a block list over an allow list because I expect the list of "file types with a prettier parser that could contain javascript fragments" will grow at a slower pace than "file types with a prettier parser that are variations of the javascript language". Fixes #98, Fixes #81
1 parent 047dc8f commit 38537ba

File tree

5 files changed

+81
-9
lines changed

5 files changed

+81
-9
lines changed
 

‎.prettierignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package.json
2+
3+
# this file doesn't exist, but we use it as a filename that should be ignored
4+
# by prettier in the tests
5+
ignore-me.js

‎eslint-plugin-prettier.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ module.exports = {
356356
const usePrettierrc =
357357
!context.options[1] || context.options[1].usePrettierrc !== false;
358358
const sourceCode = context.getSourceCode();
359+
const filepath = context.getFilename();
359360
const source = sourceCode.text;
360361

361362
// The pragma is only valid if it is found in a block comment at the very
@@ -397,19 +398,64 @@ module.exports = {
397398
context.options[0] === 'fb'
398399
? FB_PRETTIER_OPTIONS
399400
: context.options[0];
401+
400402
const prettierRcOptions =
401403
usePrettierrc &&
402404
prettier.resolveConfig &&
403405
prettier.resolveConfig.sync
404-
? prettier.resolveConfig.sync(context.getFilename(), {
406+
? prettier.resolveConfig.sync(filepath, {
405407
editorconfig: true
406408
})
407409
: null;
410+
411+
// prettier.getFileInfo was added in v1.13
412+
const prettierFileInfo =
413+
prettier.getFileInfo && prettier.getFileInfo.sync
414+
? prettier.getFileInfo.sync(filepath, {
415+
ignorePath: '.prettierignore'
416+
})
417+
: { ignored: false, inferredParser: null };
418+
419+
// Skip if file is ignored using a .prettierignore file
420+
if (prettierFileInfo.ignored) {
421+
return;
422+
}
423+
424+
const initialOptions = {};
425+
426+
// ESLint suppports processors that let you extract and lint JS
427+
// fragments within a non-JS language. In the cases where prettier
428+
// supports the same language as a processor, we want to process
429+
// the provided source code as javascript (as ESLint provides the
430+
// rules with fragments of JS) instead of guessing the parser
431+
// based off the filename. Otherwise, for instance, on a .md file we
432+
// end up trying to run prettier over a fragment of JS using the
433+
// markdown parser, which throws an error.
434+
// If we can't infer the parser from from the filename, either
435+
// because no filename was provided or because there is no parser
436+
// found for the filename, use javascript.
437+
// This is added to the options first, so that
438+
// prettierRcOptions and eslintPrettierOptions can still override
439+
// the parser.
440+
//
441+
// `parserBlocklist` should contain the list of prettier parser
442+
// names for file types where:
443+
// * Prettier supports parsing the file type
444+
// * There is an ESLint processor that extracts JavaScript snippets
445+
// from the file type.
446+
const parserBlocklist = [null, 'graphql', 'markdown', 'html'];
447+
if (
448+
parserBlocklist.indexOf(prettierFileInfo.inferredParser) !== -1
449+
) {
450+
initialOptions.parser = 'babylon';
451+
}
452+
408453
const prettierOptions = Object.assign(
409454
{},
455+
initialOptions,
410456
prettierRcOptions,
411457
eslintPrettierOptions,
412-
{ filepath: context.getFilename() }
458+
{ filepath }
413459
);
414460

415461
const prettierSource = prettier.format(source, prettierOptions);

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"eslint-plugin-self": "^1.0.1",
4444
"mocha": "^3.1.2",
4545
"moment": "^2.18.1",
46-
"prettier": "^1.10.2",
46+
"prettier": "^1.13.0",
4747
"semver": "^5.3.0",
4848
"vue-eslint-parser": "^2.0.2"
4949
},

‎test/prettier.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ ruleTester.run('prettier', rule, {
6565
code: `var foo = {bar: 0};\n`,
6666
filename: getPrettierRcJsFilename('bracket-spacing'),
6767
options: [{ bracketSpacing: false }, { usePrettierrc: false }]
68+
},
69+
// Ignores filenames in .prettierignore
70+
{
71+
code: `("");\n`,
72+
filename: getPrettierRcJsFilename('single-quote', 'ignore-me.js')
73+
},
74+
// Sets a default parser when it can't be inferred from the file extensions
75+
{
76+
code: `('');\n`,
77+
filename: getPrettierRcJsFilename('single-quote', 'dummy.qqq')
78+
},
79+
// Overwrites the parser for file extensions prettier would try to format
80+
// with not the babylon parser
81+
// In the real world, eslint-plugin-markdown would transform file contents
82+
// into JS snippets that would get passed to ESLint
83+
{
84+
code: `('');\n`,
85+
filename: getPrettierRcJsFilename('single-quote', 'dummy.md')
6886
}
6987
],
7088
invalid: [
@@ -175,9 +193,12 @@ function loadInvalidFixture(name) {
175193

176194
/**
177195
* Builds a dummy javascript file path to trick prettier into resolving a specific .prettierrc file.
178-
* @param {string} name - Prettierrc fixture basename.
196+
* @param {string} dir - Prettierrc fixture basename.
179197
* @returns {string} A javascript filename relative to the .prettierrc config.
180198
*/
181-
function getPrettierRcJsFilename(name) {
182-
return path.resolve(__dirname, `./prettierrc/${name}/dummy.js`);
199+
function getPrettierRcJsFilename(dir, file) {
200+
// Use default parameters when we drop support for node 4
201+
file = typeof file !== 'undefined' ? file : 'dummy.js';
202+
203+
return path.resolve(__dirname, `./prettierrc/${dir}/${file}`);
183204
}

‎yarn.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,9 @@ prelude-ls@~1.1.2:
801801
version "1.1.2"
802802
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
803803

804-
prettier@^1.10.2:
805-
version "1.10.2"
806-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.10.2.tgz#1af8356d1842276a99a5b5529c82dd9e9ad3cc93"
804+
prettier@^1.13.0:
805+
version "1.14.3"
806+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.14.3.tgz#90238dd4c0684b7edce5f83b0fb7328e48bd0895"
807807

808808
process-nextick-args@~1.0.6:
809809
version "1.0.7"

0 commit comments

Comments
 (0)
Please sign in to comment.