@abaplint/core
Version:
abaplint - Core API
150 lines • 6.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RulesRunner = void 0;
const syntax_1 = require("./abap/5_syntax/syntax");
const artifacts_rules_1 = require("./artifacts_rules");
const _abap_object_1 = require("./objects/_abap_object");
const skip_logic_1 = require("./skip_logic");
const excludeHelper_1 = require("./utils/excludeHelper");
class SyntaxPerformance {
constructor() {
this.results = [];
}
push(obj, runtime) {
if (runtime < 100) {
return;
}
this.results.push({
runtime: runtime,
name: obj.getType() + " " + obj.getName(),
});
}
output() {
const MAX = 10;
this.results.sort((a, b) => { return b.runtime - a.runtime; });
for (let i = 0; i < MAX; i++) {
const row = this.results[i];
if (row === undefined) {
break;
}
process.stderr.write(`\t${row.runtime}ms\t${row.name}\n`);
}
}
}
class RulesRunner {
constructor(reg) {
this.reg = reg;
this.syntaxPerformance = new SyntaxPerformance();
}
objectsToCheck(objects) {
const check = [];
const skipLogic = new skip_logic_1.SkipLogic(this.reg);
for (const obj of objects) {
if (skipLogic.skip(obj) || this.reg.isDependency(obj)) {
continue;
}
check.push(obj);
}
return check;
}
runRules(objects, input) {
var _a, _b, _c, _d, _e, _f;
const rulePerformance = {};
const issues = [];
const rules = this.reg.getConfig().getEnabledRules();
const check = this.objectsToCheck(objects);
// note: SyntaxLogic is cached, logic is run as first step in order
// not to penalize the performance of the first rule using SyntaxLogic information
(_a = input === null || input === void 0 ? void 0 : input.progress) === null || _a === void 0 ? void 0 : _a.set(check.length, "Run Syntax");
for (const obj of check) {
(_b = input === null || input === void 0 ? void 0 : input.progress) === null || _b === void 0 ? void 0 : _b.tick("Run Syntax - " + obj.getName());
if (obj instanceof _abap_object_1.ABAPObject) {
const start = Date.now();
new syntax_1.SyntaxLogic(this.reg, obj).run();
if ((input === null || input === void 0 ? void 0 : input.outputPerformance) === true) {
this.syntaxPerformance.push(obj, Date.now() - start);
}
}
}
if ((input === null || input === void 0 ? void 0 : input.outputPerformance) === true) {
process.stderr.write("Syntax Performance:\n");
this.syntaxPerformance.output();
}
(_c = input === null || input === void 0 ? void 0 : input.progress) === null || _c === void 0 ? void 0 : _c.set(rules.length, "Initialize Rules");
for (const rule of rules) {
const start = Date.now();
if ((input === null || input === void 0 ? void 0 : input.outputPerformance) === true) {
process.stderr.write("Initializing rule " + rule.getMetadata().key);
}
else {
(_d = input === null || input === void 0 ? void 0 : input.progress) === null || _d === void 0 ? void 0 : _d.tick("Initialize Rules - " + rule.getMetadata().key);
}
if (rule.initialize === undefined) {
throw new Error(rule.getMetadata().key + " missing initialize method");
}
rule.initialize(this.reg);
if ((input === null || input === void 0 ? void 0 : input.outputPerformance) === true) {
process.stderr.write(", " + (Date.now() - start) + "ms\n");
}
rulePerformance[rule.getMetadata().key] = 0;
}
(_e = input === null || input === void 0 ? void 0 : input.progress) === null || _e === void 0 ? void 0 : _e.set(check.length, "Finding Issues");
for (const obj of check) {
(_f = input === null || input === void 0 ? void 0 : input.progress) === null || _f === void 0 ? void 0 : _f.tick("Finding Issues - " + obj.getType() + " " + obj.getName());
for (const rule of rules) {
const before = Date.now();
issues.push(...rule.run(obj));
const runtime = Date.now() - before;
rulePerformance[rule.getMetadata().key] = rulePerformance[rule.getMetadata().key] + runtime;
}
}
if ((input === null || input === void 0 ? void 0 : input.outputPerformance) === true) {
const perf = [];
for (const p in rulePerformance) {
if (rulePerformance[p] > 100) { // ignore rules if it takes less than 100ms
perf.push({ name: p, time: rulePerformance[p] });
}
}
perf.sort((a, b) => { return b.time - a.time; });
for (const p of perf) {
process.stderr.write("\t" + p.time + "ms\t" + p.name + "\n");
}
}
return this.excludeIssues(issues);
}
excludeIssues(issues) {
var _a;
const ret = issues;
const globalNoIssues = this.reg.getConfig().getGlobal().noIssues || [];
const globalNoIssuesPatterns = globalNoIssues.map(x => new RegExp(x, "i"));
if (globalNoIssuesPatterns.length > 0) {
for (let i = ret.length - 1; i >= 0; i--) {
const filename = ret[i].getFilename();
if (excludeHelper_1.ExcludeHelper.isExcluded(filename, globalNoIssuesPatterns)) {
ret.splice(i, 1);
}
}
}
// exclude issues, as now we know both the filename and issue key
for (const rule of artifacts_rules_1.ArtifactsRules.getRules()) {
const key = rule.getMetadata().key;
const ruleExclude = (_a = this.reg.getConfig().readByKey(key, "exclude")) !== null && _a !== void 0 ? _a : [];
if (ruleExclude.length === 0) {
continue;
}
const ruleExcludePatterns = ruleExclude.map(x => new RegExp(x, "i"));
for (let i = ret.length - 1; i >= 0; i--) {
if (ret[i].getKey() !== key) {
continue;
}
const filename = ret[i].getFilename();
if (excludeHelper_1.ExcludeHelper.isExcluded(filename, ruleExcludePatterns)) {
ret.splice(i, 1);
}
}
}
return ret;
}
}
exports.RulesRunner = RulesRunner;
//# sourceMappingURL=rules_runner.js.map