From e785e42dbb0947bf3d510db6a3ccfea21416813c Mon Sep 17 00:00:00 2001 From: David Anson Date: Sun, 28 Jan 2024 05:11:48 +0000 Subject: [PATCH] Make scoped custom rule package identifiers work with the --rules parameter (fixes #449). --- .gitignore | 1 + markdownlint.js | 2 +- .../node_modules/@scoped/custom-rule/package.json | 6 ++++++ .../@scoped/custom-rule/scoped-rule.cjs | 10 ++++++++++ test/custom-rules/scoped-package/scoped-test.md | 1 + test/test.js | 15 +++++++++++++++ 6 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/custom-rules/scoped-package/node_modules/@scoped/custom-rule/package.json create mode 100644 test/custom-rules/scoped-package/node_modules/@scoped/custom-rule/scoped-rule.cjs create mode 100644 test/custom-rules/scoped-package/scoped-test.md diff --git a/.gitignore b/.gitignore index 5658f8a4f..99e24c745 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ build/Release node_modules # Keep it for test !test/custom-rules/relative-to-cwd/node_modules +!test/custom-rules/scoped-package/node_modules # Optional npm cache directory .npm diff --git a/markdownlint.js b/markdownlint.js index 708a3b78e..46b3cb8bb 100755 --- a/markdownlint.js +++ b/markdownlint.js @@ -199,7 +199,7 @@ program.parse(process.argv); function tryResolvePath(filepath) { try { - if (path.basename(filepath) === filepath && path.extname(filepath) === '') { + if ((path.basename(filepath) === filepath || filepath.startsWith('@')) && path.extname(filepath) === '') { // Looks like a package name, resolve it relative to cwd // Get list of directories, where requested module can be. let paths = Module._nodeModulePaths(processCwd); diff --git a/test/custom-rules/scoped-package/node_modules/@scoped/custom-rule/package.json b/test/custom-rules/scoped-package/node_modules/@scoped/custom-rule/package.json new file mode 100644 index 000000000..0925b0159 --- /dev/null +++ b/test/custom-rules/scoped-package/node_modules/@scoped/custom-rule/package.json @@ -0,0 +1,6 @@ +{ + "name": "markdownlint-cli-test-scoped-package-rule", + "main": "./scoped-rule.cjs", + "version": "0.0.1", + "private": true +} diff --git a/test/custom-rules/scoped-package/node_modules/@scoped/custom-rule/scoped-rule.cjs b/test/custom-rules/scoped-package/node_modules/@scoped/custom-rule/scoped-rule.cjs new file mode 100644 index 000000000..e92d49ae8 --- /dev/null +++ b/test/custom-rules/scoped-package/node_modules/@scoped/custom-rule/scoped-rule.cjs @@ -0,0 +1,10 @@ +module.exports = { + names: ['scoped-rule'], + description: 'Scoped rule', + tags: ['test'], + function: (_, onError) => { + onError({ + lineNumber: 1 + }); + } +}; diff --git a/test/custom-rules/scoped-package/scoped-test.md b/test/custom-rules/scoped-package/scoped-test.md new file mode 100644 index 000000000..07d82ef3a --- /dev/null +++ b/test/custom-rules/scoped-package/scoped-test.md @@ -0,0 +1 @@ +# Scoped Test diff --git a/test/test.js b/test/test.js index 8253c25f8..682385908 100644 --- a/test/test.js +++ b/test/test.js @@ -535,6 +535,21 @@ test('Custom rule from node_modules package loaded relative to cwd', async t => } }); +test('Custom rule with scoped package name via --rules', async t => { + try { + await execa(path.resolve('..', 'markdownlint.js'), ['--rules', '@scoped/custom-rule', 'scoped-test.md'], { + cwd: path.join(__dirname, 'custom-rules', 'scoped-package'), + stripFinalNewline: false + }); + t.fail(); + } catch (error) { + const expected = ['scoped-test.md:1 scoped-rule Scoped rule', ''].join('\n'); + t.is(error.stdout, ''); + t.is(error.stderr, expected); + t.is(error.exitCode, 1); + } +}); + test('Custom rule from package loaded', async t => { try { const input = '# Input\n';