-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from 20 commits
f3a6ecb
6897f6b
24821d5
9a7a17a
7d92baf
2a1ae88
54a5dcb
4f52651
ad69825
bf534cd
53e6f17
4f660e6
ccd03c6
5e6ef51
a42fa75
f123c64
c4bb231
8404ee0
85e350b
148d3bf
cfdb146
efed301
5c9c3d9
f69d367
e422fc0
489fc24
f859805
7c2b545
e2bc881
a9a8b57
6efbf89
0ff7bec
cd1e260
59adb59
6a95dc2
99b9619
30c619f
48abd24
f07c166
619ae2e
f5f382b
e583cc1
734d5ad
3fef30d
ccc3b01
8776fa6
e5e8da0
1b4b1b4
128e6e8
e9c22f9
6ac1ba7
49a19fe
d44420c
db9877c
a9bc09e
26f49a4
bf282f9
27254d4
5526de0
664dced
47e4b62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,76 @@ | ||
"use strict"; | ||
|
||
const cosmiconfig = require("cosmiconfig"); | ||
const editorconfig = require("editorconfig"); | ||
const mem = require("mem"); | ||
const minimatch = require("minimatch"); | ||
|
||
const asyncWithCache = cosmiconfig("prettier"); | ||
const asyncNoCache = cosmiconfig("prettier", { cache: false }); | ||
const syncWithCache = cosmiconfig("prettier", { sync: true }); | ||
const syncNoCache = cosmiconfig("prettier", { cache: false, sync: true }); | ||
|
||
const editorconfigAsyncNoCache = filePath => | ||
filePath && editorconfig.parse(filePath).then(editorConfigToPrettier); | ||
const editorconfigAsyncWithCache = mem(editorconfigAsyncNoCache); | ||
const editorconfigSyncNoCache = filePath => | ||
filePath && editorConfigToPrettier(editorconfig.parseSync(filePath)); | ||
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 arrow function has no braces while the above when has. |
||
const editorconfigSyncWithCache = mem(editorconfigSyncNoCache); | ||
|
||
function editorConfigToPrettier(editorConfig) { | ||
const result = {}; | ||
|
||
if (editorConfig.indent_style) { | ||
result.useTabs = editorConfig.indent_style === "tab"; | ||
} | ||
|
||
const tabWidth = editorConfig.indent_size || editorConfig.tab_width; | ||
if (tabWidth) { | ||
result.tabWidth = tabWidth; | ||
} | ||
|
||
if (editorConfig.max_line_length) { | ||
result.printWidth = editorConfig.max_line_length; | ||
} | ||
|
||
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 it important that the 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 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.
Yes, I think that's what fixed this test: https://travis-ci.org/prettier/prettier/jobs/272596635#L379
Hmm, it looks like tab_width and max_line_length must be positive. I suppose someone could set 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 guess it's all fine then 👍 |
||
return result; | ||
} | ||
|
||
function resolveConfig(filePath, opts) { | ||
const useCache = !(opts && opts.useCache === false); | ||
return (useCache ? asyncWithCache : asyncNoCache) | ||
.load(filePath) | ||
.then(result => { | ||
return !result ? null : mergeOverrides(result.config, filePath); | ||
}); | ||
return Promise.all([ | ||
(useCache ? asyncWithCache : asyncNoCache).load(filePath), | ||
(useCache ? editorconfigAsyncWithCache : editorconfigAsyncNoCache)(filePath) | ||
]).then((arr /* [result, editorConfigged] */) => | ||
mergeEditorConfig(arr[0], filePath, arr[1]) | ||
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 we put 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. |
||
); | ||
} | ||
|
||
resolveConfig.sync = (filePath, opts) => { | ||
const useCache = !(opts && opts.useCache === false); | ||
const result = (useCache ? syncWithCache : syncNoCache).load(filePath); | ||
return !result ? null : mergeOverrides(result.config, filePath); | ||
const editorConfigged = (useCache | ||
? editorconfigSyncWithCache | ||
: editorconfigSyncNoCache)(filePath); | ||
return mergeEditorConfig(result, filePath, editorConfigged); | ||
}; | ||
|
||
function mergeEditorConfig(result, filePath, editorConfigged) { | ||
if (!filePath) { | ||
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 problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think I had to add that branch in to keep the pre-existing tests passing. (related to #2997) |
||
return null; | ||
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. Unrelated: it'd be better to add a comment to describe why we return 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. It turns out we could get rid of this altogether. Fixed in 49a19fe |
||
} | ||
|
||
const config = result && result.config; | ||
|
||
return Object.assign({}, editorConfigged, mergeOverrides(config, filePath)); | ||
} | ||
|
||
function clearCache() { | ||
syncWithCache.clearCaches(); | ||
asyncWithCache.clearCaches(); | ||
|
||
editorconfigSyncWithCache.clear(); | ||
editorconfigAsyncWithCache.clear(); | ||
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. mem docs say 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. It seems we need the 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. Perhaps time to add tests for the clearCache function, then? 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 tried that when I originally added it but I was having issues with jest module mocks so I gave up. We should look at it again. 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. Oops, I added (and fixed) a test to ensure it doesn't throw. Haven't yet gotten to testing that the cache actually clears. |
||
} | ||
|
||
function resolveConfigFile(filePath) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
root = true | ||
|
||
[*] | ||
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. We should test a more specific glob as well, with different settings, like |
||
indent_style = tab | ||
tab_width = 8 | ||
max_line_length = 100 | ||
|
||
# Indentation override for all JS under lib directory | ||
[lib/**.js] | ||
indent_style = space | ||
indent_size = 2 |
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"); | ||
} |
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.
This is starting to blow up combinatorially. Any way we can apply some polymorphism magic to clean it up a bit?
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.
🤔
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.
@ikatyang I think the "manual" way is better in this case :)