1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const astUtils = require("./utils/ast-utils");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function getUseStrictDirectives(statements) {
|
25 | const directives = [];
|
26 |
|
27 | for (let i = 0; i < statements.length; i++) {
|
28 | const statement = statements[i];
|
29 |
|
30 | if (
|
31 | statement.type === "ExpressionStatement" &&
|
32 | statement.expression.type === "Literal" &&
|
33 | statement.expression.value === "use strict"
|
34 | ) {
|
35 | directives[i] = statement;
|
36 | } else {
|
37 | break;
|
38 | }
|
39 | }
|
40 |
|
41 | return directives;
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function isSimpleParameter(node) {
|
50 | return node.type === "Identifier";
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function isSimpleParameterList(params) {
|
59 | return params.every(isSimpleParameter);
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | module.exports = {
|
67 | meta: {
|
68 | type: "suggestion",
|
69 |
|
70 | docs: {
|
71 | description: "require or disallow strict mode directives",
|
72 | category: "Strict Mode",
|
73 | recommended: false,
|
74 | url: "https://eslint.org/docs/rules/strict"
|
75 | },
|
76 |
|
77 | schema: [
|
78 | {
|
79 | enum: ["never", "global", "function", "safe"]
|
80 | }
|
81 | ],
|
82 |
|
83 | fixable: "code",
|
84 | messages: {
|
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."
|
95 | }
|
96 | },
|
97 |
|
98 | create(context) {
|
99 |
|
100 | const ecmaFeatures = context.parserOptions.ecmaFeatures || {},
|
101 | scopes = [],
|
102 | classScopes = [];
|
103 | let mode = context.options[0] || "safe";
|
104 |
|
105 | if (ecmaFeatures.impliedStrict) {
|
106 | mode = "implied";
|
107 | } else if (mode === "safe") {
|
108 | mode = ecmaFeatures.globalReturn ? "global" : "function";
|
109 | }
|
110 |
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 | function shouldFix(errorType) {
|
117 | return errorType === "multiple" || errorType === "unnecessary" || errorType === "module" || errorType === "implied" || errorType === "unnecessaryInClasses";
|
118 | }
|
119 |
|
120 | |
121 |
|
122 |
|
123 |
|
124 |
|
125 | function getFixFunction(node) {
|
126 | return fixer => fixer.remove(node);
|
127 | }
|
128 |
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
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 });
|
141 | });
|
142 | }
|
143 |
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | function reportAll(nodes, messageId, fix) {
|
152 | reportSlice(nodes, 0, nodes.length, messageId, fix);
|
153 | }
|
154 |
|
155 | |
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | function reportAllExceptFirst(nodes, messageId, fix) {
|
163 | reportSlice(nodes, 1, nodes.length, messageId, fix);
|
164 | }
|
165 |
|
166 | |
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
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;
|
178 |
|
179 | if (isStrict) {
|
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]) });
|
186 | }
|
187 |
|
188 | reportAllExceptFirst(useStrictDirectives, "multiple", true);
|
189 | } else if (isParentGlobal) {
|
190 | if (isSimpleParameterList(node.params)) {
|
191 | context.report({ node, messageId: "function" });
|
192 | } else {
|
193 | context.report({
|
194 | node,
|
195 | messageId: "wrap",
|
196 | data: { name: astUtils.getFunctionNameWithKind(node) }
|
197 | });
|
198 | }
|
199 | }
|
200 |
|
201 | scopes.push(isParentStrict || isStrict);
|
202 | }
|
203 |
|
204 | |
205 |
|
206 |
|
207 |
|
208 | function exitFunctionInFunctionMode() {
|
209 | scopes.pop();
|
210 | }
|
211 |
|
212 | |
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | function enterFunction(node) {
|
220 | const isBlock = node.body.type === "BlockStatement",
|
221 | useStrictDirectives = isBlock
|
222 | ? getUseStrictDirectives(node.body.body) : [];
|
223 |
|
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));
|
229 | } else {
|
230 | context.report({ node: useStrictDirectives[0], messageId: "nonSimpleParameterList" });
|
231 | reportAllExceptFirst(useStrictDirectives, "multiple", true);
|
232 | }
|
233 | }
|
234 | }
|
235 |
|
236 | const rule = {
|
237 | Program(node) {
|
238 | const useStrictDirectives = getUseStrictDirectives(node.body);
|
239 |
|
240 | if (node.sourceType === "module") {
|
241 | mode = "module";
|
242 | }
|
243 |
|
244 | if (mode === "global") {
|
245 | if (node.body.length > 0 && useStrictDirectives.length === 0) {
|
246 | context.report({ node, messageId: "global" });
|
247 | }
|
248 | reportAllExceptFirst(useStrictDirectives, "multiple", true);
|
249 | } else {
|
250 | reportAll(useStrictDirectives, mode, shouldFix(mode));
|
251 | }
|
252 | },
|
253 | FunctionDeclaration: enterFunction,
|
254 | FunctionExpression: enterFunction,
|
255 | ArrowFunctionExpression: enterFunction
|
256 | };
|
257 |
|
258 | if (mode === "function") {
|
259 | Object.assign(rule, {
|
260 |
|
261 |
|
262 | ClassBody() {
|
263 | classScopes.push(true);
|
264 | },
|
265 | "ClassBody:exit"() {
|
266 | classScopes.pop();
|
267 | },
|
268 |
|
269 | "FunctionDeclaration:exit": exitFunctionInFunctionMode,
|
270 | "FunctionExpression:exit": exitFunctionInFunctionMode,
|
271 | "ArrowFunctionExpression:exit": exitFunctionInFunctionMode
|
272 | });
|
273 | }
|
274 |
|
275 | return rule;
|
276 | }
|
277 | };
|