|
| 1 | +import evk from "eslint-visitor-keys" |
| 2 | + |
| 3 | +const typeConversionBinaryOps = Object.freeze( |
| 4 | + new Set([ |
| 5 | + "==", |
| 6 | + "!=", |
| 7 | + "<", |
| 8 | + "<=", |
| 9 | + ">", |
| 10 | + ">=", |
| 11 | + "<<", |
| 12 | + ">>", |
| 13 | + ">>>", |
| 14 | + "+", |
| 15 | + "-", |
| 16 | + "*", |
| 17 | + "/", |
| 18 | + "%", |
| 19 | + "|", |
| 20 | + "^", |
| 21 | + "&", |
| 22 | + "in", |
| 23 | + ]) |
| 24 | +) |
| 25 | +const typeConversionUnaryOps = Object.freeze(new Set(["-", "+", "!", "~"])) |
| 26 | +const visitor = Object.freeze( |
| 27 | + Object.assign(Object.create(null), { |
| 28 | + $visit(node, options, visitorKeys) { |
| 29 | + const { type } = node |
| 30 | + |
| 31 | + if (typeof this[type] === "function") { |
| 32 | + return this[type](node, options, visitorKeys) |
| 33 | + } |
| 34 | + |
| 35 | + return this.$visitChildren(node, options, visitorKeys) |
| 36 | + }, |
| 37 | + |
| 38 | + $visitChildren(node, options, visitorKeys) { |
| 39 | + const { type } = node |
| 40 | + |
| 41 | + for (const key of visitorKeys[type] || evk.getKeys(node)) { |
| 42 | + const value = node[key] |
| 43 | + |
| 44 | + if (Array.isArray(value)) { |
| 45 | + for (const element of value) { |
| 46 | + if ( |
| 47 | + element && |
| 48 | + this.$visit(element, options, visitorKeys) |
| 49 | + ) { |
| 50 | + return true |
| 51 | + } |
| 52 | + } |
| 53 | + } else if (value && this.$visit(value, options, visitorKeys)) { |
| 54 | + return true |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return false |
| 59 | + }, |
| 60 | + |
| 61 | + ArrowFunctionExpression() { |
| 62 | + return false |
| 63 | + }, |
| 64 | + AssignmentExpression() { |
| 65 | + return true |
| 66 | + }, |
| 67 | + AwaitExpression() { |
| 68 | + return true |
| 69 | + }, |
| 70 | + BinaryExpression(node, options, visitorKeys) { |
| 71 | + if ( |
| 72 | + options.considerImplicitTypeConversion && |
| 73 | + typeConversionBinaryOps.has(node.operator) && |
| 74 | + (node.left.type !== "Literal" || node.right.type !== "Literal") |
| 75 | + ) { |
| 76 | + return true |
| 77 | + } |
| 78 | + return this.$visitChildren(node, options, visitorKeys) |
| 79 | + }, |
| 80 | + CallExpression() { |
| 81 | + return true |
| 82 | + }, |
| 83 | + FunctionExpression() { |
| 84 | + return false |
| 85 | + }, |
| 86 | + MemberExpression(node, options, visitorKeys) { |
| 87 | + if (options.considerGetters) { |
| 88 | + return true |
| 89 | + } |
| 90 | + if ( |
| 91 | + options.considerImplicitTypeConversion && |
| 92 | + node.computed && |
| 93 | + node.property.type !== "Literal" |
| 94 | + ) { |
| 95 | + return true |
| 96 | + } |
| 97 | + return this.$visitChildren(node, options, visitorKeys) |
| 98 | + }, |
| 99 | + MethodDefinition(node, options, visitorKeys) { |
| 100 | + if ( |
| 101 | + options.considerImplicitTypeConversion && |
| 102 | + node.computed && |
| 103 | + node.key.type !== "Literal" |
| 104 | + ) { |
| 105 | + return true |
| 106 | + } |
| 107 | + return this.$visitChildren(node, options, visitorKeys) |
| 108 | + }, |
| 109 | + NewExpression() { |
| 110 | + return true |
| 111 | + }, |
| 112 | + Property(node, options, visitorKeys) { |
| 113 | + if ( |
| 114 | + options.considerImplicitTypeConversion && |
| 115 | + node.computed && |
| 116 | + node.key.type !== "Literal" |
| 117 | + ) { |
| 118 | + return true |
| 119 | + } |
| 120 | + return this.$visitChildren(node, options, visitorKeys) |
| 121 | + }, |
| 122 | + UnaryExpression(node, options, visitorKeys) { |
| 123 | + if (node.operator === "delete") { |
| 124 | + return true |
| 125 | + } |
| 126 | + if ( |
| 127 | + options.considerImplicitTypeConversion && |
| 128 | + typeConversionUnaryOps.has(node.operator) && |
| 129 | + node.argument.type !== "Literal" |
| 130 | + ) { |
| 131 | + return true |
| 132 | + } |
| 133 | + return this.$visitChildren(node, options, visitorKeys) |
| 134 | + }, |
| 135 | + UpdateExpression() { |
| 136 | + return true |
| 137 | + }, |
| 138 | + YieldExpression() { |
| 139 | + return true |
| 140 | + }, |
| 141 | + }) |
| 142 | +) |
| 143 | + |
| 144 | +/** |
| 145 | + * Check whether a given node has any side effect or not. |
| 146 | + * @param {Node} node The node to get. |
| 147 | + * @param {SourceCode} sourceCode The source code object. |
| 148 | + * @param {object} [options] The option object. |
| 149 | + * @param {boolean} [options.considerGetters=false] If `true` then it considers member accesses as the node which has side effects. |
| 150 | + * @param {boolean} [options.considerImplicitTypeConversion=false] If `true` then it considers implicit type conversion as the node which has side effects. |
| 151 | + * @param {object} [options.visitorKeys=evk.KEYS] The keys to traverse nodes. Use `context.getSourceCode().visitorKeys`. |
| 152 | + * @returns {boolean} `true` if the node has a certain side effect. |
| 153 | + */ |
| 154 | +export function hasSideEffect( |
| 155 | + node, |
| 156 | + sourceCode, |
| 157 | + { considerGetters = false, considerImplicitTypeConversion = false } = {} |
| 158 | +) { |
| 159 | + return visitor.$visit( |
| 160 | + node, |
| 161 | + { considerGetters, considerImplicitTypeConversion }, |
| 162 | + sourceCode.visitorKeys || evk.KEYS |
| 163 | + ) |
| 164 | +} |
0 commit comments