UNPKG

1.35 kBJavaScriptView Raw
1/**
2 * @fileoverview Flag expressions in statement position that do not side effect
3 * @author Michael Ficarra
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Rule Definition
9//------------------------------------------------------------------------------
10
11module.exports = function(context) {
12
13 function isPragma(expression, parent) {
14
15 var isStrict = expression.type === "Literal" && expression.value === "use strict",
16 isGlobalStrict = isStrict && (parent.type === "Program" && parent.body[0].expression === expression),
17 isLocalStrict = isStrict && (parent.type === "BlockStatement" && parent.body[0].expression === expression);
18
19 return isGlobalStrict || isLocalStrict;
20 }
21
22 return {
23 "ExpressionStatement": function(node) {
24
25 var type = node.expression.type,
26 parent = context.getAncestors().pop();
27
28
29 if (
30 !/^(?:Assignment|Call|New|Update)Expression$/.test(type) &&
31 (type !== "UnaryExpression" || ["delete", "void"].indexOf(node.expression.operator) < 0) &&
32 !isPragma(node.expression, parent)
33 ) {
34 context.report(node, "Expected an assignment or function call and instead saw an expression.");
35 }
36 }
37 };
38
39};