Skip to content

Commit

Permalink
[[FIX]] Remove shelljs
Browse files Browse the repository at this point in the history
The "shelljs" package upon which JSHint depends includes a security
vulnerability [1]. Futhermore, that package's coupling to Node.js runs
contrary to JSHint's goal to support the Rhino runtime.

Replace the package with a minimal implementation.

This commit was forcibly pushed to the main development branch in order
to correct a formatting error in the original version.

[1] #3599
  • Loading branch information
jugglinmike committed Jan 24, 2022
1 parent b23e125 commit eb4609a
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 204 deletions.
17 changes: 0 additions & 17 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -46,7 +46,6 @@
"htmlparser2": "3.8.x",
"lodash": "~4.17.21",
"minimatch": "~3.0.2",
"shelljs": "0.3.x",
"strip-json-comments": "1.0.x"
},
"devDependencies": {
Expand Down
30 changes: 15 additions & 15 deletions src/cli.js
Expand Up @@ -3,13 +3,13 @@
var _ = require("lodash");
var cli = require("cli");
var path = require("path");
var shjs = require("shelljs");
var minimatch = require("minimatch");
var htmlparser = require("htmlparser2");
var exit = require("exit");
var stripJsonComments = require("strip-json-comments");
var JSHINT = require("./jshint.js").JSHINT;
var defReporter = require("./reporters/default").reporter;
var fsUtils = require("./fs-utils");

var OPTIONS = {
"config": ["c", "Custom configuration file", "string", false ],
Expand Down Expand Up @@ -89,7 +89,7 @@ function findConfig(file) {
else if (envs) {
home = path.normalize(path.join(envs, ".jshintrc"));

if (shjs.test("-e", home))
if (fsUtils.exists(home))
return home;
}

Expand All @@ -108,7 +108,7 @@ function getHomeDir() {

while (paths.length) {
homePath = paths.shift();
if (homePath && shjs.test("-e", homePath)) {
if (fsUtils.exists(homePath)) {
return homePath;
}
}
Expand Down Expand Up @@ -177,7 +177,7 @@ function findFile(name, cwd) {

var parent = path.resolve(cwd, "../");

if (shjs.test("-e", filename)) {
if (fsUtils.exists(filename)) {
findFileResults[filename] = filename;
return filename;
}
Expand All @@ -203,7 +203,7 @@ function loadIgnores(params) {
return [];
}

var lines = (file ? shjs.cat(file) : "").split("\n");
var lines = (file ? fsUtils.readFile(file) : "").split("\n");
var exclude = params.exclude || "";
lines.unshift.apply(lines, exclude.split(","));

Expand Down Expand Up @@ -237,7 +237,7 @@ function isIgnored(fp, patterns) {
return true;
}

if (shjs.test("-d", fp) && ip.match(/^[^\/\\]*[\/\\]?$/) &&
if (fsUtils.isDirectory(fp) && ip.match(/^[^\/\\]*[\/\\]?$/) &&
fp.match(new RegExp("^" + ip + ".*"))) {
return true;
}
Expand Down Expand Up @@ -419,15 +419,15 @@ function collect(fp, files, ignores, ext) {
return;
}

if (!shjs.test("-e", fp)) {
if (!fsUtils.exists(fp)) {
cli.error("Can't open " + fp);
return;
}

if (shjs.test("-d", fp)) {
shjs.ls(fp).forEach(function(item) {
if (fsUtils.isDirectory(fp)) {
fsUtils.readDirectory(fp).forEach(function(item) {
var itempath = path.join(fp, item);
if (shjs.test("-d", itempath) || item.match(ext)) {
if (fsUtils.isDirectory(itempath) || item.match(ext)) {
collect(itempath, files, ignores, ext);
}
});
Expand Down Expand Up @@ -458,8 +458,8 @@ function lint(code, results, config, data, file) {
if (config.prereq) {
config.prereq.forEach(function(fp) {
fp = path.join(config.dirname, fp);
if (shjs.test("-e", fp))
buffer.push(shjs.cat(fp));
if (fsUtils.exists(fp))
buffer.push(fsUtils.readFile(fp));
});
delete config.prereq;
}
Expand Down Expand Up @@ -529,13 +529,13 @@ var exports = {
return {};
}

if (!shjs.test("-e", fp)) {
if (!fsUtils.exists(fp)) {
cli.error("Can't find config file: " + fp);
exports.exit(1);
}

try {
var config = JSON.parse(stripJsonComments(shjs.cat(fp)));
var config = JSON.parse(stripJsonComments(fsUtils.readFile(fp)));
config.dirname = path.dirname(fp);

if (config['extends']) {
Expand Down Expand Up @@ -653,7 +653,7 @@ var exports = {
var errors = [];

try {
code = shjs.cat(file);
code = fsUtils.readFile(file);
} catch (err) {
cli.error("Can't open " + file);
exports.exit(1);
Expand Down
61 changes: 61 additions & 0 deletions src/fs-utils.js
@@ -0,0 +1,61 @@
"use strict";

var fs = require("fs");

/**
* Determine if a file or directory is present at the given filesystem path.
*
* @param {string} name
*
* @returns {boolean}
*/
exports.exists = function(name) {
return fs.existsSync(name);
};

/**
* Determine if a directory is present at the given filesystem path.
*
* @param {string} name
*
* @returns {boolean}
*/
exports.isDirectory = function(name) {
var stat;
try {
stat = fs.statSync(name);
} catch (error) {
if (error.code === "ENOENT") {
return false;
}

throw error;
}

return stat.isDirectory();
};

/**
* Read a UTF-8-encoded file.
*
* @param {string} name
*
* @returns {string}
*/
exports.readFile = function(name) {
return fs.readFileSync(name, "utf8");
};

/**
* Retrieve the name of the files and directories within a given directory.
*
* @param {string} name
*
* @returns {string[]}
*/
exports.readDirectory = function(name) {
return fs.readdirSync(name)
.filter(function(name) {
return name[0] !== ".";
});
};

0 comments on commit eb4609a

Please sign in to comment.