From 6b5899dc65d82cee89486fb2a027ed5fd153d27a Mon Sep 17 00:00:00 2001 From: Benjamin Wilking Date: Tue, 20 Feb 2024 08:55:40 +0100 Subject: [PATCH] fix review comments and add additional tests also chang the order of parsers to be used --- README.md | 15 ++++---------- markdownlint.js | 9 +++++---- package.json | 2 +- test/config-files/toml/.markdownlint.toml | 2 -- test/config-files/toml/heading-dollar.md | 3 --- test/test-config.toml | 14 +++++++++++++ test/test-config.yaml | 12 ++++++++++++ test/test.js | 24 +++++++++++++++++++++-- 8 files changed, 58 insertions(+), 23 deletions(-) delete mode 100644 test/config-files/toml/.markdownlint.toml delete mode 100644 test/config-files/toml/heading-dollar.md create mode 100644 test/test-config.toml create mode 100644 test/test-config.yaml diff --git a/README.md b/README.md index 4dc5e5837..f7493981f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ markdownlint --help Options: -V, --version output the version number - -c, --config [configFile] configuration file (JSON, JSONC, JS, or YAML) + -c, --config [configFile] configuration file (JSON, JSONC, JS, YAML, or TOML) -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 (default: []) @@ -102,15 +102,7 @@ A sample configuration file: } ``` -```toml -default = true -heading-style = { style = "atx_closed" } -ul-indent = { indent = 4 } -no-hard-tabs = false -whitespace = false -``` - -For more examples, see [.markdownlint.jsonc][markdownlint-jsonc], [.markdownlint.yaml][markdownlint-yaml], or the [style folder][style-folder]. +For more examples, see [.markdownlint.jsonc][markdownlint-jsonc], [.markdownlint.yaml][markdownlint-yaml], [test-config.toml](test/test-config.toml) or the [style folder][style-folder]. The CLI argument `--config` is not required. If it is not provided, `markdownlint-cli` looks for the file `.markdownlint.jsonc`/`.markdownlint.json`/`.markdownlint.yaml`/`.markdownlint.yml` in current folder, or for the file `.markdownlintrc` in the current or all parent folders. @@ -124,7 +116,8 @@ A JS configuration file may internally `require` one or more npm packages as a w `--enable` and `--disable` override configuration files; if a configuration file disables `MD123` and you pass `--enable MD123`, 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. +> - JS configuration files must be provided via the `--config` argument; they are not automatically loaded because running untrusted code is a security concern. +> - TOML configuration files must be provided via the `--config` argument; they are not automatically loaded. ## Exit codes diff --git a/markdownlint.js b/markdownlint.js index 108add706..8edafcf45 100755 --- a/markdownlint.js +++ b/markdownlint.js @@ -37,7 +37,8 @@ function yamlParse(text) { } function tomlParse(text) { - return require('toml').parse(text); + //It is necessary to add the prototype manually because of https://github.com/BinaryMuse/toml-node/issues/55 + return require('deep-extend')({}, require('toml').parse(text)); } const exitCodes = { @@ -47,8 +48,9 @@ const exitCodes = { unexpectedError: 4 }; -const projectConfigFiles = ['.markdownlint.jsonc', '.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml', '.markdownlint.toml']; -const configParsers = [jsoncParse, yamlParse, tomlParse]; +const projectConfigFiles = ['.markdownlint.jsonc', '.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml']; +// toml files can be (incorrectly) read by yamlParse but not vice versa -> tomlParse needs to go first in the list +const configParsers = [jsoncParse, tomlParse, yamlParse]; const fsOptions = {encoding: 'utf8'}; const processCwd = process.cwd(); @@ -57,7 +59,6 @@ function readConfiguration(userConfigFile) { // Load from well-known config files let config = rc('markdownlint', {}); - for (const projectConfigFile of projectConfigFiles) { try { fs.accessSync(projectConfigFile, fs.R_OK); diff --git a/package.json b/package.json index e8654bea3..77b696968 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "markdownlint": "~0.33.0", "minimatch": "~9.0.3", "run-con": "~1.3.2", - "toml": "^3.0.0" + "toml": "~3.0.0" }, "devDependencies": { "ava": "^6.1.1", diff --git a/test/config-files/toml/.markdownlint.toml b/test/config-files/toml/.markdownlint.toml deleted file mode 100644 index 8db87e7fc..000000000 --- a/test/config-files/toml/.markdownlint.toml +++ /dev/null @@ -1,2 +0,0 @@ -[no-trailing-punctuation] -punctuation="$" diff --git a/test/config-files/toml/heading-dollar.md b/test/config-files/toml/heading-dollar.md deleted file mode 100644 index d976ce366..000000000 --- a/test/config-files/toml/heading-dollar.md +++ /dev/null @@ -1,3 +0,0 @@ -# Heading$ - -Text diff --git a/test/test-config.toml b/test/test-config.toml new file mode 100644 index 000000000..30e4697f6 --- /dev/null +++ b/test/test-config.toml @@ -0,0 +1,14 @@ +default = true +no-hard-tabs = false +whitespace = false +MD033 = false +MD034 = false + +[MD003] +style = "atx" + +[MD007] +indent = 4 + +[MD013] +line_length = 200 diff --git a/test/test-config.yaml b/test/test-config.yaml new file mode 100644 index 000000000..ce01fb632 --- /dev/null +++ b/test/test-config.yaml @@ -0,0 +1,12 @@ +default: true +no-hard-tabs: false +whitespace: false +MD033: false +MD034: false +MD003: + style: atx +MD007: + indent: 2 +MD013: + line_length: 200 + diff --git a/test/test.js b/test/test.js index 70ee2e66b..59a31c518 100644 --- a/test/test.js +++ b/test/test.js @@ -407,6 +407,28 @@ test('configuration file can be TOML', async t => { t.is(result.exitCode, 0); }); +test('linting using a toml configuration file works', async t => { + try { + await execa('../markdownlint.js', ['--config', 'test-config.toml', '**/*.md'], {stripFinalNewline: false}); + t.fail(); + } catch (error) { + t.is(error.stdout, ''); + t.is(error.stderr.match(errorPattern).length, 15); + t.is(error.exitCode, 1); + } +}); + +test('linting using a yaml configuration file works', async t => { + try { + await execa('../markdownlint.js', ['--config', 'test-config.yaml', '**/*.md'], {stripFinalNewline: false}); + t.fail(); + } catch (error) { + t.is(error.stdout, ''); + t.is(error.stderr.match(errorPattern).length, 15); + t.is(error.exitCode, 1); + } +}); + test('error on configuration file not found', async t => { try { await execa('../markdownlint.js', ['--config', 'non-existent-file-path.yaml', 'correct.md'], {stripFinalNewline: false}); @@ -448,8 +470,6 @@ test('.markdownlint.jsonc in cwd is used automatically', getCwdConfigFileTest('j test('.markdownlint.json in cwd is used automatically', getCwdConfigFileTest('json')); -test('.markdownlint.toml in cwd is used automatically', getCwdConfigFileTest('toml')); - test('.markdownlint.yaml in cwd is used automatically', getCwdConfigFileTest('yaml')); test('.markdownlint.yml in cwd is used automatically', getCwdConfigFileTest('yml'));