UNPKG

@amplitude/ampli

Version:

Amplitude CLI

69 lines (68 loc) 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const antlr4ts_1 = require("antlr4ts"); const ParseTreeWalker_1 = require("antlr4ts/tree/ParseTreeWalker"); const GoParser_1 = require("../../antlr/go/GoParser"); const base_1 = require("../base"); const string_1 = require("../../util/string"); const GoLexer_1 = require("../../antlr/go/GoLexer"); class EventListener { constructor(entrypointNames, eventNames, addReference) { this.entrypointNames = entrypointNames; this.eventNames = eventNames; this.addReference = addReference; } enterPrimaryExpr(context) { if (!context || !context.children || context.parent instanceof GoParser_1.PrimaryExprContext) { return; } if (context.children.length === 2) { const expression = context.children[0]; if (expression instanceof GoParser_1.PrimaryExprContext && context.children[1] instanceof GoParser_1.ArgumentsContext) { const expressionParts = expression.text.split('.'); if (expressionParts.length > 1) { const target = expressionParts.slice(0, expressionParts.length - 1).join('.'); this.entrypointNames.forEach(instanceName => { if (target === instanceName) { this.addReference(lodash_1.camelCase(expressionParts[expressionParts.length - 1]), context.start.line, context.start.charPositionInLine); } }); } } } const match = /^ampli\.([^.]+)\.Builder\(\)/.exec(context.text); if (match) { this.addReference(lodash_1.camelCase(match[1]), context.start.line, context.start.charPositionInLine); } } } class GoVerifierAmpliV1 extends base_1.Verifier { constructor(ampliInstanceNames = ['ampli.Instance']) { super('go', ampliInstanceNames.map(name => `${name}.`)); this.ampliInstanceNames = ampliInstanceNames; this.getReferenceName = (eventName) => lodash_1.camelCase(eventName); } async doVerify(eventNames, workingDirs) { const sanitizedEventNames = eventNames.map(e => string_1.upperCamelCase(e)); return this.verifyEach((filePath, fileContents, addReference, addError) => { const inputStream = new antlr4ts_1.ANTLRInputStream(fileContents); const lexer = new GoLexer_1.GoLexer(inputStream); const tokenStream = new antlr4ts_1.CommonTokenStream(lexer); const parser = new GoParser_1.GoParser(tokenStream); parser.removeErrorListeners(); parser.addErrorListener({ syntaxError: (recognizer, offendingSymbol, line, charPositionInLine, msg) => { addError({ location: filePath, message: `(${line}:${charPositionInLine}) ${msg}`, }); }, }); const listener = new EventListener(this.ampliInstanceNames, sanitizedEventNames, (name, row, column) => addReference({ filePath, name, row, column })); const fileContext = parser.sourceFile(); ParseTreeWalker_1.ParseTreeWalker.DEFAULT.walk(listener, fileContext); }, workingDirs); } } exports.default = GoVerifierAmpliV1;