From afd0b4e6cbe7003d17aafa3249d214ac6b0c5898 Mon Sep 17 00:00:00 2001 From: reklatsmasters Date: Wed, 10 Oct 2018 05:12:27 +0500 Subject: [PATCH 1/5] New: option `withNodeModules` to unignore `node_modules` folders. --- README.md | 8 ++++++++ eslint-plugin-prettier.js | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cf002322..2b21b569 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,14 @@ For the list of every available exclusion rule set, please see the [readme of es }] ``` + - `withNodeModules`: Prettier ignores files located in `node_modules` directories. To opt-out from this behavior set `true`, (default: `false`). May be useful if you are using multiple `node_modules` directories, example to avoid many `../..` sequences. + + ```json + "prettier/prettier": ["error", {}, { + "withNodeModules": true + }] + ``` + - The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style. --- diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index 0c99b131..576c8eba 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -131,7 +131,8 @@ module.exports = { { type: 'object', properties: { - usePrettierrc: { type: 'boolean' } + usePrettierrc: { type: 'boolean' }, + withNodeModules: { type: 'boolean' } }, additionalProperties: true } @@ -140,6 +141,8 @@ module.exports = { create(context) { const usePrettierrc = !context.options[1] || context.options[1].usePrettierrc !== false; + const withNodeModules = + context.options[1] && context.options[1].withNodeModules !== false; const sourceCode = context.getSourceCode(); const filepath = context.getFilename(); const source = sourceCode.text; @@ -164,7 +167,8 @@ module.exports = { : null; const prettierFileInfo = prettier.getFileInfo.sync(filepath, { - ignorePath: '.prettierignore' + ignorePath: '.prettierignore', + withNodeModules }); // Skip if file is ignored using a .prettierignore file From f357a72d1811969869f4d71101dcc9ea0a04e6b0 Mon Sep 17 00:00:00 2001 From: reklatsmasters Date: Thu, 11 Oct 2018 00:19:19 +0500 Subject: [PATCH 2/5] Reverse default value. --- README.md | 4 ++-- eslint-plugin-prettier.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2b21b569..f84f5118 100644 --- a/README.md +++ b/README.md @@ -121,11 +121,11 @@ For the list of every available exclusion rule set, please see the [readme of es }] ``` - - `withNodeModules`: Prettier ignores files located in `node_modules` directories. To opt-out from this behavior set `true`, (default: `false`). May be useful if you are using multiple `node_modules` directories, example to avoid many `../..` sequences. + - `withNodeModules`: Prettier ignores files located in `node_modules` directories. To opt-out from this behavior set `false`, (default: `true`). May be useful if you are using multiple `node_modules` directories, example to avoid many `../..` sequences. ```json "prettier/prettier": ["error", {}, { - "withNodeModules": true + "withNodeModules": false }] ``` diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index 576c8eba..b30d157a 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -142,7 +142,7 @@ module.exports = { const usePrettierrc = !context.options[1] || context.options[1].usePrettierrc !== false; const withNodeModules = - context.options[1] && context.options[1].withNodeModules !== false; + !context.options[1] || context.options[1].withNodeModules !== false; const sourceCode = context.getSourceCode(); const filepath = context.getFilename(); const source = sourceCode.text; From 74301f4a95a3ea326a376b7cdc5d8d1e6c774118 Mon Sep 17 00:00:00 2001 From: reklatsmasters Date: Thu, 11 Oct 2018 02:18:14 +0500 Subject: [PATCH 3/5] Revert "Reverse default value." This reverts commit 4ab71913fcf08bb38878d50d104782a324441a6f. --- README.md | 4 ++-- eslint-plugin-prettier.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f84f5118..2b21b569 100644 --- a/README.md +++ b/README.md @@ -121,11 +121,11 @@ For the list of every available exclusion rule set, please see the [readme of es }] ``` - - `withNodeModules`: Prettier ignores files located in `node_modules` directories. To opt-out from this behavior set `false`, (default: `true`). May be useful if you are using multiple `node_modules` directories, example to avoid many `../..` sequences. + - `withNodeModules`: Prettier ignores files located in `node_modules` directories. To opt-out from this behavior set `true`, (default: `false`). May be useful if you are using multiple `node_modules` directories, example to avoid many `../..` sequences. ```json "prettier/prettier": ["error", {}, { - "withNodeModules": false + "withNodeModules": true }] ``` diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index b30d157a..576c8eba 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -142,7 +142,7 @@ module.exports = { const usePrettierrc = !context.options[1] || context.options[1].usePrettierrc !== false; const withNodeModules = - !context.options[1] || context.options[1].withNodeModules !== false; + context.options[1] && context.options[1].withNodeModules !== false; const sourceCode = context.getSourceCode(); const filepath = context.getFilename(); const source = sourceCode.text; From 19ac15ca96320cc1caa0666beb86373b6feeb425 Mon Sep 17 00:00:00 2001 From: reklatsmasters Date: Thu, 11 Oct 2018 13:58:16 +0500 Subject: [PATCH 4/5] Add tests. --- test/invalid/18.txt | 19 +++++++++++++++++++ test/prettier.js | 17 ++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/invalid/18.txt diff --git a/test/invalid/18.txt b/test/invalid/18.txt new file mode 100644 index 00000000..17fea9d3 --- /dev/null +++ b/test/invalid/18.txt @@ -0,0 +1,19 @@ +CODE: +a();;;; + +OUTPUT: +a(); + +OPTIONS: +[{}, { withNodeModules: true }] + +ERRORS: +[ + { + message: 'Delete `;;;`', + line: 1, column: 5, endLine: 1, endColumn: 8, + }, +] + +FILENAME: +node_modules/dummy.js \ No newline at end of file diff --git a/test/prettier.js b/test/prettier.js index 0ef159d0..4c01fd77 100644 --- a/test/prettier.js +++ b/test/prettier.js @@ -61,6 +61,17 @@ ruleTester.run('prettier', rule, { { code: `('');\n`, filename: getPrettierRcJsFilename('single-quote', 'dummy.md') + }, + // Should ignore files from node_modules + { + code: 'a();;;;;;\n', + filename: 'node_modules/dummy.js' + }, + // Should check files from node_modules too + { + code: 'a();\n', + filename: 'node_modules/dummy.js', + options: [{}, { withNodeModules: true }] } ], invalid: [ @@ -81,7 +92,8 @@ ruleTester.run('prettier', rule, { '14', '15', '16', - '17' + '17', + '18' ].map(loadInvalidFixture) }); @@ -130,6 +142,9 @@ function loadInvalidFixture(name) { options: eval(sections[3]), // eslint-disable-line no-eval errors: eval(sections[4]) // eslint-disable-line no-eval }; + if (sections.length >= 6) { + item.filename = sections[5]; + } return item; } From 2490a8999b9922b6e47b8be8a8eadbe956d1be45 Mon Sep 17 00:00:00 2001 From: Sebastian Jambor Date: Wed, 24 Apr 2019 18:21:45 +0200 Subject: [PATCH 5/5] use `fileInfoOptions` object --- README.md | 8 +++++--- eslint-plugin-prettier.js | 22 +++++++++++++++------- test/invalid/18.txt | 4 ++-- test/prettier.js | 6 ------ 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2b21b569..2ebde9bd 100644 --- a/README.md +++ b/README.md @@ -121,11 +121,13 @@ For the list of every available exclusion rule set, please see the [readme of es }] ``` - - `withNodeModules`: Prettier ignores files located in `node_modules` directories. To opt-out from this behavior set `true`, (default: `false`). May be useful if you are using multiple `node_modules` directories, example to avoid many `../..` sequences. - + - `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. + ```json "prettier/prettier": ["error", {}, { - "withNodeModules": true + "fileInfoOptions": { + "withNodeModules": true + } }] ``` diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index 576c8eba..5d83e0e4 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -132,7 +132,11 @@ module.exports = { type: 'object', properties: { usePrettierrc: { type: 'boolean' }, - withNodeModules: { type: 'boolean' } + fileInfoOptions: { + type: 'object', + properties: {}, + additionalProperties: true + } }, additionalProperties: true } @@ -141,8 +145,8 @@ module.exports = { create(context) { const usePrettierrc = !context.options[1] || context.options[1].usePrettierrc !== false; - const withNodeModules = - context.options[1] && context.options[1].withNodeModules !== false; + const eslintFileInfoOptions = + (context.options[1] && context.options[1].fileInfoOptions) || {}; const sourceCode = context.getSourceCode(); const filepath = context.getFilename(); const source = sourceCode.text; @@ -166,10 +170,14 @@ module.exports = { }) : null; - const prettierFileInfo = prettier.getFileInfo.sync(filepath, { - ignorePath: '.prettierignore', - withNodeModules - }); + const prettierFileInfo = prettier.getFileInfo.sync( + filepath, + Object.assign( + {}, + { ignorePath: '.prettierignore' }, + eslintFileInfoOptions + ) + ); // Skip if file is ignored using a .prettierignore file if (prettierFileInfo.ignored) { diff --git a/test/invalid/18.txt b/test/invalid/18.txt index 17fea9d3..7d037f75 100644 --- a/test/invalid/18.txt +++ b/test/invalid/18.txt @@ -5,7 +5,7 @@ OUTPUT: a(); OPTIONS: -[{}, { withNodeModules: true }] +[{}, { fileInfoOptions: { withNodeModules: true } }] ERRORS: [ @@ -16,4 +16,4 @@ ERRORS: ] FILENAME: -node_modules/dummy.js \ No newline at end of file +node_modules/dummy.js diff --git a/test/prettier.js b/test/prettier.js index 4c01fd77..5eed05ca 100644 --- a/test/prettier.js +++ b/test/prettier.js @@ -66,12 +66,6 @@ ruleTester.run('prettier', rule, { { code: 'a();;;;;;\n', filename: 'node_modules/dummy.js' - }, - // Should check files from node_modules too - { - code: 'a();\n', - filename: 'node_modules/dummy.js', - options: [{}, { withNodeModules: true }] } ], invalid: [