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

Respect EditorConfig settings #2760

Merged
merged 61 commits into from Nov 8, 2017

Conversation

josephfrazier
Copy link
Collaborator

@josephfrazier josephfrazier commented Sep 6, 2017

This is a simple way to address #42. It adds support for .editorconfig's indent_style, tab_width, and max_line_length properties.

I still need to add caching and a proper async implementation, but I think it's functionally correct. It doesn't support the end_of_line property as described in #42 (comment), but that could be added later.

This is a simple way to address prettier#42.
It adds support for .editorconfig's `indent_style`, `tab_width`,
and `max_line_length` properties.

I still need to add caching and a proper async implementation,
but I think it's functionally correct.
It doesn't support the `end_of_line` property as described in
prettier#42 (comment),
but that could be added later.
@@ -0,0 +1,6 @@
root = true

[*]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test a more specific glob as well, with different settings, like [lib/**.js] (copied from http://editorconfig.org/).

@josephfrazier
Copy link
Collaborator Author

josephfrazier commented Sep 6, 2017 via email

The tests were failing because they glob over all config-resolution test
files.
@@ -0,0 +1,5 @@
/* eslint-disable */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint-disable comments shouldn't be needed because we ignore test_integration/cli/ in .eslintignore

result.useTabs = editorConfig.indent_style === "tab";
result.tabWidth =
editorConfig.indent_size || editorConfig.tab_width || result.tabWidth;
result.printWidth = editorConfig.max_line_length || result.printWidth;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for || result.tabWidth and || result.printWidth, is it? They are always goind to be undefined.

Couldn't we just do:

const editorConfig = editorconfig.parseSync(filePath);
return {
  useTabs: ...,
  tabWidth: ...,
  printWidth: ...
}

While being an odd configuration, isn't it valid to set indent_size, tab_width and max_line_length to 0, and as such we should respect that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for || result.tabWidth and || result.printWidth, is it? They are always goind to be undefined.

Yeah, that was also causing a bug. Not sure why I did it that way, but it's fixed in 54a5dcb

While being an odd configuration, isn't it valid to set indent_size, tab_width and max_line_length to 0, and as such we should respect that?

Good point, I'll try to fix that.

@azz
Copy link
Member

azz commented Sep 7, 2017

I'm still not 100% convinced we should add this, as editors that use the prettier API (prettier-vscode, prettier-atom) will still have to implement it themselves.

edit: never mind, they get it from resolveConfig()

if (editorConfig.max_line_length) {
result.printWidth = editorConfig.max_line_length;
}

Copy link
Member

@lydell lydell Sep 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it important that the .useTabs, .tabWidth and .printWidth properties of result are missing rather than present with the value undefined if the corresponding EditorConfig setting is missing?

Couldn't we just do:

const editorConfig = editorconfig.parseSync(filePath);
return {
  useTabs: ...,
  tabWidth: ...,
  printWidth: ...
}

While being an odd configuration, isn't it valid to set indent_size, tab_width and max_line_length to 0, and as such we should respect that? Note that 0 if a falsy value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it important that the .useTabs, .tabWidth and .printWidth properties of result are missing rather than present with the value undefined if the corresponding EditorConfig setting is missing?

Yes, I think that's what fixed this test: https://travis-ci.org/prettier/prettier/jobs/272596635#L379

While being an odd configuration, isn't it valid to set indent_size, tab_width and max_line_length to 0, and as such we should respect that? Note that 0 if a falsy value.

Hmm, it looks like tab_width and max_line_length must be positive. I suppose someone could set indent_size to 0, but I'm not sure if that edge case is worth supporting right now. At least, I haven't had time to put effort into it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's all fine then 👍

@lydell
Copy link
Member

lydell commented Sep 11, 2017

@josephfrazier Is this still WIP or ready to merge?

@josephfrazier
Copy link
Collaborator Author

josephfrazier commented Sep 11, 2017

I haven't had time to add a proper async implementation, as well as caching. I think there might also be an bug in the case where there's no prettier config, but there is .editorconfig, but I'm not sure yet.

EDIT: I think this is ready now.

@josephfrazier josephfrazier self-assigned this Sep 11, 2017
Copy link
Member

@lydell lydell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the implementation! Really easy to follow.

I left two small comments. Otherwise looks good to me.

});
});

