prettier icon indicating copy to clipboard operation
prettier copied to clipboard

--range-{start,end} is ignored for --config-precedence=prefer-file

Open MunifTanjim opened this issue 3 years ago • 7 comments

Environments:

  • Prettier Version: 2.3.0
  • Running Prettier via: CLI
  • Runtime: v16
  • Operating System: Linux, macOS
  • Prettier plugins (if any):

Steps to reproduce:

  • Create file file.js
import {  a }  from 'a'  ;

import {b,  c  } from  'b-c';
  • Create file .prettierrc.js
module.exports = {}
  • Run cat file.js | prettier --stdin-filepath file.js --config-precedence=file-override --range-start 9 --range-end 10
import { a } from "a";

import {b,  c  } from  'b-c';
  • Run cat file.js | prettier --stdin-filepath file.js --config-precedence=prefer-file --range-start 9 --range-end 10
import { a } from "a";

import { b, c } from "b-c";

Expected behavior:

Both output should be the same.

Actual behavior:

--config-precedence=prefer-file ignores --range-start and --range-end flags.

MunifTanjim avatar Aug 24 '22 16:08 MunifTanjim

I can't reproduce this. Please provide complete steps to reproduce, including the contents of the two files.

thorn0 avatar Aug 24 '22 20:08 thorn0

@thorn0, sorry I wrote the command wrong previously. Updated the issue with examples.

MunifTanjim avatar Aug 24 '22 20:08 MunifTanjim

Isn't this expected behavior though?

From https://prettier.io/docs/en/cli.html#--config-precedence:

prefer-file If a config file is found will evaluate it and ignore other CLI options. If no config file is found, CLI options will evaluate as normal.

thorn0 avatar Aug 24 '22 21:08 thorn0

--range-{start,end} should be treated differently. I can't imagine anybody putting those two options in config file (that would make prettier only format a specific region of every files).

MunifTanjim avatar Aug 24 '22 21:08 MunifTanjim

I'm not familiar with the prefer-file use case, so I can't comment on it. Why doesn't file-override work for you?

thorn0 avatar Aug 24 '22 21:08 thorn0

I'm not familiar with the prefer-file use case

It's used for editor integrations. Integrations runs prettier like this:

cat file.js | prettier --stdin-filepath file.js --print-width=120 --config-precedence=prefer-file

with the expectations:

  • Prettier will find config file by using it's logic.
  • If config file is found Prettier will use it and ignore the CLI flags (e.g. --print-width will be ignored).
  • If config file is not found Prettier will use the CLI flags (e.g. --print-width will be used)

Why doesn't file-override work for you?

Like I said, it's used by editor integrations. The intention is to ignore cli flags if config file is found (i.e. when you're inside a project with prettierrc file).

  • using file-override will give precedence to config file (but also apply the cli flags for the missing options in config file)
  • using cli-override will give precedence to cli flags (but also apply config file for the missing cli flags)

So prefer-file ensures that,

  • If a config file is present, only follow it. So the project's styling preference will be respected.
  • If a config file is not present use the cli flags. Since the project doesn't have any styling preference (no prettierrc config), respect the user's choice.

I hope that makes sense. Let me know if you have any additional questions.

MunifTanjim avatar Aug 25 '22 04:08 MunifTanjim

Thanks for the explanation. The change you proposed makes sense.

thorn0 avatar Aug 25 '22 07:08 thorn0