Skip to content

Commit

Permalink
fix: don't ignore file extensions defined in .prettierrc overrides (#111
Browse files Browse the repository at this point in the history
)

Adds a new setting `--no-resolve-config` to opt out.
  • Loading branch information
bob-laz authored Sep 10, 2020
1 parent e500e3c commit 30699de
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 21 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ Prevent `git commit` if any files are fixed.

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.

### `--no-resolve-config`

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.

By default, pretty-quick will check your prettier configuration file for any overrides you define to support formatting of additional file extensions.

Example `.prettierrc` file to support formatting files with `.cmp` or `.page` extensions as html.

```
{
"printWidth": 120,
"bracketSpacing": false,
"overrides": [
{
"files": "*.{cmp,page}",
"options": {"parser": "html"}
}
],
}
```

<!-- Undocumented = Unsupported :D
### `--config`
Expand Down
24 changes: 12 additions & 12 deletions __mocks__/prettier.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const path = require('path');

const prettierMock = {
format: jest.fn().mockImplementation((input) => 'formatted:' + input),
resolveConfig: {
sync: jest.fn().mockImplementation((file) => ({ file })),
},
getSupportInfo: jest.fn().mockReturnValue({
languages: [
{
name: 'JavaScript',
extensions: ['.js'],
},
{
name: 'Markdown',
extensions: ['.md'],
},
],
}),
getFileInfo: {
sync: jest.fn().mockImplementation((file) => {
const ext = path.extname(file);
if (ext === '.js' || ext === '.md') {
return { ignored: false, inferredParser: 'babel' };
} else {
return { ignored: false, inferredParser: null };
}
}),
},
};

module.exports = prettierMock;
6 changes: 5 additions & 1 deletion bin/pretty-quick.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const mri = require('mri');

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

const args = mri(process.argv.slice(2));
const args = mri(process.argv.slice(2), {
alias: {
'resolve-config': 'resolveConfig',
},
});

const prettyQuickResult = prettyQuick(
process.cwd(),
Expand Down
25 changes: 25 additions & 0 deletions src/__tests__/isSupportedExtension.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import isSupportedExtension from '../isSupportedExtension';
import prettier from 'prettier';

afterEach(() => jest.clearAllMocks());

test('return true when file with supported extension passed in', () => {
expect(isSupportedExtension(true)('banana.js')).toEqual(true);
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.js', {
resolveConfig: true,
});
});

test('return false when file with not supported extension passed in', () => {
expect(isSupportedExtension(true)('banana.txt')).toEqual(false);
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.txt', {
resolveConfig: true,
});
});

test('do not resolve config when false passed', () => {
expect(isSupportedExtension(false)('banana.txt')).toEqual(false);
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.txt', {
resolveConfig: false,
});
});
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default (
onExamineFile,
onCheckFile,
onWriteFile,
resolveConfig = true,
} = {},
) => {
const scm = scms(currentDirectory);
Expand All @@ -42,7 +43,7 @@ export default (

const changedFiles = scm
.getChangedFiles(directory, revision, staged)
.filter(isSupportedExtension)
.filter(isSupportedExtension(resolveConfig))
.filter(createMatcher(pattern))
.filter(rootIgnorer)
.filter(cwdIgnorer);
Expand Down
10 changes: 3 additions & 7 deletions src/isSupportedExtension.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { getSupportInfo } from 'prettier';
import { getFileInfo } from 'prettier';

const extensions = getSupportInfo().languages.reduce(
(prev, language) => prev.concat(language.extensions || []),
[],
);

export default (file) => extensions.some((ext) => file.endsWith(ext));
export default (resolveConfig) => (file) =>
Boolean(getFileInfo.sync(file, { resolveConfig }).inferredParser);

0 comments on commit 30699de

Please sign in to comment.