All files / transform/passes VerifyAnnotationSignature.js

11.11% Statements 1/9
0% Branches 0/10
50% Functions 1/2
11.11% Lines 1/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                          19x                                                          
import TransformError from '../transformError.js';
import Transformation from '../transformation.js';
import Annotations from '../data/annotations';
import t from '../../parser/nodes';
 
/**
 * This validates decorators and ensures that their arguments and all are
 * conformant to the spec described in `data/decorators`. This does not do any
 * specific checks or transformations related to the decorators. This only 
 * ensures it is valid.
 */
export default class VerifyAnnotationSignature extends Transformation {
    constructor() {
        super(t.Annotation, "Verify::AnnotationSignature");
    }
    
    /** @overide */
    modify(node: Node, tool: ASTTool) {
        let name = node.name;
        let res = Annotations.get(name);
        if (res !== undefined) {
            if (res === null && node.args !== null) {
                throw new TransformError(
                    `Annotation \`${name}\` does not take any arguments but ` +
                    `you provided ${node.args.length}`,
                    node
                );
            } else if (res !== null && node.args.length !== res) {
                throw new TransformError(
                    `Annotation \`${name}\` expected exactly ${res} args; you` +
                    ` provided ${res} arguments.`,
                    node
                );
            }
        } else {
            throw new TransformError(
                `Used undeclared annotation of name ${name}`,
                node
            );
        }
    }
}