@amplitude/ampli
Version:
Amplitude CLI
99 lines (98 loc) • 4.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const antlr4ts_1 = require("antlr4ts");
const ParseTreeWalker_1 = require("antlr4ts/tree/ParseTreeWalker");
const KotlinLexer_1 = require("../../antlr/kotlin/1.3/KotlinLexer");
const KotlinParser_1 = require("../../antlr/kotlin/1.3/KotlinParser");
const base_1 = require("../base");
const string_1 = require("../../util/string");
class ItlyListener {
constructor(eventNames, addReference) {
this.eventNames = eventNames;
this.addReference = addReference;
}
enterPostfixUnaryExpression(context) {
if (!context || !context.children || (context.children.length !== 2 && 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 itlyEventClassArgs = context.children[1];
if (!itlyEventClassArgs || !(itlyEventClassArgs instanceof KotlinParser_1.PostfixUnarySuffixContext) || !itlyEventClassArgs.children || itlyEventClassArgs.children.length !== 1) {
return;
}
const itlyEventClassArgsCall = itlyEventClassArgs.children[0];
if (!itlyEventClassArgsCall || !(itlyEventClassArgsCall instanceof KotlinParser_1.CallSuffixContext)) {
return;
}
if (!this.eventNames.includes(itlyEventClassIdentifier.text)) {
return;
}
name = `track${itlyEventClassIdentifier.text}`;
}
else if (context.children.length === 3) {
if (!context || !context.children || (context.children.length !== 3 && context.children.length !== 4)) {
return;
}
const itlyClass = context.children[0];
if (!itlyClass || !(itlyClass instanceof KotlinParser_1.PrimaryExpressionContext) || itlyClass.text !== 'Itly') {
return;
}
const [, dotMethod] = context.children;
if (!dotMethod || !(dotMethod instanceof KotlinParser_1.PostfixUnarySuffixContext) || !dotMethod.children || dotMethod.children.length !== 1) {
return;
}
const dotMethodInner = dotMethod.children[0];
if (!dotMethodInner || !(dotMethodInner instanceof KotlinParser_1.NavigationSuffixContext) || !dotMethodInner.children || dotMethodInner.children.length !== 2) {
return;
}
const methodDot = dotMethodInner.children[0];
if (!methodDot || !(methodDot instanceof KotlinParser_1.MemberAccessOperatorContext) || methodDot.text !== '.') {
return;
}
const method = dotMethodInner.children[1];
if (!method || !(method instanceof KotlinParser_1.SimpleIdentifierContext)) {
return;
}
name = `track${method.text[0].toUpperCase()}${method.text.substr(1)}`;
}
if (!name) {
return;
}
this.addReference(name, context.start.line, context.start.charPositionInLine);
}
}
class KotlinVerifierV2 extends base_1.Verifier {
constructor() {
super('kt', ['Itly.', 'itly.']);
}
async doVerify(eventNames, workingDirs) {
const sanitizedEventNames = eventNames.map(e => string_1.sanitize(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 ItlyListener(sanitizedEventNames, (name, row, column) => addReference({ filePath, name, row, column }));
ParseTreeWalker_1.ParseTreeWalker.DEFAULT.walk(listener, parser.kotlinFile());
}, workingDirs);
}
}
exports.default = KotlinVerifierV2;