Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Oct 2, 2023
1 parent 59d8697 commit cfdf8bc
Show file tree
Hide file tree
Showing 15 changed files with 319 additions and 129 deletions.
281 changes: 161 additions & 120 deletions markdownlint-cli2.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions resolve-and-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* Wrapper for calling Node's require.resolve/require with an additional path.
* @param {Object} req Node's require implementation (or equivalent).
* @param {String} id Package identifier to require.
* @param {String} dir Directory to include when resolving paths.
* @param {String[]} dirs Directories to include when resolving paths.
* @returns {Object} Exported module content.
*/
const resolveAndRequire = (req, id, dir) => {
const resolveAndRequire = (req, id, dirs) => {
const resolvePaths = req.resolve.paths ? req.resolve.paths("") : [];
const paths = [ dir, ...resolvePaths ];
const paths = [ ...dirs, ...resolvePaths ];
const resolved = req.resolve(id, { paths });
return req(resolved);
};
Expand Down
10 changes: 10 additions & 0 deletions schema/markdownlint-cli2-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@
"minItems": 1
}
},
"modulePaths": {
"description": "Additional paths to resolve module locations from (only valid at the root)",
"type": "array",
"default": [],
"items": {
"description": "Path to resolve module locations from",
"type": "string",
"minLength": 1
}
},
"noInlineConfig": {
"description": "Whether to disable support of HTML comments within Markdown content",
"type": "boolean",
Expand Down
7 changes: 7 additions & 0 deletions test/markdownlint-cli2-test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,13 @@ const testCases = ({
"post": deleteDirectory
});

testCase({
"name": "modulePaths",
"args": [ "**/*.md" ],
"exitCode": 1,
"usesRequire": true
});

};

module.exports = testCases;
2 changes: 1 addition & 1 deletion test/markdownlint-cli2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ test("validateMarkdownlintConfigSchema", async (t) => {
});

test("validateMarkdownlintCli2ConfigSchema", async (t) => {
t.plan(83);
t.plan(84);

// Validate schema
const { addSchema, validate } =
Expand Down
14 changes: 14 additions & 0 deletions test/modulePaths/.markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"customRules": [
"markdownlint-rule-sample-commonjs"
],
"outputFormatters": [
[ "output-formatter-sample-commonjs" ]
],
"modulePaths": [
"../invalid",
"../customRules",
"../outputFormatters-module",
"../no-config"
]
}
6 changes: 6 additions & 0 deletions test/modulePaths/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/modulePaths/dir/hr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# hr

---
3 changes: 3 additions & 0 deletions test/modulePaths/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/modulePaths/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
38 changes: 33 additions & 5 deletions test/resolve-and-require-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ test("built-in module", (t) => {
t.plan(1);
t.deepEqual(
require("node:fs"),
resolveAndRequire(require, "fs", __dirname)
resolveAndRequire(require, "fs", [ __dirname ])
);
});

test("locally-installed module", (t) => {
t.plan(1);
t.deepEqual(
require("markdownlint"),
resolveAndRequire(require, "markdownlint", __dirname)
resolveAndRequire(require, "markdownlint", [ __dirname ])
);
});

Expand All @@ -31,7 +31,7 @@ test("relative (to __dirname) path to module", (t) => {
resolveAndRequire(
require,
"./customRules/node_modules/markdownlint-rule-sample-commonjs",
__dirname
[ __dirname ]
)
);
});
Expand All @@ -48,7 +48,7 @@ test("module in alternate node_modules", (t) => {
resolveAndRequire(
require,
"markdownlint-rule-sample-commonjs",
path.join(__dirname, "customRules")
[ path.join(__dirname, "customRules") ]
)
);
});
Expand All @@ -67,7 +67,35 @@ test("module in alternate node_modules and no require.resolve.paths", (t) => {
resolveAndRequire(
require,
"markdownlint-rule-sample-commonjs",
path.join(__dirname, "customRules")
[ path.join(__dirname, "customRules") ]
)
);
});

