Skip to content

Commit

Permalink
Handle "--" parameter per POSIX Utility Conventions 12.2 Guideline 10.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Sep 4, 2024
1 parent caa46d0 commit d66d6b8
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Cross-platform compatibility:
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases
- The path separator is forward slash (/) on all platforms; backslash (\) is automatically converted
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally
The most compatible syntax for cross-platform support:
$ markdownlint-cli2 "**/*.md" "#node_modules"
Expand Down
17 changes: 10 additions & 7 deletions markdownlint-cli2.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ Cross-platform compatibility:
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally
The most compatible syntax for cross-platform support:
$ markdownlint-cli2 "**/*.md" "#node_modules"`
Expand Down Expand Up @@ -921,26 +922,28 @@ const main = async (params) => {
let fixDefault = false;
// eslint-disable-next-line unicorn/no-useless-undefined
let configPath = undefined;
let sawDashDash = false;
let shouldShowHelp = false;
const argvFiltered = (argv || []).filter((arg) => {
if (configPath === null) {
if (sawDashDash) {
return true;
} else if (configPath === null) {
configPath = arg;
return false;
// eslint-disable-next-line unicorn/prefer-switch
} else if (arg === "--") {
sawDashDash = true;
} else if (arg === "--config") {
configPath = null;
return false;
} else if (arg === "--fix") {
fixDefault = true;
return false;
} else if (arg === "--help") {
shouldShowHelp = true;
return false;
} else if (arg === "--no-globs") {
noGlobs = true;
return false;
} else {
return true;
}
return true;
return false;
});
if (shouldShowHelp) {
return showHelp(logMessage, true);
Expand Down
36 changes: 36 additions & 0 deletions test/markdownlint-cli2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { "main": markdownlintCli2 } = require("../markdownlint-cli2.js");
const jsoncParse = require("../parsers/jsonc-parse.js");
const yamlParse = require("../parsers/yaml-parse.js");
const FsMock = require("./fs-mock");
const FsVirtual = require("../webworker/fs-virtual");

const schemaIdVersionRe = /^.*v(?<version>\d+\.\d+\.\d+).*$/u;
const markdownlintConfigSchemaDefinition = require("../schema/markdownlint-config-schema.json");
Expand Down Expand Up @@ -658,3 +659,38 @@ test("--help, glob also present", (t) => {
t.regex(stdouts[0], /^markdownlint-cli2 v/u);
});
});

test("-- stops matching parameters per POSIX Utility Conventions 12.2 Guideline 10", async (t) => {
t.plan(17);
const files = [
[ "/--fix", "# Title" ],
[ "/bad.md", "# Title" ],
[ "/good.md", "# Title\n" ]
];
const scenario = async (argv, exitCode) => t.is(
await markdownlintCli2({
argv,
"directory": "/",
"fs": new FsVirtual(files)
}),
exitCode
);
await scenario([], 2);
await scenario([ "--" ], 2);
await scenario([ "--fix" ], 2);
await scenario([ "--fix", "--" ], 2);
await scenario([ "--", "--fix" ], 1);
await scenario([ "bad.md" ], 1);
await scenario([ "bad.md", "--" ], 1);
await scenario([ "--", "bad.md" ], 1);
await scenario([ "good.md" ], 0);
await scenario([ "good.md", "--" ], 0);
await scenario([ "--", "good.md" ], 0);
await scenario([ "--fix", "--", "good.md" ], 0);
await scenario([ "--fix", "--", "bad.md" ], 0);
await scenario([ "good.md", "--", "--fix" ], 1);
await scenario([ "bad.md", "--", "--fix" ], 1);
await scenario([ "--", "--" ], 0);
files.push([ "/--", "# Title" ]);
await scenario([ "--", "--" ], 1);
});
5 changes: 5 additions & 0 deletions test/snapshots/markdownlint-cli2-test-exec.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -110,6 +111,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -166,6 +168,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -222,6 +225,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -734,6 +738,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-exec.js.snap
Binary file not shown.
5 changes: 5 additions & 0 deletions test/snapshots/markdownlint-cli2-test-fs.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -110,6 +111,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -166,6 +168,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -222,6 +225,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -734,6 +738,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-fs.js.snap
Binary file not shown.
5 changes: 5 additions & 0 deletions test/snapshots/markdownlint-cli2-test-main.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -110,6 +111,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -166,6 +168,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -222,6 +225,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down Expand Up @@ -734,6 +738,7 @@ Generated by [AVA](https://avajs.dev).
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here␊
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases␊
- The path separator is forward slash (/) on all platforms; backslash (\\) is automatically converted␊
- On any platform, passing the parameter "--" causes all remaining parameters to be treated literally␊
The most compatible syntax for cross-platform support:␊
$ markdownlint-cli2 "**/*.md" "#node_modules"␊
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-main.js.snap
Binary file not shown.
9 changes: 8 additions & 1 deletion webworker/fs-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

"use strict";

/* c8 ignore start */

const dirent = (path, directory) => {
const name = path.replace(/^.*\//u, "");
return {
Expand All @@ -16,7 +18,6 @@ const dirent = (path, directory) => {
};
};

// eslint-disable-next-line no-unused-vars
class FsVirtual {
constructor(files) {

Expand Down Expand Up @@ -86,3 +87,9 @@ class FsVirtual {
};
}
}

/* c8 ignore stop */

if (typeof module !== "undefined") {
module.exports = FsVirtual;
}

0 comments on commit d66d6b8

Please sign in to comment.