Javascript: Can't overwrite "strict" rule

Created on 12 Feb 2016  路  7Comments  路  Source: airbnb/javascript

I have the following files:

// foo.js
function foo() {
  return 42;
}

foo();
---
extends: airbnb/base
rules:
  strict: [2, "global"]

Running eslint foo.js returns successfully, even though foo.js has no "use strict"; directive. If I change to extends: eslint:recommended, it complains because of the lack of "use strict"; (as expected).

I tried to overwrite another rule, so I changed the files to:

// foo.js
function foo() {
  return 42;
}

// Remove the call to foo();
// foo();
---
extends: airbnb/base
rules:
  no-unused-vars: 0

And it worked. I was able to overwrite no-unused-vars.

I'm running [email protected] and [email protected] with Node v5.5.0 and npm v3.6.0.

Most helpful comment

Sorry for this way to late reply :(

The problem I had was to force the use of "use strict"; using the "strict": [2, "global"] rule. This did not have any effect and ESLint said that "use strict"; was not necessary when using ES6 modules.

However this was in a Node.js project and my models are not ES6 modules and I wanted them all to have strict mode enabled and I was able to force it by setting "sourceType": "script" in parserOptions like this:

{                                                                    
    "extends": "airbnb-base",                                        
    "env": {                                                         
      "node": true                                                   
    },                                                               
    "parserOptions": {                                               
      "ecmaVersion": 6,                                              
      "sourceType": "script",                                        
      "ecmaFeatures": {                                              
        "modules": false                                             
      }                                                              
    },                                                               
    "rules": {                                                       
      "strict": [2, "global"]
    }                                                                
}                                                                    

All 7 comments

If you're extending airbnb/base, that doesn't include any strict definition at all. Only the main airbnb includes the strict rule.

I'm not really sure why you wouldn't be able to enable that warning - if you try [2, "function"] does it complain?

That's odd. I tried changing to [2, "function"] as well, but it kept succeeding. It only complained when I tried some invalid value (i.e. [2, "foo"]).

You can now use the https://npmjs.com/eslint-config-airbnb-base package directly. Please reopen this if you're still having trouble.

I can not get this to work correctly even after switching to eslint-config-airbnb-base.

@ljharb airbnb-base includes this strict definition, is it interfering?

@Starefossen the separate base package includes that file but it's not included in the main export - however, it's turned on here. What problem are you having?

Sorry for this way to late reply :(

The problem I had was to force the use of "use strict"; using the "strict": [2, "global"] rule. This did not have any effect and ESLint said that "use strict"; was not necessary when using ES6 modules.

However this was in a Node.js project and my models are not ES6 modules and I wanted them all to have strict mode enabled and I was able to force it by setting "sourceType": "script" in parserOptions like this:

{                                                                    
    "extends": "airbnb-base",                                        
    "env": {                                                         
      "node": true                                                   
    },                                                               
    "parserOptions": {                                               
      "ecmaVersion": 6,                                              
      "sourceType": "script",                                        
      "ecmaFeatures": {                                              
        "modules": false                                             
      }                                                              
    },                                                               
    "rules": {                                                       
      "strict": [2, "global"]
    }                                                                
}                                                                    

@Starefossen you're solution works. I had the exact same use case for linting an AWS Lambda project. Thanks!

Was this page helpful?
0 / 5 - 0 ratings