-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Respect EditorConfig settings (#2760)
This fixes #42. It adds support for .editorconfig's `indent_style`, `indent_size`, `tab_width`, and `max_line_length` properties. It doesn't support the `end_of_line` property as described in #42 (comment), but that could be added later. The same goes for `quote_type` (prettier/prettier-atom#293 (comment)). * Make test .prettierrc not set config for all file extensions This makes it easier to keep tests isolated.
- Loading branch information
1 parent
9652ad7
commit 8f58ca0
Showing
15 changed files
with
315 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"use strict"; | ||
|
||
const editorconfig = require("editorconfig"); | ||
const mem = require("mem"); | ||
const pathRoot = require("path-root"); | ||
const editorConfigToPrettier = require("editorconfig-to-prettier"); | ||
|
||
const maybeParse = (filePath, config, parse) => { | ||
const root = filePath && pathRoot(filePath); | ||
return filePath && !config && parse(filePath, { root }); | ||
}; | ||
|
||
const editorconfigAsyncNoCache = (filePath, config) => { | ||
return Promise.resolve(maybeParse(filePath, config, editorconfig.parse)).then( | ||
editorConfigToPrettier | ||
); | ||
}; | ||
const editorconfigAsyncWithCache = mem(editorconfigAsyncNoCache); | ||
|
||
const editorconfigSyncNoCache = (filePath, config) => { | ||
return editorConfigToPrettier( | ||
maybeParse(filePath, config, editorconfig.parseSync) | ||
); | ||
}; | ||
const editorconfigSyncWithCache = mem(editorconfigSyncNoCache); | ||
|
||
function getLoadFunction(opts) { | ||
if (opts.sync) { | ||
return opts.cache ? editorconfigSyncWithCache : editorconfigSyncNoCache; | ||
} | ||
|
||
return opts.cache ? editorconfigAsyncWithCache : editorconfigAsyncNoCache; | ||
} | ||
|
||
function clearCache() { | ||
mem.clear(editorconfigSyncWithCache); | ||
mem.clear(editorconfigAsyncWithCache); | ||
} | ||
|
||
module.exports = { | ||
getLoadFunction, | ||
clearCache | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
semi: false | ||
|
||
overrides: | ||
- files: "*.js" | ||
options: | ||
semi: false | ||
- files: "*.ts" | ||
options: | ||
semi: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function f() { | ||
console.log("should have tab width 8"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function f() { | ||
console.log("should have space width 2"); | ||
} |
3 changes: 3 additions & 0 deletions
3
tests_integration/cli/config/editorconfig/lib/indent_size=tab.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function f() { | ||
console.log("should have space width 8"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# This file should be overridden by prettier.config.js | ||
|
||
[*] | ||
tab_width = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# This file should be overridden by package.json | ||
|
||
[*] | ||
tab_width = 1 | ||
|
||
[*.ts] | ||
tab_width = 1 |
Oops, something went wrong.