@amplitude/ampli
Version:
Amplitude CLI
85 lines (84 loc) • 4.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const antlr4ts_1 = require("antlr4ts");
const ParseTreeWalker_1 = require("antlr4ts/tree/ParseTreeWalker");
const KotlinLexer_1 = require("../../antlr/kotlin/1.6/KotlinLexer");
const KotlinParser_1 = require("../../antlr/kotlin/1.6/KotlinParser");
const base_1 = require("../base");
const string_1 = require("../../util/string");
class EventListener {
constructor(entrypointNames, eventNames, addReference) {
this.entrypointNames = entrypointNames;
this.eventNames = eventNames;
this.addReference = addReference;
}
enterPostfixUnaryExpression(context) {
if (!context || !context.children || (context.children.length < 1 && context.children.length > 3)) {
return;
}
let name;
if (context.children.length <= 2) {
const itlyEventClass = context.children[0];
if (!itlyEventClass || !(itlyEventClass instanceof KotlinParser_1.PrimaryExpressionContext)
|| !itlyEventClass.children || itlyEventClass.children.length !== 1) {
return;
}
const itlyEventClassIdentifier = itlyEventClass.children[0];
if (!itlyEventClassIdentifier || !(itlyEventClassIdentifier instanceof KotlinParser_1.SimpleIdentifierContext)) {
return;
}
const eventClassName = itlyEventClassIdentifier.text;
if (!this.eventNames.includes(eventClassName)) {
return;
}
name = lodash_1.camelCase(eventClassName);
this.addReference(name, context.start.line, context.start.charPositionInLine);
}
else {
this.entrypointNames.forEach(instanceName => {
const nameParts = instanceName.split('.');
if (context.children && context.children.length > nameParts.length + 1) {
const trackCall = context.children.slice(0, nameParts.length + 1).map(child => child.text.trim()).join('');
if (trackCall.startsWith(`${instanceName}.`)) {
name = context.children[nameParts.length].text;
while (name.length > 0 && (name[0] === '.' || (name[0] === '?' && instanceName.endsWith('?')))) {
name = name.slice(1);
}
if (name) {
this.addReference(name, context.start.line, context.start.charPositionInLine);
}
}
}
});
}
}
}
class KotlinVerifierV3 extends base_1.Verifier {
constructor(ampliInstanceNames = ['Itly', 'itly']) {
super('kt', 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 KotlinLexer_1.KotlinLexer(inputStream);
const tokenStream = new antlr4ts_1.CommonTokenStream(lexer);
const parser = new KotlinParser_1.KotlinParser(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 }));
ParseTreeWalker_1.ParseTreeWalker.DEFAULT.walk(listener, parser.kotlinFile());
}, workingDirs);
}
}
exports.default = KotlinVerifierV3;