Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Nov 23, 2024
1 parent d7af25c commit d852a0c
Show file tree
Hide file tree
Showing 71 changed files with 15,170 additions and 311 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
markdownlint-cli2.js eol=lf
markdownlint-cli2.mjs eol=lf
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
- name: Install markdownlint-cli2 dependencies locally
run: sudo npm install --no-package-lock --production
- name: Lint with formatters
run: node ./markdownlint-cli2 CONTRIBUTING.md README.md
run: node ./markdownlint-cli2.mjs CONTRIBUTING.md README.md

lint-dockerfile:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sarif.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: npm install --no-package-lock
- run: node markdownlint-cli2.js --config .github/sarif.markdownlint-cli2.jsonc '**/README.md' '#node_modules'
- run: node markdownlint-cli2.mjs --config .github/sarif.markdownlint-cli2.jsonc '**/README.md' '#node_modules'
continue-on-error: true
- uses: github/codeql-action/upload-sarif@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ npm-debug.log
!test/customRules/node_modules
!test/markdownItPlugins/module/node_modules
!test/outputFormatters-module/node_modules
webworker/markdownlint-cli2-webworker.js
webworker/setImmediate.js
webworker/markdownlint-cli2-webworker.cjs
webworker/setImmediate.cjs
5 changes: 1 addition & 4 deletions append-to-array.js → append-to-array.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @ts-check

"use strict";

const sliceSize = 1000;

/**
Expand All @@ -21,5 +19,4 @@ const appendToArray = (destination, source) => {
}
};

appendToArray.sliceSize = sliceSize;
module.exports = appendToArray;
export { appendToArray as default, sliceSize };
18 changes: 11 additions & 7 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ export default [
{
"ignores": [
"test/*/**",
"webworker/markdownlint-cli2-webworker.js",
"webworker/setImmediate.js"
"webworker/markdownlint-cli2-webworker.cjs",
"webworker/setImmediate.cjs"
]
},
{
"languageOptions": {
"sourceType": "commonjs"
},
"linterOptions": {
"reportUnusedDisableDirectives": true
},
Expand Down Expand Up @@ -68,10 +65,17 @@ export default [
},
{
"files": [
"**/*.mjs"
"**/*-formatter-*.js",
"webworker/*.cjs"
],
"languageOptions": {
"sourceType": "module"
"sourceType": "commonjs",
"globals": {
"__dirname": "readonly",
"__filename": "readonly",
"module": "readonly",
"require": "readonly"
}
}
}
];
5 changes: 0 additions & 5 deletions export-markdownlint-helpers.js

This file was deleted.

3 changes: 3 additions & 0 deletions export-markdownlint-helpers.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @ts-check

export { default } from "markdownlint/helpers";
5 changes: 0 additions & 5 deletions export-markdownlint.js

This file was deleted.

3 changes: 3 additions & 0 deletions export-markdownlint.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @ts-check

export { default } from "markdownlint";
70 changes: 33 additions & 37 deletions markdownlint-cli2.js → markdownlint-cli2.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#!/usr/bin/env node

// @ts-check

"use strict";

// @ts-ignore
// eslint-disable-next-line camelcase, no-inline-comments, no-undef
const dynamicRequire = (typeof __non_webpack_require__ === "undefined") ? require : /* c8 ignore next */ __non_webpack_require__;
// Capture native require implementation for dynamic loading of modules

