Skip to content

Commit

Permalink
Add support for glob expression to gitignore configuration (fixes #365).
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Aug 31, 2024
1 parent 48a1cd4 commit 37bde15
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 7 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,15 @@ of the rules within.
- The `String` is passed as the `pattern` parameter to the
[`RegExp` constructor][regexp-constructor]
- For example: `(^---\s*$[^]*?^---\s*$)(\r\n|\r|\n|$)`
- `gitignore`: `Boolean` value to ignore files referenced by `.gitignore` when
linting
- `gitignore`: `Boolean` or `String` value to automatically ignore files
referenced by `.gitignore` (or similar) when linting
- When the value `true` is specified, all `.gitignore` files in the tree are
imported (default `git` behavior)
- When a `String` value is specified, that glob pattern is used to identify
the set of ignore files to import
- The value `**/.gitignore` corresponds to the `Boolean` value `true`
- The value `.gitignore` imports only the file in the root of the tree;
this is usually equivalent and can be much faster for large trees
- This top-level setting is valid **only** in the directory from which
`markdownlint-cli2` is run
- `globs`: `Array` of `String`s defining glob expressions to append to the
Expand Down
15 changes: 13 additions & 2 deletions markdownlint-cli2.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ const enumerateFiles = async (
globPatterns,
dirToDirInfo,
gitignore,
ignoreFiles,
noRequire
) => {
const tasks = [];
Expand All @@ -488,6 +489,7 @@ const enumerateFiles = async (
"dot": true,
"expandDirectories": false,
gitignore,
ignoreFiles,
"suppressErrors": true,
fs
};
Expand Down Expand Up @@ -613,6 +615,7 @@ const createDirInfos = async (
dirToDirInfo,
optionsOverride,
gitignore,
ignoreFiles,
noRequire
) => {
await enumerateFiles(
Expand All @@ -622,6 +625,7 @@ const createDirInfos = async (
globPatterns,
dirToDirInfo,
gitignore,
ignoreFiles,
noRequire
);
await enumerateParents(
Expand Down Expand Up @@ -998,6 +1002,13 @@ const main = async (params) => {
logMessage(`Finding: ${globPatterns.join(" ")}`);
}
// Create linting tasks
const gitignore =
// https://github.com/sindresorhus/globby/issues/265
(!params.fs && (baseMarkdownlintOptions.gitignore === true));
const ignoreFiles =
(!params.fs && (typeof baseMarkdownlintOptions.gitignore === "string")) ?
baseMarkdownlintOptions.gitignore :
undefined;
const dirInfos =
await createDirInfos(
fs,
Expand All @@ -1006,8 +1017,8 @@ const main = async (params) => {
globPatterns,
dirToDirInfo,
optionsOverride,
// https://github.com/sindresorhus/globby/issues/265
!params.fs && Boolean(baseMarkdownlintOptions.gitignore),
gitignore,
ignoreFiles,
noRequire
);
// Output linting status
Expand Down
7 changes: 5 additions & 2 deletions schema/markdownlint-cli2-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@
"default": ""
},
"gitignore": {
"description": "Whether to ignore files referenced by .gitignore when linting (only valid at the root) : https://github.com/DavidAnson/markdownlint-cli2/blob/v0.13.0/README.md#markdownlint-cli2jsonc",
"type": "boolean",
"description": "Whether to ignore files referenced by .gitignore (or glob expression) (only valid at the root) : https://github.com/DavidAnson/markdownlint-cli2/blob/v0.13.0/README.md#markdownlint-cli2jsonc",
"type": [
"boolean",
"string"
],
"default": false
},
"globs": {
Expand Down
1 change: 1 addition & 0 deletions test/gitignore-root-only/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
viewme.md
3 changes: 3 additions & 0 deletions test/gitignore-root-only/.markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"gitignore": ".gitignore"
}
1 change: 1 addition & 0 deletions test/gitignore-root-only/dir/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subdir
14 changes: 14 additions & 0 deletions test/gitignore-root-only/dir/UPPER.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Title

> Tagline

# Description

Text text text
Text text text
Text text text

## Summary

Text text text
6 changes: 6 additions & 0 deletions test/gitignore-root-only/dir/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# About #

Text text text
1. List
3. List
3. List
3 changes: 3 additions & 0 deletions test/gitignore-root-only/dir/subdir/info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Information
Text ` code1` text `code2 ` text

14 changes: 14 additions & 0 deletions test/gitignore-root-only/viewme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Title

> Tagline

# Description

Text text text
Text text text
Text text text

## Summary

Text text text
6 changes: 6 additions & 0 deletions test/markdownlint-cli2-test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,12 @@ const testCases = ({
"exitCode": 1
});

testCase({
"name": "gitignore-root-only",
"args": [ "**/*.{md,MD}" ],
"exitCode": 1
});

testCase({
"name": "literal-files",
"args": [
Expand Down
2 changes: 1 addition & 1 deletion test/markdownlint-cli2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ test("validateMarkdownlintConfigSchema", async (t) => {
});

test("validateMarkdownlintCli2ConfigSchema", async (t) => {
t.plan(89);
t.plan(90);

// Validate schema
// @ts-ignore
Expand Down
32 changes: 32 additions & 0 deletions test/snapshots/markdownlint-cli2-test-exec.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,38 @@ Generated by [AVA](https://avajs.dev).
`,
}

## gitignore-root-only (exec)

> Snapshot 1
{
exitCode: 1,
formatterCodeQuality: '',
formatterJson: '',
formatterJunit: '',
formatterSarif: '',
stderr: `dir/about.md:1:3 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
dir/about.md:1:10 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
dir/about.md:4 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "1. List"]␊
dir/about.md:5:1 MD029/ol-prefix Ordered list item prefix [Expected: 2; Actual: 3; Style: 1/2/3]␊
dir/subdir/info.md:1 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## Information"]␊
dir/subdir/info.md:1 MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "## Information"]␊
dir/subdir/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊
dir/subdir/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊
dir/subdir/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
dir/UPPER.MD:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊
dir/UPPER.MD:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
dir/UPPER.MD:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "Description"]␊
dir/UPPER.MD:12:4 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊
dir/UPPER.MD:14:14 MD047/single-trailing-newline Files should end with a single newline character␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: **/*.{md,MD}␊
Linting: 3 file(s)␊
Summary: 14 error(s)␊
`,
}

## literal-files (exec)

> Snapshot 1
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-exec.js.snap
Binary file not shown.
37 changes: 37 additions & 0 deletions test/snapshots/markdownlint-cli2-test-fs.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,43 @@ Generated by [AVA](https://avajs.dev).

## gitignore (fs)

> Snapshot 1
{
exitCode: 1,
formatterCodeQuality: '',
formatterJson: '',
formatterJunit: '',
formatterSarif: '',
stderr: `dir/about.md:1:3 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
dir/about.md:1:10 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
dir/about.md:4 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "1. List"]␊
dir/about.md:5:1 MD029/ol-prefix Ordered list item prefix [Expected: 2; Actual: 3; Style: 1/2/3]␊
dir/subdir/info.md:1 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## Information"]␊
dir/subdir/info.md:1 MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "## Information"]␊
dir/subdir/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊
dir/subdir/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊
dir/subdir/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
dir/UPPER.MD:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊
dir/UPPER.MD:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
dir/UPPER.MD:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "Description"]␊
dir/UPPER.MD:12:4 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊
dir/UPPER.MD:14:14 MD047/single-trailing-newline Files should end with a single newline character␊
viewme.md:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊
viewme.md:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "Description"]␊
viewme.md:12:4 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊
viewme.md:14:14 MD047/single-trailing-newline Files should end with a single newline character␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: **/*.{md,MD}␊
Linting: 4 file(s)␊
Summary: 19 error(s)␊
`,
}

## gitignore-root-only (fs)

> Snapshot 1
{
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-fs.js.snap
Binary file not shown.
32 changes: 32 additions & 0 deletions test/snapshots/markdownlint-cli2-test-main.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,38 @@ Generated by [AVA](https://avajs.dev).
`,
}

## gitignore-root-only (main)

> Snapshot 1
{
exitCode: 1,
formatterCodeQuality: '',
formatterJson: '',
formatterJunit: '',
formatterSarif: '',
stderr: `dir/about.md:1:3 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
dir/about.md:1:10 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊
dir/about.md:4 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "1. List"]␊
dir/about.md:5:1 MD029/ol-prefix Ordered list item prefix [Expected: 2; Actual: 3; Style: 1/2/3]␊
dir/subdir/info.md:1 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## Information"]␊
dir/subdir/info.md:1 MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "## Information"]␊
dir/subdir/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊
dir/subdir/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊
dir/subdir/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
dir/UPPER.MD:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊
dir/UPPER.MD:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊
dir/UPPER.MD:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "Description"]␊
dir/UPPER.MD:12:4 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊
dir/UPPER.MD:14:14 MD047/single-trailing-newline Files should end with a single newline character␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: **/*.{md,MD}␊
Linting: 3 file(s)␊
Summary: 14 error(s)␊
`,
}

## literal-files (main)

> Snapshot 1
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-main.js.snap
Binary file not shown.

0 comments on commit 37bde15

Please sign in to comment.