Skip to content

Commit 0e39d21

Browse files
committed
Merge pull request #30 from codeclimate/include-paths-or-exclude-paths
Update to handle config.include_paths or config.exclude_paths.
2 parents fb0f1bf + a0d04e4 commit 0e39d21

File tree

1 file changed

+74
-23
lines changed

1 file changed

+74
-23
lines changed

bin/eslint.js

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,74 @@ function buildIssueJson(message, path) {
4545
return JSON.stringify(issue);
4646
}
4747

48-
// Uses glob to traverse code directory and find files to analyze,
49-
// excluding files passed in with by CLI config, and including only
50-
// files in the list of desired extensions
51-
function fileWalk(excludePaths, extensions){
52-
var analysisFiles = [];
53-
var allFiles = glob.sync("/code/**/**", {});
54-
55-
allFiles.forEach(function(file, i, a){
56-
if(excludePaths.indexOf(file.split("/code/")[1]) < 0) {
57-
if(!fs.lstatSync(file).isDirectory()) {
58-
var extension = "." + file.split(".").pop();
59-
60-
if(extensions.indexOf(extension) >= 0) {
61-
analysisFiles.push(file);
48+
function isFileWithMatchingExtension(file, extensions) {
49+
var stats = fs.lstatSync(file);
50+
var extension = "." + file.split(".").pop();
51+
return (
52+
stats.isFile() &&
53+
!stats.isSymbolicLink()
54+
&& extensions.indexOf(extension) >= 0
55+
);
56+
}
57+
58+
function exclusionBasedFileListBuilder(excludePaths) {
59+
// Uses glob to traverse code directory and find files to analyze,
60+
// excluding files passed in with by CLI config, and including only
61+
// files in the list of desired extensions.
62+
//
63+
// Deprecated style of file expansion, supported for users of the old CLI.
64+
return function(extensions) {
65+
var analysisFiles = [];
66+
var allFiles = glob.sync("/code/**/**", {});
67+
68+
allFiles.forEach(function(file, i, a){
69+
if(excludePaths.indexOf(file.split("/code/")[1]) < 0) {
70+
if(fs.lstatSync(file).isFile()) {
71+
if (isFileWithMatchingExtension(file)) {
72+
analysisFiles.push(file);
73+
}
6274
}
6375
}
64-
}
65-
});
76+
});
77+
78+
return analysisFiles;
79+
};
80+
}
6681

67-
return analysisFiles;
82+
function inclusionBasedFileListBuilder(includePaths) {
83+
// Uses glob to expand the files and directories in includePaths, filtering
84+
// down to match the list of desired extensions.
85+
return function(extensions) {
86+
var analysisFiles = [];
87+
88+
includePaths.forEach(function(fileOrDirectory, i) {
89+
if ((/\/$/).test(fileOrDirectory)) {
90+
// if it ends in a slash, expand and push
91+
var filesInThisDirectory = glob.sync(
92+
"/code/" + fileOrDirectory + "/**/**"
93+
);
94+
filesInThisDirectory.forEach(function(file, j){
95+
if (isFileWithMatchingExtension(file, extensions)) {
96+
analysisFiles.push(file);
97+
}
98+
});
99+
} else {
100+
// if not, check for ending in *.js
101+
var fullPath = "/code/" + fileOrDirectory;
102+
if (isFileWithMatchingExtension(fullPath, extensions)) {
103+
analysisFiles.push(fullPath);
104+
}
105+
}
106+
});
107+
108+
return analysisFiles;
109+
};
68110
}
69111

70112
var options = {
71113
extensions: [".js"], ignore: true, reset: false, useEslintrc: true
72114
};
73-
var ignores = [];
74-
115+
var buildFileList;
75116
runWithTiming("engineConfig", function () {
76117
if (fs.existsSync("/config.json")) {
77118
var engineConfig = JSON.parse(fs.readFileSync("/config.json"));
@@ -80,8 +121,16 @@ runWithTiming("engineConfig", function () {
80121
options.configFile = "/code/" + engineConfig.config;
81122
}
82123

83-
if (engineConfig.exclude_paths) {
84-
ignores = engineConfig.exclude_paths;
124+
if (engineConfig.include_paths) {
125+
buildFileList = inclusionBasedFileListBuilder(
126+
engineConfig.include_paths
127+
);
128+
} else if (engineConfig.exclude_paths) {
129+
var ignores = engineConfig.exclude_paths;
130+
buildFileList = exclusionBasedFileListBuilder(ignores);
131+
} else {
132+
// No includes or excludes, let's try with everything
133+
buildFileList = exclusionBasedFileListBuilder([]);
85134
}
86135

87136
if (engineConfig.extensions) {
@@ -90,8 +139,10 @@ runWithTiming("engineConfig", function () {
90139
}
91140
});
92141

93-
var analysisFiles = runWithTiming("fileWalk", function() { return fileWalk(ignores, options.extensions); }),
94-
cli = runWithTiming("cliInit", function() { return new CLIEngine(options); }),
142+
var analysisFiles = runWithTiming("buildFileList", function() {
143+
return buildFileList(options.extensions);
144+
});
145+
var cli = runWithTiming("cliInit", function() { return new CLIEngine(options); }),
95146
report = runWithTiming("cliRun", function() { return cli.executeOnFiles(analysisFiles); });
96147

97148
runWithTiming("resultsOutput",

0 commit comments

Comments
 (0)