|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const estraverse = require('estraverse'); |
| 4 | + |
| 5 | +/** |
| 6 | + * unassert |
| 7 | + * Encourages programming with assertions by providing tools to compile them away. |
| 8 | + * |
| 9 | + * https://github.com/unassert-js/unassert |
| 10 | + * |
| 11 | + * Copyright (c) 2015-2022 Takuto Wada |
| 12 | + * Licensed under the MIT license. |
| 13 | + * https://github.com/unassert-js/unassert/blob/master/LICENSE |
| 14 | + */ |
| 15 | + |
| 16 | +function isLiteral (node) { |
| 17 | + return node && node.type === 'Literal'; |
| 18 | +} |
| 19 | +function isIdentifier (node) { |
| 20 | + return node && node.type === 'Identifier'; |
| 21 | +} |
| 22 | +function isObjectPattern (node) { |
| 23 | + return node && node.type === 'ObjectPattern'; |
| 24 | +} |
| 25 | +function isMemberExpression (node) { |
| 26 | + return node && node.type === 'MemberExpression'; |
| 27 | +} |
| 28 | +function isCallExpression (node) { |
| 29 | + return node && node.type === 'CallExpression'; |
| 30 | +} |
| 31 | +function isExpressionStatement (node) { |
| 32 | + return node && node.type === 'ExpressionStatement'; |
| 33 | +} |
| 34 | +function isIfStatement (node) { |
| 35 | + return node && node.type === 'IfStatement'; |
| 36 | +} |
| 37 | +function isImportDeclaration (node) { |
| 38 | + return node && node.type === 'ImportDeclaration'; |
| 39 | +} |
| 40 | + |
| 41 | +function isBodyOfNodeHavingNonBlockStatementAsBody (node, key) { |
| 42 | + if (!node) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + if (key !== 'body') { |
| 46 | + return false; |
| 47 | + } |
| 48 | + switch (node.type) { |
| 49 | + case 'DoWhileStatement': |
| 50 | + case 'ForInStatement': |
| 51 | + case 'ForOfStatement': |
| 52 | + case 'ForStatement': |
| 53 | + case 'LabeledStatement': |
| 54 | + case 'WithStatement': |
| 55 | + case 'WhileStatement': |
| 56 | + return true; |
| 57 | + } |
| 58 | + return false; |
| 59 | +} |
| 60 | + |
| 61 | +function isBodyOfIfStatement (node, key) { |
| 62 | + return isIfStatement(node) && (key === 'consequent' || key === 'alternate'); |
| 63 | +} |
| 64 | + |
| 65 | +function isNonBlockChildOfParentNode (currentNode, parentNode, key) { |
| 66 | + return isExpressionStatement(currentNode) && isCallExpression(currentNode.expression) && |
| 67 | + (isBodyOfIfStatement(parentNode, key) || isBodyOfNodeHavingNonBlockStatementAsBody(parentNode, key)); |
| 68 | +} |
| 69 | + |
| 70 | +function createVisitor (options) { |
| 71 | + const config = Object.assign(defaultOptions(), options); |
| 72 | + const targetModules = new Set(config.modules); |
| 73 | + const targetVariables = new Set(config.variables); |
| 74 | + |
| 75 | + function isAssertionModuleName (lit) { |
| 76 | + return isLiteral(lit) && targetModules.has(lit.value); |
| 77 | + } |
| 78 | + |
| 79 | + function isAssertionVariableName (id) { |
| 80 | + return isIdentifier(id) && targetVariables.has(id.name); |
| 81 | + } |
| 82 | + |
| 83 | + function isAssertionMethod (callee) { |
| 84 | + if (!isMemberExpression(callee)) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + const obj = callee.object; |
| 88 | + if (isMemberExpression(obj)) { |
| 89 | + return isAssertionMethod(obj); |
| 90 | + } else { |
| 91 | + return isAssertionVariableName(obj); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + function isAssertionFunction (callee) { |
| 96 | + return isAssertionVariableName(callee); |
| 97 | + } |
| 98 | + |
| 99 | + function isConsoleAssert (callee) { |
| 100 | + if (!isMemberExpression(callee)) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + const { object: obj, property: prop } = callee; |
| 104 | + return isIdentifier(obj) && obj.name === 'console' && |
| 105 | + isIdentifier(prop) && prop.name === 'assert'; |
| 106 | + } |
| 107 | + |
| 108 | + function registerIdentifierAsAssertionVariable (id) { |
| 109 | + if (isIdentifier(id)) { |
| 110 | + targetVariables.add(id.name); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + function handleDestructuredAssertionAssignment (objectPattern) { |
| 115 | + for (const { value } of objectPattern.properties) { |
| 116 | + registerIdentifierAsAssertionVariable(value); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + function handleImportSpecifiers (importDeclaration) { |
| 121 | + for (const { local } of importDeclaration.specifiers) { |
| 122 | + registerIdentifierAsAssertionVariable(local); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + function registerAssertionVariables (node) { |
| 127 | + if (isIdentifier(node)) { |
| 128 | + registerIdentifierAsAssertionVariable(node); |
| 129 | + } else if (isObjectPattern(node)) { |
| 130 | + handleDestructuredAssertionAssignment(node); |
| 131 | + } else if (isImportDeclaration(node)) { |
| 132 | + handleImportSpecifiers(node); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + function isRequireAssert (id, init) { |
| 137 | + if (!isCallExpression(init)) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + const callee = init.callee; |
| 141 | + if (!isIdentifier(callee) || callee.name !== 'require') { |
| 142 | + return false; |
| 143 | + } |
| 144 | + const arg = init.arguments[0]; |
| 145 | + if (!isLiteral(arg) || !isAssertionModuleName(arg)) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + return isIdentifier(id) || isObjectPattern(id); |
| 149 | + } |
| 150 | + |
| 151 | + function isRequireAssertDotStrict (id, init) { |
| 152 | + if (!isMemberExpression(init)) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + if (!isRequireAssert(id, init.object)) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + const prop = init.property; |
| 159 | + if (!isIdentifier(prop)) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + return prop.name === 'strict'; |
| 163 | + } |
| 164 | + |
| 165 | + function isRemovalTargetRequire (id, init) { |
| 166 | + return isRequireAssert(id, init) || isRequireAssertDotStrict(id, init); |
| 167 | + } |
| 168 | + |
| 169 | + function isRemovalTargetAssertion (callee) { |
| 170 | + return isAssertionFunction(callee) || isAssertionMethod(callee) || isConsoleAssert(callee); |
| 171 | + } |
| 172 | + |
| 173 | + const nodeToRemove = new WeakSet(); |
| 174 | + |
| 175 | + return { |
| 176 | + enter: function (currentNode, parentNode) { |
| 177 | + switch (currentNode.type) { |
| 178 | + case 'ImportDeclaration': { |
| 179 | + const source = currentNode.source; |
| 180 | + if (!(isAssertionModuleName(source))) { |
| 181 | + return; |
| 182 | + } |
| 183 | + // remove current ImportDeclaration |
| 184 | + nodeToRemove.add(currentNode); |
| 185 | + this.skip(); |
| 186 | + // register local identifier(s) as assertion variable |
| 187 | + registerAssertionVariables(currentNode); |
| 188 | + break; |
| 189 | + } |
| 190 | + case 'VariableDeclarator': { |
| 191 | + if (isRemovalTargetRequire(currentNode.id, currentNode.init)) { |
| 192 | + if (parentNode.declarations.length === 1) { |
| 193 | + // remove parent VariableDeclaration |
| 194 | + nodeToRemove.add(parentNode); |
| 195 | + } else { |
| 196 | + // single var pattern |
| 197 | + // remove current VariableDeclarator |
| 198 | + nodeToRemove.add(currentNode); |
| 199 | + } |
| 200 | + this.skip(); |
| 201 | + // register local identifier(s) as assertion variable |
| 202 | + registerAssertionVariables(currentNode.id); |
| 203 | + } |
| 204 | + break; |
| 205 | + } |
| 206 | + case 'AssignmentExpression': { |
| 207 | + if (currentNode.operator !== '=') { |
| 208 | + return; |
| 209 | + } |
| 210 | + if (!isExpressionStatement(parentNode)) { |
| 211 | + return; |
| 212 | + } |
| 213 | + if (isRemovalTargetRequire(currentNode.left, currentNode.right)) { |
| 214 | + // remove parent ExpressionStatement |
| 215 | + nodeToRemove.add(parentNode); |
| 216 | + this.skip(); |
| 217 | + // register local identifier(s) as assertion variable |
| 218 | + registerAssertionVariables(currentNode.left); |
| 219 | + } |
| 220 | + break; |
| 221 | + } |
| 222 | + case 'CallExpression': { |
| 223 | + if (!isExpressionStatement(parentNode)) { |
| 224 | + return; |
| 225 | + } |
| 226 | + const callee = currentNode.callee; |
| 227 | + if (isRemovalTargetAssertion(callee)) { |
| 228 | + // remove parent ExpressionStatement |
| 229 | + nodeToRemove.add(parentNode); |
| 230 | + this.skip(); |
| 231 | + } |
| 232 | + break; |
| 233 | + } |
| 234 | + case 'AwaitExpression': { |
| 235 | + const childNode = currentNode.argument; |
| 236 | + if (isExpressionStatement(parentNode) && isCallExpression(childNode)) { |
| 237 | + const callee = childNode.callee; |
| 238 | + if (isRemovalTargetAssertion(callee)) { |
| 239 | + // remove parent ExpressionStatement |
| 240 | + nodeToRemove.add(parentNode); |
| 241 | + this.skip(); |
| 242 | + } |
| 243 | + } |
| 244 | + break; |
| 245 | + } |
| 246 | + } |
| 247 | + }, |
| 248 | + leave: function (currentNode, parentNode) { |
| 249 | + switch (currentNode.type) { |
| 250 | + case 'ImportDeclaration': |
| 251 | + case 'VariableDeclarator': |
| 252 | + case 'VariableDeclaration': |
| 253 | + case 'ExpressionStatement': |
| 254 | + break; |
| 255 | + default: |
| 256 | + return undefined; |
| 257 | + } |
| 258 | + if (nodeToRemove.has(currentNode)) { |
| 259 | + if (isExpressionStatement(currentNode)) { |
| 260 | + const path = this.path(); |
| 261 | + const key = path[path.length - 1]; |
| 262 | + if (isNonBlockChildOfParentNode(currentNode, parentNode, key)) { |
| 263 | + return { |
| 264 | + type: 'BlockStatement', |
| 265 | + body: [] |
| 266 | + }; |
| 267 | + } |
| 268 | + } |
| 269 | + this.remove(); |
| 270 | + } |
| 271 | + return undefined; |
| 272 | + } |
| 273 | + }; |
| 274 | +} |
| 275 | + |
| 276 | +function unassertAst (ast, options) { |
| 277 | + return estraverse.replace(ast, createVisitor(options)); |
| 278 | +} |
| 279 | + |
| 280 | +function defaultOptions () { |
| 281 | + return { |
| 282 | + modules: [ |
| 283 | + 'assert', |
| 284 | + 'assert/strict', |
| 285 | + 'node:assert', |
| 286 | + 'node:assert/strict' |
| 287 | + ] |
| 288 | + }; |
| 289 | +} |
| 290 | + |
| 291 | +exports.createVisitor = createVisitor; |
| 292 | +exports.defaultOptions = defaultOptions; |
| 293 | +exports.unassertAst = unassertAst; |
0 commit comments