question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Glob based configuration

See original GitHub issue

Glob based configuration

Goal

Currently ESLint allows you to cascade the configurations based on the directory structure of your project. This approach is not flexible enough if you have files that don’t share the same parent directory but you still want to have a specific ESLint configuration just for those files. This could be solved by specifying a configuration that applies to all files which match a given glob pattern.

How it works

  • The glob patterns can be configured within .eslintrc files
  • glob patterns are relative to the location of the .eslintrc in which they are defined
  • if an already resolved file matches a glob pattern in one of its corresponding .eslintrc files, the glob pattern specific configuration will be applied
  • a glob pattern based configuration has a higher precedence than the regular configuration in the same .eslintrc file
  • A glob specific configuration works exactly the same as the regular configuration in your .eslintrc, except that you can’t define nested glob based configurations. That means you can configure globals, env, ecmaFeatures, rules, plugins, extends and parser.

Note: The glob patterns are NOT used for file resolving / directory traversal.

Example for relative glob patterns

Given the following directory tree:

project-root
β”œβ”€β”€ app
β”‚   β”œβ”€β”€ lib
β”‚   β”‚   β”œβ”€β”€ foo.js
β”‚   β”‚   β”œβ”€β”€ fooSpec.js
β”‚   β”œβ”€β”€ components
β”‚   β”‚   β”œβ”€β”€ bar.js
β”‚   β”‚   β”œβ”€β”€ barSpec.js
β”‚   β”œβ”€β”€ .eslintrc
β”œβ”€β”€ server
β”‚   β”œβ”€β”€ server.js
β”‚   β”œβ”€β”€ serverSpec.js
β”œβ”€β”€ .eslintrc

The config in app/.eslintrc defines the glob pattern **/*Spec.js. This pattern is relative to the base directory of app/.eslintrc. So, this pattern would match app/lib/fooSpec.js and app/components/barSpec.js but NOT server/serverSpec.js. If you would define the same pattern in the .eslintrc file within in the project-root folder, it would match all three of the *Spec files.

Configuration Examples

Single pattern

{
  "files": [
    {
      "patterns": "**/*Spec.js",
      "rules": { "no-unused-expression": 0 },
      "env": { "mocha": true } 
    }
  ] 
} 

Multiple patterns

{
  "files": [
    {
      "patterns": [ "app/components/**/*.js", "test/unit/app/components/**/*.js" ],
      "rules": { "react/display-name": 2 },
      "ecmaFeatures": { "jsx": true },
      "plugins": [ "react" ] 
    }
  ] 
} 

As you can see patterns could be a string for a single glob pattern or an array if you like to define multiple patterns.

Open Questions / Possible Problems

  • Should we allow patterns in shareable configs? To which base path should the relative glob pattern be resolved? I think we should not allow this. Extending from a shareable config should mean that you unconditionally get the rules from this shareable configuration for all the files that you configure it in your project
  • Should this feature also work somehow for the CLIEngine?

PS: I’m not happy with the current naming of "files" and "patterns". Suggestions welcome

<bountysource-plugin>

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:204
  • Comments:87 (38 by maintainers)

github_iconTop GitHub Comments

22reactions
smablycommented, May 9, 2017

Can’t speak to timeframe, but work is progressing in #8081.

22reactions
w0rpcommented, Oct 13, 2016

I’m looking forward to this feature, as it will allow me to specify that every file ending in .spec.js, containing tests, should permit a different set of globals for writing Jasmine tests. This is a pretty common pattern for AngularJS code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Beginner's Guide: Glob Patterns | Malik Browne
Globs, also known as glob patterns are patterns that can expand a wildcard pattern into a list of pathnames that match the given...
Read more >
glob (programming) - Wikipedia
In computer programming, glob patterns specify sets of filenames with wildcard characters. ... It can be enabled by setting the extglob shell option....
Read more >
Tips for writing glob patterns in DeepSource configuration
Glob patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command rm -rf textfiles/*.txt removes (rm)Β ...
Read more >
Glob Pattern Support | Logstash Reference [8.5] - Elastic
Setting Up and Running Logstash ... Logstash supports the following patterns wherever glob patterns are allowed: *: Match any file.
Read more >
Common glob pattern - TetraScience
The Glob patterns implemented in the File Log Agent is to provide methods to traverse the file system and returning files or the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found