Skip to content

Commit 30699de

Browse files
authoredSep 10, 2020
fix: don't ignore file extensions defined in .prettierrc overrides (#111)
Adds a new setting `--no-resolve-config` to opt out.
1 parent e500e3c commit 30699de

File tree

6 files changed

+68
-21
lines changed

6 files changed

+68
-21
lines changed
 

‎README.md

+21
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,27 @@ Prevent `git commit` if any files are fixed.
105105

106106
Check that files are correctly formatted, but don't format them. This is useful on CI to verify that all changed files in the current branch were correctly formatted.
107107

108+
### `--no-resolve-config`
109+
110+
Do not resolve prettier config when determining which files to format, just use standard set of supported file types & extensions prettier supports. This may be useful if you do not need any customization and see performance issues.
111+
112+
By default, pretty-quick will check your prettier configuration file for any overrides you define to support formatting of additional file extensions.
113+
114+
Example `.prettierrc` file to support formatting files with `.cmp` or `.page` extensions as html.
115+
116+
```
117+
{
118+
"printWidth": 120,
119+
"bracketSpacing": false,
120+
"overrides": [
121+
{
122+
"files": "*.{cmp,page}",
123+
"options": {"parser": "html"}
124+
}
125+
],
126+
}
127+
```
128+
108129
<!-- Undocumented = Unsupported :D
109130
110131
### `--config`

‎__mocks__/prettier.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1+
const path = require('path');
2+
13
const prettierMock = {
24
format: jest.fn().mockImplementation((input) => 'formatted:' + input),
35
resolveConfig: {
46
sync: jest.fn().mockImplementation((file) => ({ file })),
57
},
6-
getSupportInfo: jest.fn().mockReturnValue({
7-
languages: [
8-
{
9-
name: 'JavaScript',
10-
extensions: ['.js'],
11-
},
12-
{
13-
name: 'Markdown',
14-
extensions: ['.md'],
15-
},
16-
],
17-
}),
8+
getFileInfo: {
9+
sync: jest.fn().mockImplementation((file) => {
10+
const ext = path.extname(file);
11+
if (ext === '.js' || ext === '.md') {
12+
return { ignored: false, inferredParser: 'babel' };
13+
} else {
14+
return { ignored: false, inferredParser: null };
15+
}
16+
}),
17+
},
1818
};
1919

2020
module.exports = prettierMock;

‎bin/pretty-quick.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ const mri = require('mri');
77

88
const prettyQuick = require('..').default;
99

10-
const args = mri(process.argv.slice(2));
10+
const args = mri(process.argv.slice(2), {
11+
alias: {
12+
'resolve-config': 'resolveConfig',
13+
},
14+
});
1115

1216
const prettyQuickResult = prettyQuick(
1317
process.cwd(),
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import isSupportedExtension from '../isSupportedExtension';
2+
import prettier from 'prettier';
3+
4+
afterEach(() => jest.clearAllMocks());
5+
6+
test('return true when file with supported extension passed in', () => {
7+
expect(isSupportedExtension(true)('banana.js')).toEqual(true);
8+
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.js', {
9+
resolveConfig: true,
10+
});
11+
});
12+
13+
test('return false when file with not supported extension passed in', () => {
14+
expect(isSupportedExtension(true)('banana.txt')).toEqual(false);
15+
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.txt', {
16+
resolveConfig: true,
17+
});
18+
});
19+
20+
test('do not resolve config when false passed', () => {
21+
expect(isSupportedExtension(false)('banana.txt')).toEqual(false);
22+
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.txt', {
23+
resolveConfig: false,
24+
});
25+
});

‎src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default (
2222
onExamineFile,
2323
onCheckFile,
2424
onWriteFile,
25+
resolveConfig = true,
2526
} = {},
2627
) => {
2728
const scm = scms(currentDirectory);
@@ -42,7 +43,7 @@ export default (
4243

4344
const changedFiles = scm
4445
.getChangedFiles(directory, revision, staged)
45-
.filter(isSupportedExtension)
46+
.filter(isSupportedExtension(resolveConfig))
4647
.filter(createMatcher(pattern))
4748
.filter(rootIgnorer)
4849
.filter(cwdIgnorer);

‎src/isSupportedExtension.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import { getSupportInfo } from 'prettier';
1+
import { getFileInfo } from 'prettier';
22

3-
const extensions = getSupportInfo().languages.reduce(
4-
(prev, language) => prev.concat(language.extensions || []),
5-
[],
6-
);
7-
8-
export default (file) => extensions.some((ext) => file.endsWith(ext));
3+
export default (resolveConfig) => (file) =>
4+
Boolean(getFileInfo.sync(file, { resolveConfig }).inferredParser);

0 commit comments

Comments
 (0)
Please sign in to comment.