Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: Allow linting of .hidden files/folders (fixes #4828) (#6844)
  • Loading branch information
IanVS authored and ilyavolodin committed Sep 8, 2016
1 parent 6d97c18 commit d9513b7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
30 changes: 19 additions & 11 deletions lib/util/glob-util.js
Expand Up @@ -104,28 +104,22 @@ function resolveFileGlobPatterns(patterns, options) {
* @returns {string[]} Resolved absolute filenames.
*/
function listFilesToProcess(globPatterns, options) {
options = options || { ignore: true };
const files = [],
added = {};

const cwd = (options && options.cwd) || process.cwd();

options = options || { ignore: true, dotfiles: true };
const ignoredPaths = new IgnoredPaths(options);
const globOptions = {
nodir: true,
cwd
};
const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker();

/**
* Executes the linter on a file defined by the `filename`. Skips
* unsupported file extensions and any files that are already linted.
* @param {string} filename The file to be processed
* @param {boolean} shouldWarnIgnored Whether or not a report should be made if
* the file is ignored
* @param {IgnoredPaths} ignoredPaths An instance of IgnoredPaths
* @returns {void}
*/
function addFile(filename, shouldWarnIgnored) {
function addFile(filename, shouldWarnIgnored, ignoredPaths) {
let ignored = false;
let isSilentlyIgnored;

Expand Down Expand Up @@ -160,10 +154,24 @@ function listFilesToProcess(globPatterns, options) {
const file = path.resolve(cwd, pattern);

if (shell.test("-f", file)) {
addFile(fs.realpathSync(file), !shell.test("-d", file));
const ignoredPaths = new IgnoredPaths(options);

addFile(fs.realpathSync(file), !shell.test("-d", file), ignoredPaths);
} else {

// regex to find .hidden or /.hidden patterns, but not ./relative or ../relative
const globIncludesDotfiles = /(?:(?:^\.)|(?:[\/\\]\.))[^\/\\\.].*/.test(pattern);

const ignoredPaths = new IgnoredPaths(Object.assign({}, options, {dotfiles: options.dotfiles || globIncludesDotfiles}));
const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker();
const globOptions = {
nodir: true,
dot: true,
cwd,
};

new GlobSync(pattern, globOptions, shouldIgnore).found.forEach(function(globMatch) {
addFile(path.resolve(cwd, globMatch), false);
addFile(path.resolve(cwd, globMatch), false, ignoredPaths);
});
}
});
Expand Down
@@ -0,0 +1,2 @@
var bar = "<div>Hello world!</div>";
bar(bar);
36 changes: 36 additions & 0 deletions tests/lib/cli-engine.js
Expand Up @@ -523,6 +523,22 @@ describe("CLIEngine", function() {
assert.equal(report.results[0].messages[0].message, expectedMsg);
});

it("should report on globs with explicit inclusion of dotfiles, even though ignored by default", function() {

engine = new CLIEngine({
cwd: getFixturePath("cli-engine"),
rules: {
quotes: [2, "single"]
}
});

const report = engine.executeOnFiles(["hidden/.hiddenfolder/*.js"]);

assert.equal(report.results.length, 1);
assert.equal(report.results[0].errorCount, 1);
assert.equal(report.results[0].warningCount, 0);
});

it("should not check default ignored files without --no-ignore flag", function() {

engine = new CLIEngine({
Expand Down Expand Up @@ -585,6 +601,26 @@ describe("CLIEngine", function() {
assert.equal(report.results[0].messages[0].ruleId, "quotes");
});

it("should check .hidden files if they are unignored with an --ignore-pattern", function() {

engine = new CLIEngine({
cwd: getFixturePath("cli-engine"),
ignore: true,
useEslintrc: false,
ignorePattern: "!.hidden*",
rules: {
quotes: [2, "single"]
}
});

const report = engine.executeOnFiles(["hidden/"]);

assert.equal(report.results.length, 1);
assert.equal(report.results[0].warningCount, 0);
assert.equal(report.results[0].errorCount, 1);
assert.equal(report.results[0].messages[0].ruleId, "quotes");
});

it("should report zero messages when given a pattern with a .js and a .js2 file", function() {

engine = new CLIEngine({
Expand Down
7 changes: 4 additions & 3 deletions tests/lib/util/glob-util.js
Expand Up @@ -199,16 +199,17 @@ describe("globUtil", function() {

it("should not return hidden files for standard glob patterns", function() {
const patterns = [getFixturePath("glob-util", "hidden", "**/*.js")];
const result = globUtil.listFilesToProcess(patterns);
const result = globUtil.listFilesToProcess(patterns, {
cwd: getFixturePath()
});

assert.equal(result.length, 0);
});

it("should return hidden files if included in glob pattern", function() {
const patterns = [getFixturePath("glob-util", "hidden", "**/.*.js")];
const result = globUtil.listFilesToProcess(patterns, {
cwd: getFixturePath(),
dotfiles: true
cwd: getFixturePath()
});

const file1 = getFixturePath("glob-util", "hidden", ".foo.js");
Expand Down

0 comments on commit d9513b7

Please sign in to comment.