ESLint ignore specific rule for a specific directory - eslint

Is it possible with ESLint to ignore one specific rule for an entire directory?
In my case, I would like to ignore import/prefer-default-export for a directory named commonComponents

ESLint configuration (.eslintrc) files are hierarchical:
ESLint will automatically look for them in the directory of the file to be linted, and in successive parent directories all the way up to the root directory of the filesystem. This option is useful when you want different configurations for different parts of a project or when you want others to be able to use ESLint directly without needing to remember to pass in the configuration file.
You can disable the import/prefer-default-export rule for the commonComponents directory by creating a .eslintrc file with the following content in that directory:
{
"rules": {
"import/prefer-default-export": "off"
}
}

You can also use the "overrides" key to declare rules for different glob patterns.
Have a read of Configuration Based on Glob Patterns
Sometimes a more fine-controlled configuration is necessary, for example if the configuration for files within the same directory has to be different. Therefore you can provide configurations under the overrides key that will only apply to files that match specific glob patterns, using the same format you would pass on the command line (e.g., app/**/*.test.js).
I use this to remove the no-unused-expressions rule from my test files like so;
"overrides": [{
"files": [ "*.spec.js" ],
"rules": {
"no-unused-expressions": 0
}
}]

If there are multiple directories that you want to apply your rules to, then you can create different configs for different purposes. For example:
.eslintrc.json for common config
.eslintrc-main.json for main linting and run eslint -c .eslintrc-main src test
.eslintrc-comp.json for components and run eslint -c .eslintrc-comp commonComponents fooBarComponent

YAML version :
rules:
no-unused-expressions: true
overrides:
- files: *-tests.js
rules:
no-unused-expressions: false

cretae .eslintignore file and put inside it your exluded folders. example :
node_modules/
functions/
dist/

Related

ESLint config cascading rule not working as expected

