Skip to content

Commit

Permalink
Remove unused/outdated helper packages, replace with native JavaScrip…
Browse files Browse the repository at this point in the history
…t functions (fixes #237).
  • Loading branch information
janosh committed Nov 24, 2021
1 parent 328dacc commit 2ea8a2c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 113 deletions.
75 changes: 29 additions & 46 deletions markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ const process = require('process');
const program = require('commander');

const options = program.opts();
const differenceWith = require('lodash.differencewith');
const flatten = require('lodash.flatten');
const glob = require('glob');
const markdownlint = require('markdownlint');
const rc = require('run-con');
const glob = require('glob');
const minimatch = require('minimatch');
const minimist = require('minimist');
const pkg = require('./package.json');

function jsoncParse(text) {
Expand All @@ -38,29 +35,22 @@ const processCwd = process.cwd();

function readConfiguration(userConfigFile) {
const jsConfigFile = /\.js$/i.test(userConfigFile);
const rcArgv = minimist(process.argv.slice(2));
if (jsConfigFile) {
// Prevent rc package from parsing .js config file as INI
delete rcArgv.config;
}

// Load from well-known config files
let config = rc('markdownlint', {}, rcArgv);
let config = rc('markdownlint', {});
for (const projectConfigFile of projectConfigFiles) {
try {
fs.accessSync(projectConfigFile, fs.R_OK);
const projectConfig = markdownlint.readConfigSync(projectConfigFile, configFileParsers);
config = require('deep-extend')(config, projectConfig);
config = {...config, ...projectConfig};
break;
} catch {
// Ignore failure
}
}

// Normally parsing this file is not needed,
// because it is already parsed by rc package.
// However I have to do it to overwrite configuration
// from .markdownlint.{json,yaml,yml}.
// Normally parsing this file is not needed, because it is already parsed by rc package.
// However I have to do it to overwrite configuration from .markdownlint.{json,yaml,yml}.
if (userConfigFile) {
try {
const userConfig = jsConfigFile
Expand Down Expand Up @@ -91,18 +81,17 @@ function prepareFileList(files, fileExtensions, previousResults) {
extensionGlobPart += '{' + fileExtensions.join(',') + '}';
}

files = files.map(function (file) {
files = files.map(file => {
try {
if (fs.lstatSync(file).isDirectory()) {
// Directory (file falls through to below)
if (previousResults) {
const matcher = new minimatch.Minimatch(
path.resolve(processCwd, path.join(file, '**', extensionGlobPart)), globOptions);
return previousResults.filter(function (fileInfo) {
return matcher.match(fileInfo.absolute);
}).map(function (fileInfo) {
return fileInfo.original;
});
path.resolve(processCwd, path.join(file, '**', extensionGlobPart)), globOptions
);
return previousResults.filter(
fileInfo => matcher.match(fileInfo.absolute)
).map(fileInfo => fileInfo.original);
}

return glob.sync(path.join(file, '**', extensionGlobPart), globOptions);
Expand All @@ -111,11 +100,9 @@ function prepareFileList(files, fileExtensions, previousResults) {
// Not a directory, not a file, may be a glob
if (previousResults) {
const matcher = new minimatch.Minimatch(path.resolve(processCwd, file), globOptions);
return previousResults.filter(function (fileInfo) {
return matcher.match(fileInfo.absolute);
}).map(function (fileInfo) {
return fileInfo.original;
});
return previousResults.filter(
fileInfo => matcher.match(fileInfo.absolute)
).map(fileInfo => fileInfo.original);
}

return glob.sync(file, globOptions);
Expand All @@ -124,17 +111,15 @@ function prepareFileList(files, fileExtensions, previousResults) {
// File
return file;
});
return flatten(files).map(function (file) {
return {
original: file,
relative: path.relative(processCwd, file),
absolute: path.resolve(file)
};
});
return files.flat().map(file => ({

This comment has been minimized.

Copy link
@Lenormju
original: file,
relative: path.relative(processCwd, file),
absolute: path.resolve(file)
}));
}

function printResult(lintResult) {
const results = flatten(Object.keys(lintResult).map(file => lintResult[file].map(result => {
const results = Object.keys(lintResult).flatMap(file => lintResult[file].map(result => {
if (options.json) {
return {
fileName: file,
Expand All @@ -151,7 +136,7 @@ function printResult(lintResult) {
+ (result.errorDetail ? ' [' + result.errorDetail + ']' : '')
+ (result.errorContext ? ' [Context: "' + result.errorContext + '"]' : '')
};
})));
}));

let lintResultString = '';
if (results.length > 0) {
Expand Down Expand Up @@ -241,12 +226,12 @@ function tryResolvePath(filepath) {
}

function loadCustomRules(rules) {
return flatten(rules.map(function (rule) {
return rules.flatMap(rule => {
try {
const resolvedPath = [tryResolvePath(rule)];
const fileList = flatten(prepareFileList(resolvedPath, ['js']).map(function (filepath) {
return require(filepath.absolute);
}));
const fileList = prepareFileList(resolvedPath, ['js']).flatMap(
filepath => require(filepath.absolute)
);
if (fileList.length === 0) {
throw new Error('No such rule');
}
Expand All @@ -256,7 +241,7 @@ function loadCustomRules(rules) {
console.error('Cannot load custom rule ' + rule + ': ' + error.message);
return process.exit(3);
}
}));
});
}

let ignorePath = '.markdownlintignore';
Expand All @@ -278,11 +263,9 @@ const files = prepareFileList(program.args, ['md', 'markdown'])
.filter(value => ignoreFilter(value));
const ignores = prepareFileList(options.ignore, ['md', 'markdown'], files);
const customRules = loadCustomRules(options.rules);
const diff = differenceWith(files, ignores, function (a, b) {
return a.absolute === b.absolute;
}).map(function (paths) {
return paths.original;
});
const diff = files.filter(
file => !ignores.some(ignore => ignore.absolute === file.absolute)
).map(paths => paths.original);

function lintAndPrint(stdin, files) {
files = files || [];
Expand Down
60 changes: 0 additions & 60 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"invalid": "node ./markdownlint.js --config test/test-config.json -- test/incorrect.md",
"fix": "cp test/incorrect.md test/incorrect.copy.md && node ./markdownlint.js --fix test/incorrect.copy.md ; cat test/incorrect.copy.md && rm test/incorrect.copy.md",
"test": "ava",
"watch": "npm test -- --watch --tap | tap-growl",
"watch": "npm test --watch",
"start": "node ./markdownlint.js",
"precommit": "xo && npm test"
},
Expand All @@ -38,25 +38,19 @@
"homepage": "https://github.com/igorshubovych/markdownlint-cli#readme",
"dependencies": {
"commander": "~8.3.0",
"deep-extend": "~0.6.0",
"get-stdin": "~8.0.0",
"glob": "~7.2.0",
"ignore": "~5.1.9",
"js-yaml": "^4.1.0",
"jsonc-parser": "~3.0.0",
"lodash.differencewith": "~4.5.0",
"lodash.flatten": "~4.4.0",
"markdownlint": "~0.24.0",
"markdownlint-rule-helpers": "~0.15.0",
"minimatch": "~3.0.4",
"minimist": "~1.2.5",
"run-con": "~1.2.10"
},
"devDependencies": {
"ava": "^3.15.0",
"execa": "^5.1.1",
"husky": "^7.0.4",
"tap-growl": "^3.0.0",
"test-rule-package": "./test/custom-rules/test-rule-package",
"xo": "^0.46.4"
},
Expand Down

0 comments on commit 2ea8a2c

Please sign in to comment.