Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer file type for CLI Javascript files #5122

Closed
haggholm opened this issue Sep 20, 2018 · 2 comments
Closed

Infer file type for CLI Javascript files #5122

haggholm opened this issue Sep 20, 2018 · 2 comments
Labels
area:api Issues with Prettier's Application Programming Interface locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. status:has pr Issues with an accompanying pull request. These issues will probably be fixed soon! type:enhancement A potential new feature to be added, or an improvement to how we print something
Milestone

Comments

@haggholm
Copy link
Contributor

Environments:

  • Prettier Version: 1.14.2
  • Running Prettier via: CLI
  • Runtime: Node.js v10
  • Operating System: Linux

Steps to reproduce:

prettier bin/clean

where bin/clean is a Javascript CLI script.

#!/usr/bin/env node
'use strict';

// Do stuff

Actual behavior:

[error] No parser could be inferred for file: bin/clean

Suggested behavior:

I’m guessing that Prettier infers file types from file extension. However, CLI scripts often aren’t given any extensions. It might be fairly easy to infer the type by reading the first line of such files and checking if it’s a common hashbang string, e.g. something like

if (/^#![^\s]*\/node(js)?\b/.test(firstLine)) {
  // ... use a JS parser ...
}

Alternatively, if this is deemed unsafe, perhaps (somewhat less conveniently) the existing pragma could be augmented to accept an optional parameter indicating file type.

#!/usr/bin/env node
/* @prettier:js */
'use strict';

// Do stuff

Not as convenient, and requires scanning more of the file than just the hashbang line to infer type, but if the former proposal is unsafe in some edge case I’m not thinking of (sufficiently common not to be relegated to .prettierignore), it would at last provide a way to opt in.

@lydell lydell added the area:cli Issues with Prettier's Command Line Interface label Sep 20, 2018
@j-f1 j-f1 added status:needs discussion Issues needing discussion and a decision to be made before action can be taken type:enhancement A potential new feature to be added, or an improvement to how we print something and removed area:cli Issues with Prettier's Command Line Interface labels Sep 20, 2018
@j-f1
Copy link
Member

j-f1 commented Sep 20, 2018

IMO this shouldn’t be a CLI-only feature since it could be useful for API users too. (cc @lydell since you added the label)

@haggholm haggholm mentioned this issue Sep 27, 2018
3 tasks
@haggholm
Copy link
Contributor Author

Created a PR that I think should work for both CLI and API. I don’t know if it’s any good, but if I’m missing a feature I can at least try to address it…

There’s no test, but I didn’t see any valid tests for inference to begin with (there are some weird missing-path tests). There are also some ugly swallowed exceptions because I don’t know how to address some of the weird cases exposed by the tests, where a path is provided but not to anything that can be opened.

@ikatyang ikatyang added status:has pr Issues with an accompanying pull request. These issues will probably be fixed soon! area:api Issues with Prettier's Application Programming Interface and removed status:needs discussion Issues needing discussion and a decision to be made before action can be taken labels Oct 3, 2018
@ikatyang ikatyang added this to the 1.15 milestone Oct 25, 2018
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 23, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area:api Issues with Prettier's Application Programming Interface locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. status:has pr Issues with an accompanying pull request. These issues will probably be fixed soon! type:enhancement A potential new feature to be added, or an improvement to how we print something
Projects
None yet
Development

No branches or pull requests

4 participants