My project is structured like this:
.eslintrc.json
prettier.config.js
module-1/
.eslintrc.json
module-2/
.eslintrc.json
The root .eslintrc.json is almost the same as config files in module-1/ and module-2/. So I prefer using a single config file. All the 3 config files has a "root": true. So I believe (according to doc https://eslint.org/docs/latest/user-guide/configuring/configuration-files#cascading-and-hierarchy) the file at root directory has no effect at all and only the 2 moduled config files are used now.
But when I remove those 2 files in module directories and run ESLint from one of the module directory, I was expecting the root config to be used. And since all configs are of the same content, the linting result should be the same. However this was not true. It seems that some extra rules (of prettier/prettier) were applied after the change, and I see errors that wasn't present before.
2 more facts:
If I replace the config files in module directory with a symlink pointing to the root config (instead of removing them), everything is fine.
Removing the prettier.config.js makes no difference.
What could cause location of config file (root or in module directory) to affect rules applied?
My config file looks like this:
{
"root": true,
"extends": [/* Some plugins here, possibly has something to do with prettier */],
"rules": {
/* Some rule configs here, nothing to do with prettier */
}
}

how to ignore multiple directories in nodemon

I know how to ignore one directory in nodemon. but what is the way to ignore multiple directories at once in nodemon?
I tried this..but it didn't work for me..
"nodemonConfig": {
"ignore": ["directory_1","directory_2"] }
Thanks in Advance
In your package.json file,
To ignore the multiple directories you need to put them in the array as follows:
"nodemonConfig": {
"ignore": ["directory_1/*", "directory_2/*"]
}
You can check the details here in the link. Make sure the path should be correct of directories in the configuration.

Husky/lint-staged is it possible to exclude/ignore file?

Is it possible to exclude/ignore a file when using Husky/lint-staged hooks?
Looking through the docs atm but not having any luck finding anything on this.
Was hoping there was something like an
/*ignore*/
tag that I could add.
To make lint-staged ignore certain files that were causing formatting issues.
Any thought on this greatly appreciated :)
Ended up adding
.prettierignore
file.
Not ideal but seems to do the job ok.
I finally found out how to do this (at least as of lint-staged v11.1.2)
In package.json:
"lint-staged": {
"!(path/to/excluded/dir/**/*)*.ts": [
"eslint --fix",
"prettier --write"
]
}
Note the globstar pattern is within the negation pattern and not outside it. This ensures that subdirectories are also excluded.
While configuring lint-staged in package.json or If you're using any other IDE, in order to ignore/exclude files by lint-Staged and husky hooks, you can add an "ignore" key in the lint-staged object to make it ignore whatever packages or files you want to ignore. Use the following extensible syntax:
{
"lint-staged": {
"linters": {
"src/**/*.js": ["formatter --write", "git add"],
},
"ignore": ["node_modules", "dist", "package-lock.json"] }
}
Just add the target pattern to 'linters' object and all the ignored files which you might be adding previously to .prettierignore to "ignore" key of lint-Staged object. And there you go!
If anyone still looking, take a look at this https://github.com/okonet/lint-staged#filtering-files It has good examples.
Filtering files
Linter commands work on a subset of all staged files, defined by a glob pattern. `lint-stagedĀ“ uses micromatch for matching files with the following rules:
If the glob pattern contains no slashes (/), micromatch's matchBase option will enabled, so globs match a file's basename regardless of directory:
"*.js" will match all JS files, like /test.js and /foo/bar/test.js
"!(*test).js". will match all JS files, except those ending in test.js, so foo.js but not foo.test.js
If the glob pattern does contain a slash (/), it will match for paths as well:
"./*.js" will match all JS files in the git repo root, so /test.js but not /foo/bar/test.js
"foo/**/\*.js" will match all JS files inside the/foodirectory, so/foo/bar/test.jsbut not/test.js
So I've been trying to find an answer for this for an entire day and looking at all the forums suggested that they use minimatch for glob check which might have been correct for older versions but they use micromatch for new version and to solve this issue we can use their pattern to exclude certain directories
So in your .lintstagedrc you can add the following pattern to avoid certain folders
{
"*.{json,md,html,scss}": ["prettier --write", "git add"],
["**/!(folder1|folder2)/*.ts"]: ["tslint --project tsconfig.json -c tslint.commit.json --fix", "prettier --write", "git add"]
}
So the glob here is an actual array and make sure not to pass this array within a string else it won't recognize the patterns also do not include **/*.ts the reason being lint-staged automatically converts this into a matchBase comparision if it finds / in the pattern so including this will also match against your folder1|folder2 files.
can fix in three ways:
.lintstagedrc.js
.prettierignore
lint-staged.config.js
more info : https://github.com/okonet/lint-staged/issues/797

Is the word base a "magic" word in RequireJS?

Is the word base a "magic" word in RequireJS?
What does it refer to by default?
As you confirmed, Karma is likely your culprit, which puts all resources on /base before running.
First, confirm you have the npm package "karma-requirejs" installed.
Check your Karma config file and ensure you have the "frameworks" array include "requirejs". Here's what mine looks like:
frameworks: ['jasmine', 'requirejs'],
Also, in your files array, have the last file be your require config and all other files above specifying not to be included on page load (included: false). Again, here's a sample from one of my projects Karma config files:
files: [
//other files here, such as:
{ pattern: 'app.js', included: false },
// require config needs to be last; see http://karma-runner.github.io/0.12/plus/requirejs.html
'config/require.config.js'
],
Please note that my require config bootstraps the application as well by calling require(["app"]), which starts the require/fetch process.
Assuming:
"baseUrl" is pointing to your webroot (it can be relative),
"frameworks" includes "requirejs",
"files" array includes all your dependencies but doesn't included them up front (that's require's job) and the config is last to follow,
...you should have good karma. :)
Good luck!

Loading vendor javascript as modules

I'm working on an application built with Brunch. I would like to load some* of the vendor-supplied javascript as modules, so that I can require them in my code, rather than relying on global variables. Is there some way to do this, without copying all the vendor code into my app directory?
I tried creating a vendorlib directory, but brunch doesn't seem to look anywhere bu app and vendor. I also tried making a vendor/modules directory, but brunch seems to not wrap anything found under vendor (even when I convinced it to combine those files with the files other modules found under app.)
*The "some" that I'm working on right now are Chaplin, Backbone and Underscore. If I get those to work, I'll move more over later.
You can override config.modules.wrapper and make it wrap, for example, all files in vendor/modules directory. Or you can make add more directories that are handled by brunch to config.paths.watched.
For those following along at home, this is what my config.coffee eventually looked like:
paths:
watched: ['app','vendor','test','vendorlib']
files:
javascripts:
joinTo:
'javascripts/app.js': /^app/
'javascripts/vendor.js': /^vendor/
'test/javascripts/test.js': /^test[\\/](?!vendor)/
'test/javascripts/test-vendor.js': /^test[\\/](?=vendor)/
order:
# Files in `vendor` directories are compiled before other files
# even if they aren't specified in order.before.
before: [
'vendor/scripts/console-polyfill.js',
]
after: [
'test/vendor/scripts/test-helper.js'
]
stylesheets:
joinTo:
'stylesheets/app.css': /^(app|vendor)/
'test/stylesheets/test.css': /^test/
order:
after: ['vendor/styles/helpers.css']
templates:
joinTo: 'javascripts/app.js'
modules:
nameCleaner: (path) ->
path.replace(/^(app|vendorlib)\//, '')
This lets me populate a vendorlib directory with modules from vendors that support loading as modules. I currently have Chaplin, jQuery, and Backbone in there. I had to rename them not to include the version numbers.

Resources