test("module local, relative, and in alternate node_modules", (t) => {
t.plan(3);
const dirs = [
__dirname,
path.join(__dirname, "customRules")
];
t.deepEqual(
require("markdownlint"),
resolveAndRequire(require, "markdownlint", dirs)
);
t.deepEqual(
require("./customRules/node_modules/markdownlint-rule-sample-commonjs"),
resolveAndRequire(
require,
"./customRules/node_modules/markdownlint-rule-sample-commonjs",
dirs
)
);
t.deepEqual(
require("./customRules/node_modules/markdownlint-rule-sample-commonjs"),
resolveAndRequire(
require,
"markdownlint-rule-sample-commonjs",
dirs
)
);
});
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 @@ -7190,3 +7190,35 @@ Generated by [AVA](https://avajs.dev).
Summary: 2 error(s)␊
`,
}

## modulePaths (exec)

> Snapshot 1
{
exitCode: 1,
formatterCodeQuality: '',
formatterJson: '',
formatterJunit: '',
formatterSarif: '',
stderr: `cjs: dir/about.md 1 MD021/no-multiple-space-closed-atx␊
cjs: dir/about.md 4 MD032/blanks-around-lists␊
cjs: dir/about.md 5 MD029/ol-prefix␊
cjs: dir/hr.md 3 sample-rule-commonjs␊
cjs: dir/subdir/info.md 1 MD022/blanks-around-headings/blanks-around-headers␊
cjs: dir/subdir/info.md 1 MD041/first-line-heading/first-line-h1␊
cjs: dir/subdir/info.md 2 MD038/no-space-in-code␊
cjs: dir/subdir/info.md 2 MD038/no-space-in-code␊
cjs: dir/subdir/info.md 4 MD012/no-multiple-blanks␊
cjs: viewme.md 3 MD009/no-trailing-spaces␊
cjs: viewme.md 5 MD012/no-multiple-blanks␊
cjs: viewme.md 6 MD025/single-title/single-h1␊
cjs: viewme.md 12 MD019/no-multiple-space-atx␊
cjs: viewme.md 14 MD047/single-trailing-newline␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: **/*.md␊
Linting: 4 file(s)␊
Summary: 14 error(s)␊
`,
}
Binary file modified test/snapshots/markdownlint-cli2-test-exec.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 @@ -5264,3 +5264,35 @@ Generated by [AVA](https://avajs.dev).
Summary: 2 error(s)␊
`,
}

## modulePaths (main)

> Snapshot 1
{
exitCode: 1,
formatterCodeQuality: '',
formatterJson: '',
formatterJunit: '',
formatterSarif: '',
stderr: `cjs: dir/about.md 1 MD021/no-multiple-space-closed-atx␊
cjs: dir/about.md 4 MD032/blanks-around-lists␊
cjs: dir/about.md 5 MD029/ol-prefix␊
cjs: dir/hr.md 3 sample-rule-commonjs␊
cjs: dir/subdir/info.md 1 MD022/blanks-around-headings/blanks-around-headers␊
cjs: dir/subdir/info.md 1 MD041/first-line-heading/first-line-h1␊
cjs: dir/subdir/info.md 2 MD038/no-space-in-code␊
cjs: dir/subdir/info.md 2 MD038/no-space-in-code␊
cjs: dir/subdir/info.md 4 MD012/no-multiple-blanks␊
cjs: viewme.md 3 MD009/no-trailing-spaces␊
cjs: viewme.md 5 MD012/no-multiple-blanks␊
cjs: viewme.md 6 MD025/single-title/single-h1␊
cjs: viewme.md 12 MD019/no-multiple-space-atx␊
cjs: viewme.md 14 MD047/single-trailing-newline␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: **/*.md␊
Linting: 4 file(s)␊
Summary: 14 error(s)␊
`,
}
Binary file modified test/snapshots/markdownlint-cli2-test-main.js.snap
Binary file not shown.

0 comments on commit cfdf8bc

Please sign in to comment.