|
| 1 | +import generate from "babel-generator"; |
| 2 | + |
| 3 | +type Plugin = { |
| 4 | + visitor: Visitors |
| 5 | +}; |
| 6 | + |
| 7 | +type PluginParams = { |
| 8 | + types: Object; |
| 9 | + template: (source: string) => (ids: Object) => Node; |
| 10 | +}; |
| 11 | + |
| 12 | +type Visitors = { |
| 13 | + [key: string]: Visitor |
| 14 | +} |
| 15 | + |
| 16 | +type Visitor = (path: NodePath) => void; |
| 17 | + |
| 18 | +type Node = { |
| 19 | + type: string; |
| 20 | +}; |
| 21 | + |
| 22 | +type Literal = { |
| 23 | + type: 'StringLiteral' | 'BooleanLiteral' | 'NumericLiteral' | 'NullLiteral' | 'RegExpLiteral' |
| 24 | +}; |
| 25 | + |
| 26 | +type Identifier = { |
| 27 | + type: string; |
| 28 | + name: string; |
| 29 | +}; |
| 30 | + |
| 31 | +type Scope = {}; |
| 32 | + |
| 33 | +type NodePath = { |
| 34 | + type: string; |
| 35 | + node: Node; |
| 36 | + scope: Scope; |
| 37 | +}; |
| 38 | + |
| 39 | + |
| 40 | +/** |
| 41 | + * # Design By Contract Transformer |
| 42 | + */ |
| 43 | +export default function ({types: t, template}: PluginParams): Plugin { |
| 44 | + |
| 45 | + const PRECONDITION_NAME = 'pre'; |
| 46 | + const POSTCONDITION_NAME = 'post'; |
| 47 | + const INVARIANT_NAME = 'invariant'; |
| 48 | + |
| 49 | + const preconditionAsserter: (ids: {[key: string]: Node}) => Node = template(` |
| 50 | + if (!condition) { |
| 51 | + throw new Error(message); |
| 52 | + } |
| 53 | + `); |
| 54 | + |
| 55 | + const functionVisitor: Visitors = { |
| 56 | + Function (path: NodePath): void { |
| 57 | + // This will be handled by the outer visitor, so skip it. |
| 58 | + path.skip(); |
| 59 | + }, |
| 60 | + |
| 61 | + LabeledStatement (path: NodePath): void { |
| 62 | + const label: NodePath = path.get('label'); |
| 63 | + |
| 64 | + if (label.node.name === PRECONDITION_NAME) { |
| 65 | + assemblePrecondition(path); |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + |
| 72 | + function assemblePrecondition (path: NodePath): void { |
| 73 | + const body: NodePath = path.get('body'); |
| 74 | + const fn: NodePath = path.getFunctionParent(); |
| 75 | + const name: string = fn.node.id ? `"${fn.node.id.name}" `: ' '; |
| 76 | + body.traverse({ |
| 77 | + VariableDeclaration (item: NodePath): void { |
| 78 | + throw path.buildCodeFrameError(`Preconditions cannot have side effects.`); |
| 79 | + }, |
| 80 | + Function (item: NodePath): void { |
| 81 | + throw path.buildCodeFrameError(`Preconditions cannot have side effects.`); |
| 82 | + }, |
| 83 | + AssignmentExpression (item: NodePath): void { |
| 84 | + throw path.buildCodeFrameError(`Preconditions cannot have side effects.`); |
| 85 | + }, |
| 86 | + UpdateExpression (item: NodePath): void { |
| 87 | + throw path.buildCodeFrameError(`Preconditions cannot have side effects.`); |
| 88 | + }, |
| 89 | + ExpressionStatement (statement: NodePath): void { |
| 90 | + let condition: NodePath = statement.get('expression'); |
| 91 | + let message; |
| 92 | + if (condition.isSequenceExpression()) { |
| 93 | + const expressions = condition.get('expressions'); |
| 94 | + condition = expressions[0]; |
| 95 | + message = expressions[1]; |
| 96 | + } |
| 97 | + else { |
| 98 | + message = t.stringLiteral(`Function ${name}precondition failed: ${generate(condition.node).code}`); |
| 99 | + } |
| 100 | + statement.replaceWith(preconditionAsserter({ |
| 101 | + condition, |
| 102 | + message |
| 103 | + })); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + if (body.isBlockStatement()) { |
| 108 | + path.replaceWithMultiple(path.get('body').node.body); |
| 109 | + } |
| 110 | + else { |
| 111 | + path.replaceWith(path.get('body')); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + function expression (input: string): Function { |
| 117 | + const fn: Function = template(input); |
| 118 | + return function (...args) { |
| 119 | + const node: Node = fn(...args); |
| 120 | + return getExpression(node); |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + return { |
| 125 | + visitor: { |
| 126 | + Function (path: NodePath): void { |
| 127 | + if (path.isArrowFunctionExpression() && !path.get('body').isBlockStatement()) { |
| 128 | + // Naked arrow functions cannot contain contracts. |
| 129 | + return; |
| 130 | + } |
| 131 | + path.traverse(functionVisitor); |
| 132 | + } |
| 133 | + } |
| 134 | + }; |
| 135 | +} |
0 commit comments