UNPKG

@abaplint/core

Version:
93 lines 4.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CheckTextElements = exports.CheckTextElementsConf = void 0; const issue_1 = require("../issue"); const _basic_rule_config_1 = require("./_basic_rule_config"); const Expressions = require("../abap/2_statements/expressions"); const _abap_object_1 = require("../objects/_abap_object"); const include_graph_1 = require("../utils/include_graph"); class CheckTextElementsConf extends _basic_rule_config_1.BasicRuleConfig { } exports.CheckTextElementsConf = CheckTextElementsConf; class CheckTextElements { constructor() { this.conf = new CheckTextElementsConf(); } getMetadata() { return { key: "check_text_elements", title: "Check text elements", shortDescription: `Check text elements exists or matches code`, }; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } initialize(reg) { this.reg = reg; this.graph = new include_graph_1.IncludeGraph(this.reg); return this; } run(obj) { if (!(obj instanceof _abap_object_1.ABAPObject)) { return []; } const output = []; for (const file of obj.getABAPFiles()) { const stru = file.getStructure(); if (stru === undefined) { continue; } let texts; let mainName = undefined; const expressions = stru.findAllExpressionsMulti([Expressions.TextElement, Expressions.TextElementString]); // optimize: no need to find main and texts if there are no expressions to check if (expressions.length > 0) { const mains = this.graph.listMainForInclude(file.getFilename()); if (mains.length === 1) { // todo, this only checks the first main mainName = mains[0]; const main1 = this.reg.findObjectForFile(this.reg.getFileByName(mains[0])); texts = main1.getTexts(); } else { texts = obj.getTexts(); } } for (const e of expressions) { if (e.get() instanceof Expressions.TextElement) { const token = e.findFirstExpression(Expressions.TextElementKey).getFirstToken(); const key = token.getStr().toUpperCase(); if (texts[key] === undefined) { const message = `Text element "${key}" not found` + (mainName ? ", " + mainName : ""); output.push(issue_1.Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity)); } } else { // its a Expressions.TextElementString const token = e.findFirstExpression(Expressions.TextElementKey).getFirstToken(); const code = e.getFirstToken().getStr(); const key = token.getStr().toUpperCase(); let found = texts[key]; if (found && code.startsWith("'")) { found = found.replace(/'/g, "''"); } if (found === undefined) { const message = `Text element "${key}" not found` + (mainName ? ", " + mainName : ""); output.push(issue_1.Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity)); } else if (code !== "'" + found + "'" && code !== "`" + found + "`") { output.push(issue_1.Issue.atToken(file, token, "Text does not match text element", this.getMetadata().key, this.conf.severity)); } } } } return output; } } exports.CheckTextElements = CheckTextElements; //# sourceMappingURL=check_text_elements.js.map