2 * @fileoverview Rule to control usage of strict mode directives.
3 * @author Brandon Mills
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
19 * Gets all of the Use Strict Directives in the Directive Prologue of a group of
21 * @param {ASTNode[]} statements Statements in the program or function body.
22 * @returns {ASTNode[]} All of the Use Strict Directives.
24 function getUseStrictDirectives(statements
) {
25 const directives
= [];
27 for (let i
= 0; i
< statements
.length
; i
++) {
28 const statement
= statements
[i
];
31 statement
.type
=== "ExpressionStatement" &&
32 statement
.expression
.type
=== "Literal" &&
33 statement
.expression
.value
=== "use strict"
35 directives
[i
] = statement
;
45 * Checks whether a given parameter is a simple parameter.
46 * @param {ASTNode} node A pattern node to check.
47 * @returns {boolean} `true` if the node is an Identifier node.
49 function isSimpleParameter(node
) {
50 return node
.type
=== "Identifier";
54 * Checks whether a given parameter list is a simple parameter list.
55 * @param {ASTNode[]} params A parameter list to check.
56 * @returns {boolean} `true` if the every parameter is an Identifier node.
58 function isSimpleParameterList(params
) {
59 return params
.every(isSimpleParameter
);
62 //------------------------------------------------------------------------------
64 //------------------------------------------------------------------------------
71 description
: "require or disallow strict mode directives",
72 category
: "Strict Mode",
74 url
: "https://eslint.org/docs/rules/strict"
79 enum: ["never", "global", "function", "safe"]
85 function: "Use the function form of 'use strict'.",
86 global
: "Use the global form of 'use strict'.",
87 multiple
: "Multiple 'use strict' directives.",
88 never
: "Strict mode is not permitted.",
89 unnecessary
: "Unnecessary 'use strict' directive.",
90 module
: "'use strict' is unnecessary inside of modules.",
91 implied
: "'use strict' is unnecessary when implied strict mode is enabled.",
92 unnecessaryInClasses
: "'use strict' is unnecessary inside of classes.",
93 nonSimpleParameterList
: "'use strict' directive inside a function with non-simple parameter list throws a syntax error since ES2016.",
94 wrap
: "Wrap {{name}} in a function with 'use strict' directive."
100 const ecmaFeatures
= context
.parserOptions
.ecmaFeatures
|| {},
103 let mode
= context
.options
[0] || "safe";
105 if (ecmaFeatures
.impliedStrict
) {
107 } else if (mode
=== "safe") {
108 mode
= ecmaFeatures
.globalReturn
? "global" : "function";
112 * Determines whether a reported error should be fixed, depending on the error type.
113 * @param {string} errorType The type of error
114 * @returns {boolean} `true` if the reported error should be fixed
116 function shouldFix(errorType
) {
117 return errorType
=== "multiple" || errorType
=== "unnecessary" || errorType
=== "module" || errorType
=== "implied" || errorType
=== "unnecessaryInClasses";
121 * Gets a fixer function to remove a given 'use strict' directive.
122 * @param {ASTNode} node The directive that should be removed
123 * @returns {Function} A fixer function
125 function getFixFunction(node
) {
126 return fixer
=> fixer
.remove(node
);
130 * Report a slice of an array of nodes with a given message.
131 * @param {ASTNode[]} nodes Nodes.
132 * @param {string} start Index to start from.
133 * @param {string} end Index to end before.
134 * @param {string} messageId Message to display.
135 * @param {boolean} fix `true` if the directive should be fixed (i.e. removed)
138 function reportSlice(nodes
, start
, end
, messageId
, fix
) {
139 nodes
.slice(start
, end
).forEach(node
=> {
140 context
.report({ node
, messageId
, fix
: fix
? getFixFunction(node
) : null });
145 * Report all nodes in an array with a given message.
146 * @param {ASTNode[]} nodes Nodes.
147 * @param {string} messageId Message id to display.
148 * @param {boolean} fix `true` if the directive should be fixed (i.e. removed)
151 function reportAll(nodes
, messageId
, fix
) {
152 reportSlice(nodes
, 0, nodes
.length
, messageId
, fix
);
156 * Report all nodes in an array, except the first, with a given message.
157 * @param {ASTNode[]} nodes Nodes.
158 * @param {string} messageId Message id to display.
159 * @param {boolean} fix `true` if the directive should be fixed (i.e. removed)
162 function reportAllExceptFirst(nodes
, messageId
, fix
) {
163 reportSlice(nodes
, 1, nodes
.length
, messageId
, fix
);
167 * Entering a function in 'function' mode pushes a new nested scope onto the
168 * stack. The new scope is true if the nested function is strict mode code.
169 * @param {ASTNode} node The function declaration or expression.
170 * @param {ASTNode[]} useStrictDirectives The Use Strict Directives of the node.
173 function enterFunctionInFunctionMode(node
, useStrictDirectives
) {
174 const isInClass
= classScopes
.length
> 0,
175 isParentGlobal
= scopes
.length
=== 0 && classScopes
.length
=== 0,
176 isParentStrict
= scopes
.length
> 0 && scopes
[scopes
.length
- 1],
177 isStrict
= useStrictDirectives
.length
> 0;
180 if (!isSimpleParameterList(node
.params
)) {
181 context
.report({ node
: useStrictDirectives
[0], messageId
: "nonSimpleParameterList" });
182 } else if (isParentStrict
) {
183 context
.report({ node
: useStrictDirectives
[0], messageId
: "unnecessary", fix
: getFixFunction(useStrictDirectives
[0]) });
184 } else if (isInClass
) {
185 context
.report({ node
: useStrictDirectives
[0], messageId
: "unnecessaryInClasses", fix
: getFixFunction(useStrictDirectives
[0]) });
188 reportAllExceptFirst(useStrictDirectives
, "multiple", true);
189 } else if (isParentGlobal
) {
190 if (isSimpleParameterList(node
.params
)) {
191 context
.report({ node
, messageId
: "function" });
196 data
: { name
: astUtils
.getFunctionNameWithKind(node
) }
201 scopes
.push(isParentStrict
|| isStrict
);
205 * Exiting a function in 'function' mode pops its scope off the stack.
208 function exitFunctionInFunctionMode() {
213 * Enter a function and either:
214 * - Push a new nested scope onto the stack (in 'function' mode).
215 * - Report all the Use Strict Directives (in the other modes).
216 * @param {ASTNode} node The function declaration or expression.
219 function enterFunction(node
) {
220 const isBlock
= node
.body
.type
=== "BlockStatement",
221 useStrictDirectives
= isBlock
222 ? getUseStrictDirectives(node
.body
.body
) : [];
224 if (mode
=== "function") {
225 enterFunctionInFunctionMode(node
, useStrictDirectives
);
226 } else if (useStrictDirectives
.length
> 0) {
227 if (isSimpleParameterList(node
.params
)) {
228 reportAll(useStrictDirectives
, mode
, shouldFix(mode
));
230 context
.report({ node
: useStrictDirectives
[0], messageId
: "nonSimpleParameterList" });
231 reportAllExceptFirst(useStrictDirectives
, "multiple", true);
238 const useStrictDirectives
= getUseStrictDirectives(node
.body
);
240 if (node
.sourceType
=== "module") {
244 if (mode
=== "global") {
245 if (node
.body
.length
> 0 && useStrictDirectives
.length
=== 0) {
246 context
.report({ node
, messageId
: "global" });
248 reportAllExceptFirst(useStrictDirectives
, "multiple", true);
250 reportAll(useStrictDirectives
, mode
, shouldFix(mode
));
253 FunctionDeclaration
: enterFunction
,
254 FunctionExpression
: enterFunction
,
255 ArrowFunctionExpression
: enterFunction
258 if (mode
=== "function") {
259 Object
.assign(rule
, {
261 // Inside of class bodies are always strict mode.
263 classScopes
.push(true);
269 "FunctionDeclaration:exit": exitFunctionInFunctionMode
,
270 "FunctionExpression:exit": exitFunctionInFunctionMode
,
271 "ArrowFunctionExpression:exit": exitFunctionInFunctionMode