Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: prettier/pretty-quick
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.0.0
Choose a base ref
...
head repository: prettier/pretty-quick
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.1.0
Choose a head ref
  • 5 commits
  • 10 files changed
  • 5 contributors

Commits on Aug 22, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f7317a6 View commit details

Commits on Sep 10, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    e500e3c View commit details
  2. fix: don't ignore file extensions defined in .prettierrc overrides (#111

    )
    
    Adds a new setting `--no-resolve-config` to opt out.
    bob-laz authored Sep 10, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    30699de View commit details

Commits on Sep 16, 2020

  1. doc: update npx usage for v3

    azz authored Sep 16, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8c994bc View commit details

Commits on Oct 14, 2020

  1. feat: add an --ignore-path flag (#115)

    * feat(ignore-path): define new flag for ignore files
    
    * refactor: addressing comments
    
    * test: adding --ignore-path cases
    
    * test: adding scm-hg tests
    roggervalf authored Oct 14, 2020
    1

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    eaf29e2 View commit details
Showing with 137 additions and 28 deletions.
  1. +2 −1 .travis.yml
  2. +31 −3 README.md
  3. +12 −12 __mocks__/prettier.js
  4. +6 −1 bin/pretty-quick.js
  5. +1 −1 package.json
  6. +25 −0 src/__tests__/isSupportedExtension.test.js
  7. +26 −0 src/__tests__/scm-git.test.js
  8. +26 −0 src/__tests__/scm-hg.test.js
  9. +5 −3 src/index.js
  10. +3 −7 src/isSupportedExtension.js
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ notifications:
node_js:
- 10.13
- 12
- 14
- node
install:
- yarn
@@ -22,7 +23,7 @@ deploy:
- npx semantic-release@15
on:
branch: master
node_js: '12'
node_js: '14'
branches:
except:
- /^v\d+\.\d+\.\d+$/
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -42,9 +42,11 @@ yarn pretty-quick
With [`npx`](https://npm.im/npx):

```shellsession
npx pretty-quick
npx -p prettier@latest -p pretty-quick pretty-quick
```

> Note: You can (_should_) change `latest` to a specific version of Prettier.
With `npm`:

1. Add `"pretty-quick": "pretty-quick"` to the `"scripts"` section of `package.json`.
@@ -62,7 +64,7 @@ yarn add --dev husky

In `package.json`, add:

```
```json
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
@@ -105,6 +107,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`
@@ -119,6 +142,11 @@ For example `pretty-quick --since HEAD` will format only staged files.
-->

### `--ignore-path`

Check an alternative file for ignoring files with the same format as [`.prettierignore`](https://prettier.io/docs/en/ignore#ignoring-files).
For example `pretty-quick --ignore-path=.gitignore`

## Configuration and Ignore Files

`pretty-quick` will respect your [`.prettierrc`](https://prettier.io/docs/en/configuration), [`.prettierignore`](https://prettier.io/docs/en/ignore#ignoring-files), and [`.editorconfig`](http://editorconfig.org/) files, so there's no additional setup required. Configuration files will be found by searching up the file system. `.prettierignore` files are only found from the repository root and the working directory that the command was executed from.
`pretty-quick` will respect your [`.prettierrc`](https://prettier.io/docs/en/configuration), [`.prettierignore`](https://prettier.io/docs/en/ignore#ignoring-files), and [`.editorconfig`](http://editorconfig.org/) files if you don't use `--ignore-path` . Configuration files will be found by searching up the file system. `.prettierignore` files are only found from the repository root and the working directory that the command was executed from.
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;
7 changes: 6 additions & 1 deletion bin/pretty-quick.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,12 @@ 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',
'ignore-path': 'ignorePath',
},
});

const prettyQuickResult = prettyQuick(
process.cwd(),
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@
"scripts": {
"prepublishOnly": "yarn build",
"pretty-quick": "./bin/pretty-quick.js",
"build": "babel src -d dist --copy-files --ignore __tests__",
"build": "babel src -d dist --copy-files --no-copy-ignored --ignore '**/__tests__/*.js'",
"test": "jest",
"lint": "eslint . --ignore-path=.gitignore",
"semantic-release": "semantic-release"
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,
});
});
26 changes: 26 additions & 0 deletions src/__tests__/scm-git.test.js
Original file line number Diff line number Diff line change
@@ -332,4 +332,30 @@ describe('with git', () => {
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});

test('with --ignore-path to ignore files matching patterns from the repositories root .ignorePath', () => {
const onWriteFile = jest.fn();
mockGitFs('', {
'/.ignorePath': '*.md',
});
prettyQuick('/sub-directory/', {
since: 'banana',
onWriteFile,
ignorePath: '/.ignorePath',
});
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});

test('with --ignore-path to ignore files matching patterns from the working directories .ignorePath', () => {
const onWriteFile = jest.fn();
mockGitFs('', {
'/sub-directory/.ignorePath': '*.md',
});
prettyQuick('/sub-directory/', {
since: 'banana',
onWriteFile,
ignorePath: '/.ignorePath',
});
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});
});
26 changes: 26 additions & 0 deletions src/__tests__/scm-hg.test.js
Original file line number Diff line number Diff line change
@@ -231,4 +231,30 @@ describe('with hg', () => {
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});

test('with --ignore-path to ignore files matching patterns from the repositories root .ignorePath', () => {
const onWriteFile = jest.fn();
mockHgFs({
'/.ignorePath': '*.md',
});
prettyQuick('/sub-directory/', {
since: 'banana',
onWriteFile,
ignorePath: '/.ignorePath',
});
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});

test('with --ignore-path to ignore files matching patterns from the working directories .ignorePath', () => {
const onWriteFile = jest.fn();
mockHgFs({
'/.ignorePath': '*.md',
});
prettyQuick('/sub-directory/', {
since: 'banana',
onWriteFile,
ignorePath: '/.ignorePath',
});
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
});
});
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -15,13 +15,15 @@ export default (
branch,
bail,
check,
ignorePath,
verbose,
onFoundSinceRevision,
onFoundChangedFiles,
onPartiallyStagedFile,
onExamineFile,
onCheckFile,
onWriteFile,
resolveConfig = true,
} = {},
) => {
const scm = scms(currentDirectory);
@@ -34,15 +36,15 @@ export default (

onFoundSinceRevision && onFoundSinceRevision(scm.name, revision);

const rootIgnorer = createIgnorer(directory);
const rootIgnorer = createIgnorer(directory, ignorePath);
const cwdIgnorer =
currentDirectory !== directory
? createIgnorer(currentDirectory)
? createIgnorer(currentDirectory, ignorePath)
: () => true;

const changedFiles = scm
.getChangedFiles(directory, revision, staged)
.filter(isSupportedExtension)
.filter(isSupportedExtension(resolveConfig))
.filter(createMatcher(pattern))
.filter(rootIgnorer)
.filter(cwdIgnorer);
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);