-
Notifications
You must be signed in to change notification settings - Fork 36
Respect Prettier ignore files in parent directories #1887
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
Open
72636c
wants to merge
3
commits into
main
Choose a base branch
from
lint-subdir
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+133
−7
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,23 @@ | ||
| --- | ||
| 'skuba': patch | ||
| --- | ||
|
|
||
| format, lint: Respect Prettier ignore files in parent directories | ||
|
|
||
| Previously, `skuba format` and `skuba lint` only looked for `.gitignore` and `.prettierignore` in the current working directory. Now, you can execute these commands in a subdirectory while centralising ignore files in the root directory. | ||
|
|
||
| ```text | ||
| ┌── apps/ | ||
| │ └── a/ | ||
| │ ├── src/ | ||
| │ ├── package.json | ||
| │ └── ... | ||
| │ └── b/ <- execute skuba lint here | ||
| │ ├── src/ | ||
| │ ├── package.json | ||
| │ └── ... | ||
| ├── .gitignore | ||
| ├── .prettierignore <- and it will respect this | ||
| ├── package.json | ||
| └── ... | ||
| ``` |
Empty file.
This file contains hidden or 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 @@ | ||
| /apps/ignore/src/ignore.json |
This file contains hidden or 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,13 @@ | ||
| { | ||
| "dependencies": {}, | ||
| "devDependencies": {}, | ||
| "license": "UNLICENSED", | ||
| "private": true, | ||
| "sideEffects": false, | ||
| "skuba": { | ||
| "entryPoint": null, | ||
| "template": null, | ||
| "type": "application", | ||
| "version": "0.0.1" | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| { | ||
| "key": "value" | ||
| } |
This file contains hidden or 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 @@ | ||
| { | ||
| "key": "value" | ||
| } |
This file contains hidden or 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,13 @@ | ||
| { | ||
| "dependencies": {}, | ||
| "devDependencies": {}, | ||
| "license": "UNLICENSED", | ||
| "private": true, | ||
| "sideEffects": false, | ||
| "skuba": { | ||
| "entryPoint": null, | ||
| "template": null, | ||
| "type": "application", | ||
| "version": "0.0.1" | ||
| } | ||
| } |
This file contains hidden or 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,9 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "incremental": true, | ||
| "moduleResolution": "node", | ||
| "outDir": "lib", | ||
| "skipLibCheck": true | ||
| }, | ||
| "extends": "tsconfig-seek" | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ import { | |
| resolveConfig, | ||
| } from 'prettier'; | ||
|
|
||
| import { crawlDirectory } from '../../utils/dir'; | ||
| import { crawlDirectory, locateNearestFile } from '../../utils/dir'; | ||
| import { type Logger, pluralise } from '../../utils/logging'; | ||
| import { getConsumerManifest } from '../../utils/manifest'; | ||
| import { formatPackage, parsePackage } from '../configure/processing/package'; | ||
|
|
@@ -176,11 +176,18 @@ export const runPrettier = async ( | |
| // This avoids exhibiting different behaviour than a Prettier IDE integration, | ||
| // though it may present headaches if `.gitignore` and `.prettierignore` rules | ||
| // conflict. | ||
| const relativeFilepaths = await crawlDirectory(directory, [ | ||
| '.gitignore', | ||
| '.prettierignore', | ||
| // TODO: should these bottom out at the project root? | ||
| const ignoreFilepaths = await Promise.all([ | ||
| locateNearestFile({ cwd, filename: '.gitignore' }), | ||
| locateNearestFile({ cwd, filename: '.prettierignore' }), | ||
| ]); | ||
|
|
||
| const ignoreFilenames = ignoreFilepaths.flatMap((filepath) => | ||
| filepath ? path.relative(cwd, filepath) : [], | ||
|
Contributor
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 possible for file path to be empty? |
||
| ); | ||
|
|
||
| const relativeFilepaths = await crawlDirectory(directory, ignoreFilenames); | ||
|
|
||
| logger.debug(`Discovered ${pluralise(relativeFilepaths.length, 'file')}.`); | ||
|
|
||
| const result: Result = { | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -39,14 +39,38 @@ export const crawlDirectory = async ( | |
| root: string, | ||
| ignoreFilenames = ['.gitignore'], | ||
| ) => { | ||
| const ignoreFileFilter = await createInclusionFilter( | ||
| ignoreFilenames.map((ignoreFilename) => path.join(root, ignoreFilename)), | ||
| const ignoreFilenamesByPrefix = Object.groupBy( | ||
|
Contributor
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. Fancy |
||
| ignoreFilenames, | ||
| (filename) => { | ||
| const prefix = path.relative( | ||
| path.dirname(path.resolve(root, filename)), | ||
| root, | ||
| ); | ||
|
|
||
| return prefix; | ||
| }, | ||
| ); | ||
|
|
||
| const ignores = await Promise.all( | ||
| Object.entries(ignoreFilenamesByPrefix).flatMap( | ||
| async ([prefix, filenames]) => | ||
| ({ | ||
| prefix, | ||
| filter: await createInclusionFilter( | ||
| (filenames ?? []).map((filename) => path.join(root, filename)), | ||
| ), | ||
| }) as const, | ||
| ), | ||
| ); | ||
|
|
||
| const absoluteFilenames = await crawl(root, { | ||
| includeDirName: (dirname) => !['.git', 'node_modules'].includes(dirname), | ||
| includeFilePath: (pathname) => | ||
| ignoreFileFilter(path.relative(root, pathname)), | ||
| ignores.every(({ filter, prefix }) => { | ||
| const filepath = path.join(prefix, path.relative(root, pathname)); | ||
|
|
||
| return filter(filepath); | ||
| }), | ||
| }); | ||
|
|
||
| const relativeFilepaths = absoluteFilenames.map((filepath) => | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe git root?