test("API resolveConfig.sync with no args", () => {
expect(prettier.resolveConfig.sync()).toBeNull();
expect(prettier.resolveConfig.sync()).toEqual({});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ... this (and the change just above) are breaking changes – is that something we want to avoid?

};
const editorconfigAsyncWithCache = mem(editorconfigAsyncNoCache);
const editorconfigSyncNoCache = filePath =>
filePath && editorConfigToPrettier(editorconfig.parseSync(filePath));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This arrow function has no braces while the above when has.

@lipis
Copy link
Member

lipis commented Oct 30, 2017

What's the latest on this one?! (I don't mean to rush or anything.. just curious)

@josephfrazier
Copy link
Collaborator Author

@lipis, I'd still like to address @robwise's concerns in #2760 (comment), but I haven't had time to do so. While investigating that, I also noticed that resolve-config-editorconfig prioritizes indent_size over tab_width, which is exactly backwards according to http://editorconfig.org/:

tab_width: a whole number defining the number of columns used to represent a tab character. This defaults to the value of indent_size and doesn't usually need to be specified.

If you (or anyone else) would like to help out, please feel free to post patches (preferably wrapped in a <details> element) that I can test and add to the PR.

@josephfrazier
Copy link
Collaborator Author

@robwise, I think your aforementioned concerns are now addressed. Would you mind taking another look and confirming?

@josephfrazier josephfrazier merged commit 8f58ca0 into prettier:master Nov 8, 2017
@josephfrazier
Copy link
Collaborator Author

josephfrazier commented Nov 8, 2017

After having to resolve merge conflicts with master yet again, I decided to go ahead and land this, especially since I believe all of the previously mentioned concerns have been adequately addressed.

@azz
Copy link
Member

azz commented Nov 8, 2017

Not sure adding this in a hotfix is a good idea...

azz added a commit that referenced this pull request Nov 8, 2017
@lipis
Copy link
Member

lipis commented Nov 8, 2017

Good stuff:)

@josephfrazier
Copy link
Collaborator Author

Oops @azz, I didn't realize that the next release was intended to be only a hotfix. What do you think about backing this out (#3213), releasing 1.8.2, then landing it again? It's been a rather long-running PR, so I'd really like to get it in.

As far as I can tell, the only projects this change could break are those that have .editorconfig properties without the corresponding .prettierrc options. Prettier would now take into account the extra .editorconfig properties, whereas before it wouldn't. This doesn't seem like such a big deal to me, considering that projects should edit/remove .editorconfig files that are intentionally disregarded when the code is checked in.

azz added a commit that referenced this pull request Nov 9, 2017
* Revert "Respect EditorConfig settings (#2760)"

This reverts commit 8f58ca0.
azz pushed a commit that referenced this pull request Dec 4, 2017
* Revert "Revert "Respect EditorConfig settings" (#3213)"

This reverts commit d2241fc.

* Comment out EditorConfig docs

See #3213 (comment)

* editorconfig: Support `indent_size = 0`

See #2760 (comment)
and josephfrazier/editorconfig-to-prettier@c38b84c

* Revert "Comment out EditorConfig docs"

This reverts commit ddfa529.

* Mark EditorConfig functionality as v1.9.0+

See #3255 (comment)

* editorconfig: Upgrade editorconfig-to-prettier to 0.0.4

* editorconfig: Only enable for CLI, by default

#3255 (comment)

* editorconfig: Add tests confirming that editorconfig is ignored by default in the API

#3255 (comment)

* editorconfig: Add/fix CLI option parsing

* editorconfig: Move docs from configuration.md to options.md

* editorconfig: Add `oppositeDescription` to show docs for `--no-editorconfig`

Addresses #3255 (comment)

* editorconfig: Update test snapshots

* editorconfig: Remove unnecessary options parsing code

Addresses #3255 (comment)

* editorconfig: Move docs from options.md to api.md and cli.md

Addresses #3255 (comment)

* resolveConfig: return null if both .prettierrc and .editorconfig are missing

Addresses #3255 (comment)

* Don't add now-failing tests

The way these tests work, both `tests_integration/cli/config/.prettierrc`
and `.prettierrc` apply to `tests_integration/cli/config/editorconfig/file.shouldnotexist`,
so the test wouldn't work even on master. Here's a way to confirm that:

```js
const path = require('path')
const assert = require('assert')
const prettier = require('./')

const file = './tests_integration/cli/config/editorconfig/file.shouldnotexist'
console.log(prettier.resolveConfig.sync(file))
assert(prettier.resolveConfig.sync(file) === null)
```
@josephfrazier josephfrazier deleted the editorconfig branch January 30, 2018 17:40
@josephfrazier josephfrazier restored the editorconfig branch February 12, 2018 23:10
@josephfrazier josephfrazier deleted the editorconfig branch June 12, 2018 15:52
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Sep 10, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Sep 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants