Skip to content

Commit

Permalink
Add --enable and --disable CLI flags (fixes #214)
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Nov 16, 2021
1 parent 2d2ba47 commit 3182fa0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
12 changes: 8 additions & 4 deletions README.md
Expand Up @@ -26,19 +26,20 @@ markdownlint --help
MarkdownLint Command Line Interface

Options:

-h, --help output usage information
-V, --version output the version number
-c, --config [configFile] configuration file (JSON, JSONC, JS, or YAML)
-d, --dot include files/folders with a dot (for example `.github`)
-f, --fix fix basic errors (does not work with STDIN)
-i, --ignore [file|directory|glob] file(s) to ignore/exclude
-i, --ignore [file|directory|glob] file(s) to ignore/exclude (default: [])
-j, --json write issues in json format
-o, --output [outputFile] write issues to file (no console)
-p, --ignore-path [file] path to file with ignore pattern(s)
-q, --quiet do not write issues to STDOUT
-r, --rules [file|directory|glob|package] custom rule files
-r, --rules [file|directory|glob|package] include custom rule files (default: [])
-s, --stdin read from STDIN (does not work with files)
--enable [rules...] Enable certain rules, e.g. --enable MD013 MD041
--disable [rules...] Disable certain rules, e.g. --disable MD013 MD041
-h, --help display help for command
```

### Globbing
Expand Down Expand Up @@ -101,6 +102,9 @@ If the `--config` argument is provided, the file must be valid JSON, JSONC, JS,
JS configuration files contain JavaScript code, must have the `.js` extension, and must export (via `module.exports = ...`) a configuration object of the form shown above.
A JS configuration file may internally `require` one or more npm packages as a way of reusing configuration across projects.

`--enable` and `--disable` override configuration files; if a configuration file disables `MD002` and you pass `--enable MD002`, it will be enabled.
If a rule is passed to both `--enable` and `--disable`, it will be disabled.

> JS configuration files must be provided via the `--config` argument; they are not automatically loaded because running untrusted code is a security concern.
## Exit codes
Expand Down
16 changes: 14 additions & 2 deletions markdownlint.js
Expand Up @@ -216,8 +216,10 @@ program
.option('-o, --output [outputFile]', 'write issues to file (no console)')
.option('-p, --ignore-path [file]', 'path to file with ignore pattern(s)')
.option('-q, --quiet', 'do not write issues to STDOUT')
.option('-r, --rules [file|directory|glob|package]', 'custom rule files', concatArray, [])
.option('-s, --stdin', 'read from STDIN (does not work with files)');
.option('-r, --rules [file|directory|glob|package]', 'include custom rule files', concatArray, [])
.option('-s, --stdin', 'read from STDIN (does not work with files)')
.option('--enable [rules...]', 'Enable certain rules, e.g. --enable MD013 MD041')
.option('--disable [rules...]', 'Disable certain rules, e.g. --disable MD013 MD041');

program.parse(process.argv);

Expand Down Expand Up @@ -291,6 +293,16 @@ const diff = differenceWith(files, ignores, function (a, b) {
function lintAndPrint(stdin, files) {
files = files || [];
const config = readConfiguration(options.config);

for (const rule of options.enable || []) {
// leave default values in place if rule is an object
if (!config[rule]) {
config[rule] = true;
}
}
for (const rule of options.disable || []) {
config[rule] = false;
}
const lintOptions = {
config,
customRules,
Expand Down
1 change: 1 addition & 0 deletions test/default-false-config.yml
@@ -0,0 +1 @@
default: false
69 changes: 69 additions & 0 deletions test/test.js
Expand Up @@ -851,3 +851,72 @@ test('with --quiet option does not print to stdout or stderr', async t => {
t.is(error.exitCode, 1)
}
});

test('--enable flag', async t => {
try {
await execa('../markdownlint.js',
['--enable', 'MD002', '--config', 'default-false-config.yml', 'incorrect.md'],
{stripFinalNewline: false});
t.fail();
} catch (error) {
t.is(error.stdout, '');
t.is(error.stderr, 'incorrect.md:1 MD002/first-heading-h1/first-header-h1 First heading should be a top-level heading [Expected: h1; Actual: h2]\n');
t.is(error.exitCode, 1)
}
});

test('--enable flag does not modify already enabled rules', async t => {
try {
await execa('../markdownlint.js',
['--enable', 'MD043', '--config', 'md043-config.yaml', 'correct.md'],
{stripFinalNewline: false});
t.fail();
} catch (error) {
t.is(error.stdout, '');
t.is(error.stderr, 'correct.md:1 MD043/required-headings/required-headers Required heading structure [Expected: # First; Actual: # header]\n');
t.is(error.exitCode, 1)
}
});

test('--enable flag accepts rule alias', async t => {
try {
await execa('../markdownlint.js',
['--enable', 'first-header-h1', '--config', 'default-false-config.yml', 'incorrect.md'],
{stripFinalNewline: false});
t.fail();
} catch (error) {
t.is(error.stdout, '');
t.is(error.stderr, 'incorrect.md:1 MD002/first-heading-h1/first-header-h1 First heading should be a top-level heading [Expected: h1; Actual: h2]\n');
t.is(error.exitCode, 1)
}
});

test('--disable flag', async t => {
const result = await execa('../markdownlint.js',
['--disable', 'MD002', 'MD014', 'MD022', 'MD041', '--', 'incorrect.md'],
{stripFinalNewline: false});

t.is(result.stdout, '');
t.is(result.stderr, '');
t.is(result.exitCode, 0)

try {
await execa('../markdownlint.js',
['--disable', 'MD014', 'MD014', 'MD022', '--', 'incorrect.md'],
{stripFinalNewline: false});
t.fail();
} catch (error) {
t.is(error.stdout, '');
t.is(error.stderr, 'incorrect.md:1 MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "## header 2"]\n');
t.is(error.exitCode, 1)
}
});

test('--disable flag overrides --enable flag', async t => {
const result = await execa('../markdownlint.js',
['--disable', 'MD002', '--enable', 'MD002', '--config', 'default-false-config.yml', 'incorrect.md'],
{stripFinalNewline: false});
t.is(result.stdout, '');
t.is(result.stderr, '');
t.is(result.exitCode, 0)
});

0 comments on commit 3182fa0

Please sign in to comment.