|
| 1 | +'use strict'; |
| 2 | +const upperfirst = require('lodash.upperfirst'); |
| 3 | + |
| 4 | +const nameRegexp = /^(?:[A-Z][a-z0-9]*)*Error$/; |
| 5 | + |
| 6 | +const getClassName = name => upperfirst(name).replace(/(error|)$/i, 'Error'); |
| 7 | + |
| 8 | +const getConstructorMethod = className => ` |
| 9 | + constructor() { |
| 10 | + super(); |
| 11 | + this.name = '${className}'; |
| 12 | + } |
| 13 | +`; |
| 14 | + |
| 15 | +const hasValidSuperClass = node => { |
| 16 | + if (!node.superClass) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + |
| 20 | + let name = node.superClass.name; |
| 21 | + |
| 22 | + if (node.superClass.type === 'MemberExpression') { |
| 23 | + name = node.superClass.property.name; |
| 24 | + } |
| 25 | + |
| 26 | + return nameRegexp.test(name); |
| 27 | +}; |
| 28 | + |
| 29 | +const isSuperExpression = node => node.type === 'ExpressionStatement' && node.expression.type === 'CallExpression' && node.expression.callee.type === 'Super'; |
| 30 | + |
| 31 | +const isAssignmentExpression = (node, name) => { |
| 32 | + if (node.type !== 'ExpressionStatement' || node.expression.type !== 'AssignmentExpression') { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + const lhs = node.expression.left; |
| 37 | + |
| 38 | + if (!lhs.object || lhs.object.type !== 'ThisExpression') { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + return lhs.property.name === name; |
| 43 | +}; |
| 44 | + |
| 45 | +const create = context => { |
| 46 | + return { |
| 47 | + ClassDeclaration: node => { |
| 48 | + if (!hasValidSuperClass(node)) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const name = node.id.name; |
| 53 | + const className = getClassName(name); |
| 54 | + |
| 55 | + if (name !== className) { |
| 56 | + context.report({ |
| 57 | + node: node.id, |
| 58 | + message: `Invalid class name, use \`${className}\`.` |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + const body = node.body.body; |
| 63 | + |
| 64 | + const constructor = body.find(x => x.kind === 'constructor'); |
| 65 | + |
| 66 | + if (!constructor) { |
| 67 | + context.report({ |
| 68 | + node, |
| 69 | + message: 'Add a constructor to your error.', |
| 70 | + fix: fixer => fixer.insertTextAfterRange([ |
| 71 | + node.body.start, |
| 72 | + node.body.start + 1 |
| 73 | + ], getConstructorMethod(name)) |
| 74 | + }); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const constructorBodyNode = constructor.value.body; |
| 79 | + const constructorBody = constructorBodyNode.body; |
| 80 | + |
| 81 | + const superExpression = constructorBody.find(isSuperExpression); |
| 82 | + const messageExpressionIndex = constructorBody.findIndex(x => isAssignmentExpression(x, 'message')); |
| 83 | + |
| 84 | + if (!superExpression) { |
| 85 | + context.report({ |
| 86 | + node: constructorBodyNode, |
| 87 | + message: 'Missing call to `super()` in constructor.' |
| 88 | + }); |
| 89 | + } else if (messageExpressionIndex !== -1 && superExpression.expression.arguments.length === 0) { |
| 90 | + const rhs = constructorBody[messageExpressionIndex].expression.right; |
| 91 | + |
| 92 | + context.report({ |
| 93 | + node: superExpression, |
| 94 | + message: 'Pass the error message to `super()`.', |
| 95 | + fix: fixer => fixer.insertTextAfterRange([ |
| 96 | + superExpression.start, |
| 97 | + superExpression.start + 6 |
| 98 | + ], rhs.raw || rhs.name) |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + if (messageExpressionIndex !== -1) { |
| 103 | + const expression = constructorBody[messageExpressionIndex]; |
| 104 | + |
| 105 | + context.report({ |
| 106 | + node: expression, |
| 107 | + message: 'Pass the error message to `super()` instead of setting `this.message`.', |
| 108 | + fix: fixer => fixer.removeRange([ |
| 109 | + messageExpressionIndex === 0 ? constructorBodyNode.start : constructorBody[messageExpressionIndex - 1].end, |
| 110 | + expression.end |
| 111 | + ]) |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + const nameExpression = constructorBody.find(x => isAssignmentExpression(x, 'name')); |
| 116 | + |
| 117 | + if (!nameExpression || nameExpression.expression.right.value !== name) { |
| 118 | + context.report({ |
| 119 | + node: nameExpression ? nameExpression.expression.right : constructorBodyNode, |
| 120 | + message: `The \`name\` property should be set to \`${name}\`.` |
| 121 | + }); |
| 122 | + } |
| 123 | + } |
| 124 | + }; |
| 125 | +}; |
| 126 | + |
| 127 | +module.exports = { |
| 128 | + create, |
| 129 | + meta: { |
| 130 | + fixable: 'code' |
| 131 | + } |
| 132 | +}; |
0 commit comments