Skip to content

Commit

Permalink
Update: Module overrides all 'strict' rule options (fixes eslint#4936)
Browse files Browse the repository at this point in the history
  • Loading branch information
nre committed Jan 15, 2016
1 parent 15a5b28 commit 093326d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
4 changes: 2 additions & 2 deletions docs/rules/README.md
Expand Up @@ -105,9 +105,9 @@ These are rules designed to prevent you from making mistakes. They either prescr

## Strict Mode

These rules relate to using strict mode.
These rules relate to using strict mode and strict-mode directives.

* [strict](strict.md) - controls location of Use Strict Directives
* [strict](strict.md) - require effective use of strict-mode directives

## Variables

Expand Down
28 changes: 16 additions & 12 deletions docs/rules/strict.md
@@ -1,12 +1,12 @@
# Strict Mode (strict)
# Strict-Mode Directives (strict)

A Use Strict Directive at the beginning of a script or function body enables strict mode semantics:
A strict-mode directive at the beginning of a script or function body enables strict-mode semantics:

```js
"use strict";
```

When used globally, as in the preceding example, the entire script, including all contained functions, are strict mode code. It is also possible to specify function-level strict mode, such that strict mode applies only to the function in which the directive occurs:
When used globally, as in the preceding example, the entire script, including all contained functions, are strict-mode code. It is also possible to specify function-level strict mode, such that strict mode applies only to the function in which the directive occurs:

```js
function foo() {
Expand All @@ -20,9 +20,11 @@ var bar = function() {
};
```

Unlike scripts, ECMAScript modules are always in strict mode. Strict-mode directives in ECMAScript modules have no effect.

## Rule Details

This rule is aimed at using strict directives effectively, and as such, will flag any unexpected uses or omissions of strict directives.
This rule is aimed at using strict-mode directives effectively, and as such, will flag any unexpected uses or omissions of strict-mode directives.

### Options

Expand All @@ -33,9 +35,11 @@ There are four options for this rule:
1. `"function"` - require `"use strict"` in function scopes only
1. `"safe"` - require `"use strict"` globally when inside a module wrapper and in function scopes everywhere else.

All strict-mode directives are flagged as unnecessary if ECMAScript modules are enabled (see [Configuring ESLint](configuring)). This behaviour does not depend on the rule options, but can be silenced by disabling this rule.

### "never"

This mode forbids any occurrence of a Use Strict Directive.
This mode forbids any occurrence of a strict-mode directive.

The following patterns are considered problems:

Expand Down Expand Up @@ -77,7 +81,7 @@ bar();

### "global"

This mode ensures that all code is in strict mode and that there are no extraneous Use Strict Directives at the top level or in nested functions, which are themselves already strict by virtue of being contained in strict global code. It requires that global code contains exactly one Use Strict Directive. Use Strict Directives inside functions are considered unnecessary. Multiple Use Strict Directives at any level also trigger warnings.
This mode ensures that all code is in strict mode and that there are no extraneous strict-mode directives at the top level or in nested functions, which are themselves already strict by virtue of being contained in strict global code. It requires that global code contains exactly one strict-mode directive. Strict-mode directives inside functions are considered unnecessary. Multiple strict-mode directives at any level also trigger warnings.

The following patterns are considered problems:

Expand Down Expand Up @@ -119,7 +123,7 @@ foo();

### "function" (default)

This mode ensures that all function bodies are strict mode code, while global code is not. Particularly if a build step concatenates multiple scripts, a Use Strict Directive in global code of one script could unintentionally enable strict mode in another script that was not intended to be strict code. It forbids any occurrence of a Use Strict Directive in global code. It requires exactly one Use Strict Directive in each function declaration or expression whose parent is global code. Use Strict Directives inside nested functions are considered unnecessary. Multiple Use Strict Directives at any level also trigger warnings.
This mode ensures that all function bodies are strict-mode code, while global code is not. Particularly if a build step concatenates multiple scripts, a strict-mode directive in global code of one script could unintentionally enable strict mode in another script that was not intended to be strict code. It forbids any occurrence of a strict-mode directive in global code. It requires exactly one strict-mode directive in each function declaration or expression whose parent is global code. Strict-mode directives inside nested functions are considered unnecessary. Multiple strict-mode directives at any level also trigger warnings.

The following patterns are considered problems:

Expand All @@ -129,10 +133,10 @@ The following patterns are considered problems:
"use strict"; /*error Use the function form of 'use strict'.*/
function foo() { /*error Use the function form of 'use strict'.*/
// Missing Use Strict Directive
// Missing strict-mode directive
return function() {
"use strict"; // Unnecessary; parent should contain a Strict Mode Directive
"use strict"; // Unnecessary; parent should contain a strict-mode directive
"use strict"; /*error Multiple 'use strict' directives.*/
return;
Expand Down Expand Up @@ -166,13 +170,13 @@ foo();

### "safe"

Node.js and the CommonJS module system wrap modules inside a hidden function wrapper that defines each module's scope. The wrapper makes it safe to concatenate strict-mode modules while maintaining their original `"use strict"` directives. When the `node` or `commonjs` environments are enabled or `globalReturn` is enabled in `ecmaFeatures`, ESLint considers code to be inside the module wrapper, and `"safe"` mode corresponds to `"global"` mode and enforces global `"use strict"` directives. Everywhere else, `"safe"` mode corresponds to `"function"` mode and enforces `"use strict"` directives inside top-level functions.
Node.js and the CommonJS module system wrap modules inside a hidden function wrapper that defines each module's scope. The wrapper makes it safe to concatenate strict-mode modules while maintaining their original strict-mode directives. When the `node` or `commonjs` environments are enabled or `globalReturn` is enabled in `ecmaFeatures`, ESLint considers code to be inside the module wrapper, and `"safe"` mode corresponds to `"global"` mode and enforces global strict-mode directives. Everywhere else, `"safe"` mode corresponds to `"function"` mode and enforces strict-mode directives inside top-level functions.

### "deprecated" (Removed)

**Replacement notice**: This mode, previously enabled by turning on the rule without specifying a mode, has been removed in ESLint v1.0. `"function"` mode is most similar to the deprecated behavior, and has been made the default if no mode is specified.

This mode ensures that all functions are executed in strict mode. A Use Strict Directive must be present in global code or in every top-level function declaration or expression. It does not concern itself with unnecessary Use Strict Directives in nested functions that are already strict, nor with multiple Use Strict Directives at the same level.
This mode ensures that all functions are executed in strict mode. A strict-mode directive must be present in global code or in every top-level function declaration or expression. It does not concern itself with unnecessary strict-mode directives in nested functions that are already strict, nor with multiple strict-mode directives at the same level.

The following patterns are considered problems:

Expand Down Expand Up @@ -215,4 +219,4 @@ function foo() {

## When Not To Use It

In a codebase that has both strict and non-strict code, either turn this rule off, or [selectively disable it](http://eslint.org/docs/user-guide/configuring) where necessary. For example, functions referencing `arguments.callee` are invalid in strict mode. A [full list of strict mode differences](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode/Transitioning_to_strict_mode#Differences_from_non-strict_to_strict) is available on MDN.
In a codebase that has both strict and non-strict code, either turn this rule off, or [selectively disable it](http://eslint.org/docs/user-guide/configuring) where necessary. For example, functions referencing `arguments.callee` are invalid in strict mode. A [full list of strict-mode differences](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode/Transitioning_to_strict_mode#Differences_from_non-strict_to_strict) is available on MDN.
24 changes: 12 additions & 12 deletions lib/rules/strict.js
Expand Up @@ -82,46 +82,46 @@ module.exports = function(context) {
}

//--------------------------------------------------------------------------
// "never" mode
// If this is modules, all "use strict" directives are unnecessary.
//--------------------------------------------------------------------------

if (mode === "never") {
if (context.parserOptions.sourceType === "module") {
return {
"Program": function(node) {
report(getUseStrictDirectives(node.body), messages.never);
report(getUseStrictDirectives(node.body), messages.unnecessaryInModules);
},
"FunctionDeclaration": function(node) {
report(getUseStrictDirectives(node.body.body), messages.never);
report(getUseStrictDirectives(node.body.body), messages.unnecessaryInModules);
},
"FunctionExpression": function(node) {
report(getUseStrictDirectives(node.body.body), messages.never);
report(getUseStrictDirectives(node.body.body), messages.unnecessaryInModules);
},
"ArrowFunctionExpression": function(node) {
if (node.body.type === "BlockStatement") {
report(getUseStrictDirectives(node.body.body), messages.never);
report(getUseStrictDirectives(node.body.body), messages.unnecessaryInModules);
}
}
};
}

//--------------------------------------------------------------------------
// If this is modules, all "use strict" directives are unnecessary.
// "never" mode
//--------------------------------------------------------------------------

if (context.parserOptions.sourceType === "module") {
if (mode === "never") {
return {
"Program": function(node) {
report(getUseStrictDirectives(node.body), messages.unnecessaryInModules);
report(getUseStrictDirectives(node.body), messages.never);
},
"FunctionDeclaration": function(node) {
report(getUseStrictDirectives(node.body.body), messages.unnecessaryInModules);
report(getUseStrictDirectives(node.body.body), messages.never);
},
"FunctionExpression": function(node) {
report(getUseStrictDirectives(node.body.body), messages.unnecessaryInModules);
report(getUseStrictDirectives(node.body.body), messages.never);
},
"ArrowFunctionExpression": function(node) {
if (node.body.type === "BlockStatement") {
report(getUseStrictDirectives(node.body.body), messages.unnecessaryInModules);
report(getUseStrictDirectives(node.body.body), messages.never);
}
}
};
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/strict.js
Expand Up @@ -107,6 +107,13 @@ ruleTester.run("strict", rule, {
{ message: "Strict mode is not permitted.", type: "ExpressionStatement" },
{ message: "Strict mode is not permitted.", type: "ExpressionStatement" }
]
}, {
code: "\"use strict\"; foo();",
options: ["never"],
parserOptions: { sourceType: "module" },
errors: [
{ message: "'use strict' is unnecessary inside of modules.", type: "ExpressionStatement" }
]
},

// "global" mode
Expand Down

0 comments on commit 093326d

Please sign in to comment.