-
-
Notifications
You must be signed in to change notification settings - Fork 10
test: resolve configs when --stdin-filepath is used
#58
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
Changes from 10 commits
6d71385
fffaada
585f1be
e66a9b5
c9533e7
086c44c
f1c2dc1
fd0e3b0
d5324f4
ce18801
12698fe
3581b43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ import { fastRelativePath, isNull, isString, isUndefined, negate, pluralize, tri | |
| import type { FormatOptions, Options, PluginsOptions } from "./types.js"; | ||
|
|
||
| async function run(options: Options, pluginsDefaultOptions: PluginsOptions, pluginsCustomOptions: PluginsOptions): Promise<void> { | ||
| if (options.globs.length || !isString(await getStdin())) { | ||
| if (options.globs.length || (!isString(await getStdin()) && !("stdinFilepath" in options))) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you explain this one? if there is stdin and there is a and if there's no stdin but there is a unless im reading wrong, thats what its doing now
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I based this branch on yours, you seem to have added this condition in 585f1be. Not really sure what the rationale was, but it seems to make sense, right? We
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if there's no so i probably got this condition wrong. i don't remember why i did it that way if there's no stdin or there's globs, we should |
||
| return runGlobs(options, pluginsDefaultOptions, pluginsCustomOptions); | ||
| } else { | ||
| return runStdin(options, pluginsDefaultOptions, pluginsCustomOptions); | ||
|
|
@@ -31,8 +31,24 @@ async function runStdin(options: Options, pluginsDefaultOptions: PluginsOptions, | |
| const fileName = options.stdinFilepath || "stdin"; | ||
| const fileContent = (await getStdin()) || ""; | ||
|
|
||
| const ignoreNames = options.ignore ? [".gitignore", ".prettierignore"] : []; | ||
| const isIgnored = await getIgnoreResolved(fileName, ignoreNames, true); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic is a bit more involved in |
||
| if (isIgnored) { | ||
| stdout.always(trimFinalNewline(fileContent)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This matches the v3 behavior based on the related test case, where the ignored file gets output as is. |
||
| process.exitCode = 0; | ||
| return; | ||
| } | ||
|
|
||
| const editorConfigNames = options.editorConfig ? [".editorconfig"] : []; | ||
| const editorConfig = options.editorConfig ? getEditorConfigFormatOptions(await getEditorConfigResolved(fileName, editorConfigNames, true)) : {}; | ||
|
|
||
| const prettierConfigNames = options.config ? without(Object.keys(File2Loader), ["default"]) : []; | ||
| const prettierConfig = options.config ? await getPrettierConfigResolved(fileName, prettierConfigNames, true) : {}; | ||
|
|
||
| const formatOptions = { ...editorConfig, ...prettierConfig, ...options.formatOptions }; | ||
|
|
||
| try { | ||
| const formatted = await prettier.format(fileName, fileContent, options.formatOptions, options.contextOptions, pluginsDefaultOptions, pluginsCustomOptions); | ||
| const formatted = await prettier.format(fileName, fileContent, formatOptions, options.contextOptions, pluginsDefaultOptions, pluginsCustomOptions); | ||
| if (options.check || options.list) { | ||
| if (formatted !== fileContent) { | ||
| stdout.warn("(stdin)"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| root = true | ||
|
|
||
| [*.js] | ||
| indent_style = tab | ||
| tab_width = 8 | ||
| indent_size = 2 # overridden by tab_width since indent_style = tab | ||
| max_line_length = 100 | ||
|
|
||
| # Indentation override for all JS under lib directory | ||
| [lib/**.js] | ||
| indent_style = space | ||
| indent_size = 2 | ||
|
|
||
| [lib/indent_size=tab.js] | ||
| indent_size = tab | ||
|
|
||
| [tab_width=unset.js] | ||
| tab_width = unset |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Prettier v3, this file lives in a bigger overarching fixture directory ( |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| endOfLine: 'auto' | ||
| overrides: | ||
| - files: "**/*.js" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Prettier v3, this is just
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prettier treats
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this something that we should do in v4 too or rather require the users to handle it explicitly?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there was a conversation a while back around it. im not sure if it was in discord or on here we should probably discuss it again and make a decision |
||
| options: | ||
| semi: false | ||
| - files: "**/*.ts" | ||
| options: | ||
| semi: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| function f() { | ||
| console.log("should have tab width 8"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| function f() { | ||
| console.log("should have space width 2"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| function f() { | ||
| console.log("should have space width 8"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| This isn't really a Mercurial repo, but we want to pretend it is for testing purposes. | ||
| See https://github.com/prettier/prettier/pull/3559#issuecomment-353857109 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| function f() { | ||
| console.log("should have space width 2 despite ../.editorconfig specifying 8, because ./.hg is present"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ignore/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with a deep path (stderr) 1`] = `""`; | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with a deep path (stderr) 2`] = `""`; | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with a deep path (stdout) 1`] = ` | ||
| "function f() { | ||
| console.log("should be indented with a tab") | ||
| }" | ||
| `; | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with a deep path (stdout) 2`] = ` | ||
| "function f() { | ||
| console.log("should be indented with a tab") | ||
| }" | ||
| `; | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with a deep path (write) 1`] = `[]`; | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with a deep path (write) 2`] = `[]`; | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with nonexistent directory (stderr) 1`] = `""`; | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with nonexistent directory (stdout) 1`] = ` | ||
| "function f() { | ||
| console.log("should be indented with a tab") | ||
| }" | ||
| `; | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with nonexistent directory (write) 1`] = `[]`; | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with nonexistent file (stderr) 1`] = `""`; | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with nonexistent file (stdout) 1`] = ` | ||
| "function f() { | ||
| console.log("should be indented with a tab") | ||
| }" | ||
| `; | ||
|
|
||
| exports[`apply editorconfig for stdin-filepath with nonexistent file (write) 1`] = `[]`; | ||
|
|
||
| exports[`don't apply editorconfig outside project for stdin-filepath with nonexistent directory (stderr) 1`] = `""`; | ||
|
|
||
| exports[`don't apply editorconfig outside project for stdin-filepath with nonexistent directory (stdout) 1`] = ` | ||
| "function f() { | ||
| console.log("should be indented with 2 spaces"); | ||
| }" | ||
| `; | ||
|
|
||
| exports[`don't apply editorconfig outside project for stdin-filepath with nonexistent directory (write) 1`] = `[]`; | ||
|
|
||
| exports[`format correctly if stdin content compatible with stdin-filepath (stderr) 1`] = `""`; | ||
|
|
||
| exports[`format correctly if stdin content compatible with stdin-filepath (stdout) 1`] = ` | ||
| ".name { | ||
| display: none; | ||
| }" | ||
| `; | ||
|
|
||
| exports[`format correctly if stdin content compatible with stdin-filepath (write) 1`] = `[]`; | ||
|
|
||
| exports[`gracefully handle stdin-filepath with nonexistent directory (stderr) 1`] = `""`; | ||
|
|
||
| exports[`gracefully handle stdin-filepath with nonexistent directory (stdout) 1`] = ` | ||
| ".name { | ||
| display: none; | ||
| }" | ||
| `; | ||
|
|
||
| exports[`gracefully handle stdin-filepath with nonexistent directory (write) 1`] = `[]`; | ||
|
|
||
| exports[`output file as-is if stdin-filepath matched patterns in ignore-path (stderr) 1`] = `""`; | ||
|
|
||
| exports[`output file as-is if stdin-filepath matched patterns in ignore-path (write) 1`] = `[]`; | ||
|
|
||
| exports[`throw error if stdin content incompatible with stdin-filepath (stderr) 1`] = ` | ||
| "[error] SyntaxError: Unexpected token (1:1) | ||
| [error] > 1 | .name { display: none; } | ||
| [error] | ^" | ||
| `; | ||
|
|
||
| exports[`throw error if stdin content incompatible with stdin-filepath (stdout) 1`] = `""`; | ||
|
|
||
| exports[`throw error if stdin content incompatible with stdin-filepath (write) 1`] = `[]`; |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a huge fan as
ignoreKnownneeds to be passed through multiple hoops before it ends up here, but I'm not sure how else we could solve this (getStdindoesn't populateKnown, and so we'd always end up not reading any config).I'm also not sure whether this impacts performance, i.e. the whole chain – not populating
KnowninrunStdinand resolving the configs without it.