// Requires
const pathDefault = require("node:path");
import fsx from "node:fs";
import { createRequire } from "node:module";
const dynamicRequire = createRequire(import.meta.url);
import os from "node:os";
import pathDefault from "node:path";
const pathPosix = pathDefault.posix;
const { pathToFileURL } = require("node:url");
const markdownlintLibrary = require("markdownlint");
import { pathToFileURL } from "node:url";
import esMain from "es-main";
import { globby } from "globby";
import micromatch from "micromatch";
import markdownlintLibrary from "markdownlint";
const {
applyFixes,
"getVersion": getLibraryVersion,
Expand All @@ -24,9 +24,10 @@ const {
"extendConfig": markdownlintExtendConfig,
"readConfig": markdownlintReadConfig
} = markdownlintPromises;
const appendToArray = require("./append-to-array");
const mergeOptions = require("./merge-options");
const resolveAndRequire = require("./resolve-and-require");
import { expandTildePath } from "markdownlint/helpers";
import appendToArray from "./append-to-array.mjs";
import mergeOptions from "./merge-options.mjs";
import resolveAndRequire from "./resolve-and-require.mjs";

// Variables
const packageName = "markdownlint-cli2";
Expand All @@ -41,13 +42,16 @@ const utf8 = "utf8";
const noop = () => null;

// Gets a JSONC parser
const getJsoncParse = () => require("./parsers/jsonc-parse.js");
import jsoncParse from "./parsers/jsonc-parse.mjs";
const getJsoncParse = () => jsoncParse;

// Gets a YAML parser
const getYamlParse = () => require("./parsers/yaml-parse.js");
import yamlParse from "./parsers/yaml-parse.mjs";
const getYamlParse = () => yamlParse;

// Gets an ordered array of parsers
const getParsers = () => require("./parsers/parsers.js");
import parsers from "./parsers/parsers.mjs";
const getParsers = () => parsers;

// Negates a glob
const negateGlob = (glob) => `!${glob}`;
Expand All @@ -63,15 +67,9 @@ const throwForConfigurationFile = (file, error) => {
// Return a posix path (even on Windows)
const posixPath = (p) => p.split(pathDefault.sep).join(pathPosix.sep);

// Expands a path with a tilde to an absolute path
const expandTildePath = (id) => {
const markdownlintRuleHelpers = require("markdownlint/helpers");
return markdownlintRuleHelpers.expandTildePath(id, require("node:os"));
};

// Resolves module paths relative to the specified directory
const resolveModulePaths = (dir, modulePaths) => (
modulePaths.map((path) => pathDefault.resolve(dir, expandTildePath(path)))
modulePaths.map((path) => pathDefault.resolve(dir, expandTildePath(path, os)))
);

// Read a JSON(C) or YAML file and return the object
Expand All @@ -95,7 +93,7 @@ const importOrRequireResolve = async (dirOrDirs, id, noRequire) => {
return null;
}
const dirs = Array.isArray(dirOrDirs) ? dirOrDirs : [ dirOrDirs ];
const expandId = expandTildePath(id);
const expandId = expandTildePath(id, os);
const errors = [];
try {
return resolveAndRequire(dynamicRequire, expandId, dirs);
Expand Down Expand Up @@ -213,13 +211,12 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => {
};

// Filter a list of files to ignore by glob
const removeIgnoredFiles = (dir, files, ignores) => {
const micromatch = require("micromatch");
return micromatch(
const removeIgnoredFiles = (dir, files, ignores) => (
micromatch(
files.map((file) => pathPosix.relative(dir, file)),
ignores
).map((file) => pathPosix.join(dir, file));
};
).map((file) => pathPosix.join(dir, file))
);

// Process/normalize command-line arguments and return glob patterns
const processArgv = (argv) => {
Expand Down Expand Up @@ -540,8 +537,6 @@ const enumerateFiles = async (
})
);
// Process glob patterns
// eslint-disable-next-line no-inline-comments
const { globby } = await import(/* webpackMode: "eager" */ "globby");
const files = [
...await globby(expandedDirectories, globbyOptions),
...filteredLiteralFiles
Expand Down Expand Up @@ -891,7 +886,8 @@ const outputSummary = async (
const dirs = [ dir, ...modulePaths ];
const formattersAndParams = outputFormatters
? await importOrRequireIdsAndParams(dirs, outputFormatters, noRequire)
: [ [ require("markdownlint-cli2-formatter-default") ] ];
// eslint-disable-next-line no-inline-comments, unicorn/no-await-expression-member
: [ [ (await import(/* webpackMode: "eager" */ "markdownlint-cli2-formatter-default")).default ] ];
await Promise.all(formattersAndParams.map((formatterAndParams) => {
const [ formatter, ...formatterParams ] = formatterAndParams;
return formatter(formatterOptions, ...formatterParams);
Expand All @@ -918,7 +914,7 @@ const main = async (params) => {
} = params;
const logMessage = params.logMessage || noop;
const logError = params.logError || noop;
const fs = params.fs || require("node:fs");
const fs = params.fs || fsx;
const baseDirSystem =
(directory && pathDefault.resolve(directory)) ||
process.cwd();
Expand Down Expand Up @@ -995,7 +991,7 @@ const main = async (params) => {
// Add stdin as a non-file input if necessary
if (useStdin) {
const key = pathPosix.join(baseDir, "stdin");
const { text } = require("node:stream/consumers");
const { text } = await import("node:stream/consumers");
nonFileContents = {
...nonFileContents,
[key]: await text(process.stdin)
Expand Down Expand Up @@ -1082,13 +1078,13 @@ const main = async (params) => {
return errorsPresent ? 1 : 0;
};

// Export functions
module.exports = {
// Exports
export {
main
};

// Run if invoked as a CLI
if (require.main === module) {
if (esMain(import.meta)) {
const params = {
"argv": process.argv.slice(2),
"logMessage": console.log,
Expand Down
4 changes: 1 addition & 3 deletions merge-options.js → merge-options.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @ts-check

"use strict";

/**
* Merges two options objects by combining config and replacing properties.
* @param {object} first First options object.
Expand All @@ -24,4 +22,4 @@ const mergeOptions = (first, second) => {
return merged;
};

module.exports = mergeOptions;
export default mergeOptions;
45 changes: 23 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
"url": "https://dlaa.me/"
},
"license": "MIT",
"type": "commonjs",
"main": "./markdownlint-cli2.js",
"type": "module",
"main": "./markdownlint-cli2.mjs",
"exports": {
".": "./markdownlint-cli2.js",
"./markdownlint": "./export-markdownlint.js",
"./markdownlint/helpers": "./export-markdownlint-helpers.js",
"./parsers": "./parsers/parsers.js",
"./parsers/jsonc": "./parsers/jsonc-parse.js",
"./parsers/yaml": "./parsers/yaml-parse.js"
".": "./markdownlint-cli2.mjs",
"./markdownlint": "./export-markdownlint.mjs",
"./markdownlint/helpers": "./export-markdownlint-helpers.mjs",
"./parsers": "./parsers/parsers.mjs",
"./parsers/jsonc": "./parsers/jsonc-parse.mjs",
"./parsers/yaml": "./parsers/yaml-parse.mjs"
},
"bin": {
"markdownlint-cli2": "markdownlint-cli2.js"
"markdownlint-cli2": "markdownlint-cli2.mjs"
},
"homepage": "https://github.com/DavidAnson/markdownlint-cli2",
"repository": {
Expand All @@ -39,39 +39,40 @@
"playwright-test": "playwright test --config ./webworker/playwright.config.mjs",
"playwright-test-docker": "docker run --rm --volume $PWD:/home/workdir --workdir /home/workdir --ipc=host mcr.microsoft.com/playwright:v1.49.0 npm run playwright-test",
"schema": "cpy ./node_modules/markdownlint/schema/markdownlint-config-schema.json ./schema --flat",
"test": "ava --timeout=1m test/append-to-array-test.js test/fs-mock-test.js test/fs-virtual-test.js test/markdownlint-cli2-test.js test/markdownlint-cli2-test-exec.js test/markdownlint-cli2-test-exports.js test/markdownlint-cli2-test-fs.js test/markdownlint-cli2-test-main.js test/merge-options-test.js test/resolve-and-require-test.js",
"test": "ava --timeout=1m test/append-to-array-test.mjs test/fs-mock-test.mjs test/fs-virtual-test.mjs test/markdownlint-cli2-test.mjs test/markdownlint-cli2-test-exec.mjs test/markdownlint-cli2-test-exports.mjs test/markdownlint-cli2-test-fs.mjs test/markdownlint-cli2-test-main.mjs test/merge-options-test.mjs test/resolve-and-require-test.mjs",
"test-cover": "c8 --100 npm test",
"test-docker-hub-image": "VERSION=$(node -e \"process.stdout.write(require('./package.json').version)\") && docker image rm davidanson/markdownlint-cli2:v$VERSION davidanson/markdownlint-cli2:latest || true && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2:v$VERSION \"*.md\" && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2:latest \"*.md\"",
"test-docker-hub-image-rules": "VERSION=$(node -e \"process.stdout.write(require('./package.json').version)\") && docker image rm davidanson/markdownlint-cli2-rules:v$VERSION davidanson/markdownlint-cli2-rules:latest || true && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2-rules:v$VERSION \"*.md\" && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2-rules:latest \"*.md\"",
"test-docker-image": "VERSION=$(node -e \"process.stdout.write(require('./package.json').version)\") && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2:v$VERSION \"*.md\"",
"test-docker-image-rules": "VERSION=$(node -e \"process.stdout.write(require('./package.json').version)\") && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2-rules:v$VERSION \"*.md\"",
"test-invoke-as-cli": "markdownlint-cli2 CHANGELOG.md",
"test-watch": "git ls-files | entr npm run test",
"update-snapshots": "ava --timeout=1m --update-snapshots test/markdownlint-cli2-test-exec.js test/markdownlint-cli2-test-fs.js test/markdownlint-cli2-test-main.js",
"update-snapshots": "ava --timeout=1m --update-snapshots test/markdownlint-cli2-test-exec.mjs test/markdownlint-cli2-test-fs.mjs test/markdownlint-cli2-test-main.mjs",
"webworker": "cd webworker && webpack --mode none",
"webworker-install": "npm install --no-package-lock --no-save path-browserify setimmediate stream-browserify util webpack-cli && cpy ./node_modules/setimmediate/setImmediate.js ./webworker --flat"
"webworker-install": "npm install --no-package-lock --no-save path-browserify setimmediate stream-browserify util webpack-cli && cpy ./node_modules/setimmediate/setImmediate.js ./webworker --flat --rename=setImmediate.cjs"
},
"engines": {
"node": ">=18"
},
"files": [
"append-to-array.js",
"append-to-array.mjs",
"CHANGELOG.md",
"export-markdownlint.js",
"export-markdownlint-helpers.js",
"export-markdownlint.mjs",
"export-markdownlint-helpers.mjs",
"LICENSE",
"markdownlint-cli2.js",
"merge-options.js",
"parsers/parsers.js",
"parsers/jsonc-parse.js",
"parsers/yaml-parse.js",
"markdownlint-cli2.mjs",
"merge-options.mjs",
"parsers/parsers.mjs",
"parsers/jsonc-parse.mjs",
"parsers/yaml-parse.mjs",
"README.md",
"resolve-and-require.js",
"resolve-and-require.mjs",
"schema/markdownlint-cli2-config-schema.json",
"schema/markdownlint-config-schema.json",
"schema/ValidatingConfiguration.md"
],
"dependencies": {
"es-main": "1.3.0",
"globby": "14.0.2",
"js-yaml": "4.1.0",
"jsonc-parser": "3.3.1",
Expand All @@ -94,7 +95,6 @@
"eslint-plugin-jsdoc": "50.5.0",
"eslint-plugin-n": "17.14.0",
"eslint-plugin-unicorn": "56.0.1",
"nano-spawn": "0.2.0",
"markdown-it-emoji": "3.0.0",
"markdown-it-for-inline": "2.0.1",
"markdownlint-cli2-formatter-codequality": "0.0.5",
Expand All @@ -105,6 +105,7 @@
"markdownlint-cli2-formatter-summarize": "0.0.7",
"markdownlint-cli2-formatter-template": "0.0.2",
"markdownlint-rule-extended-ascii": "0.1.0",
"nano-spawn": "0.2.0",
"npm-run-all": "4.1.5"
},
"keywords": [
Expand Down
6 changes: 2 additions & 4 deletions parsers/jsonc-parse.js → parsers/jsonc-parse.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @ts-check

"use strict";

const { parse, printParseErrorCode } = require("jsonc-parser");
import { parse, printParseErrorCode } from "jsonc-parser";

/**
* Parses a JSONC string, returning the corresponding object.
Expand All @@ -21,4 +19,4 @@ const jsoncParse = (text) => {
return result;
};

module.exports = jsoncParse;
export default jsoncParse;
Loading

0 comments on commit d852a0c

Please sign in to comment.