Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for extra file extensions #96

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions HELP
Expand Up @@ -7,3 +7,4 @@ Options:
--reporter custom reporter
--jslint-reporter use a jslint compatible xml reporter
--show-non-errors show additional data generated by jshint
--extra-ext comma-separated list of file extension to use (.js is default)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL... This seems so minute to bring up (and cause a rebase..)... but, perhaps extension should be pluralized?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, sure !

6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -48,6 +48,12 @@ Specify custom lint options (see [example/config.json](https://github.com/jshint

Note: This bypasses any .jshintrc files.

## File Extensions

Default extension for files is ".js". If you want to use JSHint with other file extensions (.json), you need to pass this extra extension as an option
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a : or . at the end of this sentence? (not a big deal, but thought I would mention, for consistency)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem


--extra-ext .json

## Default Options

The CLI uses the default options that come with JSHint. However, if it locates a .jshintrc file in your home directory (~/) it will use those options first.
Expand Down
7 changes: 4 additions & 3 deletions lib/cli.js
Expand Up @@ -73,8 +73,9 @@ module.exports = {
projectConfig = path.join(process.cwd(), '.jshintrc'),
customConfig = options["--config"],
customReporter = options["--reporter"] ? path.resolve(process.cwd(), options["--reporter"]) : null,
extraExtensionList = options["--extra-ext"],
targets = options.node;

//could be on Windows which we are looking for an attribute ending in 'node.exe'
if (targets === undefined) {
(function () {
Expand All @@ -90,7 +91,7 @@ module.exports = {
}

targets = typeof targets === "string" ? null : targets.slice(1);

extraExtensionList = typeof extraExtensionList === "string" ? extraExtensionList : "";

if (options["--version"]) {
_version();
Expand Down Expand Up @@ -134,7 +135,7 @@ module.exports = {
});
}

_print(hint.hint(targets, config, reporter, ignore));
_print(hint.hint(targets, config, reporter, ignore, extraExtensionList));
}
};

11 changes: 6 additions & 5 deletions lib/hint.js
Expand Up @@ -69,28 +69,29 @@ function _shouldIgnore(somePath, ignore) {
});
}

function _collect(filePath, files, ignore) {
function _collect(filePath, files, ignore, extraExtensionList) {
if (ignore && _shouldIgnore(filePath, ignore)) {
return;
}

extraExtensionList = extraExtensionList || "";
var regExtension = new RegExp("\.(js" + (extraExtensionList === "" ? "" : "|" + extraExtensionList.replace(/,/g,"|").replace(/[\. ]/g,"") ) +")$");
if (fs.statSync(filePath).isDirectory()) {
fs.readdirSync(filePath).forEach(function (item) {
_collect(path.join(filePath, item), files, ignore);
});
} else if (filePath.match(/\.js$/)) {
} else if (filePath.match(regExtension)) {
files.push(filePath);
}
}

module.exports = {
hint: function (targets, config, reporter, ignore) {
hint: function (targets, config, reporter, ignore, extraExtensionList) {
var files = [],
results = [],
data = [];

targets.forEach(function (target) {
_collect(target, files, ignore);
_collect(target, files, ignore, extraExtensionList);
});

files.forEach(function (file) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/cli.js
Expand Up @@ -125,6 +125,16 @@ describe("cli", function () {
expect(hint.hint.mostRecentCall.args[2]).toEqual(reporter);
});

it("interprets --extra-ext with no extension list and only uses the .js extension", function () {
cli.interpret(["node", "hint", "file.js", "file1.js", "--extra-ext"]);
expect(hint.hint.mostRecentCall.args[4]).toEqual("");
});

it("interprets --extra-ext with .json in extension list and uses the .js and .json extensions", function () {
cli.interpret(["node", "file.js", "file.js", "--extra-ext", ".json"]);
expect(hint.hint.mostRecentCall.args[4]).toEqual(".json");
});

it("reads in a .jshintignore file if present in current working directory", function () {
spyOn(path, "existsSync").andCallFake(function (path) {
return path.match(/\.jshintignore/) ? true : false;
Expand Down
22 changes: 21 additions & 1 deletion test/unit/hint.js
Expand Up @@ -28,7 +28,7 @@ describe("hint", function () {
});

it("collects files", function () {
var targets = ["file1.js", "file2.js", ".hidden"];
var targets = ["file1.js", "file2.js", ".hidden", "file4.json"];

mockJSHINT(true);
spyOn(fs, "readFileSync").andReturn("data");
Expand All @@ -41,10 +41,30 @@ describe("hint", function () {

expect(fs.readFileSync.callCount).toEqual(2);
expect(fs.readFileSync).not.toHaveBeenCalledWith(targets[2], "utf-8");
expect(fs.readFileSync).not.toHaveBeenCalledWith(targets[3], "utf-8");
expect(fs.readFileSync).toHaveBeenCalledWith(targets[0], "utf-8");
expect(fs.readFileSync).toHaveBeenCalledWith(targets[1], "utf-8");
});

it("collects files with other extensions", function () {
var targets = ["file1.js", ".hidden", "file4.json"];
var extraExt = ".json";

mockJSHINT(true);
spyOn(fs, "readFileSync").andReturn("data");

spyOn(fs, "statSync").andReturn({
isDirectory: jasmine.createSpy().andReturn(false)
});

hint.hint(targets, null, null, false, extraExt);

expect(fs.readFileSync.callCount).toEqual(2);
expect(fs.readFileSync).toHaveBeenCalledWith(targets[2], "utf-8");
expect(fs.readFileSync).not.toHaveBeenCalledWith(targets[1], "utf-8");
expect(fs.readFileSync).toHaveBeenCalledWith(targets[0], "utf-8");
});

it("collects directory files", function () {
var targets = ["dir", "file2.js"];

Expand Down