grunt-eslint icon indicating copy to clipboard operation
grunt-eslint copied to clipboard

Linting ignored files triggers a warning

Open markelog opened this issue 9 years ago • 8 comments
trafficstars

Duplicate of https://github.com/sindresorhus/grunt-eslint/issues/22, but despite that issue being "fixed" it is still reproducible.

Comment https://github.com/sindresorhus/grunt-eslint/issues/22#issuecomment-209679165 wasn't answered, so re-creating it here, with hopes to clarify plugin current state.

Thank you.

markelog avatar May 10 '16 09:05 markelog

I have the same problem.

I tried applying the fix from here: https://github.com/eslint/eslint/issues/5623#issuecomment-209789397

But if I specify path/ I get everything ignored (no idea whats going on, it still takes 27 seconds and then reports nothing?), if I specify path/**/*.js I get the warnings I don't want. The work-around I am using is to disable warnings.

lukeapage avatar May 11 '16 12:05 lukeapage

Yeah, this is actually a bit non-intuitive and might be worthy of documentation. This isn't a bug in grunt-eslint, but a feature of eslint. Their policy is ignoring any explicitly included file is a warning.

The solution is to not include them in the first place:

"target": {
     "src": [
           "files/*/js/**/*.js",
           "!files/*/js/*.min.js",
           "!files/js/**/*.js",
           "!files/*/js/*.js",
           "!files/shared/js/vendor/**/*.js",
           "!files/shared/js/found*/*.js",
           "!files/bolt/**/*.js",
           "!files/clean/js/codemirror/**/*.js",
           "!files/clean/js/vendor/**/*.js",
           "!files/clean/js/image-viewer_depricated/**/*.js",
           "!files/clean/js/slick/**/*.js",
           "!files/my-lists/**/*.js",
           "!files/jv1/**/*.js",
           "!files/testTarget/**/*.js",
           "!files/jstor/**/*.js",
           "!files/ui-guide/js/views/run_prettify.js"
     ]
  }

The better solution is to not need to exclude them because vendor files and dependencies aren't organized in the same tree. But this is the real world so use the above. 😉

StevenACoffman avatar Nov 18 '16 15:11 StevenACoffman

@markelog Can this be closed? The eslint behaviour is (currently) "by design".

Whereas grunt-eslint is a grunt plugin. If you need to ignore files, then use grunt's globbing patterns, as @StevenACoffman has suggested.

superclarkk avatar Dec 13 '16 01:12 superclarkk

@markelog https://github.com/markelog Can this be closed? The eslint behaviour is (currently) "by design".

Whereas grunt-eslint is a grunt plugin. If you need to ignore files, then use grunt's globbing patterns http://gruntjs.com/configuring-tasks#globbing-patterns, as @StevenACoffman https://github.com/StevenACoffman has suggested.

Such a solution requires duplicating the list of ignored files. I don't think that's a good solution.

A proper solution would be to utilize the CLIEngine to silence this kind of warning. That's why it was created in the first place.

mgol avatar Dec 13 '16 08:12 mgol

I second utilizing the CLIEngine if possible. Adding a list of source patterns not to lint appears to greatly reduce performance.

jscharett avatar Mar 15 '17 16:03 jscharett

Hacky and dirty but satisfying workaround :)

// fetch all files we want to ignore
  const eslintignore = grunt.file.read(".eslintignore").split("\n").map(
    e => e.trim()
  ).filter(
    e => e !== "" && !e.startsWith("#")
  ).map(e => `!${e}`);

Then below use:

      target: [
        '**'
      ].concat(eslintignore)

lukas-gitl avatar Sep 22 '17 18:09 lukas-gitl

Improved to allow for in-line comments as:

target: [
  '**'
].concat(grunt.file.read(".eslintignore").split("\n")
  .map(e => e.split("#", 1)[0].trim()).filter(e => e !== "")
  .map(e => `!${e}`))

simlu avatar Nov 12 '17 06:11 simlu

I figured out how to use @simlu's workaround for a package/repo which has multiple eslint configurations.

Say a repo has this file structure:

  • root
    • .eslintrc.js
    • .eslintignore
    • public
      • .eslintrc.js
      • .eslintignore

This is an example of how to configure grunt-eslint to use both sets of eslint files:

Code
var Path = require('path');

function trimComments(lines) {
	return lines.map(function(line) {
		var parts = line.split('#', 1);
		var noncomment = parts[0];
		// var comment = parts[1];
		return noncomment;
	});
}

function trimWhitespace(lines) {
	return lines.map(function(line) {
		return line.trim();
	});
}

function rejectBlank(lines) {
	return lines.filter(function(line) {
		var isBlank = (line === '');
		return !isBlank;
	});
}

function prependDir(lines, dpath) {
	return lines.map(function(line) {
		var isNegater = line.startsWith('!');
		return (isNegater)
			? prependToNegater(line, dpath)
			: prependToMatcher(line, dpath);
	});
}

function prependToNegater(line, dpath) {
	var fpath = line.slice(1);
	var joined = Path.join(dpath, fpath);

	return `!${joined}`;
}

function prependToMatcher(line, dpath) {
	var fpath = line;
	var joined = Path.join(dpath, fpath);

	return joined;
}

function invertGlobs(lines) {
	return lines.map(function(line) {
		var isNegater = line.startsWith('!');
		return (isNegater) ? line.slice(1) : `!${line}`;
	});
}

module.exports = function(grunt) {

	function ignoreFile(fpath) {

		var dpath = Path.dirname(fpath);
		var content = grunt.file.read(fpath);

		var lines = content.split('\n');
		lines = trimComments(lines);
		lines = trimWhitespace(lines);
		lines = rejectBlank(lines);
		lines = prependDir(lines, dpath);
		lines = invertGlobs(lines);

		return lines;
	}

	grunt.config('eslint', {
		backend: {
			options: {
				configFile: '.eslintrc.js',
				ignore: false // suppress warnings when files are being ignored
			},
			src: [
				'**/*.js',
				...ignoreFile('.eslintignore') // but still ignore files
			]
		},
		frontend: {
			options: {
				configFile: 'public/.eslintrc.js',
				ignore: false // suppress warnings when files are being ignored
			},
			src: [
				'public/**/*.js',
				...ignoreFile('public/.eslintignore') // but still ignore files
			]
		}
	});

	grunt.loadNpmTasks('grunt-eslint');
};

GreenRaccoon23 avatar May 22 '18 21:05 GreenRaccoon23